Fixed rendering issue & refactored Obj2D

pull/1/head
E. Almqvist 2 years ago
parent f17bd51cf0
commit 71d2a6b343
  1. 16
      headers/renderer.hpp
  2. 7
      shaders/fragment.glsl
  3. 6
      shaders/vertex.glsl
  4. 8
      src/main.cpp
  5. 59
      src/renderer.cpp
  6. 4
      src/shaders.cpp
  7. 2
      src/textures.cpp

@ -24,6 +24,7 @@ namespace Renderer {
RenderObject(std::vector<float> verts, std::vector<unsigned int> indices); RenderObject(std::vector<float> verts, std::vector<unsigned int> indices);
void render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform); void render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform);
void transform(glm::mat4 T); void transform(glm::mat4 T);
void preRenderHook();
private: private:
Shaders::Shader shader; Shaders::Shader shader;
unsigned int EBO; unsigned int EBO;
@ -49,20 +50,13 @@ namespace Renderer {
glm::mat4 projectionTransform = glm::mat4(1.0f); glm::mat4 projectionTransform = glm::mat4(1.0f);
}; };
class Obj2D { class Obj2D : public RenderObject {
public: public:
Shaders::Shader shader; using RenderObject::RenderObject;
Textures::Texture2D texture;
Obj2D(unsigned int indices[], unsigned int icount, float verts[], unsigned int vcount);
void setTexture(const char* t_src); void setTexture(const char* t_src);
void transform(glm::mat4 T); void preRenderHook();
void render(GLFWwindow* win);
private: private:
Textures::Texture2D texture;
void bind_texture(Textures::Texture2D texture); void bind_texture(Textures::Texture2D texture);
unsigned int EBO;
unsigned int VBO;
unsigned int VAO;
}; };
} }

@ -6,11 +6,8 @@ in vec4 VertexColor;
in vec2 TexCoord; in vec2 TexCoord;
uniform sampler2D inpTexture; uniform sampler2D inpTexture;
uniform vec3 rgbOffset = vec3(0.0, 0.0, 0.0);
uniform float r;
uniform float g;
uniform float b;
void main() { void main() {
FragColor = texture(inpTexture, TexCoord) * (VertexColor + vec4(r, g, b, 1.0f)); FragColor = texture(inpTexture, TexCoord) * (VertexColor + vec4(rgbOffset, 1.0));
} }

