From ed1cc02d3e4760d55b407009319adb5330bc71fd Mon Sep 17 00:00:00 2001 From: MichaelFisher1997 Date: Sat, 11 Jan 2025 17:48:48 +0000 Subject: [PATCH 1/5] openGL abstractiopn, segment fault!! --- .vscode/c_cpp_properties.json | 16 +++++++++ .vscode/launch.json | 55 +++++++++++++++++++++++++++++++ .vscode/settings.json | 61 +++++++++++++++++++++++++++++++++++ MakeFile | 41 ----------------------- Makefile | 21 ++++++++++++ src/IndexBuffer.cpp | 27 ++++++++++++++++ src/IndexBuffer.h | 16 +++++++++ src/Renderer.cpp | 19 +++++++++++ src/Renderer.h | 37 +++++++++++++++++++++ src/VertexArray.cpp | 39 ++++++++++++++++++++++ src/VertexArray.h | 18 +++++++++++ src/VertexBuffer.cpp | 24 ++++++++++++++ src/VertexBuffer.h | 13 ++++++++ src/VertexBufferLayout.h | 56 ++++++++++++++++++++++++++++++++ src/colormod.h | 3 ++ src/sdl.cpp | 55 ++++++++++++++++++++----------- src/sdl.hpp | 17 ++++++---- 17 files changed, 452 insertions(+), 66 deletions(-) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json delete mode 100644 MakeFile create mode 100755 Makefile create mode 100644 src/IndexBuffer.cpp create mode 100644 src/IndexBuffer.h create mode 100644 src/Renderer.cpp create mode 100644 src/Renderer.h create mode 100644 src/VertexArray.cpp create mode 100644 src/VertexArray.h create mode 100644 src/VertexBuffer.cpp create mode 100644 src/VertexBuffer.h create mode 100644 src/VertexBufferLayout.h diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..e4fbba0 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,16 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "compilerPath": "/nix/store/xcn9p4xxfbvlkpah7pwchpav4ab9d135-gcc-wrapper-14-20241116/bin/gcc", + "cStandard": "c17", + "cppStandard": "gnu++17", + "intelliSenseMode": "linux-gcc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..08caf88 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,55 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "type": "cppdbg", + "request": "launch", + "name": "Launch Program", + "program": "${workspaceFolder}/build/${input:executableName}", + "preLaunchTask": "build", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ], + "miDebuggerPath": "/usr/bin/gdb", + "logging": { + "engineLogging": true + } + }, + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/micqdf/opengl/cpp/src", + "program": "/home/micqdf/opengl/cpp/src/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "/nix/store/xcn9p4xxfbvlkpah7pwchpav4ab9d135-gdb-wrapper-14-20241116/bin/gcc", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ], + "inputs": [ + { + "type": "promptString", + "id": "executableName", + "description": "Name of your executable" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..50ea268 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,61 @@ +{ + "C_Cpp_Runner.cCompilerPath": "/nix/store/xcn9p4xxfbvlkpah7pwchpav4ab9d135-gcc-wrapper-14-20241116/bin/gcc", + "C_Cpp_Runner.cppCompilerPath": "/nix/store/xcn9p4xxfbvlkpah7pwchpav4ab9d135-g++-wrapper-14-20241116/bin/gcc", + "C_Cpp_Runner.debuggerPath": "/nix/store/xcn9p4xxfbvlkpah7pwchpav4ab9d135-gdb-wrapper-14-20241116/bin/gcc", + "C_Cpp_Runner.cStandard": "c17", + "C_Cpp_Runner.cppStandard": "gnu++17", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [ + "${workspaceFolder}/**" + ], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/MakeFile b/MakeFile deleted file mode 100644 index f2b7558..0000000 --- a/MakeFile +++ /dev/null @@ -1,41 +0,0 @@ -# A simple Makefile for compiling small SDL projects - -# set the compiler -CC := clang - -# set the compiler flags -CFLAGS := `sdl2-config --libs --cflags` -ggdb3 -O0 --std=c99 -Wall -lSDL2_image -lm -# add header files here -HDRS := - -# add source files here -SRCS := #file-name.c - -# generate names of object files -OBJS := $(SRCS:.c=.o) - -# name of executable -EXEC := #name your executable file - -# default recipe -all: $(EXEC) - -showfont: showfont.c Makefile - $(CC) -o $@ $@.c $(CFLAGS) $(LIBS) - -glfont: glfont.c Makefile - $(CC) -o $@ $@.c $(CFLAGS) $(LIBS) - -# recipe for building the final executable -$(EXEC): $(OBJS) $(HDRS) Makefile - $(CC) -o $@ $(OBJS) $(CFLAGS) - -# recipe for building object files -#$(OBJS): $(@:.o=.c) $(HDRS) Makefile -# $(CC) -o $@ $(@:.o=.c) -c $(CFLAGS) - -# recipe to clean the workspace -clean: - rm -f $(EXEC) $(OBJS) - -.PHONY: all clean diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..124b70a --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +CC = g++ +CFLAGS = -Iinclude -Wall -g +LDFLAGS = -lSDL2 -lGL -lGLEW + +SRC = src/main.cpp src/sdl.cpp +OBJ = $(SRC:.cpp=.o) +EXEC = opengl-app + +all: $(EXEC) + +$(EXEC): $(OBJ) + $(CC) -o $@ $^ $(LDFLAGS) + +%.o: %.cpp + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + rm -f $(OBJ) $(EXEC) + +run: all + ./$(EXEC) \ No newline at end of file diff --git a/src/IndexBuffer.cpp b/src/IndexBuffer.cpp new file mode 100644 index 0000000..74c7fe6 --- /dev/null +++ b/src/IndexBuffer.cpp @@ -0,0 +1,27 @@ +#include "IndexBuffer.h" +#include "Renderer.h" + +IndexBuffer::IndexBuffer(const unsigned int* data, unsigned int count) + : m_Count(count) +{ + //ASSERT(sizeof(unsigned int) == sizeof(GLuint)); + // + GLCall(glGenBuffers(1, &m_RendererID)); + GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID)); //select buffer called 'buffer' + GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(GLuint), data, GL_STATIC_DRAW)); // assigne buffer size, static as we use many times, but does not change +} + +IndexBuffer::~IndexBuffer() +{ + GLCall(glDeleteBuffers(1, &m_RendererID)); +} + +void IndexBuffer::Bind() const +{ + GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID)); //select buffer called 'buffer' +} + +void IndexBuffer::Unbind() const +{ + GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)); //select buffer called 'buffer' +} diff --git a/src/IndexBuffer.h b/src/IndexBuffer.h new file mode 100644 index 0000000..3b663e4 --- /dev/null +++ b/src/IndexBuffer.h @@ -0,0 +1,16 @@ +#pragma once + +class IndexBuffer +{ +private: + unsigned int m_RendererID; + unsigned int m_Count; +public: + IndexBuffer(const unsigned int* data, unsigned int count); + ~IndexBuffer(); + + void Bind() const; + void Unbind() const; + + inline unsigned int GetCount() const { return m_Count; } +}; diff --git a/src/Renderer.cpp b/src/Renderer.cpp new file mode 100644 index 0000000..552d629 --- /dev/null +++ b/src/Renderer.cpp @@ -0,0 +1,19 @@ +#include "Renderer.h" + + + +void GLClearError() { + while (glGetError() != GL_NO_ERROR); + +} + +bool GLLogCall() { + Color::Modifier red(Color::FG_RED); + Color::Modifier def(Color::FG_DEFAULT); + while (GLenum error = glGetError()) { + std::cout << red << "[OpenGL Error] (" << error << ")" << def << std::endl; //if error, it will return a number, this needs to be converted to hex to then look up that value inn GL docs + return false; + } + return true; +} + diff --git a/src/Renderer.h b/src/Renderer.h new file mode 100644 index 0000000..4bc906f --- /dev/null +++ b/src/Renderer.h @@ -0,0 +1,37 @@ +#pragma once +#include "colormod.h" +#include + +#include + + +#if defined(_MSC_VER) // Microsoft Visual C++ + #include + #define DEBUG_BREAK() __debugbreak() +#elif defined(__i386__) || defined(__x86_64__) + // Use inline assembly for x86/x86_64 + #define DEBUG_BREAK() __asm__ volatile("int3") +#else + // Fallback on non-x86 platforms + #include + #define DEBUG_BREAK() raise(SIGTRAP) +#endif + +// ASSERT macro that shows file, line, and the failed expression +#define ASSERT(x) \ + do { \ + if (!(x)) { \ + std::cerr << "Assertion Failed: " << #x << '\n' \ + << "File: " << __FILE__ << '\n' \ + << "Line: " << __LINE__ << std::endl; \ + DEBUG_BREAK(); \ + } \ + } while (false) + +#define GLCall(x) GLClearError();\ + x;\ + ASSERT(GLLogCall()) + + +void GLClearError(); +bool GLLogCall(); diff --git a/src/VertexArray.cpp b/src/VertexArray.cpp new file mode 100644 index 0000000..c933449 --- /dev/null +++ b/src/VertexArray.cpp @@ -0,0 +1,39 @@ +#include "VertexArray.h" +#include "VertexBuffer.h" +#include "Renderer.h" +#include "VertexBufferLayout.h" + +VertexArray::VertexArray() +{ + GLCall(glGenVertexArrays(1, &m_RendererID)); + if (!m_RendererID) { + std::cerr << "Failed to generate VAO" << std::endl; + } + + +} +VertexArray::~VertexArray() +{ + GLCall(glDeleteVertexArrays(1, &m_RendererID)); +} + +void VertexArray::AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout) +{ + Bind(); + vb.Bind(); + const auto& elements = layout.GetElements(); + GLsizei offset = 0; + for (unsigned int i = 0; i < elements.size(); i++) { + const auto& element = elements[i]; + GLCall(glEnableVertexAttribArray(i)); + GLCall(glVertexAttribPointer(i, element.count, element.type, element.normalized, layout.GetStride(), (const void*)offset)); + offset += element.count * VertexBufferElement::GetSizeOfType(element.type); + } +} + +void VertexArray::Bind() const { + GLCall(glBindVertexArray(m_RendererID)); +} +void VertexArray::Unbind() const { + GLCall(glBindVertexArray(0)); +} diff --git a/src/VertexArray.h b/src/VertexArray.h new file mode 100644 index 0000000..31a711e --- /dev/null +++ b/src/VertexArray.h @@ -0,0 +1,18 @@ +#pragma once +#include "VertexBuffer.h" +#include "VertexBufferLayout.h" + +class VertexArray +{ + private: + unsigned int m_RendererID; + + public: + VertexArray(); + ~VertexArray(); + void AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout); + + void Bind() const; + void Unbind() const; +}; + diff --git a/src/VertexBuffer.cpp b/src/VertexBuffer.cpp new file mode 100644 index 0000000..12fb9d9 --- /dev/null +++ b/src/VertexBuffer.cpp @@ -0,0 +1,24 @@ +#include "VertexBuffer.h" +#include "Renderer.h" + +VertexBuffer::VertexBuffer(const void* data, unsigned int size) +{ + GLCall(glGenBuffers(1, &m_RendererID)); + GLCall(glBindBuffer(GL_ARRAY_BUFFER, m_RendererID)); //select buffer called 'buffer' + GLCall(glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW)); // assigne buffer size, static as we use many times, but does not change +} + +VertexBuffer::~VertexBuffer() +{ + GLCall(glDeleteBuffers(1, &m_RendererID)); +} + +void VertexBuffer::Bind() const +{ + GLCall(glBindBuffer(GL_ARRAY_BUFFER, m_RendererID)); //select buffer called 'buffer' +} + +void VertexBuffer::Unbind() const +{ + GLCall(glBindBuffer(GL_ARRAY_BUFFER, 0)); //select buffer called 'buffer' +} diff --git a/src/VertexBuffer.h b/src/VertexBuffer.h new file mode 100644 index 0000000..75d8907 --- /dev/null +++ b/src/VertexBuffer.h @@ -0,0 +1,13 @@ +#pragma once + +class VertexBuffer +{ +private: + unsigned int m_RendererID; +public: + VertexBuffer(const void* data, unsigned int size); + ~VertexBuffer(); + + void Bind() const; + void Unbind() const; +}; diff --git a/src/VertexBufferLayout.h b/src/VertexBufferLayout.h new file mode 100644 index 0000000..27b18ba --- /dev/null +++ b/src/VertexBufferLayout.h @@ -0,0 +1,56 @@ +#ifndef VERTEXBUFFERLAYOUT_H +#define VERTEXBUFFERLAYOUT_H +#include +#include "Renderer.h" +struct VertexBufferElement { + unsigned int type; + unsigned int count; + unsigned char normalized; + + VertexBufferElement(unsigned int type, unsigned int count, unsigned char normalized) + : type(type), count(count), normalized(normalized) {} + + static unsigned int GetSizeOfType(unsigned int type) { + switch (type) { + case GL_FLOAT: return sizeof(GLfloat); + case GL_UNSIGNED_INT: return sizeof(GLuint); + case GL_UNSIGNED_BYTE: return sizeof(GLubyte); + } + return 0; + } +}; + +class VertexBufferLayout { +public: + VertexBufferLayout() : m_Stride(0) {} + + template + void Push(int count); + + inline const std::vector& GetElements() const { return m_Elements; } + inline unsigned int GetStride() const { return m_Stride; } + +private: + std::vector m_Elements; + unsigned int m_Stride; +}; + +// Explicit specializations +template<> +inline void VertexBufferLayout::Push(int count) { + m_Elements.push_back({ GL_FLOAT, static_cast(count), GL_FALSE }); + m_Stride += VertexBufferElement::GetSizeOfType(GL_FLOAT); +} + +template<> +inline void VertexBufferLayout::Push(int count) { + m_Elements.push_back({ GL_UNSIGNED_INT,static_cast(count), GL_FALSE }); + m_Stride += VertexBufferElement::GetSizeOfType(GL_UNSIGNED_INT); +} + +template<> +inline void VertexBufferLayout::Push(int count) { + m_Elements.push_back({ GL_UNSIGNED_BYTE, static_cast(count), GL_TRUE }); + m_Stride += VertexBufferElement::GetSizeOfType(GL_UNSIGNED_BYTE); +} +#endif // VERTEXBUFFERLAYOUT_H \ No newline at end of file diff --git a/src/colormod.h b/src/colormod.h index 181d550..5ba4e10 100644 --- a/src/colormod.h +++ b/src/colormod.h @@ -1,3 +1,5 @@ +#ifndef COLORMOD_H +#define COLORMOD_H #include namespace Color { enum Code { @@ -20,3 +22,4 @@ namespace Color { } }; } +#endif // COLORMOD_H \ No newline at end of file diff --git a/src/sdl.cpp b/src/sdl.cpp index bf8d963..bf76e53 100644 --- a/src/sdl.cpp +++ b/src/sdl.cpp @@ -1,5 +1,6 @@ #include // Include GLEW before ? #include "sdl.hpp" +#include "VertexBufferLayout.h" #include "colormod.h" #include #include @@ -54,9 +55,10 @@ SdlWindow::SdlWindow(const char* title, int width, int height) m_glContext(nullptr), m_windowedWidth(width), m_windowedHeight(height), - r(0.0f), + r(0.5f), location(), - increment(0.05f) + increment(0.05f), + ib(nullptr, 0) { // 1. Set attributes SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); @@ -95,6 +97,7 @@ SdlWindow::SdlWindow(const char* title, int width, int height) // 5. Set vsync (optional) SDL_GL_SetSwapInterval(1); //Vsync + SdlWindow::GLClearError(); // 6. Mark as running m_isRunning = true; @@ -111,28 +114,32 @@ SdlWindow::SdlWindow(const char* title, int width, int height) 0, 1, 2, 2, 3, 0 }; - //vertex buffer - unsigned int buffer; - GLCall(glGenBuffers(1, &buffer)); - GLCall(glBindBuffer(GL_ARRAY_BUFFER, buffer)); //select buffer called 'buffer' - GLCall(glBufferData(GL_ARRAY_BUFFER, 6 * 2 * sizeof(float), positions, GL_STATIC_DRAW)); // assigne buffer size, static as we use many times, but does not change - //vertext attributes / layout - GLCall(glEnableVertexAttribArray(0)); - GLCall(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0)); - //indext beffer - unsigned int ibo; //indext buffer object - GLCall(glGenBuffers(1, &ibo)); - GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo)); //select buffer called 'buffer' - GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), indices, GL_STATIC_DRAW)); // assigne buffer size, static as we use many times, but does not change + + unsigned int vao; //vertext array object + GLCall(glGenVertexArrays(1, &vao)); + GLCall(glBindVertexArray(vao)); + + VertexArray va; + VertexBuffer vb(positions, 4 * 2 * sizeof(float)); + VertexBufferLayout layout; + layout.Push(2); + va.AddBuffer(vb, layout); + + IndexBuffer ib(indices, 6); ShaderProgramSource source = parseShader("res/shaders/Basic.shader"); - unsigned int shader = createShader(source.VertexSource, source.FragmentSource); - GLCall(glUseProgram(shader)); + unsigned int m_ShaderID = createShader(source.VertexSource, source.FragmentSource); + GLCall(glUseProgram(m_ShaderID)); - GLCall(int location = glGetUniformLocation(shader, "u_Color")); + GLCall(int location = glGetUniformLocation(m_ShaderID, "u_Color")); ASSERT(location != -1); // -1 is an error GLCall(glUniform4f(location, 0.8f, 0.3f, 0.8f, 1.0f)); + GLCall(glBindVertexArray(0)); + GLCall(glUseProgram(0)); + GLCall(glBindBuffer(GL_ARRAY_BUFFER, 0)); + GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)); + } @@ -153,7 +160,12 @@ SdlWindow::~SdlWindow() { SDL_DestroyWindow(m_window); m_window = nullptr; } + if (shader) { + glDeleteProgram(shader); + shader = 0; + } + delete &ib; SDL_Quit(); } @@ -226,10 +238,15 @@ void SdlWindow::update() { void SdlWindow::render() { // Use GL calls instead of SDL’s renderer GLCall(glClearColor(0.0f, 0.0f, 0.0f, 1.0f)); //background + // GLCall(glClear(GL_COLOR_BUFFER_BIT)); + + GLCall(glUseProgram(shader)); + GLCall(glUniform4f(location, r, 0.3f, 0.8f, 1.0f)); + va.Bind(); + ib.Bind(); // TODO: Draw with OpenGL here (shaders, triangles, etc.) - GLCall(glUniform4f( location, r, 0.3f, 0.8f, 1.0f)); //glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr); GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr)); //macro assert for debugging diff --git a/src/sdl.hpp b/src/sdl.hpp index fc049f2..469a563 100644 --- a/src/sdl.hpp +++ b/src/sdl.hpp @@ -9,10 +9,10 @@ #include // or if using GLAD #endif -//#include -#include #include -#include +#include "IndexBuffer.h" +#include "VertexBufferLayout.h" +#include "VertexArray.h" // Forward declaration of classes and structs if needed // class SomethingElse; @@ -54,14 +54,19 @@ private: int m_windowedWidth; // stored width before fullscreen int m_windowedHeight; // stored height before fullscreen float r; - int location; SDL_GLContext m_glContext; float increment; // temp shader stuff std::string vetexShader; std::string fragmentShader; - unsigned int shader; - + unsigned int buffer; + unsigned int ibo; + unsigned int vao; + IndexBuffer ib; // pointer, no default constructor needed + unsigned int shader; + unsigned int location; + VertexBufferLayout layout; + VertexArray va; // Private methods void processEvents(); From 3e799fd4345c335b192f926d0b80bb15dd917bd0 Mon Sep 17 00:00:00 2001 From: MichaelFisher1997 Date: Sat, 11 Jan 2025 18:56:38 +0000 Subject: [PATCH 2/5] cleaned up a bit and got rid of some errors, still seg fualt --- Makefile | 43 ++++++++++++++++++++++++++++------------ src/IndexBuffer.cpp | 2 +- src/IndexBuffer.h | 6 +++--- src/Renderer.h | 2 +- src/VertexArray.cpp | 14 ++++++------- src/VertexBufferLayout.h | 1 + src/sdl.cpp | 26 ++++++++++++++++-------- 7 files changed, 60 insertions(+), 34 deletions(-) diff --git a/Makefile b/Makefile index 124b70a..e26ddfc 100755 --- a/Makefile +++ b/Makefile @@ -1,21 +1,38 @@ -CC = g++ -CFLAGS = -Iinclude -Wall -g -LDFLAGS = -lSDL2 -lGL -lGLEW +CC=clang++ +current_directory=$(shell pwd) -SRC = src/main.cpp src/sdl.cpp -OBJ = $(SRC:.cpp=.o) -EXEC = opengl-app +FRAMEWORKS=-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo -all: $(EXEC) +CFLAGS=-std=c++11 +CFLAGS+=-I$(current_directory) +CFLAGS+=-I$(current_directory)/../external + +LDFLAGS=-L$(current_directory)/../lib +LDFLAGS+=-lglfw3 +LDFLAGS+=-lGLEW + +SOURCES=$(wildcard *.cpp) +OBJECTS=$(patsubst %.cpp, %.o, $(SOURCES)) -$(EXEC): $(OBJ) - $(CC) -o $@ $^ $(LDFLAGS) %.o: %.cpp - $(CC) $(CFLAGS) -c $< -o $@ + $(CC) $(CFLAGS) -c -o $@ $^ +default: debug + +app: $(OBJECTS) + $(CC) $(CFLAGS) $(LDFLAGS) $(FRAMEWORKS) -o $@ $(OBJECTS) + +# Define debug and -g enables debug symbols +debug: CFLAGS+=-DDEBUG -g +debug: app + +release: app + +.PHONY: clean clean: - rm -f $(OBJ) $(EXEC) + rm -f *.o app -run: all - ./$(EXEC) \ No newline at end of file +.PHONY: debugger +debugger: debug + PATH=/usr/bin /usr/bin/lldb ./app \ No newline at end of file diff --git a/src/IndexBuffer.cpp b/src/IndexBuffer.cpp index 74c7fe6..1cf66db 100644 --- a/src/IndexBuffer.cpp +++ b/src/IndexBuffer.cpp @@ -4,7 +4,7 @@ IndexBuffer::IndexBuffer(const unsigned int* data, unsigned int count) : m_Count(count) { - //ASSERT(sizeof(unsigned int) == sizeof(GLuint)); + ASSERT(sizeof(unsigned int) == sizeof(GLuint)); // GLCall(glGenBuffers(1, &m_RendererID)); GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID)); //select buffer called 'buffer' diff --git a/src/IndexBuffer.h b/src/IndexBuffer.h index 3b663e4..d37125f 100644 --- a/src/IndexBuffer.h +++ b/src/IndexBuffer.h @@ -2,9 +2,6 @@ class IndexBuffer { -private: - unsigned int m_RendererID; - unsigned int m_Count; public: IndexBuffer(const unsigned int* data, unsigned int count); ~IndexBuffer(); @@ -13,4 +10,7 @@ public: void Unbind() const; inline unsigned int GetCount() const { return m_Count; } +private: + unsigned int m_RendererID; + unsigned int m_Count; }; diff --git a/src/Renderer.h b/src/Renderer.h index 4bc906f..8de98da 100644 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -32,6 +32,6 @@ x;\ ASSERT(GLLogCall()) - +#define INT2VOIDP(i) (void*)(uintptr_t)(i) void GLClearError(); bool GLLogCall(); diff --git a/src/VertexArray.cpp b/src/VertexArray.cpp index c933449..d33885e 100644 --- a/src/VertexArray.cpp +++ b/src/VertexArray.cpp @@ -1,7 +1,5 @@ #include "VertexArray.h" -#include "VertexBuffer.h" #include "Renderer.h" -#include "VertexBufferLayout.h" VertexArray::VertexArray() { @@ -21,13 +19,13 @@ void VertexArray::AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& la { Bind(); vb.Bind(); - const auto& elements = layout.GetElements(); - GLsizei offset = 0; + const std::vector elements = layout.GetElements(); + unsigned int offset = 0; for (unsigned int i = 0; i < elements.size(); i++) { - const auto& element = elements[i]; - GLCall(glEnableVertexAttribArray(i)); - GLCall(glVertexAttribPointer(i, element.count, element.type, element.normalized, layout.GetStride(), (const void*)offset)); - offset += element.count * VertexBufferElement::GetSizeOfType(element.type); + const auto& element = elements[i]; + GLCall(glEnableVertexAttribArray(i)); + GLCall( glVertexAttribPointer(i, element.count, element.type, element.normalized,layout.GetStride(), INT2VOIDP(offset)) ); + offset += element.count * VertexBufferElement::GetSizeOfType(element.type); } } diff --git a/src/VertexBufferLayout.h b/src/VertexBufferLayout.h index 27b18ba..dff1701 100644 --- a/src/VertexBufferLayout.h +++ b/src/VertexBufferLayout.h @@ -1,6 +1,7 @@ #ifndef VERTEXBUFFERLAYOUT_H #define VERTEXBUFFERLAYOUT_H #include +#include #include "Renderer.h" struct VertexBufferElement { unsigned int type; diff --git a/src/sdl.cpp b/src/sdl.cpp index bf76e53..0602e2f 100644 --- a/src/sdl.cpp +++ b/src/sdl.cpp @@ -13,6 +13,11 @@ #include #include +#include "Renderer.h" +#include "VertexBuffer.h" +#include "VertexArray.h" +#include "IndexBuffer.h" + #if defined(_MSC_VER) // Microsoft Visual C++ #include #define DEBUG_BREAK() __debugbreak() @@ -58,7 +63,7 @@ SdlWindow::SdlWindow(const char* title, int width, int height) r(0.5f), location(), increment(0.05f), - ib(nullptr, 0) + ib(nullptr,6) { // 1. Set attributes SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); @@ -115,24 +120,30 @@ SdlWindow::SdlWindow(const char* title, int width, int height) 2, 3, 0 }; - unsigned int vao; //vertext array object - GLCall(glGenVertexArrays(1, &vao)); - GLCall(glBindVertexArray(vao)); + //unsigned int vao; //vertext array object + //GLCall(glGenVertexArrays(1, &vao)); + //GLCall(glBindVertexArray(vao)); VertexArray va; VertexBuffer vb(positions, 4 * 2 * sizeof(float)); + ib = IndexBuffer(indices, 6); + VertexBufferLayout layout; layout.Push(2); + va.AddBuffer(vb, layout); - IndexBuffer ib(indices, 6); - ShaderProgramSource source = parseShader("res/shaders/Basic.shader"); + + std::cout << "VERTEX" << std::endl << source.VertexSource << std::endl; + std::cout << "FRAGMENT" << std::endl << source.FragmentSource << std::endl; + unsigned int m_ShaderID = createShader(source.VertexSource, source.FragmentSource); GLCall(glUseProgram(m_ShaderID)); - GLCall(int location = glGetUniformLocation(m_ShaderID, "u_Color")); + GLCall(unsigned int location = glGetUniformLocation(m_ShaderID, "u_Color")); ASSERT(location != -1); // -1 is an error + GLCall(glUniform4f(location, 0.8f, 0.3f, 0.8f, 1.0f)); GLCall(glBindVertexArray(0)); @@ -165,7 +176,6 @@ SdlWindow::~SdlWindow() { shader = 0; } - delete &ib; SDL_Quit(); } From fd13d0ba636246e18818feadc61488f1530e6cc1 Mon Sep 17 00:00:00 2001 From: MichaelFisher1997 Date: Sat, 11 Jan 2025 20:56:58 +0000 Subject: [PATCH 3/5] cleaned up a bit and got rid of some errors, still seg fualt --- src/IndexBuffer.cpp | 4 +++- src/Renderer.cpp | 3 ++- src/Renderer.h | 2 +- src/sdl.cpp | 13 +++++-------- src/sdl.hpp | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/IndexBuffer.cpp b/src/IndexBuffer.cpp index 1cf66db..4349232 100644 --- a/src/IndexBuffer.cpp +++ b/src/IndexBuffer.cpp @@ -6,13 +6,15 @@ IndexBuffer::IndexBuffer(const unsigned int* data, unsigned int count) { ASSERT(sizeof(unsigned int) == sizeof(GLuint)); // + std::cout << "m_RendererID: " << &m_RendererID << " " << m_RendererID << std::endl; GLCall(glGenBuffers(1, &m_RendererID)); GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID)); //select buffer called 'buffer' - GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(GLuint), data, GL_STATIC_DRAW)); // assigne buffer size, static as we use many times, but does not change + GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), data, GL_STATIC_DRAW)); // assigne buffer size, static as we use many times, but does not change } IndexBuffer::~IndexBuffer() { + std::cout << "m_RendererID: " << &m_RendererID << " " << m_RendererID << std::endl; GLCall(glDeleteBuffers(1, &m_RendererID)); } diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 552d629..537c7b6 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -1,9 +1,10 @@ #include "Renderer.h" +#include void GLClearError() { - while (glGetError() != GL_NO_ERROR); + //while (glGetError() != GL_NO_ERROR); } diff --git a/src/Renderer.h b/src/Renderer.h index 8de98da..75a4451 100644 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -34,4 +34,4 @@ #define INT2VOIDP(i) (void*)(uintptr_t)(i) void GLClearError(); -bool GLLogCall(); +bool GLLogCall(); \ No newline at end of file diff --git a/src/sdl.cpp b/src/sdl.cpp index 0602e2f..71c1110 100644 --- a/src/sdl.cpp +++ b/src/sdl.cpp @@ -63,7 +63,7 @@ SdlWindow::SdlWindow(const char* title, int width, int height) r(0.5f), location(), increment(0.05f), - ib(nullptr,6) + m_ib(nullptr, 0) { // 1. Set attributes SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); @@ -120,13 +120,10 @@ SdlWindow::SdlWindow(const char* title, int width, int height) 2, 3, 0 }; - //unsigned int vao; //vertext array object - //GLCall(glGenVertexArrays(1, &vao)); - //GLCall(glBindVertexArray(vao)); - VertexArray va; VertexBuffer vb(positions, 4 * 2 * sizeof(float)); - ib = IndexBuffer(indices, 6); + IndexBuffer ib(indices, 6); + m_ib = ib; VertexBufferLayout layout; layout.Push(2); @@ -255,7 +252,7 @@ void SdlWindow::render() { GLCall(glUniform4f(location, r, 0.3f, 0.8f, 1.0f)); va.Bind(); - ib.Bind(); + m_ib.Bind(); // TODO: Draw with OpenGL here (shaders, triangles, etc.) //glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr); GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr)); //macro assert for debugging @@ -363,7 +360,7 @@ SdlWindow::ShaderProgramSource SdlWindow::parseShader(const std::string& filepat } void SdlWindow::GLClearError() { - while (glGetError() != GL_NO_ERROR); + //while (glGetError() != GL_NO_ERROR); } diff --git a/src/sdl.hpp b/src/sdl.hpp index 469a563..6a03628 100644 --- a/src/sdl.hpp +++ b/src/sdl.hpp @@ -62,11 +62,11 @@ private: unsigned int buffer; unsigned int ibo; unsigned int vao; - IndexBuffer ib; // pointer, no default constructor needed unsigned int shader; unsigned int location; VertexBufferLayout layout; VertexArray va; + IndexBuffer m_ib; // Private methods void processEvents(); From 8920379ce3aef1adf31deab09dcc6fcbe68e45f9 Mon Sep 17 00:00:00 2001 From: MichaelFisher1997 Date: Sun, 12 Jan 2025 15:53:46 +0000 Subject: [PATCH 4/5] Indexbuffer improvments --- Makefile | 58 ++++++++++++------------- src/IndexBuffer.cpp | 39 +++++++++++++++-- src/IndexBuffer.h | 12 +++++- src/Renderer.cpp | 2 +- src/VertexArray.cpp | 6 ++- src/main.cpp | 2 + src/sdl.cpp | 100 +++++++++++++++++++++++++++----------------- src/sdl.hpp | 8 ++-- 8 files changed, 148 insertions(+), 79 deletions(-) diff --git a/Makefile b/Makefile index e26ddfc..e3e9861 100755 --- a/Makefile +++ b/Makefile @@ -1,38 +1,38 @@ -CC=clang++ -current_directory=$(shell pwd) +# Compiler and flags +CXX := g++ +CXXFLAGS := -g #-O0 -Wall -Wextra -FRAMEWORKS=-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo +# Libraries +LDFLAGS := -lSDL2 -lGLEW -lGL -CFLAGS=-std=c++11 -CFLAGS+=-I$(current_directory) -CFLAGS+=-I$(current_directory)/../external +# Source and output directories +SRC_DIR := src +BUILD_DIR := build +TARGET := $(BUILD_DIR)/opengl -LDFLAGS=-L$(current_directory)/../lib -LDFLAGS+=-lglfw3 -LDFLAGS+=-lGLEW +# Source files and object files +SRC := $(wildcard $(SRC_DIR)/*.cpp) +OBJ := $(SRC:$(SRC_DIR)/%.cpp=$(BUILD_DIR)/%.o) -SOURCES=$(wildcard *.cpp) -OBJECTS=$(patsubst %.cpp, %.o, $(SOURCES)) +# Default target +all: $(TARGET) +# Build the target executable +$(TARGET): $(OBJ) + mkdir -p $(BUILD_DIR) + $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS) -%.o: %.cpp - $(CC) $(CFLAGS) -c -o $@ $^ +# Build object files +$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp + mkdir -p $(BUILD_DIR) + $(CXX) $(CXXFLAGS) -c $< -o $@ -default: debug - -app: $(OBJECTS) - $(CC) $(CFLAGS) $(LDFLAGS) $(FRAMEWORKS) -o $@ $(OBJECTS) - -# Define debug and -g enables debug symbols -debug: CFLAGS+=-DDEBUG -g -debug: app - -release: app - -.PHONY: clean +# Clean up build files clean: - rm -f *.o app + rm -rf $(BUILD_DIR) -.PHONY: debugger -debugger: debug - PATH=/usr/bin /usr/bin/lldb ./app \ No newline at end of file +# Run the application +run: $(TARGET) + ./$(TARGET) + +.PHONY: all clean run diff --git a/src/IndexBuffer.cpp b/src/IndexBuffer.cpp index 4349232..55cb8e2 100644 --- a/src/IndexBuffer.cpp +++ b/src/IndexBuffer.cpp @@ -12,10 +12,41 @@ IndexBuffer::IndexBuffer(const unsigned int* data, unsigned int count) GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), data, GL_STATIC_DRAW)); // assigne buffer size, static as we use many times, but does not change } -IndexBuffer::~IndexBuffer() -{ - std::cout << "m_RendererID: " << &m_RendererID << " " << m_RendererID << std::endl; - GLCall(glDeleteBuffers(1, &m_RendererID)); +// Destructor +IndexBuffer::~IndexBuffer() { + if (m_RendererID != 0) { // Only delete if valid + GLCall(glDeleteBuffers(1, &m_RendererID)); + std::cout << "IndexBuffer destroyed with RendererID: " << m_RendererID << std::endl; + } +} + +// Move Constructor +IndexBuffer::IndexBuffer(IndexBuffer&& other) noexcept + : m_RendererID(other.m_RendererID), m_Count(other.m_Count) { + other.m_RendererID = 0; // Invalidate the moved-from object + other.m_Count = 0; + std::cout << "IndexBuffer moved (constructor)" << std::endl; +} + +// Move Assignment Operator +IndexBuffer& IndexBuffer::operator=(IndexBuffer&& other) noexcept { + if (this != &other) { + // Free existing resources + if (m_RendererID != 0) { + GLCall(glDeleteBuffers(1, &m_RendererID)); + } + + // Transfer ownership + m_RendererID = other.m_RendererID; + m_Count = other.m_Count; + + // Invalidate the moved-from object + other.m_RendererID = 0; + other.m_Count = 0; + + std::cout << "IndexBuffer moved (assignment)" << std::endl; + } + return *this; } void IndexBuffer::Bind() const diff --git a/src/IndexBuffer.h b/src/IndexBuffer.h index d37125f..ec77e6d 100644 --- a/src/IndexBuffer.h +++ b/src/IndexBuffer.h @@ -6,11 +6,19 @@ public: IndexBuffer(const unsigned int* data, unsigned int count); ~IndexBuffer(); + // Disallow copy operations + IndexBuffer(const IndexBuffer&) = delete; + IndexBuffer& operator=(const IndexBuffer&) = delete; + + // Enable move operations + IndexBuffer(IndexBuffer&& other) noexcept; + IndexBuffer& operator=(IndexBuffer&& other) noexcept; + void Bind() const; void Unbind() const; inline unsigned int GetCount() const { return m_Count; } private: - unsigned int m_RendererID; - unsigned int m_Count; + unsigned int m_RendererID = 0; + unsigned int m_Count = 0; }; diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 537c7b6..811b91b 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -4,7 +4,7 @@ void GLClearError() { - //while (glGetError() != GL_NO_ERROR); + while (glGetError() != GL_NO_ERROR); } diff --git a/src/VertexArray.cpp b/src/VertexArray.cpp index d33885e..372edb4 100644 --- a/src/VertexArray.cpp +++ b/src/VertexArray.cpp @@ -3,7 +3,11 @@ VertexArray::VertexArray() { - GLCall(glGenVertexArrays(1, &m_RendererID)); + std::cout << "VertexArray::VertexArray()" << std::endl; + std::cout << "m_RendererID: " << &m_RendererID << " " << m_RendererID << std::endl; + //GLCall(glGenVertexArrays(1, &m_RendererID)); + glGenVertexArrays(1, &m_RendererID); + std::cout << "m_RendererID: " << &m_RendererID << " " << m_RendererID << std::endl; if (!m_RendererID) { std::cerr << "Failed to generate VAO" << std::endl; } diff --git a/src/main.cpp b/src/main.cpp index f958fe7..6390123 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,9 @@ int main(int argc, char* argv[]) { // Create an instance of our SdlWindow class + std::cout << "starting" << std::endl; SdlWindow window("My SDL2 Window", 800, 600); + std::cout << "About to run: " << std::endl; // Run the main loop window.run(); diff --git a/src/sdl.cpp b/src/sdl.cpp index 71c1110..db6389b 100644 --- a/src/sdl.cpp +++ b/src/sdl.cpp @@ -51,25 +51,30 @@ struct ShaderProgramSource { }; SdlWindow::SdlWindow(const char* title, int width, int height) - : m_window(nullptr), - m_renderer(nullptr), - m_isRunning(false), - m_isFullscreen(false), - m_width(width), - m_height(height), - m_glContext(nullptr), - m_windowedWidth(width), - m_windowedHeight(height), - r(0.5f), - location(), - increment(0.05f), - m_ib(nullptr, 0) +: m_window(nullptr), + m_renderer(nullptr), + m_isRunning(false), + m_isFullscreen(false), + m_width(width), + m_height(height), + m_windowedWidth(width), + m_windowedHeight(height), + r(0.5f), + m_glContext(nullptr), + increment(0.05f), + vao(0), + layout(nullptr), + va(nullptr), + m_ib(nullptr) { + + std::cout << "Step 0: hellow world" << std::endl; + // 1. Set attributes SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); - + std::cout << "Step 1: SDL_GL_SetAttribute completed" << std::endl; // 2. Create the window with OpenGL flag m_window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, @@ -79,30 +84,42 @@ SdlWindow::SdlWindow(const char* title, int width, int height) SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN| SDL_WINDOW_RESIZABLE); if (!m_window) { - std::cerr << "Failed to create window: " << SDL_GetError() << std::endl; - return; + std::cerr << "Failed to create window: " << SDL_GetError() << std::endl; + return; } + std::cout << "Step 2: SDL_CreateWindow completed" << std::endl; - // 3. Create OpenGL context m_glContext = SDL_GL_CreateContext(m_window); if (!m_glContext) { std::cerr << "Failed to create GL context: " << SDL_GetError() << std::endl; return; } - // 4. Optionally init GLEW (if not on macOS core profile) + std::cout << "Step 3: SDL_GL_CreateContext completed" << std::endl; + + if (SDL_GL_MakeCurrent(m_window, m_glContext) != 0) { + std::cerr << "Failed to make GL context current: " << SDL_GetError() << std::endl; + return; + } + std::cout << "Step 4: SDL_GL_MakeCurrent completed" << std::endl; + #ifndef __APPLE__ glewExperimental = GL_TRUE; GLenum glewErr = glewInit(); - if (glewInit() != GLEW_OK) { - std::cerr << "Failed to init GLEW" << glewGetErrorString(glewErr) << std::endl; + if (glewErr != GLEW_OK) { + std::cerr << "Failed to init GLEW: " << glewGetErrorString(glewErr) << std::endl; return; } - glGetError(); + std::cout << "Step 5: GLEW initialized successfully" << std::endl; + glGetError(); // Clear GLEW's initial error #endif + std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl; + std::cout << "GLSL Version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl; + std::cout << "Vendor: " << glGetString(GL_VENDOR) << std::endl; + + // 5. Set vsync (optional) SDL_GL_SetSwapInterval(1); //Vsync - SdlWindow::GLClearError(); // 6. Mark as running m_isRunning = true; @@ -122,8 +139,9 @@ SdlWindow::SdlWindow(const char* title, int width, int height) VertexArray va; VertexBuffer vb(positions, 4 * 2 * sizeof(float)); - IndexBuffer ib(indices, 6); - m_ib = ib; + m_ib = new IndexBuffer(indices, 6); + + VertexBufferLayout layout; layout.Push(2); @@ -135,10 +153,11 @@ SdlWindow::SdlWindow(const char* title, int width, int height) std::cout << "VERTEX" << std::endl << source.VertexSource << std::endl; std::cout << "FRAGMENT" << std::endl << source.FragmentSource << std::endl; - unsigned int m_ShaderID = createShader(source.VertexSource, source.FragmentSource); - GLCall(glUseProgram(m_ShaderID)); + unsigned int shader = createShader(source.VertexSource, source.FragmentSource); + std::cout << "shader: " << shader << std::endl; + GLCall(glUseProgram(shader)); - GLCall(unsigned int location = glGetUniformLocation(m_ShaderID, "u_Color")); + GLCall(unsigned int location = glGetUniformLocation(shader, "u_Color")); ASSERT(location != -1); // -1 is an error GLCall(glUniform4f(location, 0.8f, 0.3f, 0.8f, 1.0f)); @@ -150,7 +169,6 @@ SdlWindow::SdlWindow(const char* title, int width, int height) } - SdlWindow::~SdlWindow() { // If using SDL Renderer, destroy it. But if you’re purely using OpenGL, you might remove it. if (m_renderer) { @@ -172,11 +190,15 @@ SdlWindow::~SdlWindow() { glDeleteProgram(shader); shader = 0; } + if (m_ib) { + delete m_ib; + m_ib = nullptr; + } SDL_Quit(); } - - +// +// void SdlWindow::run() { while (m_isRunning) { processEvents(); @@ -185,7 +207,7 @@ void SdlWindow::run() { } GLCall(glDeleteProgram(shader)); } - +// void SdlWindow::processEvents() { SDL_Event event; while (SDL_PollEvent(&event)) { @@ -231,7 +253,7 @@ SDL_Event event; } } } - +// void SdlWindow::update() { // Update game/application logic here if (r > 1.0f) { @@ -241,7 +263,7 @@ void SdlWindow::update() { } r += increment; } - +// void SdlWindow::render() { // Use GL calls instead of SDL’s renderer GLCall(glClearColor(0.0f, 0.0f, 0.0f, 1.0f)); //background @@ -249,10 +271,12 @@ void SdlWindow::render() { GLCall(glClear(GL_COLOR_BUFFER_BIT)); GLCall(glUseProgram(shader)); + std::cout << "shader: " << shader << std::endl; + std::cout << "location: " << location << std::endl; GLCall(glUniform4f(location, r, 0.3f, 0.8f, 1.0f)); - va.Bind(); - m_ib.Bind(); + va->Bind(); + m_ib->Bind(); // TODO: Draw with OpenGL here (shaders, triangles, etc.) //glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr); GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr)); //macro assert for debugging @@ -260,7 +284,7 @@ void SdlWindow::render() { // Swap buffers SDL_GL_SwapWindow(m_window); } - +// void SdlWindow::setFullscreen(bool fullscreen) { if (m_window) { m_isFullscreen = fullscreen; @@ -283,7 +307,7 @@ void SdlWindow::setFullscreen(bool fullscreen) { } } } - +// unsigned int SdlWindow::compileShader(unsigned int type, const std::string& source) { GLCall(unsigned int id = glCreateShader(type)); const char* src = source.c_str(); // <--- this string needs to exist when compiling/running @@ -360,7 +384,7 @@ SdlWindow::ShaderProgramSource SdlWindow::parseShader(const std::string& filepat } void SdlWindow::GLClearError() { - //while (glGetError() != GL_NO_ERROR); + while (glGetError() != GL_NO_ERROR); } diff --git a/src/sdl.hpp b/src/sdl.hpp index 6a03628..2a66e71 100644 --- a/src/sdl.hpp +++ b/src/sdl.hpp @@ -56,7 +56,7 @@ private: float r; SDL_GLContext m_glContext; float increment; - // temp shader stuff +// // temp shader stuff std::string vetexShader; std::string fragmentShader; unsigned int buffer; @@ -64,9 +64,9 @@ private: unsigned int vao; unsigned int shader; unsigned int location; - VertexBufferLayout layout; - VertexArray va; - IndexBuffer m_ib; + VertexBufferLayout* layout; + VertexArray* va; + IndexBuffer* m_ib; // Private methods void processEvents(); From ed0e9d89479f70f394197e67c65b23937acd7270 Mon Sep 17 00:00:00 2001 From: MichaelFisher1997 Date: Sun, 12 Jan 2025 23:49:49 +0000 Subject: [PATCH 5/5] Segment faults fixed!! --- src/IndexBuffer.cpp | 2 +- src/VertexArray.cpp | 8 +++--- src/VertexBufferLayout.h | 8 +++--- src/sdl.cpp | 57 +++++++++++++++++++--------------------- src/sdl.hpp | 15 ++++------- 5 files changed, 41 insertions(+), 49 deletions(-) diff --git a/src/IndexBuffer.cpp b/src/IndexBuffer.cpp index 55cb8e2..0e2b9a7 100644 --- a/src/IndexBuffer.cpp +++ b/src/IndexBuffer.cpp @@ -6,7 +6,7 @@ IndexBuffer::IndexBuffer(const unsigned int* data, unsigned int count) { ASSERT(sizeof(unsigned int) == sizeof(GLuint)); // - std::cout << "m_RendererID: " << &m_RendererID << " " << m_RendererID << std::endl; + //std::cout << "m_RendererID: " << &m_RendererID << " " << m_RendererID << std::endl; GLCall(glGenBuffers(1, &m_RendererID)); GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID)); //select buffer called 'buffer' GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), data, GL_STATIC_DRAW)); // assigne buffer size, static as we use many times, but does not change diff --git a/src/VertexArray.cpp b/src/VertexArray.cpp index 372edb4..5a66e37 100644 --- a/src/VertexArray.cpp +++ b/src/VertexArray.cpp @@ -4,10 +4,10 @@ VertexArray::VertexArray() { std::cout << "VertexArray::VertexArray()" << std::endl; - std::cout << "m_RendererID: " << &m_RendererID << " " << m_RendererID << std::endl; - //GLCall(glGenVertexArrays(1, &m_RendererID)); - glGenVertexArrays(1, &m_RendererID); - std::cout << "m_RendererID: " << &m_RendererID << " " << m_RendererID << std::endl; + std::cout << "m_RendererID: " << &m_RendererID << std::endl; + GLCall(glGenVertexArrays(1, &m_RendererID)); + //glGenVertexArrays(1, &m_RendererID); + std::cout << "m_RendererID: " << &m_RendererID << std::endl; if (!m_RendererID) { std::cerr << "Failed to generate VAO" << std::endl; } diff --git a/src/VertexBufferLayout.h b/src/VertexBufferLayout.h index dff1701..49ccc57 100644 --- a/src/VertexBufferLayout.h +++ b/src/VertexBufferLayout.h @@ -40,18 +40,18 @@ private: template<> inline void VertexBufferLayout::Push(int count) { m_Elements.push_back({ GL_FLOAT, static_cast(count), GL_FALSE }); - m_Stride += VertexBufferElement::GetSizeOfType(GL_FLOAT); + m_Stride += count * VertexBufferElement::GetSizeOfType(GL_FLOAT); } template<> inline void VertexBufferLayout::Push(int count) { m_Elements.push_back({ GL_UNSIGNED_INT,static_cast(count), GL_FALSE }); - m_Stride += VertexBufferElement::GetSizeOfType(GL_UNSIGNED_INT); + m_Stride += count * VertexBufferElement::GetSizeOfType(GL_UNSIGNED_INT); } template<> inline void VertexBufferLayout::Push(int count) { m_Elements.push_back({ GL_UNSIGNED_BYTE, static_cast(count), GL_TRUE }); - m_Stride += VertexBufferElement::GetSizeOfType(GL_UNSIGNED_BYTE); + m_Stride += count * VertexBufferElement::GetSizeOfType(GL_UNSIGNED_BYTE); } -#endif // VERTEXBUFFERLAYOUT_H \ No newline at end of file +#endif // VERTEXBUFFERLAYOUT_H diff --git a/src/sdl.cpp b/src/sdl.cpp index db6389b..4ebb06b 100644 --- a/src/sdl.cpp +++ b/src/sdl.cpp @@ -62,10 +62,11 @@ SdlWindow::SdlWindow(const char* title, int width, int height) r(0.5f), m_glContext(nullptr), increment(0.05f), - vao(0), - layout(nullptr), - va(nullptr), - m_ib(nullptr) + m_shader(0), + m_Location(-1), + m_IB(nullptr), + m_VB(nullptr), + m_VA(nullptr) { std::cout << "Step 0: hellow world" << std::endl; @@ -136,17 +137,14 @@ SdlWindow::SdlWindow(const char* title, int width, int height) 0, 1, 2, 2, 3, 0 }; - - VertexArray va; - VertexBuffer vb(positions, 4 * 2 * sizeof(float)); - m_ib = new IndexBuffer(indices, 6); - - + m_VA = new VertexArray(); + m_VB = new VertexBuffer(positions, 4 * 2 * sizeof(float)); + m_IB = new IndexBuffer(indices, 6); VertexBufferLayout layout; layout.Push(2); - va.AddBuffer(vb, layout); + m_VA->AddBuffer(*m_VB, layout); ShaderProgramSource source = parseShader("res/shaders/Basic.shader"); @@ -154,13 +152,15 @@ SdlWindow::SdlWindow(const char* title, int width, int height) std::cout << "FRAGMENT" << std::endl << source.FragmentSource << std::endl; unsigned int shader = createShader(source.VertexSource, source.FragmentSource); - std::cout << "shader: " << shader << std::endl; - GLCall(glUseProgram(shader)); + m_shader = shader; + std::cout << "shader: " << m_shader << std::endl; + std::cout << "shader: " << m_shader << std::endl; + GLCall(glUseProgram(m_shader)); - GLCall(unsigned int location = glGetUniformLocation(shader, "u_Color")); - ASSERT(location != -1); // -1 is an error + GLCall(m_Location = glGetUniformLocation(m_shader, "u_Color")); + ASSERT(m_Location != -1); // -1 is an error - GLCall(glUniform4f(location, 0.8f, 0.3f, 0.8f, 1.0f)); + GLCall(glUniform4f(m_Location, 0.8f, 0.3f, 0.8f, 1.0f)); GLCall(glBindVertexArray(0)); GLCall(glUseProgram(0)); @@ -186,13 +186,9 @@ SdlWindow::~SdlWindow() { SDL_DestroyWindow(m_window); m_window = nullptr; } - if (shader) { - glDeleteProgram(shader); - shader = 0; - } - if (m_ib) { - delete m_ib; - m_ib = nullptr; + if (m_shader) { + glDeleteProgram(m_shader); + m_shader = 0; } SDL_Quit(); @@ -205,7 +201,7 @@ void SdlWindow::run() { update(); render(); } - GLCall(glDeleteProgram(shader)); + GLCall(glDeleteProgram(m_shader)); } // void SdlWindow::processEvents() { @@ -270,13 +266,14 @@ void SdlWindow::render() { // GLCall(glClear(GL_COLOR_BUFFER_BIT)); - GLCall(glUseProgram(shader)); - std::cout << "shader: " << shader << std::endl; - std::cout << "location: " << location << std::endl; - GLCall(glUniform4f(location, r, 0.3f, 0.8f, 1.0f)); + GLCall(glUseProgram(m_shader)); + std::cout << "m_shader: " << m_shader << std::endl; + std::cout << "location: " << m_Location << std::endl; + std::cout << "R: " << r << std::endl; + GLCall(glUniform4f(m_Location, r, 0.3f, 0.8f, 1.0f)); - va->Bind(); - m_ib->Bind(); + m_VA->Bind(); + m_IB->Bind(); // TODO: Draw with OpenGL here (shaders, triangles, etc.) //glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr); GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr)); //macro assert for debugging diff --git a/src/sdl.hpp b/src/sdl.hpp index 2a66e71..dbe6066 100644 --- a/src/sdl.hpp +++ b/src/sdl.hpp @@ -57,16 +57,11 @@ private: SDL_GLContext m_glContext; float increment; // // temp shader stuff - std::string vetexShader; - std::string fragmentShader; - unsigned int buffer; - unsigned int ibo; - unsigned int vao; - unsigned int shader; - unsigned int location; - VertexBufferLayout* layout; - VertexArray* va; - IndexBuffer* m_ib; + unsigned int m_shader; + GLint m_Location; + VertexArray* m_VA; + IndexBuffer* m_IB; + VertexBuffer* m_VB; // Private methods void processEvents();