Elevate Engine 1
Loading...
Searching...
No Matches
Cubemap.cpp
Go to the documentation of this file.
1#include "eepch.h"
2
3#include "Cubemap.h"
8
9#include <stb/stb_image.h>
10#include <rapidjson/filereadstream.h>
11#include <rapidjson/document.h>
12
13Elevate::Cubemap::Cubemap(std::string paths[6], std::string skyboxFilePath)
14{
15 m_renderState.Cullface = false;
16 m_renderState.DepthWrite = false;
17 m_renderState.DepthTest = false;
18
19 m_filePath = skyboxFilePath;
20
21 // Vertex & Index Buffers
22 m_VertexBuffer.reset(Elevate::VertexBuffer::Create(&s_skyboxVertices[0], sizeof(s_skyboxVertices)));
23 m_VertexBuffer->SetLayout({
24 { Elevate::ShaderDataType::Float3, "a_Position" },
25 });
26
27 m_IndexBuffer.reset(Elevate::IndexBuffer::Create(&s_skyboxIndices[0], sizeof(s_skyboxIndices) / sizeof(unsigned int)));
28 m_VertexArray.reset(VertexArray::Create());
29 m_VertexArray->AddVertexBuffer(m_VertexBuffer);
30 m_VertexArray->SetIndexBuffer(m_IndexBuffer);
31 m_VertexArray->Unbind();
32
33 // Shaders
34 std::string vert = std::string(EE_SHADER_HEADER) +
35#include "ElevateEngine/Renderer/Cubemap/skybox.vert"
36 ;
37 std::string frag = std::string(EE_SHADER_HEADER) +
38#include "ElevateEngine/Renderer/Cubemap/skybox.frag"
39 ;
40 m_cubemapShader = Elevate::Shader::Create(vert, frag);
41
42 // Texture
43 GLCheck(glGenTextures(1, &m_textureID));
44 GLCheck(glBindTexture(GL_TEXTURE_CUBE_MAP, m_textureID));
45
46 int width, height, nrChannels;
47 for (int i = 0; i < 6; i++)
48 {
49 unsigned char* data = stbi_load(paths[i].c_str(), &width, &height, &nrChannels, 0);
50 if (data)
51 {
52 GLCheck(glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data));
53 stbi_image_free(data);
54 }
55 else
56 {
57 EE_CORE_ERROR("Error : Unable to load cubemap texture [{}] : {} with the following skybox file : {}", i, paths[i].c_str(), skyboxFilePath);
58 return;
59 }
60 }
61 GLCheck(glGenerateMipmap(GL_TEXTURE_CUBE_MAP));
62
63 GLCheck(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
64 GLCheck(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
65 GLCheck(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
66 GLCheck(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
67 GLCheck(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE));
68}
69
71{
72 std::string resolvedPath = PathResolver::Resolve(filePath);
73
74 FILE* fp = fopen(resolvedPath.c_str(), "r");
75 if (!fp)
76 {
77 EE_CORE_ERROR("Error : Cannot open cubemap config file ({})", resolvedPath);
78 return nullptr;
79 }
80
81 char readBuffer[65536];
82 rapidjson::FileReadStream is(fp, readBuffer, sizeof(readBuffer));
83 rapidjson::Document doc;
84 doc.ParseStream(is);
85 fclose(fp);
86
87 // todo add more error catching, check if member exists or return
88 std::string paths[6] =
89 {
90 PathResolver::Resolve(doc["right"].GetString()),
91 PathResolver::Resolve(doc["left"].GetString()),
92 PathResolver::Resolve(doc["up"].GetString()),
93 PathResolver::Resolve(doc["down"].GetString()),
94 PathResolver::Resolve(doc["front"].GetString()),
95 PathResolver::Resolve(doc["back"].GetString())
96 };
97
98 return new Cubemap(paths, resolvedPath);
99}
100
101void Elevate::Cubemap::Draw(std::shared_ptr<Shader> shader)
102{
103 if (!shader) shader = m_cubemapShader;
104
105 // todo send as a render command
106 Renderer::PushRenderState(m_renderState);
107 Renderer::BindShader(shader);
108 GLCheck(glBindTexture(GL_TEXTURE_CUBE_MAP, m_textureID));
109 Renderer::DrawArray(m_VertexArray);
110}
111
113{
114 Renderer::BindShader(m_cubemapShader);
115 m_cubemapShader->SetUniformMatrix4fv("projection", proj);
116}
117
119{
120 Renderer::BindShader(m_cubemapShader);
121 m_cubemapShader->SetUniformMatrix4fv("view", view);
122}
123
125{
126 return m_filePath;
127}
#define EE_SHADER_HEADER
Definition Core.h:32
#define GLCheck(x)
Definition GLDebug.h:11
void Draw(std::shared_ptr< Shader > shader=nullptr)
Definition Cubemap.cpp:101
static Cubemap * CreateFromFile(const std::string &filePath)
Definition Cubemap.cpp:70
void SetViewMatrix(glm::mat4 data)
Definition Cubemap.cpp:118
std::string GetFilePath()
Definition Cubemap.cpp:124
Cubemap(std::string paths[6], std::string skyboxFilePath="")
Definition Cubemap.cpp:13
void SetProjectionMatrix(glm::mat4 data)
Definition Cubemap.cpp:112
static IndexBuffer * Create(const void *vertices, uint32_t count)
Definition Buffer.cpp:23
static std::string Resolve(const std::string &virtualPath)
static void PushRenderState(const RenderState &newState)
Definition Renderer.cpp:91
static bool BindShader(const std::shared_ptr< Shader > &shader)
Definition Renderer.cpp:30
static void DrawArray(const VertexArray *vao, DrawPrimitiveType primitive=DrawPrimitiveType::Triangles)
Definition Renderer.cpp:73
static std::shared_ptr< Shader > Create(const std::string &vertexSource, const std::string &fragmentSouce)
Definition Shader.cpp:48
static VertexArray * Create()
static VertexBuffer * Create(const void *vertices, const uint32_t size)
Definition Buffer.cpp:11