This commit is contained in:
Dane Johnson 2021-01-23 11:54:34 -06:00
parent 14ddf47d0d
commit 776b297150
16 changed files with 199 additions and 3 deletions

View 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"; }

View 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
View 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
View 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 */

View File

@ -12,6 +12,7 @@
#include "Shaders/FlatShader.h" #include "Shaders/FlatShader.h"
#include "Shaders/ScreenShader.h" #include "Shaders/ScreenShader.h"
#include "Shaders/SkyboxShader.h"
#include "Screen.h" #include "Screen.h"
@ -20,6 +21,8 @@
#include "Node.h" #include "Node.h"
#include "Mesh.h" #include "Mesh.h"
#include "Light.h" #include "Light.h"
#include "Skybox.h"
#include "Scripting/Lua.h" #include "Scripting/Lua.h"
// Thirdparty Includes // Thirdparty Includes
@ -87,6 +90,8 @@ int main() {
Screen screen; Screen screen;
ScreenShader *screenShader = new ScreenShader(); ScreenShader *screenShader = new ScreenShader();
SkyboxShader *skyboxShader = new SkyboxShader();
FlatShader *shader = new FlatShader(); FlatShader *shader = new FlatShader();
@ -127,6 +132,18 @@ int main() {
// Render the scene tree // Render the scene tree
render(root, shader, Matrix(1.0f)); 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 // Stop rendering to texture
screen.Disable(); screen.Disable();
// // Render the screen // // Render the screen

View File

@ -35,6 +35,16 @@ function init()
light.diffuse = 1.0 light.diffuse = 1.0
light.specular = 0.1 light.specular = 0.1
couch.Node.GetRoot().children:Append(light) couch.Node.GetRoot().children:Append(light)
skybox = couch.Skybox.FromFiles(
"skybox/right.jpg",
"skybox/left.jpg",
"skybox/top.jpg",
"skybox/bottom.jpg",
"skybox/front.jpg",
"skybox/back.jpg"
)
couch.Node.GetRoot().children:Append(skybox)
ball = couch.Mesh.FromFile("cube.obj") ball = couch.Mesh.FromFile("cube.obj")
material = ball:GetMaterial(0) material = ball:GetMaterial(0)
@ -44,6 +54,7 @@ function init()
couch.Node.GetRoot().children:Append(ball) couch.Node.GetRoot().children:Append(ball)
ball1 = couch.Mesh.FromFile("ball.obj") ball1 = couch.Mesh.FromFile("ball.obj")
print(material.diffuse.b) print(material.diffuse.b)
material = ball1:GetMaterial(0) material = ball1:GetMaterial(0)
ball1:SetMaterial(0, material) ball1:SetMaterial(0, material)

BIN
demo/skybox/back.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 723 KiB

BIN
demo/skybox/bottom.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 274 KiB

BIN
demo/skybox/front.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 462 KiB

BIN
demo/skybox/left.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 588 KiB

BIN
demo/skybox/right.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 525 KiB

BIN
demo/skybox/top.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 338 KiB

View File

@ -15,6 +15,7 @@
#include "Material.h" #include "Material.h"
#include "Camera.h" #include "Camera.h"
#include "Light.h" #include "Light.h"
#include "Skybox.h"
%} %}
%rename("%(strip:[script_])s") ""; %rename("%(strip:[script_])s") "";
@ -45,7 +46,6 @@ public:
} }
} }
%include "types.h" %include "types.h"
%include "constants.h" %include "constants.h"
%include "Node.h" %include "Node.h"
@ -55,4 +55,4 @@ public:
%include "Material.h" %include "Material.h"
%include "Camera.h" %include "Camera.h"
%include "Light.h" %include "Light.h"
%include "Skybox.h"

View File

@ -12,7 +12,9 @@ add_shader(flat.vert)
add_shader(flat.frag) add_shader(flat.frag)
add_shader(screen.vert) add_shader(screen.vert)
add_shader(screen.frag) add_shader(screen.frag)
add_shader(skybox.vert)
add_shader(skybox.frag)
add_custom_target(shader_headers add_custom_target(shader_headers
DEPENDS flat.vert.h flat.frag.h screen.vert.h screen.frag.h DEPENDS flat.vert.h flat.frag.h screen.vert.h screen.frag.h skybox.vert.h skybox.frag.h
COMMENT "Generated shaders headers.") COMMENT "Generated shaders headers.")

11
shaders/skybox.frag Normal file
View File

@ -0,0 +1,11 @@
#version 330 core
out vec4 FragColor;
in vec3 UV;
uniform samplerCube skybox;
void main() {
FragColor = texture(skybox, UV);
}

14
shaders/skybox.vert Normal file
View File

@ -0,0 +1,14 @@
#version 330 core
layout (location = 0) in vec3 pos;
out vec3 UV;
uniform mat4 VIEW; // Assuming this is free of translation
uniform mat4 PROJECTION;
void main() {
UV = pos;
vec4 projectedPos = PROJECTION * VIEW * vec4(pos, 1.0);
gl_Position = projectedPos.xyww;
}