We got a shape on screen again!

master
Joey Pollack 3 years ago
parent 593a16c9cc
commit 733c832b81

@ -41,6 +41,7 @@ IF NOT "!BUILD_ERRORLEVEL!"=="0" (
)
xcopy /y test_data\engine_state.json build\Debug\
xcopy /y src\renderer\shaders\* build\Debug\*
)

@ -102,7 +102,7 @@ namespace lunarium
if (type == GL_DEBUG_TYPE_ERROR)
{
Logger::GIError(LogCategory::GRAPHICS,
Logger::Error(LogCategory::GRAPHICS,
"%s (type: %s, source: %s, severity: %s), message: %s",
(type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""),
RenderContext::mDebugMsgSources[source].c_str(),

@ -11,12 +11,30 @@
#include "shaders/defaultShaders.h"
#include <utils/logger.h>
#include <utils/helpers.h>
#include <glad/gl.h>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <core/core.h>
namespace lunarium
{
Renderer2D::Renderer2D()
: mpCamera(nullptr)
{
}
struct QuadTestData
{
Color color;
BufferLayout mBufferLayout;
VertexBuffer* mVertexBuffer;
IndexBuffer* mIndexBuffer;
Shader* mQuadShader;
} mTestData;
OpRes Renderer2D::Initialize()
{
mpCamera = nullptr;
@ -44,6 +62,39 @@ namespace lunarium
u8 data[4] = {255, 128, 0, 0};
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();
}
@ -67,6 +118,7 @@ namespace lunarium
void Renderer2D::ResetFrameStats()
{
mFrameStats.DrawCalls = 0;
mFrameStats.NumTris = 0;
}
Renderer2D::FrameStats Renderer2D::GetFrameStats() const
@ -77,8 +129,12 @@ namespace lunarium
void Renderer2D::BeginDraw(OrthographicCamera* 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);
//ResetFrameStats();
}
void Renderer2D::EndDraw()
@ -91,6 +147,18 @@ namespace lunarium
/////////////////////////////////////////////////////////////////////
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;
if (texture)
{
@ -120,7 +188,7 @@ namespace lunarium
// FIRST
QuadData::Vertex v1;
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.color = color;
v1.tex_slot = texture_slot;
@ -129,7 +197,7 @@ namespace lunarium
// SECOND
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.color = color;
v2.tex_slot = texture_slot;
@ -138,7 +206,7 @@ namespace lunarium
// THIRD
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.color = color;
v3.tex_slot = texture_slot;
@ -147,7 +215,7 @@ namespace lunarium
// FOURTH
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.color = color;
v4.tex_slot = texture_slot;
@ -168,8 +236,11 @@ namespace lunarium
}
mQuadData.mNumQuads++;
mFrameStats.NumTris += 2;
*/
}
void Renderer2D::DrawSprite(Texture& image, glm::vec2 position, Color color, float angle)
{
Logger::Warn(LogCategory::GRAPHICS, "Renderer2D::DrawSprite is not yet implemented");
@ -225,10 +296,11 @@ namespace lunarium
/////////////////////////////////////////////////////////////////////
void Renderer2D::Flush()
{
// TODO: Draw calls
mQuadData.mQuadShader->Use();
if (mQuadData.mNumQuads > 0)
{
mQuadData.mVertexBuffer->Bind();
mQuadData.mIndexBuffer->Bind();
mQuadData.mQuadShader->Use();
for (int i = 0; i < mLoadedTextures.size(); i++)
{
mLoadedTextures[i]->Bind(i);
@ -236,24 +308,25 @@ namespace lunarium
Uniform umodel;
umodel.Type = UniformType::FMAT4;
umodel.Location = 0;
umodel.Name = "Model";
umodel.Location = -1;
umodel.Name = "model";
Uniform uprojview;
uprojview.Type = UniformType::FMAT4;
uprojview.Location = 0;
uprojview.Name = "ProjView";
uprojview.Location = -1;
uprojview.Name = "projview";
mQuadData.mQuadShader->SetUniform(umodel, (void*)glm::value_ptr(glm::mat4(1.0f)));
mQuadData.mQuadShader->SetUniform(uprojview, (void*)glm::value_ptr(mpCamera->GetViewProjection()));
mQuadData.mQuadShader->SetUniform(umodel, (void*)glm::value_ptr(glm::mat4(1.0f))).LogIfFailed(LogCategory::GRAPHICS);
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
mLoadedTextures.clear();
mQuadData.mVertexBuffer->Clear();
mQuadData.mIndexBuffer->Clear();
mQuadData.mNumQuads = 0;
// TODO: Add the debug texture back to the map
mLoadedTextures.push_back(mpDebugTexture);

@ -26,8 +26,9 @@ namespace lunarium
public:
struct FrameStats
{
int NumTris = 0;
int DrawCalls = 0;
} mFrameStats;
};
Texture* GetDebugTexture();
void SetClearColor(Color color);
@ -50,6 +51,12 @@ namespace lunarium
void Shutdown();
void Flush();
private:
Renderer2D();
Renderer2D(const Renderer2D&) = delete;
Renderer2D(const Renderer2D&&) = delete;
Renderer2D operator=(const Renderer2D&) = delete;
private: // data
struct QuadData
@ -60,11 +67,18 @@ namespace lunarium
const int MaxIndices = MaxQuads * 6;
const int TextureSlots = 32;
// const glm::vec4 vert_pos[4] = {
// { -0.5f, 0.5f, 0.0f, 1.0f }, // TOP LEFT
// { 0.5f, 0.5f, 0.0f, 1.0f }, // TOP RIGHT
// { 0.5f, -0.5f, 0.0f, 1.0f }, // BOTTOM RIGHT
// { -0.5f, -0.5f, 0.0f, 1.0f } // BOTTOM LEFT
// };
const glm::vec4 vert_pos[4] = {
{ -0.5f, 0.5f, 0.0f, 1.0f }, // TOP LEFT
{ 0.5f, 0.5f, 0.0f, 1.0f }, // TOP RIGHT
{ 0.5f, -0.5f, 0.0f, 1.0f }, // BOTTOM RIGHT
{ -0.5f, -0.5f, 0.0f, 1.0f } // BOTTOM LEFT
{ -0.5f, -0.5f, 0.0f, 1.0f },
{ 0.5f, -0.5f, 0.0f, 1.0f },
{ 0.5f, 0.5f, 0.0f, 1.0f },
{ -0.5f, 0.5f, 0.0f, 1.0f }
};
const glm::vec2 vert_tex[4] = {
@ -82,7 +96,8 @@ namespace lunarium
int tex_slot;
};
const u32 indices[6] = { 0, 1, 2, 0, 2, 3 };
//const u32 indices[6] = { 0, 1, 2, 0, 2, 3 };
const u32 indices[6] = { 0, 1, 2, 2, 3, 0 };
BufferLayout mBufferLayout;
VertexBuffer* mVertexBuffer;
@ -90,10 +105,15 @@ namespace lunarium
Shader* mQuadShader;
} mQuadData;
FrameStats mFrameStats;
OrthographicCamera* mpCamera;
std::vector<Texture*> mLoadedTextures;
Texture* mpDebugTexture;
Color mClearColor;
/// DEBUG STUFF
u32 mTVAO;
u32 mTVBO;
};
}

@ -132,9 +132,13 @@ namespace lunarium
OpRes Shader::SetUniform(Uniform& uniform, void* values)
{
if (uniform.Location < 1)
if (uniform.Location == -1)
{
GetUniformLocation(uniform);
if (uniform.Location == -1)
{
return OpRes::Fail("Could not find location for uniform: %s", uniform.Name.c_str());
}
}
switch (uniform.Type)

@ -36,7 +36,7 @@ namespace lunarium
{
std::string Name;
UniformType Type;
u32 Location;
i32 Location;
};
class Shader

@ -58,8 +58,9 @@ namespace lunarium
layout (binding = 0) uniform sampler2D u_Textures[32];\
void main()\
{\
color = texture(u_Textures[TexSlot], Input.TexCoord) * Input.Color;\
color = Input.Color;\
}";
//color = texture(u_Textures[TexSlot], Input.TexCoord) * Input.Color;\
const char* DefaultSpriteVertex = "#version 450 core\n\
layout(location = 0) in vec4 vertex;\

@ -0,0 +1,11 @@
#version 450 core
//in vec2 TexCoords;
out vec4 color;
//uniform sampler2D image;
uniform vec3 spriteColor;
void main()
{
color = vec4(spriteColor, 1.0);// * texture(image, TexCoords);
}

@ -0,0 +1,10 @@
#version 450 core
layout (location = 0) in vec2 vertex; // <vec2 position>
//out vec2 TexCoords;
void main()
{
gl_Position = vec4(vertex, 0.0, 1.0);
}

@ -8,6 +8,7 @@
#include "texture.h"
#include <glad/gl.h>
#include <utils/logger.h>
namespace lunarium
{
@ -19,6 +20,12 @@ namespace lunarium
Texture* Texture::Create(u8* data, u32 width, u32 height, TextureFormat format)
{
if (((i32)width) < 1 || ((i32)height) < 1)
{
Logger::Error(LogCategory::GRAPHICS, "Failed to create texture, width and/or height values invalid: %d, %d", width, height);
return nullptr;
}
u32 formats[2] = { GL_RGB, GL_RGBA };
Texture* t = new Texture();
@ -26,10 +33,16 @@ namespace lunarium
t->mWidth = width;
t->mHeight = height;
glGenTextures(1, &t->mGLID);
glCreateTextures(GL_TEXTURE_2D, 1, &t->mGLID);
t->Bind();
glTexImage2D(GL_TEXTURE_2D, 0, formats[(int)format], width, height, 0, formats[(int)format], GL_UNSIGNED_BYTE, data);
int error = glGetError();
if (error > 0)
{
Logger::Error(LogCategory::GRAPHICS, "Error setting texture data");
}
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
@ -52,6 +65,7 @@ namespace lunarium
void Texture::Bind(u32 slot) const
{
glBindTextureUnit(slot, mGLID);
}
void Texture::Unbind() const

@ -69,8 +69,8 @@ namespace lunarium
for (int i = 0; i < mLayout.size(); i++)
{
glEnableVertexAttribArray(i);
glVertexAttribPointer(i, mLayout[i].NumValues, gl_types[mLayout[i].Type], GL_FALSE, stride, (GLvoid*)offsets[i]);
glEnableVertexAttribArray(i);
}
}

@ -25,7 +25,6 @@ namespace lunarium
{
}
void BaseScene::OnKeyPress(InputManager::KeyPress kp)
{

@ -119,7 +119,7 @@ namespace lunarium
void PhysicsScene::RenderScene()
{
Renderer2D g = Core::Graphics();
Renderer2D& g = Core::Graphics();
// This scaling is a little backwards.
// Info about scaling factor:

@ -11,7 +11,6 @@
#include <gui/gui.h>
#include <utils/helpers.h>
#include <utils/logger.h>
#include <renderer/renderer2D.h>
#include <renderer/texture.h>
#include <renderer/frame_buffer.h>
#include <renderer/orthographic_camera.h>
@ -47,18 +46,18 @@ namespace lunarium
mFrameBufferTwo = FrameBuffer::Create(1024, 1024); //Core::Graphics().CreateRenderTexture(1024, 1024, 4);
// Load test image
int w, h, n;
stbi_set_flip_vertically_on_load(1);
unsigned char* buffer = stbi_load("LinkToThePast1_sized.png", &w, &h, &n, 0);
// int w, h, n;
// stbi_set_flip_vertically_on_load(1);
// unsigned char* buffer = stbi_load("LinkToThePast1_sized.png", &w, &h, &n, 0);
TextureFormat format = TextureFormat::RGBA;
if (n == 3)
{
format = TextureFormat::RGB;
}
mpTestImageLoad = Texture::Create(buffer, w, h, format);
mSrcWidth = w;
mSrcHeight = h;
// TextureFormat format = TextureFormat::RGBA;
// if (n == 3)
// {
// format = TextureFormat::RGB;
// }
// mpTestImageLoad = Texture::Create(buffer, w, h, format);
// mSrcWidth = w;
// mSrcHeight = h;
angle = 0.0f;
box_angle = 0.0f;
@ -176,35 +175,31 @@ namespace lunarium
mpRenderedImage = mFrameBufferOne->GetTexture();
box_angle += 0.01f;
int draws = Core::Graphics().GetFrameStats().DrawCalls;
Core::Graphics().ResetFrameStats();
int w, h;
Core::MainWindow().GetFramebufferSize(&w, &h);
OrthographicCamera main_cam(Vec2f { 0.0f, 0.0f }, Sizef { (float)w, (float)h });
Core::Graphics().BeginDraw(&main_cam);
RenderScene();
RenderScene(Core::Graphics());
Core::GUI().NewFrame();
ImGui::Begin("TEST INFO");
ImGui::Text("Draw calls: %d", draws);
ImGui::End();
DrawStatsGUI();
Core::GUI().EndFrame();
Core::Graphics().EndDraw();
Core::MainWindow().SwapBuffers();
mRenderStats = Core::Graphics().GetFrameStats();
Core::Graphics().ResetFrameStats();
}
void SimpleRenderScene::RenderScene()
void SimpleRenderScene::RenderScene(Renderer2D& g)
{
Renderer2D g = Core::Graphics();
// g.DrawImage(*mpRenderedImage, Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mpRenderedImage->GetWidth(), (float)mpRenderedImage->GetHeight()),
// Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mImageSize.Width, (float)mImageSize.Height), Color(1.0f, 1.0f, 1.0f, 1.0f), angle);
g.DrawQuad(Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mImageSize.Width, (float)mImageSize.Height), Color(0.0f, 0.0f, 0.0f, 1.0f), nullptr, angle);
g.DrawQuad(Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mImageSize.Width, (float)mImageSize.Height), Color(01.0f, 0.0f, 0.0f, 1.0f), nullptr, angle);
g.DrawQuad(Rectangle(400, 400, 128.0f, 128.0f), Color(0.0f, 1.0f, 0.0f, 1.0f), nullptr, box_angle);
g.DrawQuad(Rectangle(200, 200, 128.0f, 128.0f), Color(0.0f, 1.0f, 0.0f, 1.0f));//, nullptr, box_angle);
//g->DrawImage(*mpTestImageLoad, glm::vec2(0.0f, 0.0f), Color::White());
//Rectangle src = Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mpTestImageLoad->GetWidth(), (float)mpTestImageLoad->GetHeight());
@ -216,4 +211,23 @@ namespace lunarium
// g->DrawImage(*mpTestImageLoad, src,
// dest, Color(1.0f, 1.0f, 1.0f, 0.8f));
}
void SimpleRenderScene::DrawStatsGUI()
{
ImGui::SetNextWindowSize(ImVec2(400, 300));
ImGui::Begin("RENDER INFO");
ImGui::BeginChild("Per Frame", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 3.5f), true);
ImGui::Text("Per Frame");
ImGui::Separator();
ImGui::Text("Number of triangles: %d", mRenderStats.NumTris);
ImGui::Text("Draw calls: %d", mRenderStats.DrawCalls);
ImGui::EndChild();
ImGui::BeginChild("General", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 2.5f), true);
ImGui::Text("General Info");
ImGui::Separator();
ImGui::Text("General render info...");
ImGui::EndChild();
ImGui::End();
}
}

