fast-nuclear-sim/src/graphics/shader.cpp

116 lines
2.3 KiB
C++
Raw Normal View History

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;
layout (location = 2) in vec3 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-21 01:36:21 +11:00
out flat sampler2D tex;
out vec2 texPos;
2024-01-21 22:33:55 +11:00
out float zVal;
2024-01-21 01:36:21 +11:00
void main()
{
2024-01-21 22:33:55 +11:00
vec4 pos = model * vec4(aPos, 1.0);
2024-01-22 23:53:10 +11:00
mat3 model_norm = mat3(model);
vec3 normal = aNormal;
vec3 cNormal = vec3(0.f, 0.f, 1.f) * model_norm;
zVal = dot(normal, cNormal);// / (length(aNormal) * length(cNormal));
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
in flat sampler2D tex;
in vec2 texPos;
2024-01-21 22:33:55 +11:00
in float zVal;
2024-01-21 01:36:21 +11:00
out vec4 FragColour;
2024-01-21 12:58:37 +11:00
uniform bool do_tex;
uniform mat4 tex_mat;
2024-01-21 01:36:21 +11:00
void main()
{
2024-01-21 12:58:37 +11:00
vec4 texdata = do_tex ? texture2D(tex, texPos) : vec4(1);
2024-01-21 22:33:55 +11:00
FragColour = tex_mat * texdata * vec4(zVal, zVal, zVal, 1);
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;
int shader::gl_do_tex;
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");
gl_do_tex = glGetUniformLocation(prog_id, "do_tex");
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;
}