Skip to content

Commit

Permalink
SImple2Drenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Prtm2110 committed Jan 15, 2025
1 parent c47f252 commit 97bd65a
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 50 deletions.
6 changes: 5 additions & 1 deletion Spraky-core/Spraky-core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
<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\simple2drenderer.cpp" />
<ClCompile Include="x64\src\graphics\shader.cpp" />
<ClCompile Include="x64\src\graphics\window.cpp" />
<ClCompile Include="x64\src\maths\mat4.cpp" />
Expand All @@ -150,10 +151,13 @@
<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\renderable2d.h" />
<ClInclude Include="x64\src\graphics\renderer2d.h" />
<ClInclude Include="x64\src\graphics\shader.h" />
<ClInclude Include="x64\src\graphics\simple2drenderer.h" />
<ClInclude Include="x64\src\graphics\window.h" />
<ClInclude Include="x64\src\maths\mat4.h" />
<ClInclude Include="x64\src\maths\math.h" />
<ClInclude Include="x64\src\maths\maths.h" />
<ClInclude Include="x64\src\maths\maths_func.h" />
<ClInclude Include="x64\src\maths\vec2.h" />
<ClInclude Include="x64\src\maths\vec3.h" />
Expand Down
14 changes: 13 additions & 1 deletion Spraky-core/Spraky-core.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
<ClCompile Include="x64\src\graphics\buffers\vertexarray.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="x64\src\graphics\simple2drenderer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="x64\src\graphics\window.h">
Expand All @@ -62,7 +65,7 @@
<ClInclude Include="x64\src\maths\vec4.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="x64\src\maths\math.h">
<ClInclude Include="x64\src\maths\maths.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="x64\src\maths\mat4.h">
Expand All @@ -86,6 +89,15 @@
<ClInclude Include="x64\src\graphics\buffers\vertexarray.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="x64\src\graphics\renderable2d.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="x64\src\graphics\renderer2d.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="x64\src\graphics\simple2drenderer.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="x64\src\shaders\basic.frag" />
Expand Down
64 changes: 25 additions & 39 deletions Spraky-core/main.cpp
Original file line number Diff line number Diff line change
@@ -1,77 +1,63 @@
#include <iostream>
#include <GL/glew.h>
#include "x64/src/graphics/window.h"
#include "x64/src/maths/math.h"
#include "x64/src/maths/maths.h"
#include "x64/src/utils/fileutils.h"
#include "x64/src/graphics/shader.h"
#include "x64/src/graphics/buffers/buffer.h"
#include "x64/src/graphics/buffers/vertexarray.h"
#include "x64/src/graphics/buffers/indexbuffer.h"
#include "x64/src/graphics/renderable2d.h"
#include "x64/src/graphics/renderer2d.h"
#include "x64/src/graphics/simple2drenderer.h"


int main() {
using namespace sparky;
using namespace graphics;
using namespace maths;
using namespace utils;

// Create a window
std::cout << "Creating window..." << std::endl;
Window window("Sparky!!", 1280, 800);
if (glewInit() != GLEW_OK) {
std::cerr << "Failed to initialize GLEW" << std::endl;
return -1;
}

glClearColor(0.2f, 1.3f, 0.8f, 0.9f);

// Initialize OpenGL VAO
std::cout << "Initializing VAO..." << std::endl;
GLfloat vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
-0.5f, 0.5f, 0.0f
};

GLushort indices[] = {
0, 1, 2,
2, 3, 0
};

VertexArray vao;
Buffer* vbo = new Buffer(vertices, 4 * 3, 3);
IndexBuffer ibo(indices, 6);

std::cout << "Adding buffer to VAO..." << std::endl;
vao.addBuffer(vbo, 0);

mat4 ortho = mat4::orthographic(0.0f, 16.0f, 0.0f, 9.0f, -1.0f, 1.0f);



std::cout << "Loading shader..." << std::endl;
Shader shader("x64/src/shaders/basic.vert", "x64/src/shaders/basic.frag");
shader.enable();
shader.setUniformMat4("pr_matrix", ortho);
shader.setUniformMat4("ml_matrix", mat4::translation(vec3(4, 4.5, 0)));
shader.setUniform2f("light_pos", vec2(4.0f, 1.5f)); // Center of the square
shader.setUniform4f("colour", vec4(0.2f, 0.3f, 0.8f, 1.0f)); // Black color for the glow

Renderable2D sprite(vec3(4, 1, 0), vec2(4, 4), vec4(1, 0, 1, 1), shader);
Simple2DRenderer renderer;

shader.setUniform4f("colour", vec4(0.2f, 0.3f, 0.8f, 1.0f));





