Basics für skybox v1.2

This commit is contained in:
JonasD1337 2024-06-21 11:36:29 +02:00
parent 0a56d2faaa
commit 2ddca33da4
8 changed files with 165 additions and 92 deletions

10
obj/skybox.mtl Normal file
View File

@ -0,0 +1,10 @@
# Material Count: 1
newmtl Material
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2

51
obj/skybox.obj Normal file
View File

@ -0,0 +1,51 @@
# Blender v3.0.1 OBJ File: ''
# www.blender.org
mtllib skybox.mtl
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -1.000000
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
vt 0.001020 0.000000
vt 0.333333 0.001529
vt 0.332314 0.500000
vt 0.000000 0.498471
vt 0.666667 0.501529
vt 0.665647 1.000000
vt 0.333333 0.998471
vt 0.334353 0.500000
vt 0.001020 1.000000
vt 0.000000 0.501529
vt 0.666667 0.498471
vt 0.665647 0.000000
vt 0.667686 0.000000
vt 0.998980 0.500000
vt 1.000000 0.001529
vt 1.000000 0.998471
vt 0.667686 1.000000
vn 0.000000 0.000000 -1.000000
vn -1.000000 -0.000000 -0.000000
vn -0.000000 -0.000000 1.000000
vn -0.000001 0.000000 1.000000
vn 1.000000 -0.000000 0.000000
vn 1.000000 0.000000 0.000001
vn 0.000000 1.000000 -0.000000
vn -0.000000 -1.000000 0.000000
usemtl Material
s off
f 5/1/1 1/2/1 4/3/1
f 5/1/1 4/3/1 8/4/1
f 3/5/2 7/6/2 8/7/2
f 3/5/2 8/7/2 4/8/2
f 2/9/3 6/10/3 3/7/3
f 6/10/4 7/3/4 3/7/4
f 1/8/5 5/2/5 2/11/5
f 5/2/6 6/12/6 2/11/6
f 5/11/7 8/13/7 6/14/7
f 8/13/7 7/15/7 6/14/7
f 1/14/8 2/16/8 3/17/8
f 1/14/8 3/17/8 4/5/8

View File

@ -5,7 +5,7 @@ OTHER_LIBS = -lm
ALL_LIBS = $(GLEW_LIBS) $(GLFW_LIBS) $(OTHER_LIBS) ALL_LIBS = $(GLEW_LIBS) $(GLFW_LIBS) $(OTHER_LIBS)
OBJ = matrixMath.o transformation.o wavefrontobj.o OBJ = matrixMath.o transformation.o wavefrontobj.o
SHADERS = fragmentShader.c vertexShader.c SHADERS = fragmentShader.c vertexShader.c skyboxVertexShader.c skyboxFragmentShader.c
cg1.out: test.out main.o $(OBJ) $(SHADERS) cg1.out: test.out main.o $(OBJ) $(SHADERS)
./test.out ./test.out

View File

