Skip to content

Commit

Permalink
Vertex array added and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Prtm2110 committed Jan 11, 2025
1 parent 3068e35 commit 64e5567
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 17 deletions.
4 changes: 4 additions & 0 deletions Spraky-core/Spraky-core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="x64\src\graphics\buffers\buffer.cpp" />
<ClCompile Include="x64\src\graphics\buffers\indexbuffer.cpp" />
<ClCompile Include="x64\src\graphics\buffers\vertexarray.cpp" />
<ClCompile Include="x64\src\graphics\shader.cpp" />
<ClCompile Include="x64\src\graphics\window.cpp" />
<ClCompile Include="x64\src\maths\mat4.cpp" />
Expand All @@ -145,7 +147,9 @@
<ClCompile Include="x64\src\utils\fileutils.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="x64\src\graphics\buffers\indexbuffer.h" />
<ClInclude Include="x64\src\graphics\buffers\buffer.h" />
<ClInclude Include="x64\src\graphics\buffers\vertexarray.h" />
<ClInclude Include="x64\src\graphics\shader.h" />
<ClInclude Include="x64\src\graphics\window.h" />
<ClInclude Include="x64\src\maths\mat4.h" />
Expand Down
12 changes: 12 additions & 0 deletions Spraky-core/Spraky-core.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
<ClCompile Include="x64\src\graphics\buffers\buffer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="x64\src\graphics\buffers\indexbuffer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="x64\src\graphics\buffers\vertexarray.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="x64\src\graphics\window.h">
Expand Down Expand Up @@ -74,6 +80,12 @@
<ClInclude Include="x64\src\graphics\buffers\buffer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="x64\src\graphics\buffers\indexbuffer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="x64\src\graphics\buffers\vertexarray.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="x64\src\shaders\basic.frag" />
Expand Down
4 changes: 2 additions & 2 deletions Spraky-core/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions Spraky-core/x64/src/graphics/buffers/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }


Expand Down
29 changes: 29 additions & 0 deletions Spraky-core/x64/src/graphics/buffers/indexbuffer.cpp
Original file line number Diff line number Diff line change
@@ -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);
}

}
}
24 changes: 24 additions & 0 deletions Spraky-core/x64/src/graphics/buffers/indexbuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include<GL/glew.h>

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


};
}
}
32 changes: 32 additions & 0 deletions Spraky-core/x64/src/graphics/buffers/vertexarray.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
20 changes: 20 additions & 0 deletions Spraky-core/x64/src/graphics/buffers/vertexarray.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include <GL/glew.h>
#include <vector>
#include "buffer.h"

namespace sparky{
namespace graphics {
class VertexArray {
private:
GLuint m_ArrayID;
std::vector<Buffer*> m_Buffers;
public:
VertexArray();
~VertexArray();
void addBuffer(Buffer* buffer, GLuint index);
void bind() const;
void unbind() const;
};
}
}
8 changes: 4 additions & 4 deletions Spraky-core/x64/src/maths/mat4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion Spraky-core/x64/src/maths/mat4.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
15 changes: 5 additions & 10 deletions Spraky-core/x64/src/shaders/basic.vert
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 64e5567

Please sign in to comment.