Skip to content

Commit

Permalink
Working Spinning Cube Sample (#52)
Browse files Browse the repository at this point in the history
* Working Spinning Cube Sample (#2)

* WIP Spinning Cube Sample

* Working Spinning Cube Sample

* refactored & commented

* bug fix

* Vertex Shader matrices calculation fix
  • Loading branch information
abhayMore authored Aug 28, 2024
1 parent 415aaa9 commit 7b895f9
Show file tree
Hide file tree
Showing 19 changed files with 194 additions and 82 deletions.
9 changes: 8 additions & 1 deletion Core/Application/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace CGL::Core
CGL_LOG(CoreApp, Trace, "Running application");

auto startTime = std::chrono::steady_clock::now();
auto lastFrameTime = startTime;
OnInit();

SDL_Event e;
Expand All @@ -65,7 +66,11 @@ namespace CGL::Core
}
}

OnUpdate(e);
// Calculate deltatime
auto currentFrameTime = std::chrono::steady_clock::now();
f32 deltaTime = std::chrono::duration<f32>(currentFrameTime - lastFrameTime).count();

OnUpdate(e, deltaTime);

// Call begin and end frame before calling render itself
m_renderer->BeginFrame();
Expand All @@ -83,6 +88,8 @@ namespace CGL::Core
m_isRunning = false;
}
}

lastFrameTime = currentFrameTime;
}

OnShutdown();
Expand Down
2 changes: 1 addition & 1 deletion Core/Application/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace CGL::Core

protected:
virtual bool OnInit();
virtual void OnUpdate(const SDL_Event& e) = 0;
virtual void OnUpdate(const SDL_Event& e, f32 deltaTime) = 0;
virtual void OnRender() = 0;
virtual void OnShutdown();
virtual void OnResize(u32 width, u32 height);
Expand Down
2 changes: 2 additions & 0 deletions Core/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <vector>
#include <array>
#include <utility>
#include <typeindex>
#include <cstddef>

#include <Core/Types.h>
#endif
Expand Down
1 change: 1 addition & 0 deletions Core/Graphics/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace CGL::Graphics
u32 TypeSize = 0;
u32 Count = 0;
void* Data = nullptr;
std::type_index VertexType = typeid(void);
};

#if defined(CGL_RHI_DX11)
Expand Down
3 changes: 2 additions & 1 deletion Core/Graphics/RHI/OpenGL/OPENGLIndexBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ namespace CGL::Graphics
{
struct OPENGLIndexBuffer
{
u32 Stride;
u32 IndicesCount;
u32 Size;
GLuint EBO;
};
} // namespace CGL::Graphics
143 changes: 84 additions & 59 deletions Core/Graphics/RHI/OpenGL/OPENGLRenderer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <Core/Graphics/Renderer.h>
#include <Core/Graphics/RHI/OpenGL/OPENGLRendererImpl.h>
#include <Core/Graphics/RHI/OpenGL/OPENGLVertexAttributes.h>

