use strings on shaders

This commit is contained in:
Benjamin Palko 2024-10-06 19:01:34 -04:00
parent f0059ce731
commit a20016d5e9
2 changed files with 26 additions and 18 deletions

View file

@ -9,25 +9,28 @@ static const Vertex vertices[3] = {{{-0.6f, -0.4f}, {1.f, 0.f, 0.f}},
{{0.6f, -0.4f}, {0.f, 1.f, 0.f}}, {{0.6f, -0.4f}, {0.f, 1.f, 0.f}},
{{0.f, 0.6f}, {0.f, 0.f, 1.f}}}; {{0.f, 0.6f}, {0.f, 0.f, 1.f}}};
static const char *vertex_shader_text = static const std::string vertex_shader_text = R"(
"#version 330\n" #version 330
"uniform mat4 MVP;\n" uniform mat4 MVP;
"in vec3 vCol;\n" in vec3 vCol;
"in vec2 vPos;\n" in vec2 vPos;
"out vec3 color;\n" out vec3 color;
"void main()\n" void main()
"{\n" {
" gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n" gl_Position = MVP * vec4(vPos, 0.0, 1.0);
" color = vCol;\n" color = vCol;
"}\n"; }
)";
static const char *fragment_shader_text = "#version 330\n" static const std::string fragment_shader_text = R"(
"in vec3 color;\n" #version 330
"out vec4 fragment;\n" in vec3 color;
"void main()\n" out vec4 fragment;
"{\n" void main()
" fragment = vec4(color, 1.0);\n" {
"}\n"; fragment = vec4(color, 1.0);
}
)";
ShaderSystem::ShaderSystem() { ShaderSystem::ShaderSystem() {
glGenBuffers(1, &vertex_buffer); glGenBuffers(1, &vertex_buffer);
@ -80,6 +83,8 @@ int ShaderSystem::Draw(int width, int height, float time) {
return 0; return 0;
} }
Shader::Shader(GLuint shader) { this->shader = shader; }
void Shader::SetSource(std::string source) { this->SetSource(source.c_str()); }
void Shader::SetSource(const char *source) { void Shader::SetSource(const char *source) {
glShaderSource(shader, 1, &source, NULL); glShaderSource(shader, 1, &source, NULL);
} }

View file

@ -1,5 +1,6 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <linmath.h> #include <linmath.h>
#include <string>
class ShaderSystem { class ShaderSystem {
GLuint vertex_buffer; GLuint vertex_buffer;
@ -12,6 +13,7 @@ class ShaderSystem {
public: public:
ShaderSystem(); ShaderSystem();
int Draw(int width, int height, float time); int Draw(int width, int height, float time);
GLuint *CreateBuffer();
}; };
class Shader { class Shader {
@ -22,6 +24,7 @@ protected:
GLuint shader; GLuint shader;
public: public:
void SetSource(std::string source);
void SetSource(const char *source); void SetSource(const char *source);
void Compile(); void Compile();
void Attach(GLuint program); void Attach(GLuint program);