-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
178 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.