diff --git a/obj/skybox.mtl b/obj/skybox.mtl new file mode 100644 index 0000000..2b7f4dd --- /dev/null +++ b/obj/skybox.mtl @@ -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 \ No newline at end of file diff --git a/obj/skybox.obj b/obj/skybox.obj new file mode 100644 index 0000000..65f9144 --- /dev/null +++ b/obj/skybox.obj @@ -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 \ No newline at end of file diff --git a/src/Makefile b/src/Makefile index 1ea898c..b05d374 100644 --- a/src/Makefile +++ b/src/Makefile @@ -5,7 +5,7 @@ OTHER_LIBS = -lm ALL_LIBS = $(GLEW_LIBS) $(GLFW_LIBS) $(OTHER_LIBS) 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) ./test.out diff --git a/src/fragmentShader.glsl b/src/fragmentShader.glsl index 92c8570..f72245d 100644 --- a/src/fragmentShader.glsl +++ b/src/fragmentShader.glsl @@ -5,7 +5,6 @@ in vec3 fragmentPosition; in vec2 textureCoordinate; flat in mat3 TBN; -in vec3 skyboxCoord; uniform float shininess; @@ -18,18 +17,12 @@ uniform sampler2D night; uniform sampler2D clouds; uniform sampler2D ocean; uniform sampler2D normalMap; -uniform samplerCube skyboxSampler; -uniform bool isSkybox; float emissionStrength = 0.0; void main() { - if (isSkybox) { - // for skybox-texture - gl_FragColor = texture(skyboxSampler, skyboxCoord); - } else { // for regular-textures vec4 color = vec4(texture(day, textureCoordinate).rgb, 1.0); @@ -62,5 +55,4 @@ void main() { // SPECULAR specular * lightColor * color; - } } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 8e8bcb0..c7030a9 100644 --- a/src/main.c +++ b/src/main.c @@ -3,7 +3,9 @@ #include #include "vertexShader.c" +#include "skyboxVertexShader.c" #include "fragmentShader.c" +#include "skyboxFragmentShader.c" #include "sceneGraph.c" #include "matrixMath.h" @@ -21,6 +23,10 @@ GLuint program; GLuint vao; +// skybox data +GLuint skyboxProgram; +GLuint skyboxVAO, skyboxVBO; +GLuint skyboxTexture; #define NUM_TEXTURES 5 @@ -60,15 +66,12 @@ const GLfloat pi = 3.14159f; vec3 cameraPosition = {0.0f, 3.0f, 5.5f}; char* defaultModel = "../obj/monkey.obj"; +char* skyboxModel = "../obj/skybox.obj"; char* model; // Define a global scene graph root node SceneNode* rootNode; -// skybox date -GLuint skyboxVAO, skyboxVBO; -GLuint skyboxTexture; - // input handler for camera movement void handleInputs(double deltaTime) { if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { @@ -115,7 +118,7 @@ void loadTexture(char* textureFile, GLuint* texture) { GLenum channelFormats[] = { 0, GL_RED, - GL_RG, + GL_RG, GL_RGB, GL_RGBA }; @@ -158,69 +161,76 @@ void loadCubemap(const char* faces[6], GLuint* textureID) { } void initSkybox() { - float skyboxVertices[] = { - // 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, +// create and compile skybox vertex shader + 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, + GLuint skyboxVertexShader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(skyboxVertexShader, 1, &vertexTextConst, &skyboxVertexShader_glsl_len); + 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, - 1.0f, 1.0f, -1.0f, - 1.0f, -1.0f, -1.0f, + GLint status; + 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, - -1.0f, -1.0f, 1.0f, + if (!status) { + printf("Error compiling skybox vertex shader: "); + GLchar infoLog[1024]; + glGetShaderInfoLog(skyboxVertexShader, 1024, NULL, infoLog); + printf("%s",infoLog); + exit(1); + } - -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, + 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 - }; - glGenVertexArrays(1, &skyboxVAO); - glGenBuffers(1, &skyboxVBO); - 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); + // create and compile skybox fragment shader + const GLchar *fragmentTextConst = skyboxFragmentShader_glsl; - const char* faces[6] = { - "../texture/skybox/right.jpg", - "../texture/skybox/left.jpg", - "../texture/skybox/top.jpg", - "../texture/skybox/bottom.jpg", - "../texture/skybox/front.jpg", - "../texture/skybox/back.jpg" - }; - loadCubemap(faces, &skyboxTexture); + GLuint skyboxFragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(skyboxFragmentShader, 1, &fragmentTextConst, &skyboxFragmentShader_glsl_len); + glCompileShader(skyboxFragmentShader); + + glGetShaderiv(skyboxFragmentShader, GL_COMPILE_STATUS, &status); + + if (!status) { + printf("Error compiling skybox fragment shader: "); + 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) { @@ -228,9 +238,9 @@ void renderSkybox(mat4* viewMatrix, mat4* projectionMatrix) { glUseProgram(program); mat4 view = *viewMatrix; - view.m30 = 0.0f; - view.m31 = 0.0f; - view.m32 = 0.0f; + view.m30 = 0.0f; + view.m31 = 0.0f; + view.m32 = 0.0f; glUniformMatrix4fv(glGetUniformLocation(program, "view"), 1, GL_FALSE, (GLfloat*)&view); glUniformMatrix4fv(glGetUniformLocation(program, "projection"), 1, GL_FALSE, (GLfloat*)projectionMatrix); @@ -266,7 +276,6 @@ void init(void) { vertexTextConst = NULL; - // create and compile fragment shader const GLchar *fragmentTextConst = fragmentShader_glsl; @@ -406,8 +415,8 @@ void init(void) { glClearColor(0.1f, 0.1f, 0.1f, 1.0f); - // initialize skybox - initSkybox(); + // initialize skybox + initSkybox(); } void updateStats() { @@ -552,8 +561,8 @@ void draw(void) { // draw!!1 glDrawArrays(GL_TRIANGLES, 0, numFaces * 3); - // draw skybox - renderSkybox(&viewingTransformation, &projectionTransformation); + // draw skybox + renderSkybox(&viewingTransformation, &projectionTransformation); } // change viewport size and adjust aspect ratio when changing window size @@ -606,7 +615,7 @@ int main(int argc, char **argv) { glfwSwapBuffers(window); glfwPollEvents(); } - freeSceneNode(rootNode); + freeSceneNode(rootNode); glfwTerminate(); return 0; diff --git a/src/skyboxFragmentShader.glsl b/src/skyboxFragmentShader.glsl new file mode 100644 index 0000000..1a84fd4 --- /dev/null +++ b/src/skyboxFragmentShader.glsl @@ -0,0 +1,10 @@ +#version 330 core +out vec4 FragColor; + +in vec3 TexCoords; + +uniform samplerCube skybox; + +void main(){ + FragColor = texture(skybox, TexCoords); +} \ No newline at end of file diff --git a/src/skyboxVertexShader.glsl b/src/skyboxVertexShader.glsl new file mode 100644 index 0000000..afd9479 --- /dev/null +++ b/src/skyboxVertexShader.glsl @@ -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; +} \ No newline at end of file diff --git a/src/vertexShader.glsl b/src/vertexShader.glsl index b8c6f87..cddb233 100644 --- a/src/vertexShader.glsl +++ b/src/vertexShader.glsl @@ -7,26 +7,14 @@ layout (location = 3) in vec3 aTangent; uniform mat4 modelView; uniform mat3 normalModelView; uniform mat4 projection; -uniform bool isSkybox; out vec3 normal; out vec3 fragmentPosition; out vec2 textureCoordinate; flat out mat3 TBN; -out vec3 skyboxCoord; 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 textureCoordinate = aTextureCoordinate; @@ -47,5 +35,4 @@ void main() { gl_Position = projection * modelViewPos; fragmentPosition = vec3(modelViewPos); - } } \ No newline at end of file