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