Skip to content

Commit 4ac0f83

Browse files
Implemented Skybox support
- Skybox class logic - Cubemap with OpenGL implementation - Skybox rendering at last stage in the first render pass - Option in Editor to enable Skybox
1 parent bee89f9 commit 4ac0f83

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+382
-20
lines changed

Engine/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ set(ENGINE_HEADERS
3333
"src/Renderer/RHI/VertexArray.h"
3434
"src/Renderer/RHI/Texture.h"
3535
"src/Renderer/RHI/RenderTarget.h"
36+
"src/Renderer/RHI/Cubemap.h"
3637
"src/Renderer/Camera.h"
3738
"src/Renderer/CameraController.h"
3839
"src/Renderer/OrthographicCameraController.h"
@@ -44,6 +45,7 @@ set(ENGINE_HEADERS
4445
"src/Renderer/Mesh.h"
4546
"src/Renderer/RenderTopology.h"
4647
"src/Renderer/PostProcessor.h"
48+
"src/Renderer/Skybox.h"
4749
"src/Platform/Windows/WindowsWindow.h"
4850
"src/Platform/OpenGL/OpenGLBuffer.h"
4951
"src/Platform/OpenGL/OpenGLContext.h"
@@ -52,6 +54,7 @@ set(ENGINE_HEADERS
5254
"src/Platform/OpenGL/OpenGLVertexArray.h"
5355
"src/Platform/OpenGL/OpenGLTexture.h"
5456
"src/Platform/OpenGL/OpenGLRenderTarget.h"
57+
"src/Platform/OpenGL/OpenGLCubemap.h"
5558
"src/Scene/Entity.h"
5659
"src/Scene/Component.h"
5760
"src/Scene/Behavior.h"
@@ -114,13 +117,15 @@ set(ENGINE_SOURCES
114117
"src/Renderer/Mesh.cpp"
115118
"src/Renderer/Material.cpp"
116119
"src/Renderer/PostProcessor.cpp"
120+
"src/Renderer/Skybox.cpp"
117121
"src/Platform/OpenGL/OpenGLBuffer.cpp"
118122
"src/Platform/OpenGL/OpenGLContext.cpp"
119123
"src/Platform/OpenGL/OpenGLRendererAPI.cpp"
120124
"src/Platform/OpenGL/OpenGLShader.cpp"
121125
"src/Platform/OpenGL/OpenGLVertexArray.cpp"
122126
"src/Platform/OpenGL/OpenGLTexture.cpp"
123127
"src/Platform/OpenGL/OpenGLRenderTarget.cpp"
128+
"src/Platform/OpenGL/OpenGLCubemap.cpp"
124129
"src/Platform/Windows/WindowsInput.cpp"
125130
"src/Platform/Windows/WindowsWindow.cpp"
126131
"src/Scene/Behavior.cpp"

Engine/assets/shaders/skybox.frag

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#version 450 core
2+
out vec4 FragColor;
3+
4+
in vec3 TexCoords;
5+
6+
uniform samplerCube skybox;
7+
8+
void main()
9+
{
10+
FragColor = texture(skybox, TexCoords);
11+
}

Engine/assets/shaders/skybox.vert

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#version 450 core
2+
3+
layout (location = 0) in vec3 aPos;
4+
5+
out vec3 TexCoords;
6+
7+
uniform mat4 u_ViewProjection;
8+
9+
void main()
10+
{
11+
TexCoords = aPos;
12+
vec4 pos = u_ViewProjection * vec4(aPos, 1.0);
13+
gl_Position = pos.xyww;
14+
}

Engine/src/Core/Application.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ void Application::OnWindowResize(const events::WindowResizeEvent& e)
202202
OnWindowResizeApp();
203203
}
204204

205-
void Application::UpdateFps(Timer& fpsCounter, const float elapsed, const float elapsedMs)
205+
void Application::UpdateFps(Timer& fpsCounter, const float elapsed, const float elapsedMs) const
206206
{
207207
static float accumTime = 0.0f;
208208
static float accumTimeMs = 0.0f;

Engine/src/Core/Application.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,22 @@ class Application {
4444
Renderer& GetRenderer() { return m_renderer; }
4545

4646
protected:
47-
virtual void OnCreate() {};
48-
virtual void OnUpdate(float dt) {};
49-
virtual void OnRender(float dt) {};
50-
virtual void OnProcessInput(float dt) {};
47+
virtual void OnCreate() { };
48+
virtual void OnUpdate(float dt) { };
49+
virtual void OnRender(float dt) { };
50+
virtual void OnProcessInput(float dt) { };
5151
#if MODULE_EDITOR_ENABLED
52-
virtual void OnImguiRender() {};
52+
virtual void OnImguiRender() { };
5353
#endif
54-
virtual void OnDestroy() {};
54+
virtual void OnDestroy() { };
5555

56-
virtual void OnWindowResizeApp() {};
56+
virtual void OnWindowResizeApp() { };
5757

5858
private:
5959
void OnWindowClose(const events::WindowCloseEvent& e);
6060
void OnWindowResize(const events::WindowResizeEvent& e);
6161

62-
void UpdateFps(Timer& fpsCounter, const float elapsed, const float elapsedMs);
62+
void UpdateFps(Timer& fpsCounter, const float elapsed, const float elapsedMs) const;
6363

6464
protected:
6565
ecs::Entity m_orthoCameraEntity { ecs::INVALID_ENTITY_ID };

Engine/src/Editor/Editor.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void Editor::OnImGuiRender()
3939
bool isVSync = window->IsVSync();
4040
bool isFullScreen = window->IsFullScreen();
4141
bool isMSAAEnabled = renderer.IsMSAAEnabled();
42+
bool isSkyboxEnabled = renderer.IsSkyboxEnabled();
4243
bool isBlinnPhongEnabled = renderer.IsBlinnPhongEnabled();
4344

4445
// ============ Settings panel ============
@@ -57,6 +58,10 @@ void Editor::OnImGuiRender()
5758
renderer.EnableMSAA(isMSAAEnabled);
5859
}
5960

61+
if (ImGui::Checkbox("Skybox", &isSkyboxEnabled)) {
62+
renderer.EnableSkybox(isSkyboxEnabled);
63+
}
64+
6065
if (ImGui::Checkbox("Blinn-Phong", &isBlinnPhongEnabled)) {
6166
renderer.EnableBlinnPhong(isBlinnPhongEnabled);
6267
}

Engine/src/Events/EventManager.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ EventManager gEventManager;
66

77
void EventManager::Shutdown()
88
{
9+
m_eventsQueue.clear();
910
m_subscribers.clear();
11+
m_subscribersByEventId.clear();
1012
}
1113

1214
void EventManager::Subscribe(EventType eventType, UniquePtr<IEventHandlerWrapper>&& handler, EventId eventId)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "OpenGLCubemap.h"
2+
3+
#include <glad/gl.h>
4+
5+
namespace elv {
6+
7+
OpenGLCubemap::OpenGLCubemap(const std::vector<TextureManager::LoadedTextureInfo>& texturesInfo)
8+
{
9+
// Generate the cubemap texture
10+
glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &m_id);
11+
12+
// Specify the storage for the cubemap texture
13+
glTextureStorage2D(m_id, 1, GL_RGB8, texturesInfo[0].width, texturesInfo[0].height);
14+
15+
// Upload the texture data for each face of the cubemap
16+
for (unsigned int i = 0; i < texturesInfo.size(); ++i) {
17+
if (texturesInfo[i].data) {
18+
glTextureSubImage3D(
19+
m_id,
20+
0, // Level of detail
21+
0, 0, i, // Offset (x, y, layer)
22+
texturesInfo[i].width, // Width of the face
23+
texturesInfo[i].height, // Height of the face
24+
1, // Depth (1 for single-layer 2D)
25+
GL_RGB, // Format of input data
26+
GL_UNSIGNED_BYTE, // Data type of input data
27+
texturesInfo[i].data // Pointer to the texture data
28+
);
29+
}
30+
}
31+
32+
// Set texture parameters using DSA
33+
glTextureParameteri(m_id, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
34+
glTextureParameteri(m_id, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
35+
glTextureParameteri(m_id, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
36+
glTextureParameteri(m_id, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
37+
glTextureParameteri(m_id, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
38+
}
39+
40+
void OpenGLCubemap::Bind()
41+
{
42+
glBindTexture(GL_TEXTURE_CUBE_MAP, m_id);
43+
}
44+
45+
} // namespace elv
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include "Renderer/RHI/Cubemap.h"
4+
#include "Resources/TextureManager.h"
5+
6+
namespace elv {
7+
8+
class OpenGLCubemap : public Cubemap {
9+
public:
10+
OpenGLCubemap(const std::vector<TextureManager::LoadedTextureInfo>& texturesInfo);
11+
12+
virtual void Bind() override;
13+
14+
private:
15+
uint32_t m_id { 0 };
16+
uint32_t m_slot { 0 };
17+
};
18+
19+
} // namespace elv

Engine/src/Platform/OpenGL/OpenGLRendererAPI.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ void OpenGLRendererAPI::EnableDepthTesting(bool enabled)
8585
}
8686
}
8787

88+
void OpenGLRendererAPI::EnableDepthMask(bool enabled)
89+
{
90+
glDepthMask(enabled ? GL_TRUE : GL_FALSE);
91+
}
92+
8893
void OpenGLRendererAPI::EnableMSAA(bool enabled)
8994
{
9095
if (enabled) {
@@ -120,4 +125,9 @@ void OpenGLRendererAPI::DrawIndexed(const SharedPtr<VertexArray>& vertexArray, c
120125
glDrawElements(GetTopology(topology), count, GL_UNSIGNED_INT, nullptr);
121126
}
122127

128+
void OpenGLRendererAPI::DrawArrays(const SharedPtr<VertexArray>& vertexArray, const std::uint32_t verticesAmount, const RenderTopology topology)
129+
{
130+
glDrawArrays(GetTopology(topology), 0, verticesAmount);
131+
}
132+
123133
} // namespace elv

0 commit comments

Comments
 (0)