@ -10,6 +10,7 @@
#define SIMPLE_RENDER_SCENE_H_
#include "base_scene.h"
#include <renderer/renderer2D.h>
#include <core/types.h>
#include <string>
@ -24,7 +25,7 @@ namespace lunarium
~SimpleRenderScene();
virtual void OnLoad();
virtual void OnTick(double delta);
void RenderScene();
void RenderScene(Renderer2D& g);
private:
@ -40,11 +41,16 @@ namespace lunarium
float mSrcWidth;
float mSrcHeight;
Renderer2D::FrameStats mRenderStats;
struct GridTestObj
{
int X;
std::string msg;
};
private:
void DrawStatsGUI();
};
}

@ -11,6 +11,7 @@
#include <nfd.hpp>
#include <algorithm>
#include <fstream>
#ifdef WIN32
#include <windows.h>
@ -308,4 +309,31 @@ namespace lunarium
filename.erase(filename.begin());
return filename;
}
/////////////////////////////////////////////////////////////////////
// FILE
/////////////////////////////////////////////////////////////////////
std::string File::ReadTextFile(std::string filename)
{
std::ifstream ifs(filename);
if (!ifs.is_open())
{
Logger::Error(LogCategory::UTILITIES, "Could not open file: %s", filename.c_str());
return "";
}
std::string contents;
while (ifs)
{
contents += ifs.get();
}
contents.erase(contents.end() - 1);
ifs.close();
ifs.clear();
return contents;
}
}

@ -91,6 +91,12 @@ namespace lunarium
static std::string TrimFileNameFromPath(std::string path);
static std::string GetFileExtension(std::string filename);
};
class File
{
public:
static std::string ReadTextFile(std::string filename);
};
}
#endif // HELPERS_H_
Loading…
Cancel
Save