@ -5,7 +5,6 @@ in vec3 fragmentPosition;
in vec2 textureCoordinate; in vec2 textureCoordinate;
flat in mat3 TBN; flat in mat3 TBN;
in vec3 skyboxCoord;
uniform float shininess; uniform float shininess;
@ -18,18 +17,12 @@ uniform sampler2D night;
uniform sampler2D clouds; uniform sampler2D clouds;
uniform sampler2D ocean; uniform sampler2D ocean;
uniform sampler2D normalMap; uniform sampler2D normalMap;
uniform samplerCube skyboxSampler;
uniform bool isSkybox;
float emissionStrength = 0.0; float emissionStrength = 0.0;
void main() { void main() {
if (isSkybox) {
// for skybox-texture
gl_FragColor = texture(skyboxSampler, skyboxCoord);
} else {
// for regular-textures // for regular-textures
vec4 color = vec4(texture(day, textureCoordinate).rgb, 1.0); vec4 color = vec4(texture(day, textureCoordinate).rgb, 1.0);
@ -62,5 +55,4 @@ void main() {
// SPECULAR // SPECULAR
specular * lightColor * color; specular * lightColor * color;
}
} }

View File

@ -3,7 +3,9 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "vertexShader.c" #include "vertexShader.c"
#include "skyboxVertexShader.c"
#include "fragmentShader.c" #include "fragmentShader.c"
#include "skyboxFragmentShader.c"
#include "sceneGraph.c" #include "sceneGraph.c"
#include "matrixMath.h" #include "matrixMath.h"
@ -21,6 +23,10 @@
GLuint program; GLuint program;
GLuint vao; GLuint vao;
// skybox data
GLuint skyboxProgram;
GLuint skyboxVAO, skyboxVBO;
GLuint skyboxTexture;
#define NUM_TEXTURES 5 #define NUM_TEXTURES 5
@ -60,15 +66,12 @@ const GLfloat pi = 3.14159f;
vec3 cameraPosition = {0.0f, 3.0f, 5.5f}; vec3 cameraPosition = {0.0f, 3.0f, 5.5f};
char* defaultModel = "../obj/monkey.obj"; char* defaultModel = "../obj/monkey.obj";
char* skyboxModel = "../obj/skybox.obj";
char* model; char* model;
// Define a global scene graph root node // Define a global scene graph root node
SceneNode* rootNode; SceneNode* rootNode;
// skybox date
GLuint skyboxVAO, skyboxVBO;
GLuint skyboxTexture;
// input handler for camera movement // input handler for camera movement
void handleInputs(double deltaTime) { void handleInputs(double deltaTime) {
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
@ -158,69 +161,76 @@ void loadCubemap(const char* faces[6], GLuint* textureID) {
} }
void initSkybox() { void initSkybox() {
float skyboxVertices[] = { // create and compile skybox vertex shader
// positions const GLchar *vertexTextConst = skyboxVertexShader_glsl;
-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, GLuint skyboxVertexShader = glCreateShader(GL_VERTEX_SHADER);
-1.0f, -1.0f, -1.0f, glShaderSource(skyboxVertexShader, 1, &vertexTextConst, &skyboxVertexShader_glsl_len);
-1.0f, 1.0f, -1.0f, glCompileShader(skyboxVertexShader);
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f, GLint status;
1.0f, -1.0f, 1.0f, glGetShaderiv(skyboxVertexShader, GL_COMPILE_STATUS, &status);
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, if (!status) {
-1.0f, 1.0f, 1.0f, printf("Error compiling skybox vertex shader: ");
1.0f, 1.0f, 1.0f, GLchar infoLog[1024];
1.0f, 1.0f, 1.0f, glGetShaderInfoLog(skyboxVertexShader, 1024, NULL, infoLog);
1.0f, -1.0f, 1.0f, printf("%s",infoLog);
-1.0f, -1.0f, 1.0f, exit(1);
}
-1.0f, 1.0f, -1.0f, vertexTextConst = NULL;
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
};
glGenVertexArrays(1, &skyboxVAO); // create and compile skybox fragment shader
glGenBuffers(1, &skyboxVBO); const GLchar *fragmentTextConst = skyboxFragmentShader_glsl;
glBindVertexArray(skyboxVAO);
glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glBindVertexArray(0);
const char* faces[6] = { GLuint skyboxFragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
"../texture/skybox/right.jpg", glShaderSource(skyboxFragmentShader, 1, &fragmentTextConst, &skyboxFragmentShader_glsl_len);
"../texture/skybox/left.jpg", glCompileShader(skyboxFragmentShader);
"../texture/skybox/top.jpg",
"../texture/skybox/bottom.jpg", glGetShaderiv(skyboxFragmentShader, GL_COMPILE_STATUS, &status);
"../texture/skybox/front.jpg",
"../texture/skybox/back.jpg" if (!status) {
}; printf("Error compiling skybox fragment shader: ");
loadCubemap(faces, &skyboxTexture); GLchar infoLog[1024];
glGetShaderInfoLog(skyboxFragmentShader, 1024, NULL, infoLog);
printf("%s",infoLog);
exit(1);
}
// create and link skybox shader program
skyboxProgram = glCreateProgram();
glAttachShader(skyboxProgram, skyboxVertexShader);
glAttachShader(skyboxProgram, skyboxFragmentShader);
glLinkProgram(skyboxProgram);
glGetProgramiv(skyboxProgram, GL_LINK_STATUS, &status);
if (!status) {
printf("Error linking skybox program: ");
GLchar infoLog[1024];
glGetProgramInfoLog(skyboxProgram, 1024, NULL, infoLog);
printf("%s",infoLog);
exit(1);
}
glValidateProgram(skyboxProgram);
glGetProgramiv(skyboxProgram, GL_VALIDATE_STATUS, &status);
if (!status) {
printf("Error validating skybox program: ");
GLchar infoLog[1024];
glGetProgramInfoLog(skyboxProgram, 1024, NULL, infoLog);
printf("%s",infoLog);
exit(1);
}
// Read Model Stuff einfügen
//TODO
//skyboxTexture = loadCubemap(faces); --> faces noch machen
} }
void renderSkybox(mat4* viewMatrix, mat4* projectionMatrix) { void renderSkybox(mat4* viewMatrix, mat4* projectionMatrix) {
@ -228,9 +238,9 @@ void renderSkybox(mat4* viewMatrix, mat4* projectionMatrix) {
glUseProgram(program); glUseProgram(program);
mat4 view = *viewMatrix; mat4 view = *viewMatrix;
view.m30 = 0.0f; view.m30 = 0.0f;
view.m31 = 0.0f; view.m31 = 0.0f;
view.m32 = 0.0f; view.m32 = 0.0f;
glUniformMatrix4fv(glGetUniformLocation(program, "view"), 1, GL_FALSE, (GLfloat*)&view); glUniformMatrix4fv(glGetUniformLocation(program, "view"), 1, GL_FALSE, (GLfloat*)&view);
glUniformMatrix4fv(glGetUniformLocation(program, "projection"), 1, GL_FALSE, (GLfloat*)projectionMatrix); glUniformMatrix4fv(glGetUniformLocation(program, "projection"), 1, GL_FALSE, (GLfloat*)projectionMatrix);
@ -266,7 +276,6 @@ void init(void) {
vertexTextConst = NULL; vertexTextConst = NULL;
// create and compile fragment shader // create and compile fragment shader
const GLchar *fragmentTextConst = fragmentShader_glsl; const GLchar *fragmentTextConst = fragmentShader_glsl;
@ -406,8 +415,8 @@ void init(void) {
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
// initialize skybox // initialize skybox
initSkybox(); initSkybox();
} }
void updateStats() { void updateStats() {
@ -552,8 +561,8 @@ void draw(void) {
// draw!!1 // draw!!1
glDrawArrays(GL_TRIANGLES, 0, numFaces * 3); glDrawArrays(GL_TRIANGLES, 0, numFaces * 3);
// draw skybox // draw skybox
renderSkybox(&viewingTransformation, &projectionTransformation); renderSkybox(&viewingTransformation, &projectionTransformation);
} }
// change viewport size and adjust aspect ratio when changing window size // change viewport size and adjust aspect ratio when changing window size
@ -606,7 +615,7 @@ int main(int argc, char **argv) {
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
} }
freeSceneNode(rootNode); freeSceneNode(rootNode);
glfwTerminate(); glfwTerminate();
return 0; return 0;

View File

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

View File

@ -0,0 +1,14 @@
#version 330 core
layout (location = 0) in vec3 aPos;
out vec3 TexCoords;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
void main(){
TexCoords = aPos;
vec4 pos = projection * view * model * vec4(aPos, 1.0);
gl_Position = pos.xyww;
}

View File

@ -7,26 +7,14 @@ layout (location = 3) in vec3 aTangent;
uniform mat4 modelView; uniform mat4 modelView;
uniform mat3 normalModelView; uniform mat3 normalModelView;
uniform mat4 projection; uniform mat4 projection;
uniform bool isSkybox;
out vec3 normal; out vec3 normal;
out vec3 fragmentPosition; out vec3 fragmentPosition;
out vec2 textureCoordinate; out vec2 textureCoordinate;
flat out mat3 TBN; flat out mat3 TBN;
out vec3 skyboxCoord;
void main() { void main() {
if (isSkybox) {
// for skybox
skyboxCoord = aPosition;
vec4 modelViewPos = modelView * vec4(aPosition, 1.0);
gl_Position = projection * vec4(aPosition, 1.0);
fragmentPosition = vec3(modelViewPos);
} else {
// for regular objects // for regular objects
textureCoordinate = aTextureCoordinate; textureCoordinate = aTextureCoordinate;
@ -47,5 +35,4 @@ void main() {
gl_Position = projection * modelViewPos; gl_Position = projection * modelViewPos;
fragmentPosition = vec3(modelViewPos); fragmentPosition = vec3(modelViewPos);
}
} }