Brenta Engine 1.2
Loading...
Searching...
No Matches
skybox.cpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#include <brenta/renderer/skybox.hpp>
7#include <brenta/renderer/opengl/cubemap.hpp>
8#include <brenta/renderer/opengl/shader.hpp>
9#include <brenta/renderer/material.hpp>
10#include <brenta/renderer/mesh.hpp>
11
12#include "../src/renderer/shaders/c/skybox_vs.c"
13#include "../src/renderer/shaders/c/skybox_fs.c"
14
15using namespace brenta;
16
17Skybox::Skybox(const tenno::vector<std::filesystem::path>& faces)
18{
19 auto cubemap = tenno::make_shared<Cubemap>(faces);
20 auto maybe_skybox_shader = Shader::create({
21 { Shader::Type::Vertex, skybox_vs },
22 { Shader::Type::Fragment, skybox_fs }
23 });
24 if (!maybe_skybox_shader)
25 {
26 ERROR("Error creating shader");
27 return;
28 }
29 this->material =
30 tenno::make_shared<Material>(tenno::move(maybe_skybox_shader.value()));
31 this->material->set_texture("skybox", cubemap, 0);
32 auto skybox_mesh_builder =
34 .vertices({
35 { glm::vec3(-1.0f, -1.0f, -1.0f), glm::vec3(0.0f), glm::vec2(0.0f) },
36 { glm::vec3( 1.0f, -1.0f, -1.0f), glm::vec3(0.0f), glm::vec2(0.0f) },
37 { glm::vec3(-1.0f, 1.0f, -1.0f), glm::vec3(0.0f), glm::vec2(0.0f) },
38 { glm::vec3( 1.0f, 1.0f, -1.0f), glm::vec3(0.0f), glm::vec2(0.0f) },
39 { glm::vec3(-1.0f, -1.0f, 1.0f), glm::vec3(0.0f), glm::vec2(0.0f) },
40 { glm::vec3( 1.0f, -1.0f, 1.0f), glm::vec3(0.0f), glm::vec2(0.0f) },
41 { glm::vec3(-1.0f, 1.0f, 1.0f), glm::vec3(0.0f), glm::vec2(0.0f) },
42 { glm::vec3( 1.0f, 1.0f, 1.0f), glm::vec3(0.0f), glm::vec2(0.0f) },
43 })
44 .indices({
45 2, 0, 1,
46 1, 3, 2,
47 4, 0, 2,
48 2, 6, 4,
49 1, 5, 7,
50 7, 3, 1,
51 4, 6, 7,
52 7, 5, 4,
53 2, 3, 7,
54 7, 6, 2,
55 0, 4, 1,
56 1, 4, 5,
57 })
58 .texture(cubemap);
59 this->mesh = tenno::make_shared<Mesh>(skybox_mesh_builder.build());
60}
61
62void Skybox::draw()
63{
64 if (!this->material || !this->mesh)
65 return;
66
67 glDepthFunc(GL_LEQUAL); // Allow drawing at the far plane (1.0)
68 glDepthMask(GL_FALSE); // Don't overwrite depth with skybox pixels
69
70 this->material->apply();
71 this->mesh->draw();
72
73 glDepthMask(GL_TRUE);
74 glDepthFunc(GL_LESS);
75}