namespace CGL::Graphics
{
Expand Down Expand Up @@ -50,7 +51,8 @@ namespace CGL::Graphics
void Renderer::BeginFrame_OPENGL()
{
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(m_clearColor[0], m_clearColor[1], m_clearColor[2], m_clearColor[3]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

void Renderer::EndFrame_OPENGL()
Expand All @@ -59,47 +61,46 @@ namespace CGL::Graphics
}

void Renderer::Resize_OPENGL(u32 width, u32 height)
{
{
glViewport(0, 0, width, height);
}
}

void Renderer::SetPrimitiveTopology_OPENGL(PrimitiveType topology)
{
void Renderer::SetPrimitiveTopology_OPENGL(PrimitiveType topology)
{
assert(GetImpl());
GetImpl()->SetPrimitive(Mapping::PrimitiveTopology[size_t(topology)]);
}
}

void Renderer::SetVertexShader_OPENGL([[maybe_unused]] const VertexShader& shader)
{
void Renderer::SetVertexShader_OPENGL([[maybe_unused]] const VertexShader& shader)
{
// This function is currently empty because shader creation and compilation are handled
// by separate functions. The SetVertexShader function will be implemented to switch to
// an active vertex shader for each frame as needed.
assert(GetImpl());
}
}

void Renderer::SetPixelShader_OPENGL( [[maybe_unused]] const PixelShader& shader)
{
void Renderer::SetPixelShader_OPENGL( [[maybe_unused]] const PixelShader& shader)
{
// This function is currently empty because shader creation and compilation are handled
// by separate functions. The SetPixelShader function will be implemented to switch to
// an active pixel shader for each frame as needed.
assert(GetImpl() );
}
}

void Renderer::SetVertexBuffer_OPENGL(const VertexBuffer& buffer)
{
void Renderer::SetVertexBuffer_OPENGL(const VertexBuffer& buffer)
{
assert(GetImpl());
glBindVertexArray(buffer.VAO);
}
}

void Renderer::SetIndexBuffer_OPENGL([[maybe_unused]] const IndexBuffer& buffer)
{
// This function is currently empty. The implementation will involve binding the index buffer
// object. As more Samples are implemented this function will be implemented asap.
void Renderer::SetIndexBuffer_OPENGL( const IndexBuffer& buffer)
{
assert(GetImpl());
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer.EBO);
}

ShaderCompileResult Renderer::CompileVertexShader_OPENGL(const ShaderSource& source, VertexShader* outShader)
{
ShaderCompileResult Renderer::CompileVertexShader_OPENGL(const ShaderSource& source, VertexShader* outShader)
{
assert(GetImpl() && outShader);

CompileConfig cfg{};
Expand All @@ -116,10 +117,10 @@ namespace CGL::Graphics
}

return result;
}
}

ShaderCompileResult Renderer::CompilePixelShader_OPENGL(const ShaderSource& source, PixelShader* outShader)
{
ShaderCompileResult Renderer::CompilePixelShader_OPENGL(const ShaderSource& source, PixelShader* outShader)
{
assert(GetImpl() && outShader);

CompileConfig cfg{};
Expand All @@ -136,10 +137,10 @@ namespace CGL::Graphics
}

return result;
}
}

void Renderer::LinkShaders_OPENGL(Material* material)
{
void Renderer::LinkShaders_OPENGL(Material* material)
{
material->m_id = glCreateProgram();
glAttachShader(material->m_id, material->GetVertexShader()->Shader.VertexShader);
glAttachShader(material->m_id, material->GetPixelShader()->Shader.PixelShader);
Expand All @@ -160,67 +161,91 @@ namespace CGL::Graphics
glDeleteShader(material->GetVertexShader()->Shader.VertexShader);
glDeleteShader(material->GetPixelShader()->Shader.PixelShader);
}
}
}

VertexBuffer Renderer::CreateVertexBuffer_OPENGL(const BufferSource& source)
{
VertexBuffer Renderer::CreateVertexBuffer_OPENGL(const BufferSource& source)
{
assert(source.Type == BufferType::Vertex);
assert(GetImpl());

VertexBuffer vb;
vb.Stride = source.TypeSize * source.Count;
vb.Size = source.TypeSize * source.Count;
glGenVertexArrays(1, &vb.VAO);
glGenBuffers(1, &vb.VBO);
glBindVertexArray(vb.VAO);

glBindBuffer(GL_ARRAY_BUFFER, vb.VBO);
glBufferData(GL_ARRAY_BUFFER, vb.Stride, source.Data, Mapping::BufferUsage[size_t(source.Usage)]);
glBufferData(GL_ARRAY_BUFFER, vb.Size, source.Data, Mapping::BufferUsage[size_t(source.Usage)]);

// Position Attribute
glVertexAttribPointer(0, sizeof(VertexTypes::PositionColor::Position) / sizeof(float), GL_FLOAT, GL_FALSE, source.TypeSize, (void*)offsetof(Graphics::VertexTypes::PositionColor,Graphics::VertexTypes::PositionColor::Position));
glEnableVertexAttribArray(0);

// Color Attribute
glVertexAttribPointer(1, sizeof(VertexTypes::PositionColor::Color)/sizeof(float), GL_FLOAT, GL_FALSE, source.TypeSize, (void*)offsetof(Graphics::VertexTypes::PositionColor, Graphics::VertexTypes::PositionColor::Color));
glEnableVertexAttribArray(1);

glBindBuffer(GL_ARRAY_BUFFER, 0);
if (source.VertexType == typeid(VertexTypes::Position))
{
OPENGLEnableVertexAttributes<VertexTypes::Position>();
}
else if (source.VertexType == typeid(VertexTypes::PositionColor))
{
OPENGLEnableVertexAttributes<VertexTypes::PositionColor>();
}
glBindVertexArray(0);

return vb;
}
}

