Refactor & Shader class

pull/2/head
E. Almqvist 2 years ago
parent 25a0f13ba2
commit 3574738dd8
  1. 2
      CMakeLists.txt
  2. 22
      headers/shaders.hpp
  3. 5
      shaders/fragment.glsl
  4. 3
      shaders/vertex.glsl
  5. 36
      src/main.cpp
  6. 56
      src/shaders.cpp
  7. 25
      src/shaders.hpp

@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.24.2)
project(Hohmann VERSION 1.0) project(Hohmann VERSION 1.0)
set(SOURCES src/main.cpp) file(GLOB SOURCES src/*.cpp headers/*.hpp)
add_executable(Hohmann ${SOURCES}) add_executable(Hohmann ${SOURCES})
# GLFW submodule # GLFW submodule

@ -0,0 +1,22 @@
#include <glad/glad.h>
#include <string>
#include <vector>
namespace Shaders {
std::string loadSourceFromFile(const char* file);
unsigned int compile(GLenum shadertype, const char* shaderSource);
unsigned int compileFromFile(GLenum shadertype, const char* file);
unsigned int createProgram(std::vector<unsigned int> shaders);
class Shader {
public:
unsigned int id;
Shader(const char* vertPath, const char* fragPath);
void use();
void setBool(const std::string &name, bool val) const;
void setInt(const std::string &name, int val) const;
void setFloat(const std::string &name, float val) const;
};
}

@ -1,8 +1,7 @@
#version 330 core #version 330 core
out vec4 FragColor; out vec4 FragColor;
in vec4 vertexColor;
uniform vec4 inputColor;
void main() { void main() {
FragColor = inputColor; FragColor = vertexColor;
} }

@ -1,9 +1,10 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
out vec4 vertexColor; out vec4 vertexColor;
void main() { void main() {
gl_Position = vec4(aPos, 1.0); gl_Position = vec4(aPos, 1.0);
vertexColor = vec4(1.0f, 1.0f, 1.0f, 0.0f); vertexColor = vec4(aColor, 1.0);
} }

@ -4,7 +4,7 @@
#include <vector> #include <vector>
#include <math.h> #include <math.h>
#include "shaders.cpp" #include "../headers/shaders.hpp"
#define WINDOW_WIDTH 640 #define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480 #define WINDOW_HEIGHT 480
@ -13,10 +13,10 @@
#define FRAG_SHADER_SRC_FILE "shaders/fragment.glsl" #define FRAG_SHADER_SRC_FILE "shaders/fragment.glsl"
float verts[] = { float verts[] = {
0.5f, 0.5f, 0.0f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.0f -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f
}; };
unsigned int indices[] = { unsigned int indices[] = {
0, 1, 3, 0, 1, 3,
@ -36,20 +36,11 @@ void processInput(GLFWwindow *win) {
struct RenderObj { struct RenderObj {
unsigned int EBO; unsigned int EBO;
unsigned int VBO; Shaders::Shader shader;
unsigned int VAO;
unsigned int shaderProg;
}; };
RenderObj preRenderCallback() { RenderObj preRenderCallback() {
// Compile a bunch of shaders etc Shaders::Shader shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE);
unsigned int vertShader = Shader::compileFromFile(GL_VERTEX_SHADER, VERT_SHADER_SRC_FILE);
unsigned int fragShader = Shader::compileFromFile(GL_FRAGMENT_SHADER, FRAG_SHADER_SRC_FILE);
unsigned int shaders[2] = {vertShader, fragShader};
// Create a shader program & link the shaders etc
unsigned int shaderProg;
shaderProg = Shader::createProgram( std::vector<unsigned int> (std::begin(shaders), std::end(shaders)) );
// Vertex buffer object // Vertex buffer object
unsigned int VBO; unsigned int VBO;
@ -66,16 +57,21 @@ RenderObj preRenderCallback() {
//glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW); //glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
// Set attrib pointers // Set attrib pointers
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); // Pos
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
// Color
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3*sizeof(float)));
glEnableVertexAttribArray(1);
unsigned int EBO; unsigned int EBO;
glGenBuffers(1, &EBO); glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
return RenderObj {EBO, VBO, VAO, shaderProg}; return RenderObj {EBO, shader};
} }
void renderCallback(RenderObj ro) { void renderCallback(RenderObj ro) {
@ -84,9 +80,9 @@ void renderCallback(RenderObj ro) {
float time = glfwGetTime(); float time = glfwGetTime();
float gVal = (sin(time) / 1.5f) + 0.5f; float gVal = (sin(time) / 1.5f) + 0.5f;
int vertColLocation = glGetUniformLocation(ro.shaderProg, "inputColor"); int vertColLocation = glGetUniformLocation(ro.shader.id, "inputColor");
glUseProgram(ro.shaderProg); ro.shader.use();
glUniform4f(vertColLocation, gVal, gVal, gVal, 1.0f); glUniform4f(vertColLocation, gVal, gVal, gVal, 1.0f);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ro.EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ro.EBO);

@ -1,27 +1,30 @@
#include <fstream> #include <fstream>
#include <glad/glad.h> #include <glad/glad.h>
#include <sstream>
#include <vector> #include <vector>
#include <string> #include <string>
#include <iostream> #include <iostream>
namespace Shader { #include "../headers/shaders.hpp"
std::string loadSourceFromFile(const char* file) {
std::string content;
std::ifstream fh(file, std::ios::in);
if(!fh.is_open()) { namespace Shaders {
printf("Unable to read file \"%s\"!", file); std::string loadSourceFromFile(const char* fp) {
return ""; std::string src;
} std::ifstream fh;
std::string line = ""; fh.exceptions(std::ifstream::failbit | std::ifstream::badbit);
while(!fh.eof()) { try {
std::getline(fh, line); fh.open(fp);
content.append(line + "\n"); std::stringstream sstr;
} sstr << fh.rdbuf();
fh.close();
fh.close(); src = sstr.str();
return content.c_str(); return src;
} catch (std::ifstream::failure err) {
printf("[ERROR] Unable to read file \"%s\\n", fp);
exit(1);
}
} }
unsigned int compile(GLenum shadertype, const char* shaderSource) { unsigned int compile(GLenum shadertype, const char* shaderSource) {
@ -72,4 +75,27 @@ namespace Shader {
return shaderProg; return shaderProg;
} }
Shader::Shader(const char* vertPath, const char* fragPath) {
unsigned int vertShader = compileFromFile(GL_VERTEX_SHADER, vertPath);
unsigned int fragShader = compileFromFile(GL_FRAGMENT_SHADER, fragPath);
unsigned int shaders[2] = {vertShader, fragShader};
// Shader program ID
id = Shaders::createProgram( std::vector<unsigned int> (std::begin(shaders), std::end(shaders)) );
}
void Shader::use() { glUseProgram(id); }
void Shader::setInt(const std::string &name, int val) const {
glUniform1i(glGetUniformLocation(id, name.c_str()), val);
}
void Shader::setFloat(const std::string &name, float val) const {
glUniform1f(glGetUniformLocation(id, name.c_str()), val);
}
void Shader::setBool(const std::string &name, bool val) const {
glUniform1i(glGetUniformLocation(id, name.c_str()), (int)val) ;
}
} }

@ -0,0 +1,25 @@
#ifndef SHADERS_H
#define SHADERS_H
#include <glad/glad.h>
#include <string>
#include <vector>
namespace Shaders {
std::string loadSourceFromFile(const char* file);
unsigned int compile(GLenum shadertype, const char* shaderSource);
unsigned int compileFromFile(GLenum shadertype, const char* file);
unsigned int createProgram(std::vector<unsigned int> shaders);
class Shader {
public:
unsigned int id;
Shader(const char* vertPath, const char* fragPath);
void use();
void setBool(const std::string &name, bool val) const;
void setInt(const std::string &name, int val) const;
void setFloat(const std::string &name, float val) const;
};
}
#endif
Loading…
Cancel
Save