std::cout << "Entering main loop..." << std::endl;
while (!window.closed()) {

window.clear();
vao.bind();

ibo.bind();

glDrawElements(GL_TRIANGLES, ibo.getCount(), GL_UNSIGNED_SHORT, 0); // Use GL_UNSIGNED_SHORT

// Check for OpenGL errors

vao.unbind();

double x, y;
window.getMousePosition(x, y);
shader.setUniform2f("light_pos", vec2(x * 16.0f / 1280.0f, 9.0f - y * 9.0f / 800.0f)); // Center of the square

renderer.submit(&sprite);
renderer.flush();



window.update();
}
delete vbo; // Clean up dynamically allocated memory

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

#include "../maths/maths.h"
#include "../maths/vec3.h"
#include "../maths/vec2.h"
#include "../maths/vec4.h"

#include "buffers/buffer.h"
#include "buffers/indexbuffer.h"
#include "buffers/vertexarray.h"
#include "shader.h"


namespace sparky{
namespace graphics {

class Renderable2D
{
protected:
maths::vec3 m_Position;
maths::vec2 m_Size;
maths::vec4 m_Color;

VertexArray* m_VertexArray;
IndexBuffer* m_IndexBuffer;
Shader* m_Shader;

public:
Renderable2D(sparky::maths::vec3 position, sparky::maths::vec2 size, sparky::maths::vec4 color, Shader &shader)
: m_Position(position), m_Size(size), m_Color(color), m_Shader(&shader)
{
m_VertexArray = new VertexArray();
GLfloat vertices[] = {
0, 0, 0,
0, size.y, 0,
size.x, size.y, 0,
size.x, 0, 0
};
GLfloat colors[]= {
color.x, color.y, color.z, color.w,
color.x, color.y, color.z, color.w,
color.x, color.y, color.z, color.w,
color.x, color.y, color.z, color.w
};
m_VertexArray->addBuffer(new Buffer(vertices, 4 * 3, 3), 0);
m_VertexArray->addBuffer(new Buffer(colors, 4 * 4, 4), 1);

GLushort indices[] = { 0, 1, 2, 2, 3, 0 };
m_IndexBuffer = new IndexBuffer(indices, 6);
}

~Renderable2D()
{
delete m_VertexArray;
delete m_IndexBuffer;
}
public:
inline const VertexArray* getVAO() const { return m_VertexArray; }
inline const IndexBuffer* getIBO() const { return m_IndexBuffer; }

inline Shader& getShader() const { return *m_Shader; }

inline const maths::vec3& getPosition() const { return m_Position; }
inline const maths::vec2& getSize() const { return m_Size; }
inline const maths::vec4& getColor() const { return m_Color; }
};
}
}
17 changes: 17 additions & 0 deletions Spraky-core/x64/src/graphics/renderer2d.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "renderable2d.h"
#include <GL/glew.h>

#include "../maths/maths.h"

namespace sparky {
namespace graphics {
class Renderer2D
{
public:
virtual void submit(const Renderable2D* renderable) = 0;
virtual void flush() = 0;
};
}
}
2 changes: 1 addition & 1 deletion Spraky-core/x64/src/graphics/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <vector>
#include <GL/glew.h>
#include "../utils/fileutils.h"
#include "../maths/math.h"
#include "../maths/maths.h"
namespace sparky {
namespace graphics {

Expand Down
27 changes: 27 additions & 0 deletions Spraky-core/x64/src/graphics/simple2drenderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "simple2drenderer.h"

namespace sparky {
namespace graphics {

void Simple2DRenderer::submit(const Renderable2D* renderable)
{
m_RenderQueue.push_back(renderable);

}
void Simple2DRenderer::flush()
{
while (!m_RenderQueue.empty()) {
const Renderable2D& renderable = *m_RenderQueue.front();
renderable.getVAO()->bind();
renderable.getIBO()->bind();

renderable.getShader().setUniformMat4("ml_matrix", maths::mat4::translation(renderable.getPosition()));
glDrawElements(GL_TRIANGLES, renderable.getIBO()->getCount(), GL_UNSIGNED_SHORT, nullptr);

renderable.getIBO()->unbind();
renderable.getVAO()->unbind();
m_RenderQueue.pop_front();
}
}
}
}
17 changes: 17 additions & 0 deletions Spraky-core/x64/src/graphics/simple2drenderer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once
#include "renderable2d.h"
#include <deque>

namespace sparky {
namespace graphics {
class Simple2DRenderer
{
public:
std::deque<const Renderable2D*> m_RenderQueue;
Simple2DRenderer() {}
public:
virtual void submit(const Renderable2D* renderable);
virtual void flush();
};
}
}
5 changes: 5 additions & 0 deletions Spraky-core/x64/src/graphics/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ namespace sparky {
}

void Window::update() {
GLenum err = glGetError();
if (err != GL_NO_ERROR) {
std::cerr << "OpenGL Error: " << err << std::endl;
}

glfwPollEvents();
glfwSwapBuffers(m_Window);
}
Expand Down
8 changes: 0 additions & 8 deletions Spraky-core/x64/src/maths/math.h

This file was deleted.

0 comments on commit 97bd65a

Please sign in to comment.