IndexBuffer Renderer::CreateIndexBuffer_OPENGL( [[maybe_unused]] const BufferSource& source)
{
IndexBuffer Renderer::CreateIndexBuffer_OPENGL(const BufferSource& source)
{
assert(source.Type == BufferType::Index);
assert(GetImpl());

IndexBuffer ib;
ib.Stride = source.TypeSize * source.Count;
ib.Size = source.TypeSize * source.Count;
ib.IndicesCount = source.Count;

glGenBuffers(1, &ib.EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib.EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ib.Stride, source.Data, Mapping::BufferUsage[size_t(source.Usage)]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ib.Size, source.Data, Mapping::BufferUsage[size_t(source.Usage)]);

return ib;
}

void Renderer::CreateConstantBuffer_OPENGL([[maybe_unused]] const BufferSource& source, [[maybe_unused]] GLuint& buffer){}
}

void Renderer::SetConstantBufferData_OPENGL([[maybe_unused]] GLuint* buffer, [[maybe_unused]] const void* data, [[maybe_unused]] size_t size){}

void Renderer::SetConstantBuffer_OPENGL([[maybe_unused]] ShaderType type, [[maybe_unused]] u32 startSlot, [[maybe_unused]] const GLuint& buffer){}
void Renderer::CreateConstantBuffer_OPENGL(const BufferSource& source, GLuint& outBuffer)
{
assert(source.Type == BufferType::Constant);
assert(GetImpl());

void Renderer::Draw_OPENGL(u32 vertexCount, u32 startVertex)
{
glGenBuffers(1, &outBuffer);

glBindBuffer(GL_UNIFORM_BUFFER, outBuffer);
glBufferData(GL_UNIFORM_BUFFER, source.TypeSize, nullptr, Mapping::BufferUsage[size_t(source.Usage)]);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, outBuffer);
}

void Renderer::SetConstantBufferData_OPENGL(const GLuint& buffer, const void* data, size_t size)
{
assert(buffer);
glBindBuffer(GL_UNIFORM_BUFFER, buffer);

glBufferSubData(GL_UNIFORM_BUFFER, 0, size, data);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}

void Renderer::SetConstantBuffer_OPENGL([[maybe_unused]] ShaderType type, [[maybe_unused]] u32 startSlot, [[maybe_unused]] const GLuint& buffer)
{
// This function is currently empty because uniform buffer binding is managed directly
// by the shader's 'binding' layout qualifier. If runtime shader switching or dynamic
// buffer management is needed, this function will handle the necessary binding logic.
assert(buffer);
}

void Renderer::Draw_OPENGL(u32 vertexCount, u32 startVertex)
{
assert(GetImpl());
glDrawArrays(GetImpl()->GetPrimitive(), startVertex, vertexCount);
}
}

void Renderer::DrawIndexed_OPENGL([[maybe_unused]] u32 indexCount, [[maybe_unused]] u32 startIndex, [[maybe_unused]] u32 baseVertex)
{
void Renderer::DrawIndexed_OPENGL(u32 indexCount, u32 startIndex, u32 baseVertex)
{
assert(GetImpl());
glDrawElementsBaseVertex(GetImpl()->GetPrimitive(), indexCount, GL_UNSIGNED_INT, (void*)(startIndex * sizeof(GLuint)), baseVertex);
}
}

