cleaned up a bit and got rid of some errors, still seg fualt

This commit is contained in:
MichaelFisher1997 2025-01-11 18:56:38 +00:00
parent ed1cc02d3e
commit 3e799fd434
7 changed files with 60 additions and 34 deletions

View File

@ -1,21 +1,38 @@
CC = g++ CC=clang++
CFLAGS = -Iinclude -Wall -g current_directory=$(shell pwd)
LDFLAGS = -lSDL2 -lGL -lGLEW
SRC = src/main.cpp src/sdl.cpp FRAMEWORKS=-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo
OBJ = $(SRC:.cpp=.o)
EXEC = opengl-app
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 %.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: clean:
rm -f $(OBJ) $(EXEC) rm -f *.o app
run: all .PHONY: debugger
./$(EXEC) debugger: debug
PATH=/usr/bin /usr/bin/lldb ./app

View File

@ -4,7 +4,7 @@
IndexBuffer::IndexBuffer(const unsigned int* data, unsigned int count) IndexBuffer::IndexBuffer(const unsigned int* data, unsigned int count)
: m_Count(count) : m_Count(count)
{ {
//ASSERT(sizeof(unsigned int) == sizeof(GLuint)); ASSERT(sizeof(unsigned int) == sizeof(GLuint));
// //
GLCall(glGenBuffers(1, &m_RendererID)); GLCall(glGenBuffers(1, &m_RendererID));
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID)); //select buffer called 'buffer' GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID)); //select buffer called 'buffer'

View File

@ -2,9 +2,6 @@
class IndexBuffer class IndexBuffer
{ {
private:
unsigned int m_RendererID;
unsigned int m_Count;
public: public:
IndexBuffer(const unsigned int* data, unsigned int count); IndexBuffer(const unsigned int* data, unsigned int count);
~IndexBuffer(); ~IndexBuffer();
@ -13,4 +10,7 @@ public:
void Unbind() const; void Unbind() const;
inline unsigned int GetCount() const { return m_Count; } inline unsigned int GetCount() const { return m_Count; }
private:
unsigned int m_RendererID;
unsigned int m_Count;
}; };

View File

@ -32,6 +32,6 @@
x;\ x;\
ASSERT(GLLogCall()) ASSERT(GLLogCall())
#define INT2VOIDP(i) (void*)(uintptr_t)(i)
void GLClearError(); void GLClearError();
bool GLLogCall(); bool GLLogCall();

View File

@ -1,7 +1,5 @@
#include "VertexArray.h" #include "VertexArray.h"
#include "VertexBuffer.h"
#include "Renderer.h" #include "Renderer.h"
#include "VertexBufferLayout.h"
VertexArray::VertexArray() VertexArray::VertexArray()
{ {
@ -21,13 +19,13 @@ void VertexArray::AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& la
{ {
Bind(); Bind();
vb.Bind(); vb.Bind();
const auto& elements = layout.GetElements(); const std::vector<VertexBufferElement> elements = layout.GetElements();
GLsizei offset = 0; unsigned int offset = 0;
for (unsigned int i = 0; i < elements.size(); i++) { for (unsigned int i = 0; i < elements.size(); i++) {
const auto& element = elements[i]; const auto& element = elements[i];
GLCall(glEnableVertexAttribArray(i)); GLCall(glEnableVertexAttribArray(i));
GLCall(glVertexAttribPointer(i, element.count, element.type, element.normalized, layout.GetStride(), (const void*)offset)); GLCall( glVertexAttribPointer(i, element.count, element.type, element.normalized,layout.GetStride(), INT2VOIDP(offset)) );
offset += element.count * VertexBufferElement::GetSizeOfType(element.type); offset += element.count * VertexBufferElement::GetSizeOfType(element.type);
} }
} }

View File

@ -1,6 +1,7 @@
#ifndef VERTEXBUFFERLAYOUT_H #ifndef VERTEXBUFFERLAYOUT_H
#define VERTEXBUFFERLAYOUT_H #define VERTEXBUFFERLAYOUT_H
#include <vector> #include <vector>
#include <GL/glew.h>
#include "Renderer.h" #include "Renderer.h"
struct VertexBufferElement { struct VertexBufferElement {
unsigned int type; unsigned int type;

View File

@ -13,6 +13,11 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include "Renderer.h"
#include "VertexBuffer.h"
#include "VertexArray.h"
#include "IndexBuffer.h"
#if defined(_MSC_VER) // Microsoft Visual C++ #if defined(_MSC_VER) // Microsoft Visual C++
#include <intrin.h> #include <intrin.h>
#define DEBUG_BREAK() __debugbreak() #define DEBUG_BREAK() __debugbreak()
@ -58,7 +63,7 @@ SdlWindow::SdlWindow(const char* title, int width, int height)
r(0.5f), r(0.5f),
location(), location(),
increment(0.05f), increment(0.05f),
ib(nullptr, 0) ib(nullptr,6)
{ {
// 1. Set attributes // 1. Set attributes
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); 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 2, 3, 0
}; };
unsigned int vao; //vertext array object //unsigned int vao; //vertext array object
GLCall(glGenVertexArrays(1, &vao)); //GLCall(glGenVertexArrays(1, &vao));
GLCall(glBindVertexArray(vao)); //GLCall(glBindVertexArray(vao));
VertexArray va; VertexArray va;
VertexBuffer vb(positions, 4 * 2 * sizeof(float)); VertexBuffer vb(positions, 4 * 2 * sizeof(float));
ib = IndexBuffer(indices, 6);
VertexBufferLayout layout; VertexBufferLayout layout;
layout.Push<float>(2); layout.Push<float>(2);
va.AddBuffer(vb, layout); va.AddBuffer(vb, layout);
IndexBuffer ib(indices, 6);
ShaderProgramSource source = parseShader("res/shaders/Basic.shader"); 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); unsigned int m_ShaderID = createShader(source.VertexSource, source.FragmentSource);
GLCall(glUseProgram(m_ShaderID)); 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 ASSERT(location != -1); // -1 is an error
GLCall(glUniform4f(location, 0.8f, 0.3f, 0.8f, 1.0f)); GLCall(glUniform4f(location, 0.8f, 0.3f, 0.8f, 1.0f));
GLCall(glBindVertexArray(0)); GLCall(glBindVertexArray(0));
@ -165,7 +176,6 @@ SdlWindow::~SdlWindow() {
shader = 0; shader = 0;
} }
delete &ib;
SDL_Quit(); SDL_Quit();
} }