Merge pull request #1 from TipeSor/main

Stuff
This commit is contained in:
CJ Satnarine
2025-02-16 23:28:21 -05:00
committed by GitHub
6 changed files with 123 additions and 18 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
/build /build
/.git

View File

@@ -16,6 +16,8 @@ add_executable(
src/classes/Classes.h src/classes/Classes.h
src/classes/Shader.h src/classes/Shader.h
src/classes/Shader.cpp src/classes/Shader.cpp
src/classes/Shape.h
src/classes/Shape.cpp
src/classes/debug.cpp src/classes/debug.cpp
src/classes/debug.h src/classes/debug.h
src/classes/ElementBufferObject.h src/classes/ElementBufferObject.h
@@ -26,6 +28,6 @@ add_executable(
src/classes/VertexBufferObject.cpp src/classes/VertexBufferObject.cpp
src/classes/Texture.h src/classes/Texture.h
src/classes/Texture.cpp src/classes/Texture.cpp
) )
target_link_libraries(learn_shaders glfw OpenGL::GL) target_link_libraries(learn_shaders glfw OpenGL::GL)

28
src/classes/Shape.cpp Normal file
View File

@@ -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();
}

26
src/classes/Shape.h Normal file
View File

@@ -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

View File

@@ -4,6 +4,7 @@
*/ */
#include "classes/ElementBufferObject.h" #include "classes/ElementBufferObject.h"
#include "classes/Shape.h"
#include "classes/VertexArrayObject.h" #include "classes/VertexArrayObject.h"
#include "classes/VertexBufferObject.h" #include "classes/VertexBufferObject.h"
#include "glad/glad.h" #include "glad/glad.h"
@@ -16,7 +17,7 @@ void processInput(GLFWwindow *window);
// Constants. // Constants.
const unsigned int WINDOW_WIDTH = 800; const unsigned int WINDOW_WIDTH = 800;
const unsigned int WINDOW_HEIGHT = 600; const unsigned int WINDOW_HEIGHT = 800;
float vertices[] = { float vertices[] = {
1.0f, 1.0f, 0.0f, // top right 1.0f, 1.0f, 0.0f, // top right
@@ -30,6 +31,42 @@ unsigned int indices[] = {
1, 2, 3 // second Triangle 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) { int main(void) {
// Initialisation and configuration. // Initialisation and configuration.
glfwInit(); glfwInit();
@@ -54,34 +91,29 @@ int main(void) {
} }
// Shader stuff. // Shader stuff.
Shader shader("../src/shaders/vertexShader.vert.glsl", "../src/shaders/fragmentShader.frag.glsl"); Shader shader("../src/shaders/vertexShader.vert.glsl", "../src/shaders/frag_rgb_shader.frag.glsl");
VertexArrayObject VAO; Shape shape1(vertices, sizeof(vertices), indices, sizeof(indices));
VAO.Bind(); // Shape shape2(vert2, sizeof(vert2), indi, sizeof(indi));
VertexBufferObject VBO(vertices, sizeof(vertices));
VBO.Bind();
ElementBufferObject EBO(indices, sizeof(indices));
EBO.Bind();
VAO.LinkVBO(VBO, 0);
// while loop goes here. // while loop goes here.
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
processInput(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); glClear(GL_COLOR_BUFFER_BIT);
shader.Activate();
VAO.Bind();
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
shape1.Render(shader);
// shape2.Render(shader);
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
} }
VAO.Delete(); shape1.Delete();
VBO.Delete(); // shape2.Delete();
EBO.Delete();
shader.Delete(); shader.Delete();
glfwDestroyWindow(window); glfwDestroyWindow(window);

View File

@@ -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. - y;
FragColour = vec4(r, g, b, 1.0f);
}