-
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
34 changed files
with
1,487 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#include "BatchRenderer2D.h" | ||
|
||
namespace rabbit { | ||
namespace graphics { | ||
|
||
BatchRenderer2D::BatchRenderer2D() | ||
{ | ||
init(); | ||
} | ||
|
||
BatchRenderer2D::~BatchRenderer2D() | ||
{ | ||
delete m_IBO; | ||
glDeleteBuffers(1, &m_VBO); | ||
} | ||
|
||
void BatchRenderer2D::init() | ||
{ | ||
glGenVertexArrays(1, &m_VAO); | ||
glGenBuffers(1, &m_VBO); | ||
|
||
glBindVertexArray(m_VAO); | ||
glBindBuffer(GL_ARRAY_BUFFER, m_VBO); | ||
glBufferData(GL_ARRAY_BUFFER, RENDERER_BUFFER_SIZE, NULL, GL_DYNAMIC_DRAW); | ||
glEnableVertexAttribArray(SHADER_VERTEX_INDEX); | ||
glEnableVertexAttribArray(SHADER_COLOR_INDEX); | ||
glVertexAttribPointer(SHADER_VERTEX_INDEX, 3, GL_FLOAT, GL_FALSE, RENDERER_VERTEX_SIZE, (const GLvoid*)0); | ||
glVertexAttribPointer(SHADER_COLOR_INDEX, 4, GL_FLOAT, GL_FALSE, RENDERER_VERTEX_SIZE, (const GLvoid*)(3 * sizeof(GLfloat))); | ||
glBindBuffer(GL_ARRAY_BUFFER, 0); | ||
|
||
GLuint* indices = new GLuint[RENDERER_INDICES_SIZE]; | ||
|
||
int offset = 0; | ||
for (int i = 0; i < RENDERER_INDICES_SIZE; i += 6) | ||
{ | ||
indices[i] = offset + 0; | ||
indices[i + 1] = offset + 1; | ||
indices[i + 2] = offset + 2; | ||
|
||
indices[i + 3] = offset + 2; | ||
indices[i + 4] = offset + 3; | ||
indices[i + 5] = offset + 0; | ||
|
||
offset += 4; | ||
} | ||
|
||
m_IBO = new IndexBuffer( indices, RENDERER_INDICES_SIZE); | ||
|
||
glBindVertexArray(0); | ||
} | ||
|
||
void BatchRenderer2D::begin() | ||
{ | ||
glBindBuffer(GL_ARRAY_BUFFER, m_VBO); | ||
m_Buffer = (VertexData*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); | ||
} | ||
|
||
void BatchRenderer2D::submit(const Renderable2D* renderable) | ||
{ | ||
const maths::vec3& position = renderable->getPosition(); | ||
const maths::vec2& size = renderable->getSize(); | ||
const maths::vec4& color = renderable->getColor(); | ||
|
||
m_Buffer->vertex = position; | ||
m_Buffer->color = color; | ||
m_Buffer++; | ||
|
||
m_Buffer->vertex = maths::vec3(position.x, position.y + size.y, position.z); | ||
m_Buffer->color = color; | ||
m_Buffer++; | ||
|
||
m_Buffer->vertex = maths::vec3(position.x + size.x, position.y + size.y, position.z); | ||
m_Buffer->color = color; | ||
m_Buffer++; | ||
|
||
m_Buffer->vertex = maths::vec3(position.x + size.x, position.y, position.z); | ||
m_Buffer->color = color; | ||
m_Buffer++; | ||
|
||
m_IndexCount += 6; | ||
} | ||
|
||
void BatchRenderer2D::end() | ||
{ | ||
glUnmapBuffer(GL_ARRAY_BUFFER); | ||
glBindBuffer(GL_ARRAY_BUFFER, 0); | ||
} | ||
|
||
void BatchRenderer2D::flush() | ||
{ | ||
glBindVertexArray(m_VAO); | ||
m_IBO->bind(); | ||
|
||
glDrawElements(GL_TRIANGLES, m_IndexCount, GL_UNSIGNED_INT, NULL); | ||
|
||
m_IBO->unbind(); | ||
glBindVertexArray(0); | ||
|
||
m_IndexCount = 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,38 @@ | ||
#pragma once | ||
|
||
#include "renderer2d.h" | ||
#include "buffers/indexbuffer.h" | ||
|
||
namespace rabbit { | ||
namespace graphics { | ||
|
||
#define RENDERER_MAX_SPRITES 60000 | ||
#define RENDERER_VERTEX_SIZE sizeof(VertexData) | ||
#define RENDERER_SPRITE_SIZE RENDERER_VERTEX_SIZE * 4 | ||
#define RENDERER_BUFFER_SIZE RENDERER_SPRITE_SIZE * RENDERER_MAX_SPRITES | ||
#define RENDERER_INDICES_SIZE RENDERER_MAX_SPRITES * 6 | ||
|
||
#define SHADER_VERTEX_INDEX 0 | ||
#define SHADER_COLOR_INDEX 1 | ||
|
||
class BatchRenderer2D : public Renderer2D | ||
{ | ||
private: | ||
GLuint m_VAO; | ||
GLuint m_VBO; | ||
IndexBuffer* m_IBO; | ||
GLsizei m_IndexCount; | ||
VertexData* m_Buffer; | ||
public: | ||
BatchRenderer2D(); | ||
~BatchRenderer2D(); | ||
void begin(); | ||
void submit(const Renderable2D* renderable) override; | ||
void end(); | ||
void flush() override; | ||
private: | ||
void init(); | ||
}; | ||
|
||
} | ||
} |
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,33 @@ | ||
#include "buffer.h" | ||
|
||
using namespace rabbit; | ||
using namespace graphics; | ||
|
||
namespace rabbit { | ||
namespace graphics { | ||
|
||
Buffer::Buffer(GLfloat* data, GLsizei count, GLuint componenetCount) | ||
: m_ComponentCount(componenetCount) | ||
{ | ||
//m_ComponentCount = componenetCount;this is same as the above line | ||
glGenBuffers(1, &m_BufferID); | ||
glBindBuffer(GL_ARRAY_BUFFER, m_BufferID); | ||
glBufferData(GL_ARRAY_BUFFER, count * sizeof(GLfloat), data, GL_STATIC_DRAW); | ||
glBindBuffer(GL_ARRAY_BUFFER, 0); | ||
|
||
} | ||
Buffer::~Buffer() | ||
{ | ||
glDeleteBuffers(1, &m_BufferID); | ||
} | ||
void Buffer::bind() const | ||
{ | ||
glBindBuffer(GL_ARRAY_BUFFER, m_BufferID); | ||
} | ||
void Buffer::unbind() const | ||
{ | ||
glBindBuffer(GL_ARRAY_BUFFER, 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,25 @@ | ||
#pragma once | ||
|
||
#include<GL/glew.h> | ||
|
||
namespace rabbit { | ||
namespace graphics { | ||
|
||
class Buffer { | ||
|
||
private: | ||
GLuint m_BufferID; | ||
GLuint m_ComponentCount; | ||
|
||
public: | ||
Buffer(GLfloat* data, GLsizei count, GLuint componenetCount); | ||
~Buffer(); | ||
void bind() const; | ||
void unbind() const; | ||
|
||
inline GLuint getComponentCount() const { return m_ComponentCount; } | ||
|
||
|
||
}; | ||
} | ||
} |
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,41 @@ | ||
#include "indexbuffer.h" | ||
|
||
namespace rabbit { | ||
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); | ||
} | ||
|
||
IndexBuffer::IndexBuffer(GLuint* data, GLsizei count) | ||
: m_Count(count) | ||
{ | ||
glGenBuffers(1, &m_BufferID); | ||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_BufferID); | ||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(GLuint), data, GL_STATIC_DRAW); | ||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | ||
} | ||
|
||
IndexBuffer::~IndexBuffer() | ||
{ | ||
glDeleteBuffers(1, &m_BufferID); | ||
} | ||
|
||
void IndexBuffer::bind() const | ||
{ | ||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_BufferID); | ||
} | ||
|
||
void IndexBuffer::unbind() const | ||
{ | ||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 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,27 @@ | ||
#pragma once | ||
|
||
#include<GL/glew.h> | ||
|
||
namespace rabbit { | ||
namespace graphics { | ||
|
||
class IndexBuffer { | ||
|
||
private: | ||
GLuint m_BufferID; | ||
GLuint m_Count; | ||
|
||
public: | ||
IndexBuffer(GLushort* data, GLsizei count); | ||
IndexBuffer(GLuint* data, GLsizei count); // Add this constructor | ||
|
||
~IndexBuffer(); | ||
void bind() const; | ||
void unbind() const; | ||
|
||
inline GLuint getCount() const { return m_Count; } | ||
|
||
|
||
}; | ||
} | ||
} |
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,33 @@ | ||
#include "vertexarray.h" | ||
|
||
namespace rabbit { | ||
namespace graphics { | ||
VertexArray::VertexArray() | ||
{ | ||
glGenVertexArrays(1, &m_ArrayID); | ||
} | ||
VertexArray::~VertexArray() | ||
{ | ||
for (int i = 0; i < m_Buffers.size(); i++) | ||
delete m_Buffers[i]; | ||
|
||
glDeleteVertexArrays(1, &m_ArrayID); | ||
} | ||
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); | ||
} | ||
} | ||
} |
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,20 @@ | ||
#pragma once | ||
#include <GL/glew.h> | ||
#include <vector> | ||
#include "buffer.h" | ||
|
||
namespace rabbit{ | ||
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; | ||
}; | ||
} | ||
} |
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,39 @@ | ||
#pragma once | ||
|
||
#include "buffers/buffer.h" | ||
#include "buffers/indexbuffer.h" | ||
#include "buffers/vertexarray.h" | ||
|
||
#include "../maths/maths.h" | ||
#include "shader.h" | ||
|
||
namespace rabbit { | ||
namespace graphics { | ||
|
||
struct VertexData | ||
{ | ||
maths::vec3 vertex; | ||
maths::vec4 color; | ||
}; | ||
|
||
class Renderable2D | ||
{ | ||
protected: | ||
maths::vec3 m_Position; | ||
maths::vec2 m_Size; | ||
maths::vec4 m_Color; | ||
public: | ||
Renderable2D(maths::vec3 position, maths::vec2 size, maths::vec4 color) | ||
: m_Position(position), m_Size(size), m_Color(color) | ||
{ | ||
} | ||
|
||
virtual ~Renderable2D() {} | ||
|
||
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 rabbit { | ||
namespace graphics { | ||
class Renderer2D | ||
{ | ||
public: | ||
virtual void submit(const Renderable2D* renderable) = 0; | ||
virtual void flush() = 0; | ||
}; | ||
} | ||
} |
Oops, something went wrong.