diff --git a/.vscode/settings.json b/.vscode/settings.json index e3a9507..a8ca8e4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -47,6 +47,41 @@ "stdexcept": "cpp", "streambuf": "cpp", "cinttypes": "cpp", - "typeinfo": "cpp" + "typeinfo": "cpp", + "charconv": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "format": "cpp", + "ios": "cpp", + "locale": "cpp", + "mutex": "cpp", + "queue": "cpp", + "ranges": "cpp", + "ratio": "cpp", + "span": "cpp", + "stop_token": "cpp", + "thread": "cpp", + "xfacet": "cpp", + "xiosbase": "cpp", + "xlocale": "cpp", + "xlocbuf": "cpp", + "xlocinfo": "cpp", + "xlocmes": "cpp", + "xlocmon": "cpp", + "xlocnum": "cpp", + "xloctime": "cpp", + "xmemory": "cpp", + "xstring": "cpp", + "xtr1common": "cpp", + "xutility": "cpp", + "chrono": "cpp", + "condition_variable": "cpp", + "iomanip": "cpp", + "semaphore": "cpp", + "variant": "cpp", + "forward_list": "cpp", + "map": "cpp", + "xstddef": "cpp", + "xtree": "cpp" } } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 690b353..3223ec0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,10 @@ cmake_minimum_required(VERSION 3.5.0) project(GraphicsTest VERSION 0.1.0 LANGUAGES C CXX) +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + include(CTest) enable_testing() @@ -11,11 +15,10 @@ include(CPack) include_directories(./dep ./include) link_directories(../libs/glfw-3.3.9/build/src ../libs/NBEngine/lib) -add_library(NBDraw Draw.cpp) +add_library(NBDrawing Draw.cpp Shader.cpp) - -add_executable(GraphicsTest main.cpp funcs.cpp shader.cpp ../libs/glad/src/glad.c) -target_link_libraries(GraphicsTest glfw3 NBWindows NBEvents) +add_executable(GraphicsTest main.cpp funcs.cpp Shader.cpp Draw.cpp ../libs/glad/src/glad.c) +target_link_libraries(GraphicsTest glfw3 NBWindows NBDrawing) set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE) set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE) diff --git a/Draw.cpp b/Draw.cpp index ffd0576..cfb370e 100644 --- a/Draw.cpp +++ b/Draw.cpp @@ -2,4 +2,210 @@ namespace NB{ +DrawWarning::DrawWarning(const std::string& msg) : std::runtime_error(msg) {} + +uint8_t GLSLTypeSize(GLenum type) { + switch(type) { + case GL_SHORT: + case GL_UNSIGNED_SHORT: + case GL_HALF_FLOAT: + return 2; + break; + case GL_INT: + case GL_UNSIGNED_INT: + case GL_FLOAT: + return 4; + break; + case GL_DOUBLE: + return 8; + break; + default: + return 1; + break; + } +} + +uint8_t GLSLPrimitiveVertexSize(GLenum type, unsigned int num_prims) { + switch(type) { + case GL_POINTS: + return num_prims; + case GL_LINES: + return num_prims*2; + break; + case GL_LINE_STRIP: + return num_prims+1; + break; + case GL_LINE_LOOP: + return num_prims; + break; + case GL_TRIANGLES: + return num_prims*3; + break; + case GL_TRIANGLE_STRIP: + case GL_TRIANGLE_FAN: + return num_prims+2; + break; + case GL_QUADS: + return 4*num_prims; + break; + case GL_QUAD_STRIP: + return 2*num_prims+2; + break; + default: + return num_prims; + break; + } +} + +DrawBuffer::DrawBuffer( + std::vector vas, + const Shader& shader +) : _vertex_attributes(vas), _shader(shader), + _vert_data(GL_ARRAY_BUFFER), _elmt_data(GL_ELEMENT_ARRAY_BUFFER) {} + +unsigned int DrawBuffer::vertSize() const { + size_t num_attrs = _vertex_attributes.size(); + size_t vert_size = 0; + for (int i = 0; i < num_attrs; ++i) { + vert_size += _vertex_attributes[i].GLSLSize*GLSLTypeSize(_vertex_attributes[i].GLSLType); + } + return vert_size; +} + +std::vector DrawBuffer::getLayout() const { + unsigned int num_attrs = _vertex_attributes.size(); + std::vector ret(num_attrs); + for (int i = 0; i < num_attrs; ++i) { + ret[i] = _vertex_attributes[i].ptr; + } + + return ret; +} + +void DrawBuffer::draw() const { + _elmt_data.bind(); + glDrawElements(_gl_primitive_type, _elmt_data.size(), GL_UNSIGNED_INT, 0); +} + +const Shader DrawBuffer::shader() const { + return _shader; +} + +bool DrawBuffer::keepLocalElement() const { return _elmt_data.keepLocal(); } + +bool DrawBuffer::keepLocalVertex() const { return _vert_data.keepLocal(); } + +unsigned int DrawBuffer::elementBufferID() const { return _elmt_data.id(); } + +unsigned int DrawBuffer::vertexBufferID() const { return _vert_data.id(); } + +bool DrawBuffer::keepLocalElement(bool keep) { return _elmt_data.keepLocal(keep); } + +bool DrawBuffer::keepLocalVertex(bool keep) { return _vert_data.keepLocal(keep); } + +const Shader DrawBuffer::shader(const Shader& new_shader) { + _shader = new_shader; + return _shader; +} + +std::vector DrawBuffer::setLayout() { + int vert_size = vertSize(); + unsigned int num_attrs = _vertex_attributes.size(); + std::vector ret(num_attrs); + unsigned int curr_byte = 0; + for (int i = 0; i < num_attrs; ++i) { + _vertex_attributes[i].ptr.stride = vert_size; + _vertex_attributes[i].ptr.offset = curr_byte; + curr_byte += _vertex_attributes[i].GLSLSize * GLSLTypeSize(_vertex_attributes[i].GLSLType); + ret[i] = _vertex_attributes[i].ptr; + } + + return getLayout(); +} + +std::vector DrawBuffer::setLayout(std::vector vap) { + unsigned int num_attrs = _vertex_attributes.size(); + if (vap.size() != num_attrs) { + throw DrawWarning("DrawBuffer Vertex Attributes and passed Vertex Attributes do not match."); + } + for (int i = 0; i < num_attrs; ++i) { + if (vap[i].stride == -1) { + throw DrawWarning("Unset Vertex Attribute Pointer in index " + std::to_string(i) + "."); + } + _vertex_attributes[i].ptr = vap[i]; + } + + return getLayout(); +} + +void DrawBuffer::generateElementBuffer(unsigned int num_prims, GLenum gl_primitive_type) { + generateElementBuffer(std::vector(GLSLPrimitiveVertexSize(gl_primitive_type, num_prims))); +} + +void DrawBuffer::generateElementBuffer(std::vector elements, GLenum gl_primitive_type) { + _num_prims = elements.size(); + _gl_primitive_type = gl_primitive_type; + _elmt_data.setData(elements); +} + +void DrawBuffer::generateVertexBuffer(unsigned int num_verts) { + generateVertexBuffer(std::vector(num_verts * vertSize())); +} + +void DrawBuffer::generateVertexBuffer(std::vector vert_data) { + int vert_size = vertSize(); + if (vert_data.size() % vert_size != 0) { + throw DrawWarning("Starting data does not match vertex size of " + std::to_string(vert_size) + "."); + } + _num_verts = vert_data.size() / vert_size; + unsigned int num_attrs = _vertex_attributes.size(); + for (int i = 0; i < num_attrs; ++i) { + if (_vertex_attributes[i].ptr.stride == -1) { + setLayout(); + i = num_attrs+1; + } + } + + // TODO: Make it not static draw all the time + _vert_data.setData(vert_data); + + glGenVertexArrays(1, &_VAO); + glBindVertexArray(_VAO); + _vert_data.bind(); + for (GLuint i = 0; i < num_attrs; ++i) { + glVertexAttribPointer( + i, + _vertex_attributes[i].GLSLSize, + _vertex_attributes[i].GLSLType, + _vertex_attributes[i].GLSLNormalization, + _vertex_attributes[i].ptr.stride, + (void*)(_vertex_attributes[i].ptr.offset) + ); + glEnableVertexAttribArray(i); + } + _elmt_data.bind(); +} + +void DrawBuffer::loadElementBuffer(const std::vector& elements, unsigned int offset, GLenum gl_primitive_type) { + if (!_elmt_data.isInitialized()) { + std::vector load_data(offset); + load_data.insert(load_data.end(), elements.begin(), elements.end()); + generateElementBuffer(load_data); + } else { + if (gl_primitive_type != _gl_primitive_type) { + throw DrawWarning("Attempting to overload GL primitive type."); + } + _elmt_data.loadData(elements, offset); + } +} + +void DrawBuffer::loadVertexBuffer(const std::vector& data, unsigned int offset) { + if (!_vert_data.isInitialized()) { + std::vector load_data(offset); + load_data.insert(load_data.end(), data.begin(), data.end()); + generateVertexBuffer(load_data); + } else { + _vert_data.loadData(data, offset); + }} + } \ No newline at end of file diff --git a/data.cpp b/data.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/funcs.cpp b/funcs.cpp index 632b11f..a09df0b 100644 --- a/funcs.cpp +++ b/funcs.cpp @@ -1,70 +1,11 @@ #include "funcs.h" void framebuffer_size_callback(GLFWwindow* window, int width, int height) { - int windowX, windowY; glViewport(0, 0, width, height); - glfwGetFramebufferSize(window, &windowX, &windowY); - float ratioX = (float)sizeX / (float)windowX; - float ratioY = (float)sizeY / (float)windowY; - float* screenCoords = scaleGridPoints(gridX, gridY, ratioX, ratioY, tileCoords); - glBufferData(GL_ARRAY_BUFFER, 5*gridX*gridY*sizeof(float), screenCoords, GL_STATIC_DRAW); } void processInput(GLFWwindow* window) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { glfwSetWindowShouldClose(window, true); } -} - -float* makeGridPoints(unsigned int X, unsigned int Y) { - int numPoints = 5*X*Y; - float xStep = 1/ ((float)(X-1)); - float yStep = 1/ ((float)(Y-1)); - float* ret = new float[numPoints]; - for (int i = 0; i < Y; i++) { - for (int j = 0; j < X; j++) { - ret[5*(i*X+j)] = 2*xStep*j-1.0f; - ret[5*(i*X+j)+1] = 2*yStep*i-1.0f; - ret[5*(i*X+j)+2] = 0.0; - ret[5*(i*X+j)+3] = yStep*i; - ret[5*(i*X+j)+4] = xStep*j; - } - } - - return ret; -} - -float* scaleGridPoints(unsigned int X, unsigned int Y, float ratioX, float ratioY, float* points) { - ratioX /= 2; - ratioY /= 2; - float* ret = new float[5*X*Y]; - for (int i = 0; i < X*Y; i++) { - ret[5*i] = ratioX*points[5*i]; - ret[5*i+1] = ratioY*points[5*i+1]; - ret[5*i+2] = 0.0; - ret[5*i+3] = points[5*i+3]; - ret[5*i+4] = points[5*i+4]; - } - - return ret; -} - -unsigned int* makeGridIndex(unsigned int X, unsigned int Y) { - unsigned int tiles = (X-1)*(Y-1); - unsigned int* ret = new unsigned int[tiles*6]; - unsigned int tileInd; - for (int i = 0; i < Y-1; i++) { - for (int j = 0; j < X-1; j++) { - tileInd = i*(X-1)+j; - ret[6*tileInd] = i*X+j; - ret[6*tileInd+1] = (i+1)*X+j; - ret[6*tileInd+2] = (i+1)*X+(j+1); - - ret[6*tileInd+3] = i*X+j; - ret[6*tileInd+4] = i*X+j+1; - ret[6*tileInd+5] = (i+1)*X+(j+1); - } - } - - return ret; } \ No newline at end of file diff --git a/include/Buffers.h b/include/Buffers.h new file mode 100644 index 0000000..7f8b3eb --- /dev/null +++ b/include/Buffers.h @@ -0,0 +1,162 @@ +#pragma once +#ifndef _NB_BUFFER +#define _NB_BUFFER + +#include +#include +#include + +#include +#include +#include "Shader.h" + +namespace NB{ + +class BufferWarning : public std::runtime_error { +public: + BufferWarning(const std::string& msg) : std::runtime_error(msg) {} +}; + +template +class BufferManager { +public: + BufferManager( + GLenum buffer_type, + bool keep_local=true, + GLenum usage=GL_STATIC_DRAW + ) : _buffer_type(buffer_type), _initialized(false), _keep_local(keep_local), _usage(usage), + _size(0), _max_size(0) {} + BufferManager( + GLenum buffer_type, + const std::vector& data, + bool keep_local=true, + GLenum usage=GL_STATIC_DRAW + ) : _buffer_type(buffer_type), _keep_local(keep_local), _usage(usage) { + setData(data); + } + BufferManager( + GLenum buffer_type, + unsigned int size, + bool keep_local=true, + GLenum usage=GL_STATIC_DRAW + ) : BufferManager(buffer_type, std::vector(size), keep_local, usage) {} + + GLenum bufferType() const { return _buffer_type; } + GLenum usageType() const { return _usage; } + unsigned int id() const { return _id; } + unsigned int size() const { return _size; } + unsigned int capacity() const { return _max_size; } + bool isInitialized() const { return _initialized; } + bool keepLocal() const { return _keep_local; } + std::vector getDataDirect() const { + std::vector ret(_size); + glBindBuffer(_buffer_type, _id); + glGetBufferSubData(_buffer_type, 0, sizeof(T)*_size, ret.data()); + glBindBuffer(_buffer_type, 0); + + return ret; + } + std::vector getData() const { + if(_keep_local) { + return _data; + } else { + return getDataDirect(); + } + } + unsigned int bind() const { glBindBuffer(_buffer_type, _id); return _id; } + bool isBound() const { return (getBound(_buffer_type) == _id); } + + GLenum usageType(GLenum usage) { _usage=usage; return usageType(); } + bool keepLocal(bool keep_local) { + if(keep_local && !_keep_local) { + _data = getDataDirect(); + } + _keep_local = keep_local; + return _keep_local; + } + bool initialize(unsigned int n=1) { + if (!_initialized) { + setData(std::vector(n)); + } + return _initialized; + } + void loadData(const std::vector& newData, unsigned int offset=0) { + if ( newData.size()+offset > _max_size ) { + throw BufferWarning("Attempting to overflow buffer of capacity " + std::to_string(_max_size) + "."); + } + glBindBuffer(_buffer_type, _id); + glBufferSubData(_buffer_type, offset*sizeof(T), sizeof(T)*newData.size(), newData.data()); + if(_keep_local) { + memcpy(_data.data()+offset*sizeof(T), newData.data(), sizeof(T)*newData.size()); + } + glBindBuffer(_buffer_type, 0); + } + void setData(const std::vector& set_data) { + if (_keep_local) {_data = set_data;} + if (!_initialized) { + glGenBuffers(1, &_id); + _initialized = true; + } + glBindBuffer(_buffer_type, _id); + if (set_data.size() <= _max_size) { + glBufferSubData(_buffer_type, 0, sizeof(T)*_size, _data.data()); + _size = _data.size(); + } else { + _size = _data.size(); + _max_size = _size; + glBufferData(_buffer_type, sizeof(T)*_max_size, _data.data(), _usage); + } + glBindBuffer(_buffer_type, 0); + } + unsigned int resize(unsigned int n) { + if (_keep_local) { _data.resize(n); } + if (n > _max_size) { + _max_size = n; + if (_initialized) { + glBindBuffer(_buffer_type, _id); + glBufferData(_buffer_type, sizeof(T)*_max_size, _data.data(), _usage); + glBindBuffer(_buffer_type, 0); + } + } + _size = n; + return _size; + } + unsigned int shrink_to_fit() { + if (_size < _max_size) { + if (_keep_local) { + _data.shrink_to_fit(); + _size = _data.size(); + glBindBuffer(_buffer_type, _id); + glBufferData(_buffer_type, _size*sizeof(T), _data.data(),_usage); + glBindBuffer(_buffer_type, 0); + _max_size = _size; + } else { + _data = getDataDirect(); + _size = _data.data(); + _max_size = _size; + } + } + return _size; + } + + static unsigned int getBound(GLenum buffer) { + unsigned int ret; + glGetIntegerv(buffer, &ret); + return ret; + } + +private: + const GLenum _buffer_type; + + GLenum _usage; + std::vector _data; + unsigned int _id, _size, _max_size; + bool _initialized; + bool _keep_local; + +}; + + +} + +#endif \ No newline at end of file diff --git a/include/Draw.h b/include/Draw.h index 32a833b..5a7042a 100644 --- a/include/Draw.h +++ b/include/Draw.h @@ -2,11 +2,82 @@ #ifndef _NB_DRAW #define _NB_DRAW +#include +#include +#include + +#include +#include + +#include "Buffers.h" +#include "Shader.h" + namespace NB{ -class NBDrawObject { - + +class DrawWarning : public std::runtime_error { +public: + DrawWarning(const std::string&); }; +uint8_t GLSLTypeSize(GLenum); + +uint8_t GLSLPrimitiveVertexSize(GLenum, unsigned int num_prims=1); + +template +std::vector rawDataVector(const std::vector& vec) { + unsigned int num_bytes = vec.size() * sizeof(T); + std::vector ret(num_bytes); + memcpy(ret.data(), vec.data(), num_bytes); + + return ret; +} + +struct VertexAttributePointer { + int32_t offset; + GLsizei stride = -1; +}; + +struct VertexAttribute { + const GLint GLSLSize; + const GLenum GLSLType; + const GLboolean GLSLNormalization; + VertexAttributePointer ptr; +}; + +class DrawBuffer { +public: + DrawBuffer(std::vector, const Shader&); + //DrawBuffer(std::initializer_list, std::initializer_list); + + unsigned int vertSize() const; + std::vector getLayout() const; + const Shader shader() const; + bool keepLocalElement() const; + bool keepLocalVertex() const; + unsigned int elementBufferID() const; + unsigned int vertexBufferID() const; + virtual void draw() const; + + bool keepLocalElement(bool); + bool keepLocalVertex(bool); + const Shader shader(const Shader&); + virtual std::vector setLayout(); + virtual std::vector setLayout(std::vector); + virtual void generateElementBuffer(std::vector, GLenum gl_primitive_type=GL_TRIANGLES); + virtual void generateElementBuffer(unsigned int, GLenum gl_primitive_type=GL_TRIANGLES); + virtual void generateVertexBuffer(std::vector); + virtual void generateVertexBuffer(unsigned int); + virtual void loadElementBuffer(const std::vector&, unsigned int offset=0, GLenum gl_primitive_type=GL_TRIANGLES); + virtual void loadVertexBuffer(const std::vector&, unsigned int offset=0); + +protected: + std::vector _vertex_attributes; + BufferManager _vert_data; + BufferManager _elmt_data; + Shader _shader; + unsigned int _num_verts, _num_prims, _VAO; + GLenum _gl_primitive_type; +}; } #endif \ No newline at end of file diff --git a/include/funcs.h b/include/funcs.h index d6bfc0a..cee6e5c 100644 --- a/include/funcs.h +++ b/include/funcs.h @@ -3,26 +3,14 @@ #define _FUNCS_HEADER_ #include #include -#include #include #include #include #include #include -using namespace std::chrono_literals; - -extern unsigned int gridX, gridY, sizeX, sizeY; -extern float* tileCoords; - void framebuffer_size_callback(GLFWwindow* window, int width, int height); void processInput(GLFWwindow* window); -float* makeGridPoints(unsigned int X=2, unsigned int Y=2); - -float* scaleGridPoints(unsigned int X, unsigned int Y, float ratioX, float ratioY, float* points); - -unsigned int* makeGridIndex(unsigned int X=2, unsigned int Y=2); - #endif \ No newline at end of file diff --git a/include/shader.h b/include/shader.h index 057ad10..d38e0b3 100644 --- a/include/shader.h +++ b/include/shader.h @@ -1,4 +1,7 @@ #pragma once +#ifndef _NB_SHADER +#define _NB_SHADER + #include #include #include @@ -8,13 +11,24 @@ #include #include +namespace NB { + class Shader { public: - unsigned int id; Shader(const char* vertexPath, const char* fragmentPath); + Shader(); + Shader(const Shader&); + Shader& operator=(const Shader&); + void use() const; void setBool(const std::string& name, bool value) const; void setInt(const std::string& name, int value) const; void setFloat(const std::string& name, float value) const; void setMat4(const std::string& name, glm::mat4& value) const; -}; \ No newline at end of file + unsigned int id; + +private: +}; + +} +#endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index ded5e30..9eb0080 100644 --- a/main.cpp +++ b/main.cpp @@ -6,12 +6,7 @@ #include #include #include -#include "data.cpp" - -unsigned int gridX = 8, gridY = 8; -unsigned int sizeX=500, sizeY=500; -float* tileCoords = makeGridPoints(gridX, gridY); -unsigned int* tileIndices = makeGridIndex(gridX, gridY); +#include "Draw.h" int main() { NB::NBWindow myWindow(800, 600, "Howdy Naif!"); @@ -20,47 +15,38 @@ int main() { myWindow.init(); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); - - // Create Data - float* screenCoords = scaleGridPoints(gridX, gridY, (float)sizeX/(float)800, (float)sizeY/(float)600, tileCoords); - - // Use and set shader - Shader myShader("../shaders/vert.vs", "../shaders/frag.fs"); + NB::Shader myShader("../shaders/vert.vs", "../shaders/frag.fs"); myShader.use(); - - // Create Vertex Buffer and bind it to array buffer, then specifiy how to use that data - unsigned int VAO, VBO; - glGenVertexArrays(1, &VAO); - glGenBuffers(1, &VBO); - glBindVertexArray(VAO); - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, 5*gridX*gridY*sizeof(float), screenCoords, GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)0); - glEnableVertexAttribArray(0); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)(3*sizeof(float))); - glEnableVertexAttribArray(1); - delete[] screenCoords; - // Element Buffers - unsigned int EBO; - glGenBuffers(1, &EBO); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6*(gridX-1)*(gridY-1)*sizeof(unsigned int), tileIndices, GL_STATIC_DRAW); + std::vector attrs = { + {2, GL_FLOAT, false}, + {3, GL_FLOAT, false} + }; + NB::DrawBuffer myBuffer(attrs, myShader); + myBuffer.generateElementBuffer({0, 1, 2}, GL_TRIANGLES); + std::vector data_float = { + -0.5, -0.5, 1.0, 0.0, 0.0, + 0.5, -0.5, 0.0, 1.0, 0.0, + 0.0, 0.5, 0.0, 0.0, 1.0 + }; + myBuffer.generateVertexBuffer(NB::rawDataVector(data_float)); - myShader.use(); - glBindVertexArray(VAO); - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - glProvokingVertex(GL_FIRST_VERTEX_CONVENTION); - glBindBuffer(GL_ARRAY_BUFFER, VBO); + //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + //glProvokingVertex(GL_FIRST_VERTEX_CONVENTION); // Window loop + GLenum err; while(!glfwWindowShouldClose(window)) { + while(err=glGetError()) { + std::cout << "[GL ERROR] " << err << "\n"; + } // Input checking processInput(window); // Rendering Loop - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - glDrawElements(GL_TRIANGLES, (gridX-1)*(gridY-1)*6, GL_UNSIGNED_INT, 0); + myShader.use(); + glClearColor(0.2f, 0.4f, 0.3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); + myBuffer.draw(); // Events and buffer swap glfwPollEvents(); @@ -69,7 +55,5 @@ int main() { glfwTerminate(); - delete[] tileCoords; - //delete[] tileIndices; return 0; } \ No newline at end of file diff --git a/shader.cpp b/shader.cpp index 93e7e8f..2241436 100644 --- a/shader.cpp +++ b/shader.cpp @@ -1,4 +1,6 @@ -#include "shader.h" +#include "Shader.h" + +namespace NB{ Shader::Shader(const char* vertexPath, const char* fragmentPath) { std::string vertexCode, fragmentCode; @@ -60,6 +62,12 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath) { glDeleteShader(fragment); } +Shader::Shader() { id = 0x0; } + +Shader::Shader(const Shader& cpy_shader) { id = cpy_shader.id; } + +Shader& Shader::operator=(const Shader& cpy_shader) { id = cpy_shader.id; } + void Shader::use() const{ glUseProgram(id); } @@ -78,4 +86,6 @@ void Shader::setFloat(const std::string& name, float value) const { void Shader::setMat4(const std::string& name, glm::mat4& value) const { glUniformMatrix4fv(glGetUniformLocation(id, name.c_str()), 1, GL_FALSE, glm::value_ptr(value)); +} + } \ No newline at end of file diff --git a/shaders/frag.fs b/shaders/frag.fs index 033c702..2df3d68 100644 --- a/shaders/frag.fs +++ b/shaders/frag.fs @@ -1,8 +1,7 @@ #version 330 core -flat in vec3 ourColor; +in vec3 vertColor; out vec4 FragColor; - void main() { - FragColor = vec4(ourColor, 1.0); + FragColor = vec4(vertColor, 0.0); } \ No newline at end of file diff --git a/shaders/vert.vs b/shaders/vert.vs index 71c419e..8c801c0 100644 --- a/shaders/vert.vs +++ b/shaders/vert.vs @@ -1,11 +1,9 @@ #version 330 core +layout (location=0) in vec2 aPos; +layout (location=1) in vec3 aColor; -layout(location=0) in vec3 aPos; -layout(location=1) in vec2 val; - -flat out vec3 ourColor; - +out vec3 vertColor; void main() { - gl_Position = vec4(aPos, 1.0); - ourColor = vec3(val.x, 0.0, val.y); + vertColor = aColor; + gl_Position = vec4(aPos, 0.0, 1.0); } \ No newline at end of file