2024-01-21 01:36:21 +11:00
|
|
|
|
|
|
|
#include <GL/glew.h>
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "shader.hpp"
|
|
|
|
#include "window.hpp"
|
|
|
|
|
|
|
|
using namespace sim::graphics;
|
|
|
|
|
|
|
|
static const char* VERTEX_SHADER = R"(
|
|
|
|
#version 460 core
|
|
|
|
#extension GL_ARB_bindless_texture : require
|
|
|
|
|
|
|
|
layout (location = 0) in sampler2D aTex;
|
|
|
|
layout (location = 1) in vec2 aTexPos;
|
2024-01-26 00:30:14 +11:00
|
|
|
layout (location = 2) in vec4 aPos;
|
2024-01-22 23:53:10 +11:00
|
|
|
layout (location = 3) in vec3 aNormal;
|
2024-01-21 01:36:21 +11:00
|
|
|
|
2024-01-21 22:33:55 +11:00
|
|
|
uniform mat4 model;
|
|
|
|
uniform mat4 projection;
|
|
|
|
|
2024-01-23 17:46:33 +11:00
|
|
|
out float brightness;
|
2024-01-21 01:36:21 +11:00
|
|
|
out flat sampler2D tex;
|
|
|
|
out vec2 texPos;
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
2024-01-26 00:30:14 +11:00
|
|
|
vec4 pos = model * aPos;
|
2024-01-23 17:46:33 +11:00
|
|
|
vec3 cNormal = vec3(0.f, 0.f, 1.f) * mat3(model);
|
2024-01-22 23:53:10 +11:00
|
|
|
|
2024-01-23 17:46:33 +11:00
|
|
|
brightness = dot(normalize(aNormal), normalize(cNormal)) * 0.25f + 0.75f;
|
2024-01-22 23:53:10 +11:00
|
|
|
|
2024-01-21 22:33:55 +11:00
|
|
|
gl_Position = projection * pos;
|
2024-01-21 01:36:21 +11:00
|
|
|
texPos = aTexPos;
|
|
|
|
tex = aTex;
|
|
|
|
}
|
|
|
|
|
|
|
|
)";
|
|
|
|
|
|
|
|
static const char* FRAGMENT_SHADER = R"(
|
|
|
|
#version 460 core
|
|
|
|
#extension GL_ARB_bindless_texture : require
|
|
|
|
|
2024-01-23 17:46:33 +11:00
|
|
|
in float brightness;
|
2024-01-21 01:36:21 +11:00
|
|
|
in flat sampler2D tex;
|
|
|
|
in vec2 texPos;
|
|
|
|
|
|
|
|
out vec4 FragColour;
|
|
|
|
|
2024-01-21 12:58:37 +11:00
|
|
|
uniform mat4 tex_mat;
|
|
|
|
|
2024-01-21 01:36:21 +11:00
|
|
|
void main()
|
|
|
|
{
|
2024-01-24 18:34:04 +11:00
|
|
|
vec4 texdata = texture2D(tex, texPos);
|
2024-01-23 17:46:33 +11:00
|
|
|
FragColour = tex_mat * texdata * vec4(vec3(brightness), 1);
|
2024-01-24 18:34:04 +11:00
|
|
|
|
|
|
|
if(FragColour.a == 0) discard;
|
2024-01-21 01:36:21 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
)";
|
|
|
|
|
|
|
|
static unsigned int prog_id;
|
|
|
|
|
2024-01-21 12:58:37 +11:00
|
|
|
int shader::gl_tex_mat;
|
2024-01-21 22:33:55 +11:00
|
|
|
int shader::gl_model;
|
|
|
|
int shader::gl_projection;
|
2024-01-21 12:58:37 +11:00
|
|
|
|
2024-01-21 01:36:21 +11:00
|
|
|
static int load_shader(const char** src, int type)
|
|
|
|
{
|
|
|
|
int id = glCreateShader(type);
|
|
|
|
|
|
|
|
glShaderSource(id, 1, src, nullptr);
|
|
|
|
glCompileShader(id);
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int shader::init_program()
|
|
|
|
{
|
|
|
|
int success;
|
|
|
|
int vsh_id = load_shader(&VERTEX_SHADER, GL_VERTEX_SHADER);
|
|
|
|
int fsh_id = load_shader(&FRAGMENT_SHADER, GL_FRAGMENT_SHADER);
|
|
|
|
prog_id = glCreateProgram();
|
|
|
|
|
|
|
|
glAttachShader(prog_id, vsh_id);
|
|
|
|
glAttachShader(prog_id, fsh_id);
|
|
|
|
glLinkProgram(prog_id);
|
|
|
|
glGetProgramiv(prog_id, GL_LINK_STATUS, &success);
|
|
|
|
|
|
|
|
if(!success)
|
|
|
|
{
|
|
|
|
char infoLog[512];
|
|
|
|
glGetProgramInfoLog(prog_id, 512, NULL, infoLog);
|
|
|
|
std::cout << "Shader Link Error: " << infoLog << std::endl;
|
|
|
|
window::close();
|
|
|
|
return 0;
|
|
|
|
}
|
2024-01-21 12:58:37 +11:00
|
|
|
|
|
|
|
gl_tex_mat = glGetUniformLocation(prog_id, "tex_mat");
|
2024-01-21 22:33:55 +11:00
|
|
|
gl_model = glGetUniformLocation(prog_id, "model");
|
|
|
|
gl_projection = glGetUniformLocation(prog_id, "projection");
|
2024-01-21 01:36:21 +11:00
|
|
|
|
|
|
|
glUseProgram(prog_id);
|
|
|
|
glDeleteShader(vsh_id);
|
|
|
|
glDeleteShader(fsh_id);
|
|
|
|
|
|
|
|
return prog_id;
|
|
|
|
}
|
|
|
|
|