@ -6,9 +6,9 @@ layout (location = 2) in vec2 aTexCoord;
out vec4 VertexColor; out vec4 VertexColor;
out vec2 TexCoord; out vec2 TexCoord;
uniform mat4 model = mat4(1.0f); uniform mat4 model = mat4(1.0);
uniform mat4 view = mat4(1.0f); uniform mat4 view = mat4(1.0);
uniform mat4 projection = mat4(1.0f); uniform mat4 projection = mat4(1.0);
void main() { void main() {
gl_Position = projection * view * model * vec4(aPos, 1.0); gl_Position = projection * view * model * vec4(aPos, 1.0);

@ -77,9 +77,10 @@ int main() {
float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
Renderer::RenderObject ro(verts, indices); Renderer::Obj2D ro(verts, indices);
renderer.spawnObject(ro); renderer.spawnObject(ro);
// ro.setTexture(RUSTY_METAL_TEXTURE); renderer.setCamera(glm::vec3(0.0f, 0.0f, -8.0f));
ro.setTexture(RUSTY_METAL_TEXTURE);
// Window width & height // Window width & height
while (!glfwWindowShouldClose(win)) { while (!glfwWindowShouldClose(win)) {
@ -91,7 +92,8 @@ int main() {
/* OBJECT RENDERING */ /* OBJECT RENDERING */
float time = glfwGetTime(); float time = glfwGetTime();
float gVal = (sin(time) / 1.5f) + 0.5f; float gVal = sin(time) / 10.5f;
renderer.setCamera(glm::vec3(0.0f, 0.0f, -gVal));
// Transformation // Transformation
float rotang = time; float rotang = time;

@ -80,12 +80,16 @@ namespace Renderer {
glEnableVertexAttribArray(2); glEnableVertexAttribArray(2);
} }
void RenderObject::preRenderHook() {}
void RenderObject::render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform) { void RenderObject::render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform) {
shader.setMat4("view", cameraTransform); shader.setMat4("view", cameraTransform);
shader.setMat4("projection", projectionTransform); shader.setMat4("projection", projectionTransform);
shader.use(); shader.use();
preRenderHook();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glDrawElements(GL_TRIANGLES, indicesVec.size(), GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, indicesVec.size(), GL_UNSIGNED_INT, 0);
} }
@ -94,65 +98,16 @@ namespace Renderer {
shader.setMat4("model", T); shader.setMat4("model", T);
} }
void Obj2D::transform(glm::mat4 T) { // Obj2D
shader.setMat4("model", T);
}
void Obj2D::setTexture(const char* t_src) { void Obj2D::setTexture(const char* t_src) {
texture.texture_src = t_src; texture.texture_src = t_src;
texture.load(); texture.load();
} }
void Obj2D::render(GLFWwindow* win) { void Obj2D::preRenderHook() {
int width, height; printf("OBJ2D was here!\n");
// Camera
glm::mat4 view = glm::mat4(1.0f);
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -8.0f)); // Camera position
shader.setMat4("view", view);
glfwGetWindowSize(win, &width, &height);
glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f);
shader.setMat4("projection", projection);
// Use the shader etc
shader.use();
if (texture.loaded) if (texture.loaded)
texture.bind(); texture.bind();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
Obj2D::Obj2D(unsigned int indices[], unsigned int icount, float verts[], unsigned int vcount)
: shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE) {
// Vertex Array Object
glGenVertexArrays(1, &VAO); // gen the VAO
glBindVertexArray(VAO); // bind it
// Copy the verts into the buffer
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vcount, verts, GL_DYNAMIC_DRAW); // for moving stuff
// Copy the indices for the verts into another buffer
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, icount, indices, GL_STATIC_DRAW);
// Pos
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)0);
glEnableVertexAttribArray(0);
// Color
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(3*sizeof(float)));
glEnableVertexAttribArray(1);
// Texture
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(6*sizeof(float)));
glEnableVertexAttribArray(2);
} }
// Private stuff // Private stuff

@ -81,10 +81,10 @@ namespace Shaders {
Shader::Shader(const char* vertPath, const char* fragPath) { Shader::Shader(const char* vertPath, const char* fragPath) {
unsigned int vertShader = compileFromFile(GL_VERTEX_SHADER, vertPath); unsigned int vertShader = compileFromFile(GL_VERTEX_SHADER, vertPath);
unsigned int fragShader = compileFromFile(GL_FRAGMENT_SHADER, fragPath); unsigned int fragShader = compileFromFile(GL_FRAGMENT_SHADER, fragPath);
unsigned int shaders[2] = {vertShader, fragShader}; std::vector<unsigned int> shaders({vertShader, fragShader});
// Shader program ID // Shader program ID
id = Shaders::createProgram( std::vector<unsigned int> (std::begin(shaders), std::end(shaders)) ); id = Shaders::createProgram(shaders);
} }
void Shader::use() { glUseProgram(id); } void Shader::use() { glUseProgram(id); }

@ -29,13 +29,11 @@ namespace Textures {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
printf("[%i] \"%s\" - ", id, texture_src);
unsigned char* data = stbi_load(texture_src, &width, &height, &nrChannels, 0); unsigned char* data = stbi_load(texture_src, &width, &height, &nrChannels, 0);
if (data) { if (data) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
printf("Loaded!\n");
loaded = true; loaded = true;
} else { } else {
printf("[ERROR] Failed to load texture \"%s\"\n", texture_src); printf("[ERROR] Failed to load texture \"%s\"\n", texture_src);

Loading…
Cancel
Save