Skybox
This commit is contained in:
12
core/Shaders/SkyboxShader.cpp
Normal file
12
core/Shaders/SkyboxShader.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "SkyboxShader.h"
|
||||
#include "skybox.vert.h"
|
||||
#include "skybox.frag.h"
|
||||
|
||||
SkyboxShader::SkyboxShader() : Shader(skybox_vert, skybox_frag) {}
|
||||
|
||||
void SkyboxShader::UpdateSkybox(Skybox skybox) {
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, skybox.id);
|
||||
}
|
||||
|
||||
Name SkyboxShader::GetName() const { return "Skybox Shader"; }
|
||||
14
core/Shaders/SkyboxShader.h
Normal file
14
core/Shaders/SkyboxShader.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef SKYBOXSHADER_H
|
||||
#define SKYBOXSHADER_H
|
||||
|
||||
#include "Shader.h"
|
||||
#include "Skybox.h"
|
||||
|
||||
class SkyboxShader : public Shader {
|
||||
public:
|
||||
SkyboxShader();
|
||||
Name GetName() const;
|
||||
void UpdateSkybox(Skybox skybox);
|
||||
};
|
||||
|
||||
#endif /* SKYBOXSHADER_H */
|
||||
94
core/Skybox.cpp
Normal file
94
core/Skybox.cpp
Normal file
@@ -0,0 +1,94 @@
|
||||
#include "Skybox.h"
|
||||
|
||||
cfloat vertices[] = {
|
||||
// positions
|
||||
-1.0f, 1.0f, -1.0f,
|
||||
-1.0f, -1.0f, -1.0f,
|
||||
1.0f, -1.0f, -1.0f,
|
||||
1.0f, -1.0f, -1.0f,
|
||||
1.0f, 1.0f, -1.0f,
|
||||
-1.0f, 1.0f, -1.0f,
|
||||
|
||||
-1.0f, -1.0f, 1.0f,
|
||||
-1.0f, -1.0f, -1.0f,
|
||||
-1.0f, 1.0f, -1.0f,
|
||||
-1.0f, 1.0f, -1.0f,
|
||||
-1.0f, 1.0f, 1.0f,
|
||||
-1.0f, -1.0f, 1.0f,
|
||||
|
||||
1.0f, -1.0f, -1.0f,
|
||||
1.0f, -1.0f, 1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
1.0f, 1.0f, -1.0f,
|
||||
1.0f, -1.0f, -1.0f,
|
||||
|
||||
-1.0f, -1.0f, 1.0f,
|
||||
-1.0f, 1.0f, 1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
1.0f, -1.0f, 1.0f,
|
||||
-1.0f, -1.0f, 1.0f,
|
||||
|
||||
-1.0f, 1.0f, -1.0f,
|
||||
1.0f, 1.0f, -1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
-1.0f, 1.0f, 1.0f,
|
||||
-1.0f, 1.0f, -1.0f,
|
||||
|
||||
-1.0f, -1.0f, -1.0f,
|
||||
-1.0f, -1.0f, 1.0f,
|
||||
1.0f, -1.0f, -1.0f,
|
||||
1.0f, -1.0f, -1.0f,
|
||||
-1.0f, -1.0f, 1.0f,
|
||||
1.0f, -1.0f, 1.0f
|
||||
};
|
||||
|
||||
Skybox::Skybox() {
|
||||
glGenVertexArrays(1, &cube);
|
||||
glBindVertexArray(cube);
|
||||
|
||||
Id vbo;
|
||||
glGenBuffers(1, &vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
// We're just going to use position data
|
||||
// UV data is the same
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * (sizeof(cfloat)), (void*) 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
Name Skybox::GetType() const { return "Skybox"; }
|
||||
|
||||
Skybox *Skybox::FromFiles(const char *right, const char* left, const char* top, const char* bottom, const char* front, const char* back) {
|
||||
// HOCUS: https://learnopengl.com/Advanced-OpenGL/Cubemaps
|
||||
Skybox *sb = new Skybox();
|
||||
glGenTextures(1, &sb->id);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, sb->id);
|
||||
|
||||
int width, height, nrChannels;
|
||||
unsigned char* data;
|
||||
const char* files[] = {right, left, top, bottom, front, back};
|
||||
for (int i = 0; i < 6; i++) {
|
||||
data = stbi_load(files[i], &width, &height, &nrChannels, 3);
|
||||
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
|
||||
0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
|
||||
stbi_image_free(data);
|
||||
}
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
||||
|
||||
return sb;
|
||||
}
|
||||
|
||||
void Skybox::DrawSkybox() {
|
||||
glBindVertexArray(cube);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
21
core/Skybox.h
Normal file
21
core/Skybox.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef SKYBOX_H
|
||||
#define SKYBOX_H
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "types.h"
|
||||
#include "Node.h"
|
||||
#include "Material.h"
|
||||
|
||||
class Skybox : public Node {
|
||||
public:
|
||||
Skybox();
|
||||
virtual Name GetType() const;
|
||||
static Skybox *FromFiles(const char *right, const char* left, const char* top, const char* bottom, const char* front, const char* back);
|
||||
void DrawSkybox();
|
||||
Id id;
|
||||
private:
|
||||
Id cube;
|
||||
};
|
||||
|
||||
#endif /* SKYBOX_H */
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
#include "Shaders/FlatShader.h"
|
||||
#include "Shaders/ScreenShader.h"
|
||||
#include "Shaders/SkyboxShader.h"
|
||||
|
||||
#include "Screen.h"
|
||||
|
||||
@@ -20,6 +21,8 @@
|
||||
#include "Node.h"
|
||||
#include "Mesh.h"
|
||||
#include "Light.h"
|
||||
#include "Skybox.h"
|
||||
|
||||
#include "Scripting/Lua.h"
|
||||
|
||||
// Thirdparty Includes
|
||||
@@ -87,6 +90,8 @@ int main() {
|
||||
|
||||
Screen screen;
|
||||
ScreenShader *screenShader = new ScreenShader();
|
||||
|
||||
SkyboxShader *skyboxShader = new SkyboxShader();
|
||||
|
||||
FlatShader *shader = new FlatShader();
|
||||
|
||||
@@ -127,6 +132,18 @@ int main() {
|
||||
// Render the scene tree
|
||||
render(root, shader, Matrix(1.0f));
|
||||
|
||||
// Render the skybox
|
||||
Skybox *skybox = Util::FindNodeByType<Skybox>(root, "Skybox");
|
||||
if (skybox) {
|
||||
glDepthFunc(GL_LEQUAL);
|
||||
skyboxShader->Use();
|
||||
skyboxShader->UpdateView(glm::mat4(glm::mat3(view)));
|
||||
skyboxShader->UpdateProjection(projection);
|
||||
skyboxShader->UpdateSkybox(*skybox);
|
||||
skybox->DrawSkybox();
|
||||
glDepthFunc(GL_LESS);
|
||||
}
|
||||
|
||||
// Stop rendering to texture
|
||||
screen.Disable();
|
||||
// // Render the screen
|
||||
|
||||
Reference in New Issue
Block a user