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++
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)
.PHONY: debugger
debugger: debug
PATH=/usr/bin /usr/bin/lldb ./app

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -13,6 +13,11 @@
#include <string>
#include <sstream>
#include "Renderer.h"
#include "VertexBuffer.h"
#include "VertexArray.h"
#include "IndexBuffer.h"
#if defined(_MSC_VER) // Microsoft Visual C++
#include <intrin.h>
#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<float>(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();
}