Re-write of renderer is more or less stable and hooked back up to the core engine.
Quads do not render but gui windows do.master
parent
ec492b119f
commit
593a16c9cc
@ -0,0 +1,91 @@
|
||||
/******************************************************************************
|
||||
* File - index_buffer.h
|
||||
* Author - Joey Pollack
|
||||
* Date - 2022/08/05 (y/m/d)
|
||||
* Mod Date - 2022/08/05 (y/m/d)
|
||||
* Description - OpenGL index buffer object
|
||||
******************************************************************************/
|
||||
|
||||
#include "index_buffer.h"
|
||||
#include <utils/logger.h>
|
||||
|
||||
#include <glad/gl.h>
|
||||
|
||||
namespace lunarium
|
||||
{
|
||||
IndexBuffer::IndexBuffer(u32 size, u32* indices)
|
||||
: mIsStatic(true), mGLID(0), mIndex(0), mSize(size)
|
||||
{
|
||||
mIsStatic = indices != nullptr;
|
||||
glGenBuffers(1, &mGLID);
|
||||
Bind();
|
||||
|
||||
if (indices)
|
||||
{
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, indices, GL_STATIC_DRAW);
|
||||
}
|
||||
else
|
||||
{
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, nullptr, GL_DYNAMIC_DRAW);
|
||||
}
|
||||
}
|
||||
|
||||
// IndexBuffer::IndexBuffer(u32 size)
|
||||
// : mIsStatic(false), mGLID(0), mIndex(0), mSize(size
|
||||
// {
|
||||
// glGenBuffers(1, &mGLID);
|
||||
// Bind();
|
||||
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
|
||||
|
||||
// }
|
||||
|
||||
|
||||
bool IndexBuffer::IsStatic() const
|
||||
{
|
||||
return mIsStatic;
|
||||
}
|
||||
|
||||
void IndexBuffer::Clear()
|
||||
{
|
||||
if (mIsStatic)
|
||||
{
|
||||
Logger::Warn(LogCategory::GRAPHICS, "Attemping to clear a static VertexBuffer");
|
||||
}
|
||||
|
||||
mIndex = 0;
|
||||
}
|
||||
|
||||
void IndexBuffer::Bind()
|
||||
{
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mGLID);
|
||||
}
|
||||
|
||||
|
||||
bool IndexBuffer::PushIndices(const u32* indices, u32 size)
|
||||
{
|
||||
if (mIsStatic)
|
||||
{
|
||||
Logger::Warn(LogCategory::GRAPHICS, "Cannot push indices into a static buffer!");
|
||||
return false;
|
||||
}
|
||||
|
||||
// NOTE: BE CAREFUL! mSize is the size in bytes, not number of vertices.
|
||||
if (mIndex + size >= mSize)
|
||||
{
|
||||
// Vertices do not fit into the buffer
|
||||
Logger::Warn(LogCategory::GRAPHICS, "Cannot push indices into buffer - not enough space left! Space left: %n, data size: %n", mSize - mIndex, size);
|
||||
return false;
|
||||
}
|
||||
|
||||
Bind();
|
||||
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, mIndex, size, indices);
|
||||
|
||||
mIndex += size;
|
||||
return true;
|
||||
}
|
||||
|
||||
u32 IndexBuffer::GetGLID() const
|
||||
{
|
||||
return mGLID;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
/******************************************************************************
|
||||
* File - index_buffer.h
|
||||
* Author - Joey Pollack
|
||||
* Date - 2022/08/05 (y/m/d)
|
||||
* Mod Date - 2022/08/05 (y/m/d)
|
||||
* Description - OpenGL index buffer object
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef LUNARIUM_INDEX_BUFFER_H_
|
||||
#define LUNARIUM_INDEX_BUFFER_H_
|
||||
|
||||
#include <core/common_defs.h>
|
||||
#include <utils/op_res.h>
|
||||
|
||||
namespace lunarium
|
||||
{
|
||||
class IndexBuffer
|
||||
{
|
||||
public:
|
||||
|
||||
IndexBuffer(u32 size, u32* indices = nullptr);
|
||||
//IndexBuffer(u32 size);
|
||||
|
||||
bool IsStatic() const;
|
||||
void Clear();
|
||||
void Bind();
|
||||
|
||||
/// Returns false if the data does not fit in the buffer
|
||||
/// size is the size in bytes
|
||||
bool PushIndices(const u32* indices, u32 size);
|
||||
u32 GetGLID() const;
|
||||
|
||||
private:
|
||||
u32 mGLID;
|
||||
bool mIsStatic;
|
||||
u32 mSize;
|
||||
u32 mIndex;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif // LUNARIUM_INDEX_BUFFER_H_
|
||||
@ -0,0 +1,18 @@
|
||||
/******************************************************************************
|
||||
* File - render_common.h
|
||||
* Author - Joey Pollack
|
||||
* Date - 2022/08/08 (y/m/d)
|
||||
* Mod Date - 2022/08/08 (y/m/d)
|
||||
* Description - Common include files for the render system
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef LUNARIUM_RENDER_COMMON_H_
|
||||
#define LUNARIUM_RENDER_COMMON_H_
|
||||
|
||||
#include "frame_buffer.h"
|
||||
#include "index_buffer.h"
|
||||
#include "shader.h"
|
||||
#include "texture.h"
|
||||
#include "vertex_buffer.h"
|
||||
|
||||
#endif // LUNARIUM_RENDER_COMMON_H_
|
||||
@ -0,0 +1,261 @@
|
||||
/******************************************************************************
|
||||
* File - renderer2D.h
|
||||
* Author - Joey Pollack
|
||||
* Date - 2022/07/13 (y/m/d)
|
||||
* Mod Date - 2022/07/13 (y/m/d)
|
||||
* Description - The main 2D render class
|
||||
******************************************************************************/
|
||||
|
||||
#include "renderer2D.h"
|
||||
#include "orthographic_camera.h"
|
||||
#include "shaders/defaultShaders.h"
|
||||
|
||||
#include <utils/logger.h>
|
||||
#include <glad/gl.h>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
namespace lunarium
|
||||
{
|
||||
OpRes Renderer2D::Initialize()
|
||||
{
|
||||
mpCamera = nullptr;
|
||||
|
||||
// INIT QUAD DATA
|
||||
mQuadData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 3 }); // Position
|
||||
mQuadData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 2 }); // Tex_coords
|
||||
mQuadData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 4 }); // Color
|
||||
mQuadData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::INT32, 1 }); // Texture Sampler Index
|
||||
mQuadData.mVertexBuffer = VertexBuffer::Create(mQuadData.mBufferLayout, mQuadData.MaxVertices);
|
||||
|
||||
mQuadData.mIndexBuffer = new IndexBuffer(mQuadData.MaxIndices * sizeof(u32));
|
||||
|
||||
|
||||
mQuadData.mQuadShader = new Shader(OGLDefaultShaders.DefaultShapeVertex, nullptr, OGLDefaultShaders.DefaultShapeFragment);
|
||||
if (!mQuadData.mQuadShader->IsValid())
|
||||
{
|
||||
return OpRes::Fail("Failed to create the quad shader");
|
||||
}
|
||||
|
||||
// TODO: INIT SPRITE DATA
|
||||
|
||||
|
||||
// TODO: INIT DEBUG TEXTURE
|
||||
u8 data[4] = {255, 128, 0, 0};
|
||||
mpDebugTexture = Texture::Create(data, 1, 1);
|
||||
|
||||
return OpRes::OK();
|
||||
}
|
||||
|
||||
void Renderer2D::Shutdown()
|
||||
{
|
||||
VertexBuffer::Destroy(&mQuadData.mVertexBuffer);
|
||||
delete[] mQuadData.mIndexBuffer;
|
||||
}
|
||||
|
||||
void Renderer2D::SetClearColor(Color color)
|
||||
{
|
||||
mClearColor = color;
|
||||
glClearColor(color.R, color.G, color.B, color.A);
|
||||
}
|
||||
|
||||
Color Renderer2D::GetClearColor()
|
||||
{
|
||||
return mClearColor;
|
||||
}
|
||||
|
||||
void Renderer2D::ResetFrameStats()
|
||||
{
|
||||
mFrameStats.DrawCalls = 0;
|
||||
}
|
||||
|
||||
Renderer2D::FrameStats Renderer2D::GetFrameStats() const
|
||||
{
|
||||
return mFrameStats;
|
||||
}
|
||||
|
||||
void Renderer2D::BeginDraw(OrthographicCamera* pCamera)
|
||||
{
|
||||
mpCamera = pCamera;
|
||||
glViewport(0, 0, pCamera->GetViewport().Width, pCamera->GetViewport().Height);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
}
|
||||
|
||||
void Renderer2D::EndDraw()
|
||||
{
|
||||
Flush();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
// DRAW METHODS
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
void Renderer2D::DrawQuad(Rectangle quad, Color color, Texture* texture, float angle)
|
||||
{
|
||||
int texture_slot = -1;
|
||||
if (texture)
|
||||
{
|
||||
for (int i = 0; i < mLoadedTextures.size(); i++)
|
||||
{
|
||||
if (texture == mLoadedTextures[i])
|
||||
{
|
||||
texture_slot = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (-1 == texture_slot)
|
||||
{
|
||||
mLoadedTextures.push_back(texture);
|
||||
texture_slot = mLoadedTextures.size() - 1;
|
||||
}
|
||||
}
|
||||
|
||||
float vertices[40];
|
||||
unsigned char* vertices_wl = (unsigned char*)vertices; // vertices write location pointer
|
||||
|
||||
glm::mat4 model(1.0f);
|
||||
model = glm::translate(model, glm::vec3(quad.CenterPoint().x, quad.CenterPoint().y, 0.0f));
|
||||
model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, 1.0f));
|
||||
|
||||
// FIRST
|
||||
QuadData::Vertex v1;
|
||||
int vert_size = sizeof(QuadData::Vertex);
|
||||
v1.pos = mQuadData.vert_pos[0] * model;
|
||||
v1.tex_coord = mQuadData.vert_tex[0];
|
||||
v1.color = color;
|
||||
v1.tex_slot = texture_slot;
|
||||
memcpy(vertices_wl, &v1, vert_size);
|
||||
vertices_wl += vert_size;
|
||||
|
||||
// SECOND
|
||||
QuadData::Vertex v2;
|
||||
v2.pos = mQuadData.vert_pos[1] * model;
|
||||
v2.tex_coord = mQuadData.vert_tex[1];
|
||||
v2.color = color;
|
||||
v2.tex_slot = texture_slot;
|
||||
memcpy(vertices_wl, &v2, vert_size);
|
||||
vertices_wl += vert_size;
|
||||
|
||||
// THIRD
|
||||
QuadData::Vertex v3;
|
||||
v3.pos = mQuadData.vert_pos[2] * model;
|
||||
v3.tex_coord = mQuadData.vert_tex[2];
|
||||
v3.color = color;
|
||||
v3.tex_slot = texture_slot;
|
||||
memcpy(vertices_wl, &v3, vert_size);
|
||||
vertices_wl += vert_size;
|
||||
|
||||
// FOURTH
|
||||
QuadData::Vertex v4;
|
||||
v4.pos = mQuadData.vert_pos[3] * model;
|
||||
v4.tex_coord = mQuadData.vert_tex[3];
|
||||
v4.color = color;
|
||||
v4.tex_slot = texture_slot;
|
||||
memcpy(vertices_wl, &v4, vert_size);
|
||||
|
||||
if (!mQuadData.mVertexBuffer->PushVertices(vertices, 4))
|
||||
{
|
||||
Logger::Info(LogCategory::GRAPHICS, "Quad VertexBuffer is full, flushing and retrying");
|
||||
Flush();
|
||||
DrawQuad(quad, color, texture);
|
||||
}
|
||||
|
||||
// INDICES
|
||||
if (!mQuadData.mIndexBuffer->PushIndices(mQuadData.indices, 6 * sizeof(u32)))
|
||||
{
|
||||
Logger::Error(LogCategory::GRAPHICS, "Quad IndexBuffer is full - This shouldn't happen because the VertexBuffer should fill up first!");
|
||||
return;
|
||||
}
|
||||
|
||||
mQuadData.mNumQuads++;
|
||||
}
|
||||
|
||||
void Renderer2D::DrawSprite(Texture& image, glm::vec2 position, Color color, float angle)
|
||||
{
|
||||
Logger::Warn(LogCategory::GRAPHICS, "Renderer2D::DrawSprite is not yet implemented");
|
||||
// float half_width = image.GetWidth() / 2;
|
||||
// float half_height = image.GetHeight() / 2;
|
||||
// DrawImage(image, Rectangle::MakeFromTopLeft(0, 0, image.GetWidth(), image.GetHeight()),
|
||||
// Rectangle(position.x + half_width, position.y + half_height, half_width, half_height), color, angle);
|
||||
}
|
||||
|
||||
void Renderer2D::DrawSprite(Texture& image, Rectangle source, Rectangle destination, Color color, float angle)
|
||||
{
|
||||
Logger::Warn(LogCategory::GRAPHICS, "Renderer2D::DrawSprite is not yet implemented");
|
||||
// glm::mat4 id = glm::mat4(1.0f);
|
||||
// glm::vec3 pos = glm::vec3(destination.X, destination.Y, 0.0f);
|
||||
// glm::mat4 trans = glm::translate(id, pos);
|
||||
// trans = glm::rotate(trans, angle, glm::vec3(0.0f, 0.0f, 1.0f));
|
||||
// trans = glm::scale(trans, glm::vec3(destination.HalfWidth * 2, destination.HalfHeight * 2, 1.0f));
|
||||
|
||||
|
||||
// mImageShader.MakeActive();
|
||||
|
||||
// float xScale = (source.HalfWidth * 2) / image.GetWidth();
|
||||
// float xOffset = source.left() / image.GetWidth();
|
||||
// float yScale = (source.HalfHeight * 2) / image.GetHeight();
|
||||
// float yOffset = source.top() / image.GetHeight();
|
||||
|
||||
|
||||
// // * -1.0f on yScale will flip the image vertically
|
||||
// mImageShader.SetUniformf("uvManip", { xScale, xOffset, yScale * -1.0f, yOffset});
|
||||
// mImageShader.SetUniformMatrix("model", 1, glm::value_ptr(trans));
|
||||
// mImageShader.SetUniformMatrix("projection", 1, glm::value_ptr(mProjection));
|
||||
// mImageShader.SetUniformf("spriteColor", { color.R, color.G, color.B, color.A });
|
||||
|
||||
|
||||
// glActiveTexture(GL_TEXTURE0);
|
||||
// glBindTexture(GL_TEXTURE_2D, image.GetGLTextureID());
|
||||
|
||||
// glBindVertexArray(mImageVAO);
|
||||
// glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
// glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void Renderer2D::DrawString(const char* string, Rectangle boundingArea, Color color, float scale, int font)
|
||||
{
|
||||
Logger::Warn(LogCategory::GRAPHICS, "Renderer2D::DrawString is not yet implemented");
|
||||
// mText.DrawString(font, string, glm::vec2(boundingArea.left(), boundingArea.top()),
|
||||
// glm::vec2(boundingArea.right(), boundingArea.bottom()), color, scale, mProjection);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
// HELPERS
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
void Renderer2D::Flush()
|
||||
{
|
||||
// TODO: Draw calls
|
||||
mQuadData.mQuadShader->Use();
|
||||
mQuadData.mVertexBuffer->Bind();
|
||||
mQuadData.mIndexBuffer->Bind();
|
||||
for (int i = 0; i < mLoadedTextures.size(); i++)
|
||||
{
|
||||
mLoadedTextures[i]->Bind(i);
|
||||
}
|
||||
|
||||
Uniform umodel;
|
||||
umodel.Type = UniformType::FMAT4;
|
||||
umodel.Location = 0;
|
||||
umodel.Name = "Model";
|
||||
|
||||
Uniform uprojview;
|
||||
uprojview.Type = UniformType::FMAT4;
|
||||
uprojview.Location = 0;
|
||||
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()));
|
||||
|
||||
glDrawElements(GL_TRIANGLES, mQuadData.mNumQuads * 6, GL_UNSIGNED_INT, nullptr);
|
||||
mFrameStats.DrawCalls++;
|
||||
|
||||
// Reset drawing data
|
||||
mLoadedTextures.clear();
|
||||
mQuadData.mVertexBuffer->Clear();
|
||||
mQuadData.mIndexBuffer->Clear();
|
||||
|
||||
// TODO: Add the debug texture back to the map
|
||||
mLoadedTextures.push_back(mpDebugTexture);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,211 @@
|
||||
/******************************************************************************
|
||||
* File - shader.cpp
|
||||
* Author - Joey Pollack
|
||||
* Date - 2022/08/04 (y/m/d)
|
||||
* Mod Date - 2022/08/04 (y/m/d)
|
||||
* Description - opengl shader system
|
||||
******************************************************************************/
|
||||
|
||||
#include "shader.h"
|
||||
#include <utils/logger.h>
|
||||
|
||||
#include <glad/gl.h>
|
||||
#include <string>
|
||||
|
||||
namespace lunarium
|
||||
{
|
||||
Shader::Shader(const char* vert_source, const char* geo_source, const char* frag_source)
|
||||
: mGLID(0)
|
||||
{
|
||||
// Validate parameters
|
||||
if (!vert_source)
|
||||
{
|
||||
Logger::Error(LogCategory::GRAPHICS, "Failed to create Shader - vert_source can not be nullptr!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!frag_source)
|
||||
{
|
||||
Logger::Error(LogCategory::GRAPHICS, "Failed to create Shader - vert_source can not be nullptr!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Compile source code
|
||||
u32 vert_id = CompileSource(vert_source, GL_VERTEX_SHADER);
|
||||
u32 geo_id = (geo_source ? CompileSource(geo_source, GL_GEOMETRY_SHADER) : 0 );
|
||||
u32 frag_id = CompileSource(frag_source, GL_FRAGMENT_SHADER);
|
||||
|
||||
// Validate compilation
|
||||
if (vert_id < 1 || frag_id < 1)
|
||||
{
|
||||
// Free any succesfully compiled shader sources
|
||||
if (vert_id > 0) glDeleteShader(vert_id);
|
||||
if (geo_id > 0) glDeleteShader(geo_id);
|
||||
if (frag_id > 0) glDeleteShader(frag_id);
|
||||
|
||||
Logger::Error(LogCategory::GRAPHICS, "Failed to create shader program");
|
||||
return;
|
||||
}
|
||||
|
||||
// Link shader program
|
||||
mGLID = glCreateProgram();
|
||||
glAttachShader(mGLID, vert_id);
|
||||
if (geo_id > 0) glAttachShader(mGLID, geo_id);
|
||||
glAttachShader(mGLID, frag_id);
|
||||
glLinkProgram(mGLID);
|
||||
|
||||
// check for linking errors
|
||||
int success;
|
||||
const int buffer_size = 512;
|
||||
char info_log[buffer_size];
|
||||
glGetProgramiv(mGLID, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(mGLID, 512, NULL, info_log);
|
||||
Logger::Error(LogCategory::GRAPHICS, "Failed to link shader program: %s", info_log);
|
||||
glDeleteProgram(mGLID);
|
||||
mGLID = 0;
|
||||
}
|
||||
|
||||
// Clean up
|
||||
if (vert_id > 0) glDeleteShader(vert_id);
|
||||
if (geo_id > 0) glDeleteShader(geo_id);
|
||||
if (frag_id > 0) glDeleteShader(frag_id);
|
||||
}
|
||||
|
||||
Shader::~Shader()
|
||||
{
|
||||
if (IsValid())
|
||||
{
|
||||
glDeleteProgram(mGLID);
|
||||
mGLID = 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool Shader::IsValid() const
|
||||
{
|
||||
return mGLID > 0;
|
||||
}
|
||||
|
||||
|
||||
void Shader::Use() const
|
||||
{
|
||||
glUseProgram(mGLID);
|
||||
}
|
||||
|
||||
OpRes Shader::GetAllUniforms(std::vector<Uniform>& uniforms)
|
||||
{
|
||||
uniforms.clear();
|
||||
|
||||
GLint size; // size of the variable
|
||||
GLenum type; // type of the variable (float, vec3 or mat4, etc)
|
||||
|
||||
const GLsizei buffer_size = 16; // maximum name length
|
||||
GLchar name[buffer_size]; // variable name in GLSL
|
||||
GLsizei length; // name length
|
||||
int count = -1;
|
||||
|
||||
glGetProgramiv(mGLID, GL_ACTIVE_UNIFORMS, &count);
|
||||
Logger::Debug(LogCategory::GRAPHICS, "Active Uniforms for shader %n: %n", mGLID, count);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
glGetActiveUniform(mGLID, (GLuint)i, buffer_size, &length, &size, &type, name);
|
||||
u32 location = glGetUniformLocation(mGLID, name);
|
||||
Logger::Debug(LogCategory::GRAPHICS, "Uniform #%n Type: %u Name: %s Location: %n", i, type, name, location);
|
||||
Uniform u;
|
||||
u.Location = location;
|
||||
u.Type = GLToUniformType(type);
|
||||
u.Name = std::string(name);
|
||||
uniforms.push_back(u);
|
||||
}
|
||||
|
||||
return OpRes::OK();
|
||||
}
|
||||
|
||||
|
||||
void Shader::GetUniformLocation(Uniform& uniform)
|
||||
{
|
||||
uniform.Location = glGetUniformLocation(mGLID, uniform.Name.c_str());
|
||||
}
|
||||
|
||||
|
||||
OpRes Shader::SetUniform(Uniform& uniform, void* values)
|
||||
{
|
||||
if (uniform.Location < 1)
|
||||
{
|
||||
GetUniformLocation(uniform);
|
||||
}
|
||||
|
||||
switch (uniform.Type)
|
||||
{
|
||||
case UniformType::F1: glUniform1fv(uniform.Location, 1, (GLfloat*)values); break;
|
||||
case UniformType::F2: glUniform2fv(uniform.Location, 1, (GLfloat*)values); break;
|
||||
case UniformType::F3: glUniform3fv(uniform.Location, 1, (GLfloat*)values); break;
|
||||
case UniformType::F4: glUniform4fv(uniform.Location, 1, (GLfloat*)values); break;
|
||||
|
||||
case UniformType::I1: glUniform1iv(uniform.Location, 1, (GLint*)values); break;
|
||||
case UniformType::I2: glUniform2iv(uniform.Location, 1, (GLint*)values); break;
|
||||
case UniformType::I3: glUniform3iv(uniform.Location, 1, (GLint*)values); break;
|
||||
case UniformType::I4: glUniform4iv(uniform.Location, 1, (GLint*)values); break;
|
||||
|
||||
case UniformType::FMAT3: glUniformMatrix3fv(uniform.Location, 1, GL_FALSE, (GLfloat*)values); break;
|
||||
case UniformType::FMAT4: glUniformMatrix4fv(uniform.Location, 1, GL_FALSE, (GLfloat*)values); break;
|
||||
|
||||
default: return OpRes::Fail("Can not set uniform value - Unknown type");
|
||||
}
|
||||
|
||||
return OpRes::OK();
|
||||
}
|
||||
|
||||
|
||||
u32 Shader::CompileSource(const char* source, u32 source_type)
|
||||
{
|
||||
u32 id = glCreateShader(source_type);
|
||||
glShaderSource(id, 1, &source, NULL);
|
||||
glCompileShader(id);
|
||||
|
||||
int success;
|
||||
const int buffer_size = 512;
|
||||
char info_log[buffer_size];
|
||||
glGetShaderiv(id, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(id, buffer_size, NULL, info_log);
|
||||
std::string type = "";
|
||||
switch (source_type)
|
||||
{
|
||||
case GL_VERTEX_SHADER: type = "Vertex"; break;
|
||||
case GL_GEOMETRY_SHADER: type = "Geometry"; break;
|
||||
case GL_FRAGMENT_SHADER: type = "Fragment"; break;
|
||||
}
|
||||
Logger::Error(LogCategory::GRAPHICS, "Failed to compile %s shader source: %s", type.c_str(), info_log);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
UniformType Shader::GLToUniformType(u32 type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case GL_FLOAT: return UniformType::F1;
|
||||
case GL_FLOAT_VEC2: return UniformType::F2;
|
||||
case GL_FLOAT_VEC3: return UniformType::F3;
|
||||
case GL_FLOAT_VEC4: return UniformType::F4;
|
||||
|
||||
case GL_INT: return UniformType::I1;
|
||||
case GL_INT_VEC2: return UniformType::I2;
|
||||
case GL_INT_VEC3: return UniformType::I3;
|
||||
case GL_INT_VEC4: return UniformType::I4;
|
||||
|
||||
case GL_FLOAT_MAT3: return UniformType::FMAT3;
|
||||
case GL_FLOAT_MAT4: return UniformType::FMAT4;
|
||||
}
|
||||
|
||||
Logger::Warn(LogCategory::GRAPHICS, "Can not convert GL uniform type - Unknown type: %u", type);
|
||||
return UniformType::UNKNOWN;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
/******************************************************************************
|
||||
* File - shader.h
|
||||
* Author - Joey Pollack
|
||||
* Date - 2022/08/04 (y/m/d)
|
||||
* Mod Date - 2022/08/04 (y/m/d)
|
||||
* Description - opengl shader system
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef LUNARIUM_SHADER_H_
|
||||
#define LUNARIUM_SHADER_H_
|
||||
|
||||
#include <core/common_defs.h>
|
||||
#include <utils/op_res.h>
|
||||
#include <vector>
|
||||
|
||||
namespace lunarium
|
||||
{
|
||||
enum class UniformType
|
||||
{
|
||||
F1,
|
||||
F2,
|
||||
F3,
|
||||
F4,
|
||||
FMAT3,
|
||||
FMAT4,
|
||||
|
||||
I1,
|
||||
I2,
|
||||
I3,
|
||||
I4,
|
||||
|
||||
UNKNOWN,
|
||||
};
|
||||
|
||||
struct Uniform
|
||||
{
|
||||
std::string Name;
|
||||
UniformType Type;
|
||||
u32 Location;
|
||||
};
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
Shader(const char* vert_source, const char* geo_source, const char* frag_source);
|
||||
~Shader();
|
||||
bool IsValid() const;
|
||||
|
||||
void Use() const;
|
||||
|
||||
OpRes GetAllUniforms(std::vector<Uniform>& uniforms);
|
||||
void GetUniformLocation(Uniform& uniform);
|
||||
OpRes SetUniform(Uniform& uniform, void* values);
|
||||
|
||||
private:
|
||||
u32 mGLID;
|
||||
|
||||
private: // HELPERS
|
||||
|
||||
/// Compile the given shader source
|
||||
/// Returns the glid of the compiled source
|
||||
/// source_type is the GLenum value
|
||||
u32 CompileSource(const char* source, u32 source_type);
|
||||
UniformType GLToUniformType(u32 type);
|
||||
};
|
||||
}
|
||||
|
||||
#endif // LUNARIUM_SHADER_H_
|
||||
Loading…
Reference in New Issue