From 64e5567abe62b608601725e16906f3e2b2642ea6 Mon Sep 17 00:00:00 2001 From: Prtm2110 Date: Sat, 11 Jan 2025 19:46:23 +0530 Subject: [PATCH] Vertex array added and bug fixes --- Spraky-core/Spraky-core.vcxproj | 4 +++ Spraky-core/Spraky-core.vcxproj.filters | 12 +++++++ Spraky-core/main.cpp | 4 +-- Spraky-core/x64/src/graphics/buffers/buffer.h | 1 + .../x64/src/graphics/buffers/indexbuffer.cpp | 29 +++++++++++++++++ .../x64/src/graphics/buffers/indexbuffer.h | 24 ++++++++++++++ .../x64/src/graphics/buffers/vertexarray.cpp | 32 +++++++++++++++++++ .../x64/src/graphics/buffers/vertexarray.h | 20 ++++++++++++ Spraky-core/x64/src/maths/mat4.cpp | 8 ++--- Spraky-core/x64/src/maths/mat4.h | 2 +- Spraky-core/x64/src/shaders/basic.vert | 15 +++------ 11 files changed, 134 insertions(+), 17 deletions(-) create mode 100644 Spraky-core/x64/src/graphics/buffers/indexbuffer.cpp create mode 100644 Spraky-core/x64/src/graphics/buffers/indexbuffer.h create mode 100644 Spraky-core/x64/src/graphics/buffers/vertexarray.cpp create mode 100644 Spraky-core/x64/src/graphics/buffers/vertexarray.h diff --git a/Spraky-core/Spraky-core.vcxproj b/Spraky-core/Spraky-core.vcxproj index 1245386..b447873 100644 --- a/Spraky-core/Spraky-core.vcxproj +++ b/Spraky-core/Spraky-core.vcxproj @@ -136,6 +136,8 @@ + + @@ -145,7 +147,9 @@ + + diff --git a/Spraky-core/Spraky-core.vcxproj.filters b/Spraky-core/Spraky-core.vcxproj.filters index 4d33ddf..7f0f254 100644 --- a/Spraky-core/Spraky-core.vcxproj.filters +++ b/Spraky-core/Spraky-core.vcxproj.filters @@ -42,6 +42,12 @@ Source Files + + Source Files + + + Source Files + @@ -74,6 +80,12 @@ Header Files + + Header Files + + + Header Files + diff --git a/Spraky-core/main.cpp b/Spraky-core/main.cpp index ae0b891..39d170a 100644 --- a/Spraky-core/main.cpp +++ b/Spraky-core/main.cpp @@ -4,7 +4,7 @@ #include "x64/src/maths/math.h" #include "x64/src/utils/fileutils.h" #include "x64/src/graphics/shader.h" - +#include "x64/src/graphics/buffers/buffer.h" int main() { using namespace sparky; @@ -46,7 +46,7 @@ int main() { Shader shader("x64/src/shaders/basic.vert", "x64/src/shaders/basic.frag"); shader.enable(); shader.setUniformMat4("pr_matrix", ortho); - shader.setUniformMat4("ml_matrix", mat4::tranlation(vec3(8, 4.5, 0))); + shader.setUniformMat4("ml_matrix", mat4::translation(vec3(8, 4.5, 0))); shader.setUniform2f("light_pos", vec2(8.0f, 4.5f)); // Center of the square shader.setUniform4f("colour", vec4(0.0f, 0.0f, 0.0f, 1.0f )); // Black color for the glow diff --git a/Spraky-core/x64/src/graphics/buffers/buffer.h b/Spraky-core/x64/src/graphics/buffers/buffer.h index eeeedf0..1ba0a2b 100644 --- a/Spraky-core/x64/src/graphics/buffers/buffer.h +++ b/Spraky-core/x64/src/graphics/buffers/buffer.h @@ -15,6 +15,7 @@ namespace sparky { Buffer(GLfloat* data, GLsizei count, GLuint componenetCount); void bind() const; void unbind() const; + inline GLuint getComponentCount() const { return m_ComponentCount; } diff --git a/Spraky-core/x64/src/graphics/buffers/indexbuffer.cpp b/Spraky-core/x64/src/graphics/buffers/indexbuffer.cpp new file mode 100644 index 0000000..2291c55 --- /dev/null +++ b/Spraky-core/x64/src/graphics/buffers/indexbuffer.cpp @@ -0,0 +1,29 @@ +#include "indexbuffer.h" + +using namespace sparky; +using namespace graphics; + +namespace sparky { + namespace graphics { + + IndexBuffer::IndexBuffer(GLushort* data, GLsizei count) + : m_Count(count) + { + glGenBuffers(1, &m_BufferID); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_BufferID); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(GLushort), data, GL_STATIC_DRAW); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + } + + void IndexBuffer::bind() const + { + glBindBuffer(GL_ARRAY_BUFFER, m_BufferID); + } + void IndexBuffer::unbind() const + { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + } + + } +} diff --git a/Spraky-core/x64/src/graphics/buffers/indexbuffer.h b/Spraky-core/x64/src/graphics/buffers/indexbuffer.h new file mode 100644 index 0000000..79825b8 --- /dev/null +++ b/Spraky-core/x64/src/graphics/buffers/indexbuffer.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +namespace sparky { + namespace graphics { + + class IndexBuffer { + + private: + GLuint m_BufferID; + GLuint m_Count; + + public: + IndexBuffer(GLushort* data, GLsizei count); + void bind() const; + void unbind() const; + + inline GLuint getCount() const { return m_Count; } + + + }; + } +} \ No newline at end of file diff --git a/Spraky-core/x64/src/graphics/buffers/vertexarray.cpp b/Spraky-core/x64/src/graphics/buffers/vertexarray.cpp new file mode 100644 index 0000000..82a80ed --- /dev/null +++ b/Spraky-core/x64/src/graphics/buffers/vertexarray.cpp @@ -0,0 +1,32 @@ +#include "vertexarray.h" + +namespace sparky { + namespace graphics { + VertexArray::VertexArray() + { + glGenVertexArrays(1, &m_ArrayID); + } + VertexArray::~VertexArray() + { + for (int i = 0; i < m_Buffers.size(); i++) + delete m_Buffers[i]; + } + void VertexArray::addBuffer(Buffer* buffer, GLuint index) + { + bind(); + buffer->bind(); + glEnableVertexAttribArray(index); + glVertexAttribPointer(index, buffer->getComponentCount(), GL_FLOAT, GL_FALSE, 0, 0); + buffer->unbind(); + unbind(); + } + void VertexArray::bind() const + { + glBindVertexArray(m_ArrayID); + } + void VertexArray::unbind() const + { + glBindVertexArray(0); + } + } +} diff --git a/Spraky-core/x64/src/graphics/buffers/vertexarray.h b/Spraky-core/x64/src/graphics/buffers/vertexarray.h new file mode 100644 index 0000000..bfb7c79 --- /dev/null +++ b/Spraky-core/x64/src/graphics/buffers/vertexarray.h @@ -0,0 +1,20 @@ +#pragma once +#include +#include +#include "buffer.h" + + namespace sparky{ + namespace graphics { + class VertexArray { + private: + GLuint m_ArrayID; + std::vector m_Buffers; + public: + VertexArray(); + ~VertexArray(); + void addBuffer(Buffer* buffer, GLuint index); + void bind() const; + void unbind() const; + }; + } +} \ No newline at end of file diff --git a/Spraky-core/x64/src/maths/mat4.cpp b/Spraky-core/x64/src/maths/mat4.cpp index 74387d5..544aa4e 100644 --- a/Spraky-core/x64/src/maths/mat4.cpp +++ b/Spraky-core/x64/src/maths/mat4.cpp @@ -92,11 +92,11 @@ namespace sparky{ } - mat4 mat4::tranlation(const vec3& translation) { + mat4 mat4::translation(const vec3& translation) { // Corrected spelling mat4 result(1.0f); - result.elements[0 + 3 * 4] = translation.x; - result.elements[1 + 3 * 4] = translation.y; - result.elements[2 + 3 * 4] = translation.z; + result.elements[12] = translation.x; + result.elements[13] = translation.y; + result.elements[14] = translation.z; return result; } diff --git a/Spraky-core/x64/src/maths/mat4.h b/Spraky-core/x64/src/maths/mat4.h index ce9c22d..25e7224 100644 --- a/Spraky-core/x64/src/maths/mat4.h +++ b/Spraky-core/x64/src/maths/mat4.h @@ -26,7 +26,7 @@ namespace sparky{ static mat4 orthographic(float left, float right, float bottom, float top, float near, float far); static mat4 perspective(float fov, float aspectRatio, float near, float far); - static mat4 tranlation(const vec3& translation); + static mat4 translation(const vec3& translation); static mat4 rotation(float angle, const vec3& axis); static mat4 scale(const vec3& scale); diff --git a/Spraky-core/x64/src/shaders/basic.vert b/Spraky-core/x64/src/shaders/basic.vert index d4a15a8..ef4b49c 100644 --- a/Spraky-core/x64/src/shaders/basic.vert +++ b/Spraky-core/x64/src/shaders/basic.vert @@ -1,15 +1,10 @@ #version 330 core -layout (location = 0) in vec4 position; -uniform mat4 pr_matrix; -uniform mat4 vw_matrix = mat4(1.0); -uniform mat4 ml_matrix = mat4(1.0); +layout(location = 0) in vec3 position; -out vec2 lightPos; -out vec4 pos; +uniform mat4 pr_matrix; +uniform mat4 ml_matrix; -void main() -{ - gl_Position = pr_matrix * vw_matrix * ml_matrix * position; - pos = position; +void main() { + gl_Position = pr_matrix * ml_matrix * vec4(position, 1.0); }