|
|
|
@ -11,12 +11,30 @@
|
|
|
|
#include "shaders/defaultShaders.h"
|
|
|
|
#include "shaders/defaultShaders.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <utils/logger.h>
|
|
|
|
#include <utils/logger.h>
|
|
|
|
|
|
|
|
#include <utils/helpers.h>
|
|
|
|
#include <glad/gl.h>
|
|
|
|
#include <glad/gl.h>
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <core/core.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace lunarium
|
|
|
|
namespace lunarium
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
Renderer2D::Renderer2D()
|
|
|
|
|
|
|
|
: mpCamera(nullptr)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct QuadTestData
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Color color;
|
|
|
|
|
|
|
|
BufferLayout mBufferLayout;
|
|
|
|
|
|
|
|
VertexBuffer* mVertexBuffer;
|
|
|
|
|
|
|
|
IndexBuffer* mIndexBuffer;
|
|
|
|
|
|
|
|
Shader* mQuadShader;
|
|
|
|
|
|
|
|
} mTestData;
|
|
|
|
|
|
|
|
|
|
|
|
OpRes Renderer2D::Initialize()
|
|
|
|
OpRes Renderer2D::Initialize()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
mpCamera = nullptr;
|
|
|
|
mpCamera = nullptr;
|
|
|
|
@ -44,6 +62,39 @@ namespace lunarium
|
|
|
|
u8 data[4] = {255, 128, 0, 0};
|
|
|
|
u8 data[4] = {255, 128, 0, 0};
|
|
|
|
mpDebugTexture = Texture::Create(data, 1, 1);
|
|
|
|
mpDebugTexture = Texture::Create(data, 1, 1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DEBUG: INIT TEST DATA
|
|
|
|
|
|
|
|
mTestData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 2}); // Position
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GLfloat vertices[6][2] = {
|
|
|
|
|
|
|
|
{ -0.5f, 0.5f, },
|
|
|
|
|
|
|
|
{ 0.5f, -0.5f, },
|
|
|
|
|
|
|
|
{ -0.5f, -0.5f, },
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{ -0.5f, 0.5f, },
|
|
|
|
|
|
|
|
{ 0.5f, 0.5f, },
|
|
|
|
|
|
|
|
{ 0.5f, -0.5f, }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
mTestData.mVertexBuffer = VertexBuffer::Create(mTestData.mBufferLayout, 6, vertices);
|
|
|
|
|
|
|
|
std::string vert_source = File::ReadTextFile("test.vert");
|
|
|
|
|
|
|
|
std::string frag_source = File::ReadTextFile("test.frag");
|
|
|
|
|
|
|
|
mTestData.mQuadShader = new Shader(vert_source.c_str(), nullptr, frag_source.c_str());
|
|
|
|
|
|
|
|
if (!mTestData.mQuadShader->IsValid())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return OpRes::Fail("Failed to create the test shader");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glGenVertexArrays(1, &mTVAO);
|
|
|
|
|
|
|
|
glGenBuffers(1, &mTVBO);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, mTVBO);
|
|
|
|
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 12, vertices, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glBindVertexArray(mTVAO);
|
|
|
|
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
|
|
|
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
|
|
|
|
glBindVertexArray(0);
|
|
|
|
|
|
|
|
|
|
|
|
return OpRes::OK();
|
|
|
|
return OpRes::OK();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -67,6 +118,7 @@ namespace lunarium
|
|
|
|
void Renderer2D::ResetFrameStats()
|
|
|
|
void Renderer2D::ResetFrameStats()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
mFrameStats.DrawCalls = 0;
|
|
|
|
mFrameStats.DrawCalls = 0;
|
|
|
|
|
|
|
|
mFrameStats.NumTris = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Renderer2D::FrameStats Renderer2D::GetFrameStats() const
|
|
|
|
Renderer2D::FrameStats Renderer2D::GetFrameStats() const
|
|
|
|
@ -77,8 +129,12 @@ namespace lunarium
|
|
|
|
void Renderer2D::BeginDraw(OrthographicCamera* pCamera)
|
|
|
|
void Renderer2D::BeginDraw(OrthographicCamera* pCamera)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
mpCamera = pCamera;
|
|
|
|
mpCamera = pCamera;
|
|
|
|
glViewport(0, 0, pCamera->GetViewport().Width, pCamera->GetViewport().Height);
|
|
|
|
//glViewport(0, 0, pCamera->GetViewport().Width, pCamera->GetViewport().Height);
|
|
|
|
|
|
|
|
int w, h;
|
|
|
|
|
|
|
|
Core::MainWindow().GetFramebufferSize(&w, &h);
|
|
|
|
|
|
|
|
glViewport(0, 0, w, h);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
//ResetFrameStats();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Renderer2D::EndDraw()
|
|
|
|
void Renderer2D::EndDraw()
|
|
|
|
@ -91,6 +147,18 @@ namespace lunarium
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
void Renderer2D::DrawQuad(Rectangle quad, Color color, Texture* texture, float angle)
|
|
|
|
void Renderer2D::DrawQuad(Rectangle quad, Color color, Texture* texture, float angle)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mTestData.color = color;
|
|
|
|
|
|
|
|
//mTestData.mVertexBuffer->Bind();
|
|
|
|
|
|
|
|
mTestData.mQuadShader->Use();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Uniform uni { "spriteColor", UniformType::F3, -1 };
|
|
|
|
|
|
|
|
mTestData.mQuadShader->SetUniform(uni, color.arr).LogIfFailed(LogCategory::GRAPHICS, "Failed to set uniform for the test shader!");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glBindVertexArray(mTVAO);
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
int texture_slot = -1;
|
|
|
|
int texture_slot = -1;
|
|
|
|
if (texture)
|
|
|
|
if (texture)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -120,7 +188,7 @@ namespace lunarium
|
|
|
|
// FIRST
|
|
|
|
// FIRST
|
|
|
|
QuadData::Vertex v1;
|
|
|
|
QuadData::Vertex v1;
|
|
|
|
int vert_size = sizeof(QuadData::Vertex);
|
|
|
|
int vert_size = sizeof(QuadData::Vertex);
|
|
|
|
v1.pos = mQuadData.vert_pos[0] * model;
|
|
|
|
v1.pos = mQuadData.vert_pos[0];// * model;
|
|
|
|
v1.tex_coord = mQuadData.vert_tex[0];
|
|
|
|
v1.tex_coord = mQuadData.vert_tex[0];
|
|
|
|
v1.color = color;
|
|
|
|
v1.color = color;
|
|
|
|
v1.tex_slot = texture_slot;
|
|
|
|
v1.tex_slot = texture_slot;
|
|
|
|
@ -129,7 +197,7 @@ namespace lunarium
|
|
|
|
|
|
|
|
|
|
|
|
// SECOND
|
|
|
|
// SECOND
|
|
|
|
QuadData::Vertex v2;
|
|
|
|
QuadData::Vertex v2;
|
|
|
|
v2.pos = mQuadData.vert_pos[1] * model;
|
|
|
|
v2.pos = mQuadData.vert_pos[1];// * model;
|
|
|
|
v2.tex_coord = mQuadData.vert_tex[1];
|
|
|
|
v2.tex_coord = mQuadData.vert_tex[1];
|
|
|
|
v2.color = color;
|
|
|
|
v2.color = color;
|
|
|
|
v2.tex_slot = texture_slot;
|
|
|
|
v2.tex_slot = texture_slot;
|
|
|
|
@ -138,7 +206,7 @@ namespace lunarium
|
|
|
|
|
|
|
|
|
|
|
|
// THIRD
|
|
|
|
// THIRD
|
|
|
|
QuadData::Vertex v3;
|
|
|
|
QuadData::Vertex v3;
|
|
|
|
v3.pos = mQuadData.vert_pos[2] * model;
|
|
|
|
v3.pos = mQuadData.vert_pos[2];// * model;
|
|
|
|
v3.tex_coord = mQuadData.vert_tex[2];
|
|
|
|
v3.tex_coord = mQuadData.vert_tex[2];
|
|
|
|
v3.color = color;
|
|
|
|
v3.color = color;
|
|
|
|
v3.tex_slot = texture_slot;
|
|
|
|
v3.tex_slot = texture_slot;
|
|
|
|
@ -147,7 +215,7 @@ namespace lunarium
|
|
|
|
|
|
|
|
|
|
|
|
// FOURTH
|
|
|
|
// FOURTH
|
|
|
|
QuadData::Vertex v4;
|
|
|
|
QuadData::Vertex v4;
|
|
|
|
v4.pos = mQuadData.vert_pos[3] * model;
|
|
|
|
v4.pos = mQuadData.vert_pos[3];// * model;
|
|
|
|
v4.tex_coord = mQuadData.vert_tex[3];
|
|
|
|
v4.tex_coord = mQuadData.vert_tex[3];
|
|
|
|
v4.color = color;
|
|
|
|
v4.color = color;
|
|
|
|
v4.tex_slot = texture_slot;
|
|
|
|
v4.tex_slot = texture_slot;
|
|
|
|
@ -168,8 +236,11 @@ namespace lunarium
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mQuadData.mNumQuads++;
|
|
|
|
mQuadData.mNumQuads++;
|
|
|
|
|
|
|
|
mFrameStats.NumTris += 2;
|
|
|
|
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Renderer2D::DrawSprite(Texture& image, glm::vec2 position, Color color, float angle)
|
|
|
|
void Renderer2D::DrawSprite(Texture& image, glm::vec2 position, Color color, float angle)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Logger::Warn(LogCategory::GRAPHICS, "Renderer2D::DrawSprite is not yet implemented");
|
|
|
|
Logger::Warn(LogCategory::GRAPHICS, "Renderer2D::DrawSprite is not yet implemented");
|
|
|
|
@ -225,35 +296,37 @@ namespace lunarium
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
void Renderer2D::Flush()
|
|
|
|
void Renderer2D::Flush()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// TODO: Draw calls
|
|
|
|
if (mQuadData.mNumQuads > 0)
|
|
|
|
mQuadData.mQuadShader->Use();
|
|
|
|
|
|
|
|
mQuadData.mVertexBuffer->Bind();
|
|
|
|
|
|
|
|
mQuadData.mIndexBuffer->Bind();
|
|
|
|
|
|
|
|
for (int i = 0; i < mLoadedTextures.size(); i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
mLoadedTextures[i]->Bind(i);
|
|
|
|
mQuadData.mVertexBuffer->Bind();
|
|
|
|
}
|
|
|
|
mQuadData.mIndexBuffer->Bind();
|
|
|
|
|
|
|
|
mQuadData.mQuadShader->Use();
|
|
|
|
Uniform umodel;
|
|
|
|
for (int i = 0; i < mLoadedTextures.size(); i++)
|
|
|
|
umodel.Type = UniformType::FMAT4;
|
|
|
|
{
|
|
|
|
umodel.Location = 0;
|
|
|
|
mLoadedTextures[i]->Bind(i);
|
|
|
|
umodel.Name = "Model";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Uniform uprojview;
|
|
|
|
Uniform umodel;
|
|
|
|
uprojview.Type = UniformType::FMAT4;
|
|
|
|
umodel.Type = UniformType::FMAT4;
|
|
|
|
uprojview.Location = 0;
|
|
|
|
umodel.Location = -1;
|
|
|
|
uprojview.Name = "ProjView";
|
|
|
|
umodel.Name = "model";
|
|
|
|
|
|
|
|
|
|
|
|
mQuadData.mQuadShader->SetUniform(umodel, (void*)glm::value_ptr(glm::mat4(1.0f)));
|
|
|
|
Uniform uprojview;
|
|
|
|
mQuadData.mQuadShader->SetUniform(uprojview, (void*)glm::value_ptr(mpCamera->GetViewProjection()));
|
|
|
|
uprojview.Type = UniformType::FMAT4;
|
|
|
|
|
|
|
|
uprojview.Location = -1;
|
|
|
|
|
|
|
|
uprojview.Name = "projview";
|
|
|
|
|
|
|
|
|
|
|
|
glDrawElements(GL_TRIANGLES, mQuadData.mNumQuads * 6, GL_UNSIGNED_INT, nullptr);
|
|
|
|
mQuadData.mQuadShader->SetUniform(umodel, (void*)glm::value_ptr(glm::mat4(1.0f))).LogIfFailed(LogCategory::GRAPHICS);
|
|
|
|
mFrameStats.DrawCalls++;
|
|
|
|
mQuadData.mQuadShader->SetUniform(uprojview, (void*)glm::value_ptr(mpCamera->GetViewProjection())).LogIfFailed(LogCategory::GRAPHICS);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glDrawElements(GL_TRIANGLES, mQuadData.mNumQuads * 6, GL_UNSIGNED_INT, nullptr);
|
|
|
|
|
|
|
|
mFrameStats.DrawCalls++;
|
|
|
|
|
|
|
|
}
|
|
|
|
// Reset drawing data
|
|
|
|
// Reset drawing data
|
|
|
|
mLoadedTextures.clear();
|
|
|
|
mLoadedTextures.clear();
|
|
|
|
mQuadData.mVertexBuffer->Clear();
|
|
|
|
mQuadData.mVertexBuffer->Clear();
|
|
|
|
mQuadData.mIndexBuffer->Clear();
|
|
|
|
mQuadData.mIndexBuffer->Clear();
|
|
|
|
|
|
|
|
mQuadData.mNumQuads = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Add the debug texture back to the map
|
|
|
|
// TODO: Add the debug texture back to the map
|
|
|
|
mLoadedTextures.push_back(mpDebugTexture);
|
|
|
|
mLoadedTextures.push_back(mpDebugTexture);
|
|
|
|
|