Files
dt/shaderClass.h
T
2026-11-02 19:35:36 -05:00

68 lines
1.8 KiB
C++

#ifndef SHADER_CLASS_H
#define SHADER_CLASS_H
#include<glad/glad.h>
#include<GLFW/glfw3.h>
#include<glm/glm.hpp>
#include<string>
#include<unordered_map>
#include<fstream>
#include <sstream>
#include <iostream>
#include <cerrno>
std::string get_file_contents(const char* filename);
class Shader {
public:
GLuint ID;
Shader(const char* vertexFile, const char* fragFile);
void Set1F(const char* uniform, float value) {
glUniform1f(GetLocation(uniform), value);
};
void Set2F(const char* uniform, glm::vec2 value) {
glUniform2f(GetLocation(uniform), value.x,value.y);
};
void Set3F(const char* uniform, glm::vec3 value) {
glUniform3f(GetLocation(uniform), value.x,value.y,value.z);
};
void Set4F(const char* uniform, glm::vec4 value) {
glUniform4f(GetLocation(uniform), value.x, value.y, value.z, value.w);
};
void SetMat4(const char* uniform, glm::mat4 value) {
glUniformMatrix4fv(GetLocation(uniform),1,GL_FALSE,&value[0][0]);
};
// Uploads a whole uniform array in one call
void SetMat4Array(const char* uniform, const glm::mat4* values, int count) {
glUniformMatrix4fv(GetLocation(uniform),count,GL_FALSE,&values[0][0][0]);
};
void SetInt(const char* uniform, GLuint value) {
glUniform1i(GetLocation(uniform), value);
};
void Activate();
void Delete();
private:
// Asking the driver is slow and locations never change once linked.
// Uniforms the shader doesn't have cache as -1, which glUniform ignores.
GLint GetLocation(const char* uniform) {
auto existing = uniformLocations.find(uniform);
if (existing != uniformLocations.end())
return existing->second;
GLint location = glGetUniformLocation(ID, uniform);
uniformLocations.emplace(uniform, location);
return location;
}
std::unordered_map<std::string, GLint> uniformLocations;
};
#endif