From 96a06451d198d3cd9ecba1ecd8fdd615bce3f451 Mon Sep 17 00:00:00 2001 From: Tipe Date: Sun, 16 Feb 2025 03:21:05 +0200 Subject: [PATCH 1/2] stuff --- CMakeLists.txt | 4 +- src/classes/Shape.cpp | 28 ++++++++++++ src/classes/Shape.h | 26 +++++++++++ src/main.cpp | 66 ++++++++++++++++++++------- src/shaders/frag_rgb_shader.frag.glsl | 16 +++++++ 5 files changed, 122 insertions(+), 18 deletions(-) create mode 100644 src/classes/Shape.cpp create mode 100644 src/classes/Shape.h create mode 100644 src/shaders/frag_rgb_shader.frag.glsl diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c2580a..b9c2bea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,8 @@ add_executable( src/classes/Classes.h src/classes/Shader.h src/classes/Shader.cpp + src/classes/Shape.h + src/classes/Shape.cpp src/classes/debug.cpp src/classes/debug.h src/classes/ElementBufferObject.h @@ -26,6 +28,6 @@ add_executable( src/classes/VertexBufferObject.cpp src/classes/Texture.h src/classes/Texture.cpp -) + ) target_link_libraries(learn_shaders glfw OpenGL::GL) diff --git a/src/classes/Shape.cpp b/src/classes/Shape.cpp new file mode 100644 index 0000000..9bdca37 --- /dev/null +++ b/src/classes/Shape.cpp @@ -0,0 +1,28 @@ +#include "Shape.h" +#include "../glad/glad.h" +#include "ElementBufferObject.h" +#include "Shader.h" +#include "VertexArrayObject.h" +#include "VertexBufferObject.h" + +Shape::Shape(float vert[], unsigned int vertCount, unsigned int indi[], + unsigned int indiCount) + : VBO(vert, vertCount), EBO(indi, indiCount), indi_count(indiCount) { + VAO.Bind(); + VBO.Bind(); + EBO.Bind(); + + VAO.LinkVBO(VBO, 0); +} + +void Shape::Render(Shader &shader) { + shader.Activate(); + VAO.Bind(); + glDrawElements(GL_TRIANGLES, indi_count, GL_UNSIGNED_INT, 0); +} + +void Shape::Delete() { + VAO.Delete(); + VBO.Delete(); + EBO.Delete(); +} diff --git a/src/classes/Shape.h b/src/classes/Shape.h new file mode 100644 index 0000000..fa77860 --- /dev/null +++ b/src/classes/Shape.h @@ -0,0 +1,26 @@ +#ifndef SHAPE_H +#define SHAPE_H + +#include "../glad/glad.h" +#include "ElementBufferObject.h" +#include "Shader.h" +#include "VertexArrayObject.h" +#include "VertexBufferObject.h" + +class Shape { + public: + VertexArrayObject VAO; + VertexBufferObject VBO; + ElementBufferObject EBO; + + unsigned indi_count; + + Shape(float vert[], unsigned int vertCount, unsigned int indi[], + unsigned int indiCount); + + virtual void Render(Shader &shader); + + virtual void Delete(); +}; + +#endif diff --git a/src/main.cpp b/src/main.cpp index c49de04..4809aa9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ */ #include "classes/ElementBufferObject.h" +#include "classes/Shape.h" #include "classes/VertexArrayObject.h" #include "classes/VertexBufferObject.h" #include "glad/glad.h" @@ -16,7 +17,7 @@ void processInput(GLFWwindow *window); // Constants. const unsigned int WINDOW_WIDTH = 800; -const unsigned int WINDOW_HEIGHT = 600; +const unsigned int WINDOW_HEIGHT = 800; float vertices[] = { 1.0f, 1.0f, 0.0f, // top right @@ -30,6 +31,42 @@ unsigned int indices[] = { 1, 2, 3 // second Triangle }; +float vert1[] = { + 0.0f, 1.0f, 0.0f, + 0.5f, -0.50f, 0.0f, + -0.5f, -0.50f, 0.0f +}; + +float vert2[] = { + 0.0f, -1.0f, 0.0f, + 0.5f, 0.50f, 0.0f, + -0.5f, 0.50f, 0.0f +}; + +float vert3[] = { + 0.0f, 1.0f, 0.0f, + 1.0f, 0.25f, 0.0f, + 1.0f, -0.25f, 0.0f, + 0.0f, -1.0f, 0.0f, + -1.0f, -0.25f, 0.0f, + -1.0f, 0.25f, 0.0f, + 0.0f, 0.0f, 0.0f, + +}; + +unsigned int indi[] = { + 0, 1, 2 +}; + +unsigned int indi2[] = { + 0, 6, 1, + 1, 6, 2, + 2, 6, 3, + 3, 6, 4, + 4, 6, 5, + 5, 6, 0 +}; + int main(void) { // Initialisation and configuration. glfwInit(); @@ -54,34 +91,29 @@ int main(void) { } // Shader stuff. - Shader shader("../src/shaders/vertexShader.vert.glsl", "../src/shaders/fragmentShader.frag.glsl"); - - VertexArrayObject VAO; - VAO.Bind(); - VertexBufferObject VBO(vertices, sizeof(vertices)); - VBO.Bind(); - ElementBufferObject EBO(indices, sizeof(indices)); - EBO.Bind(); - - VAO.LinkVBO(VBO, 0); + Shader shader("../src/shaders/vertexShader.vert.glsl", "../src/shaders/frag_rgb_shader.frag.glsl"); + Shape shape1(vertices, sizeof(vertices), indices, sizeof(indices)); + // Shape shape2(vert2, sizeof(vert2), indi, sizeof(indi)); + // while loop goes here. while (!glfwWindowShouldClose(window)) { processInput(window); - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); - shader.Activate(); - VAO.Bind(); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + shape1.Render(shader); + // shape2.Render(shader); + glfwSwapBuffers(window); glfwPollEvents(); } - VAO.Delete(); - VBO.Delete(); - EBO.Delete(); + shape1.Delete(); + // shape2.Delete(); + shader.Delete(); glfwDestroyWindow(window); diff --git a/src/shaders/frag_rgb_shader.frag.glsl b/src/shaders/frag_rgb_shader.frag.glsl new file mode 100644 index 0000000..c1e2c54 --- /dev/null +++ b/src/shaders/frag_rgb_shader.frag.glsl @@ -0,0 +1,16 @@ +#version 330 core + +out vec4 FragColour; + +void main() { + vec2 pixelPos = gl_FragCoord.xy; + + float x = pixelPos.x / 800.0f; + float y = pixelPos.y / 800.0f; + + float r = 1 - x + y; + float g = x + y; + float b = 1.5 - y; + + FragColour = vec4(r, g, b, 1.0f); +} From a6dd0f9d6ad47058d64b585370698705904d1722 Mon Sep 17 00:00:00 2001 From: Tipe Date: Sun, 16 Feb 2025 03:22:56 +0200 Subject: [PATCH 2/2] stuff --- .gitignore | 1 + src/shaders/frag_rgb_shader.frag.glsl | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 796b96d..30e0c03 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /build +/.git diff --git a/src/shaders/frag_rgb_shader.frag.glsl b/src/shaders/frag_rgb_shader.frag.glsl index c1e2c54..fbb17df 100644 --- a/src/shaders/frag_rgb_shader.frag.glsl +++ b/src/shaders/frag_rgb_shader.frag.glsl @@ -10,7 +10,7 @@ void main() { float r = 1 - x + y; float g = x + y; - float b = 1.5 - y; + float b = 1. - y; FragColour = vec4(r, g, b, 1.0f); }