#endif // CGL_RHI_OPENGL
}
41 changes: 41 additions & 0 deletions Core/Graphics/RHI/OpenGL/OPENGLVertexAttributes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "OPENGLVertexAttributes.h"

namespace CGL::Graphics
{
template <>
void OPENGLEnableVertexAttributes<VertexTypes::Position>()
{
glEnableVertexAttribArray(0); // Position Attribute
glVertexAttribPointer(
0,
sizeof(Graphics::VertexTypes::Position::Position) / sizeof(float),
GL_FLOAT,
GL_FALSE,
sizeof(Graphics::VertexTypes::Position),
(void*)offsetof(Graphics::VertexTypes::Position, Position)
);
}

template <>
void OPENGLEnableVertexAttributes<VertexTypes::PositionColor>() {
glEnableVertexAttribArray(0); // Position Attribute
glVertexAttribPointer(
0,
sizeof(Graphics::VertexTypes::PositionColor::Position) / sizeof(float),
GL_FLOAT,
GL_FALSE,
sizeof(Graphics::VertexTypes::PositionColor),
(void*)offsetof(Graphics::VertexTypes::PositionColor, Position)
);

glEnableVertexAttribArray(1); // Color Attribute
glVertexAttribPointer(
1,
sizeof(Graphics::VertexTypes::PositionColor::Color) / sizeof(float),
GL_FLOAT,
GL_FALSE,
sizeof(Graphics::VertexTypes::PositionColor),
(void*)offsetof(Graphics::VertexTypes::PositionColor, Color)
);
}
}
22 changes: 22 additions & 0 deletions Core/Graphics/RHI/OpenGL/OPENGLVertexAttributes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <GL/glew.h>
#include <Core/Graphics/Types.h>

namespace CGL::Graphics
{
template <typename>
void OPENGLEnableVertexAttributes();

template <>
void OPENGLEnableVertexAttributes<VertexTypes::Position>();

template <>
void OPENGLEnableVertexAttributes<VertexTypes::PositionColor>();

template <>
void OPENGLEnableVertexAttributes<VertexTypes::PositionTexture>();

template <>
void OPENGLEnableVertexAttributes<VertexTypes::PositionColorTexture>();
}
2 changes: 1 addition & 1 deletion Core/Graphics/RHI/OpenGL/OPENGLVertexBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace CGL::Graphics
{
struct OPENGLVertexBuffer
{
u32 Stride;
u32 Size;
u32 Offset;
GLuint VBO;
GLuint VAO;
Expand Down
2 changes: 1 addition & 1 deletion Core/Graphics/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ namespace CGL::Graphics
ShaderCompileResult CompilePixelShader_OPENGL(const ShaderSource& source, PixelShader* outShader);
void LinkShaders_OPENGL(Material* material);
void CreateConstantBuffer_OPENGL(const BufferSource& source, GLuint& outBuffer);
void SetConstantBufferData_OPENGL(GLuint* buffer, const void* data, size_t size);
void SetConstantBufferData_OPENGL(const GLuint& buffer, const void* data, size_t size);
void SetConstantBuffer_OPENGL(ShaderType type, u32 startSlot, const GLuint& buffer);
VertexBuffer CreateVertexBuffer_OPENGL(const BufferSource& source);
IndexBuffer CreateIndexBuffer_OPENGL(const BufferSource& source);
Expand Down
2 changes: 1 addition & 1 deletion Core/Graphics/Renderer.inl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace CGL::Graphics
#if defined(CGL_RHI_DX11)
SetConstantBufferData_D3D11(buffer.Buffer.Get(), static_cast<const void*>(&data), sizeof(T));
#elif defined(CGL_RHI_OPENGL)
SetConstantBufferData_OPENGL(buffer.Buffer.Get(), static_cast<const void*>(&data), sizeof(T));
SetConstantBufferData_OPENGL(buffer.Buffer, static_cast<const void*>(&data), sizeof(T));
#endif
}
template <typename T>
Expand Down
Loading

0 comments on commit 7b895f9

Please sign in to comment.