Loading...
Searching...
No Matches
SkyboxRenderer.cpp
Go to the documentation of this file.
1//
2// Created by denzel on 14/06/2025.
3//
4
5#include "hellfire/graphics/renderer/SkyboxRenderer.h"
6
7#include <glm/gtc/type_ptr.hpp>
8
9#include "../core/Application.h"
10#include <GL/glew.h>
11#include "Skybox.h"
12#include "hellfire/utilities/ServiceLocator.h"
13
14namespace hellfire {
15 // skyboxes cube vertices
16 float skyboxVertices[] = {
17 // positions
18 -1.0f, 1.0f, -1.0f,
19 -1.0f, -1.0f, -1.0f,
20 1.0f, -1.0f, -1.0f,
21 1.0f, -1.0f, -1.0f,
22 1.0f, 1.0f, -1.0f,
23 -1.0f, 1.0f, -1.0f,
24
25 -1.0f, -1.0f, 1.0f,
26 -1.0f, -1.0f, -1.0f,
27 -1.0f, 1.0f, -1.0f,
28 -1.0f, 1.0f, -1.0f,
29 -1.0f, 1.0f, 1.0f,
30 -1.0f, -1.0f, 1.0f,
31
32 1.0f, -1.0f, -1.0f,
33 1.0f, -1.0f, 1.0f,
34 1.0f, 1.0f, 1.0f,
35 1.0f, 1.0f, 1.0f,
36 1.0f, 1.0f, -1.0f,
37 1.0f, -1.0f, -1.0f,
38
39 -1.0f, -1.0f, 1.0f,
40 -1.0f, 1.0f, 1.0f,
41 1.0f, 1.0f, 1.0f,
42 1.0f, 1.0f, 1.0f,
43 1.0f, -1.0f, 1.0f,
44 -1.0f, -1.0f, 1.0f,
45
46 -1.0f, 1.0f, -1.0f,
47 1.0f, 1.0f, -1.0f,
48 1.0f, 1.0f, 1.0f,
49 1.0f, 1.0f, 1.0f,
50 -1.0f, 1.0f, 1.0f,
51 -1.0f, 1.0f, -1.0f,
52
53 -1.0f, -1.0f, -1.0f,
54 -1.0f, -1.0f, 1.0f,
55 1.0f, -1.0f, -1.0f,
56 1.0f, -1.0f, -1.0f,
57 -1.0f, -1.0f, 1.0f,
58 1.0f, -1.0f, 1.0f
59 };
60
62 if (skybox_vao_ != 0)
63 glDeleteVertexArrays(1, &skybox_vao_);
64 if (skybox_vbo_ != 0)
65 glDeleteBuffers(1, &skybox_vbo_);
66 if (skybox_shader_ != 0)
67 glDeleteProgram(skybox_shader_);
68 }
69
71 if (!initialized_) {
74 }
75 }
76
78 glGenVertexArrays(1, &skybox_vao_);
79 glGenBuffers(1, &skybox_vbo_);
80
81 glBindVertexArray(skybox_vao_);
82 glBindBuffer(GL_ARRAY_BUFFER, skybox_vbo_);
83 glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
84
85 glEnableVertexAttribArray(0);
86 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), static_cast<void *>(nullptr));
87
88 glBindVertexArray(0);
89 }
90
92 skybox_shader_ = ServiceLocator::get_service<ShaderManager>()->load_shader_from_files(
93 "assets/shaders/skybox.vert", "assets/shaders/skybox.frag");
94 }
95
96 void SkyboxRenderer::render(const Skybox &skybox, const CameraComponent *camera) const {
97 if (!skybox.is_loaded()) return;
98
99 // Save current depth state
100 glDepthFunc(GL_LEQUAL);
101
102 glUseProgram(skybox_shader_);
103
104 // Remove translation from view matrix
105 glm::mat4 view = glm::mat4(glm::mat3(camera->get_view_matrix()));
106 glm::mat4 projection = camera->get_projection_matrix();
107
108 glUniformMatrix4fv(glGetUniformLocation(skybox_shader_, "view"), 1, GL_FALSE, glm::value_ptr(view));
109 glUniformMatrix4fv(glGetUniformLocation(skybox_shader_, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
110
111 // Set tint and exposure
112 glUniform3fv(glGetUniformLocation(skybox_shader_, "tint"), 1, glm::value_ptr(skybox.get_tint()));
113 glUniform1f(glGetUniformLocation(skybox_shader_, "exposure"), skybox.get_exposure());
114
115 // Bind cubemap
116 glActiveTexture(GL_TEXTURE0);
117 glBindTexture(GL_TEXTURE_CUBE_MAP, skybox.get_cubemap());
118 glUniform1i(glGetUniformLocation(skybox_shader_, "skyboxes"), 0);
119
120 // Draw skyboxes cube
121 glBindVertexArray(skybox_vao_);
122 glDrawArrays(GL_TRIANGLES, 0, 36);
123 glBindVertexArray(0);
124
125 // Restore depth state
126 glDepthFunc(GL_LESS);
127 }
128}
void render(const Skybox &skybox, const CameraComponent *camera) const
bool is_loaded() const
Definition Skybox.h:31
float get_exposure() const
Definition Skybox.h:29
float skyboxVertices[]