From c26a4724f064af980e7e4b6fda2d9eb7c1147f7f Mon Sep 17 00:00:00 2001 From: Luca Conte Date: Sun, 23 Jun 2024 15:49:27 +0200 Subject: [PATCH] verschlimmbesserung und so --- src/main.c | 82 +++++++++++++++++++++++++++++++++---- src/skyboxVertexShader.glsl | 6 +-- 2 files changed, 78 insertions(+), 10 deletions(-) diff --git a/src/main.c b/src/main.c index 11109af..50ca9e3 100644 --- a/src/main.c +++ b/src/main.c @@ -47,7 +47,8 @@ char* textureFiles[NUM_TEXTURES] = { "../texture/earth/normal.png" }; -int numFaces = 0; +int numFaces = 0; +int skyboxNumFaces = 0; bool exitRequested = false; @@ -155,7 +156,7 @@ void loadSkyboxTexture() { data = stbi_load(faces[i], &width, &height, &nrChannels, 0); if (data) { glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, - 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); + 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); stbi_image_free(data); } else { printf("Failed to load texture %s\n", faces[i]); @@ -236,12 +237,78 @@ void initSkybox() { exit(1); } + ParsedObjFile skyboxObj = readObjFile(skyboxModel); + skyboxNumFaces = skyboxObj.length; + printf("skybox faces: %d\n", skyboxNumFaces); + + // write faces to buffer + GLuint triangleVertexBufferObject; + glGenBuffers(1, &triangleVertexBufferObject); + glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferObject); + glBufferData(GL_ARRAY_BUFFER, skyboxObj.length * sizeof(face), skyboxObj.faces, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + + // create vertex array object + glGenVertexArrays(1, &skyboxVAO); + glBindVertexArray(skyboxVAO); + glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferObject); + + // vertex positions + glVertexAttribPointer( + 0, + 3, + GL_FLOAT, + GL_FALSE, + sizeof(vertex), + 0 + ); + glEnableVertexAttribArray(0); + + // vertex normals + glVertexAttribPointer( + 1, + 3, + GL_FLOAT, + GL_FALSE, + sizeof(vertex), + (void*) offsetof(vertex, normal) + ); + glEnableVertexAttribArray(1); + + // vertex texture coordinates + glVertexAttribPointer( + 2, + 2, + GL_FLOAT, + GL_FALSE, + sizeof(vertex), + (void*) offsetof(vertex, texture) + ); + glEnableVertexAttribArray(2); + + // face tangents + glVertexAttribPointer( + 3, + 3, + GL_FLOAT, + GL_FALSE, + sizeof(vertex), + (void*) offsetof(vertex, tangent) + ); + glEnableVertexAttribArray(3); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + loadSkyboxTexture(); } void renderSkybox(mat4* viewMatrix, mat4* projectionMatrix) { - glDepthFunc(GL_LEQUAL); glUseProgram(skyboxProgram); + glDepthFunc(GL_LEQUAL); + glDepthMask(GL_FALSE); + glDisable(GL_CULL_FACE); mat4 view = *viewMatrix; view.m30 = 0.0f; @@ -251,9 +318,11 @@ void renderSkybox(mat4* viewMatrix, mat4* projectionMatrix) { glUniformMatrix4fv(glGetUniformLocation(skyboxProgram, "projection"), 1, GL_FALSE, (GLfloat*)projectionMatrix); glBindVertexArray(skyboxVAO); - glActiveTexture(GL_TEXTURE0); + + glUniform1i(glGetUniformLocation(skyboxProgram, "skybox"), 10); + glActiveTexture(GL_TEXTURE10); glBindTexture(GL_TEXTURE_CUBE_MAP, skyboxTexture); - glDrawArrays(GL_TRIANGLES, 0, 36); + glDrawArrays(GL_TRIANGLES, 0, skyboxNumFaces * 3); glBindVertexArray(0); glDepthFunc(GL_LESS); @@ -568,7 +637,7 @@ void draw(void) { glDrawArrays(GL_TRIANGLES, 0, numFaces * 3); // draw skybox - renderSkybox(&viewingTransformation, &projectionTransformation); + renderSkybox(&viewingTransformation, &projection); } // change viewport size and adjust aspect ratio when changing window size @@ -615,7 +684,6 @@ int main(int argc, char **argv) { printf("OpenGL version supported by this platform (%s):\n", glGetString(GL_VERSION)); init(); - initSkybox(); // exit when window should close or exit is requested (ESC) while (!glfwWindowShouldClose(window) && !exitRequested) { diff --git a/src/skyboxVertexShader.glsl b/src/skyboxVertexShader.glsl index afd9479..fdf8f12 100644 --- a/src/skyboxVertexShader.glsl +++ b/src/skyboxVertexShader.glsl @@ -1,14 +1,14 @@ #version 330 core layout (location = 0) in vec3 aPos; +layout (location = 2) in vec3 texPos; 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); + TexCoords = texPos; + vec4 pos = projection * view * vec4(aPos, 1.0); gl_Position = pos.xyww; } \ No newline at end of file