verschlimmbesserung und so

This commit is contained in:
Luca Conte 2024-06-23 15:49:27 +02:00
parent 9f73463a13
commit c26a4724f0
2 changed files with 78 additions and 10 deletions

View File

@ -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) {

View File

@ -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;
}