2024-01-21 01:36:21 +11:00
|
|
|
|
|
|
|
#include <GL/glew.h>
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
2024-01-28 00:02:54 +11:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
2024-01-21 01:36:21 +11:00
|
|
|
|
|
|
|
#include "shader.hpp"
|
|
|
|
#include "window.hpp"
|
|
|
|
|
|
|
|
using namespace sim::graphics;
|
|
|
|
|
|
|
|
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-28 00:02:54 +11:00
|
|
|
static int load_shader(const char* src, int type)
|
2024-01-21 01:36:21 +11:00
|
|
|
{
|
|
|
|
int id = glCreateShader(type);
|
|
|
|
|
2024-01-28 00:02:54 +11:00
|
|
|
glShaderSource(id, 1, &src, nullptr);
|
2024-01-21 01:36:21 +11:00
|
|
|
glCompileShader(id);
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2024-01-28 00:02:54 +11:00
|
|
|
static std::string read_shader(const char* path)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
std::ifstream file(path, std::ios::binary);
|
|
|
|
char buff[1024];
|
|
|
|
|
|
|
|
while(!file.eof())
|
|
|
|
{
|
|
|
|
file.read(buff, 1024);
|
|
|
|
ss.write(buff, file.gcount());
|
|
|
|
}
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2024-01-21 01:36:21 +11:00
|
|
|
unsigned int shader::init_program()
|
|
|
|
{
|
2024-01-28 00:02:54 +11:00
|
|
|
std::string shader_vsh = read_shader("../assets/shader/main.vsh");
|
|
|
|
std::string shader_fsh = read_shader("../assets/shader/main.fsh");
|
|
|
|
|
2024-01-21 01:36:21 +11:00
|
|
|
int success;
|
2024-01-28 00:02:54 +11:00
|
|
|
int vsh_id = load_shader(shader_vsh.c_str(), GL_VERTEX_SHADER);
|
|
|
|
int fsh_id = load_shader(shader_fsh.c_str(), GL_FRAGMENT_SHADER);
|
2024-01-21 01:36:21 +11:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|