test commit yeet

This commit is contained in:
JonasJan2 2024-06-17 13:17:01 +02:00
parent e77be76490
commit d790a7f61c
11 changed files with 398 additions and 37 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

2
.idea/cg1_purple.iml Normal file
View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CIDR" type="CPP_MODULE" version="4" />

103
.idea/editor.xml Normal file
View File

@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="BackendCodeEditorSettings">
<option name="/Default/Housekeeping/GlobalSettingsUpgraded/IsUpgraded/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppClangFormat/EnableClangFormatSupport/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/EditorConfig/EnableClangFormatSupport/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_BINARY_EXPRESSIONS_CHAIN/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_CALLS_CHAIN/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_EXPRESSION/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_FOR_STMT/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTIPLE_DECLARATION/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_TERNARY/@EntryValue" value="ALIGN_ALL" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_CLASS_DEFINITION/@EntryValue" value="1" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/KEEP_BLANK_LINES_IN_DECLARATIONS/@EntryValue" value="2" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/KEEP_BLANK_LINES_IN_CODE/@EntryValue" value="2" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/KEEP_USER_LINEBREAKS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_CASE_FROM_SWITCH/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_COMMENT/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INT_ALIGN_EQ/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SIMPLE_BLOCK_STYLE/@EntryValue" value="DO_NOT_CHANGE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_COMMA_IN_TEMPLATE_ARGS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_COMMA_IN_TEMPLATE_PARAMS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_FOR_SEMICOLON/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_FOR_SEMICOLON/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_UNARY_OPERATOR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_ARRAY_ACCESS_BRACKETS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_CAST_EXPRESSION_PARENTHESES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_INITIALIZER_BRACES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_METHOD_PARENTHESES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_INITIALIZER_BRACES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPECIAL_ELSE_IF_TREATMENT/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_CAST_EXPRESSION_PARENTHESES/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_AFTER_BINARY_OPSIGN/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_TERNARY_OPSIGNS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/TYPE_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/OTHER_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/CASE_BLOCK_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_FUNCTION_DECLARATION/@EntryValue" value="1" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_FUNCTION_DEFINITION/@EntryValue" value="1" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/PLACE_WHILE_ON_NEW_LINE/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/PLACE_ELSE_ON_NEW_LINE/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/PLACE_CATCH_ON_NEW_LINE/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/NAMESPACE_INDENTATION/@EntryValue" value="All" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_ARGUMENT/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_EXTENDS_LIST/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_PARAMETER/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_TYPE_ARGUMENT/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_TYPE_PARAMETER/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_DECLARATIONS/@EntryValue" value="0" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_ACCESS_SPECIFIERS_FROM_CLASS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_CLASS_MEMBERS_FROM_ACCESS_SPECIFIERS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/LINE_BREAK_AFTER_COLON_IN_MEMBER_INITIALIZER_LISTS/@EntryValue" value="ON_SINGLE_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/MEMBER_INITIALIZER_LIST_STYLE/@EntryValue" value="DO_NOT_CHANGE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/PLACE_NAMESPACE_DEFINITIONS_ON_SAME_LINE/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_COLON_IN_BITFIELD_DECLARATOR/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_COLON_IN_BITFIELD_DECLARATOR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_EXTENDS_COLON/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_EXTENDS_COLON/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_FOR_COLON/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_FOR_COLON/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_PTR_IN_DATA_MEMBER/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_PTR_IN_DATA_MEMBERS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_PTR_IN_METHOD/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_PTR_IN_NESTED_DECLARATOR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_REF_IN_DATA_MEMBER/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_REF_IN_DATA_MEMBERS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_REF_IN_METHOD/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_PTR_IN_ABSTRACT_DECL/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_PTR_IN_DATA_MEMBER/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_PTR_IN_DATA_MEMBERS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_PTR_IN_METHOD/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_REF_IN_ABSTRACT_DECL/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_REF_IN_DATA_MEMBER/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_REF_IN_DATA_MEMBERS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_REF_IN_METHOD/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_TEMPLATE_ARGS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_TEMPLATE_PARAMS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BETWEEN_CLOSING_ANGLE_BRACKETS_IN_TEMPLATE_ARGS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_TEMPLATE_PARAMS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_TEMPLATE_ARGS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_TEMPLATE_PARAMS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_DECLARATION_PARENTHESES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_BLOCKS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_INVOCATION_LPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_AFTER_INVOCATION_LPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_INVOCATION_RPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_DECLARATION_LPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_AFTER_DECLARATION_LPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_DECLARATION_RPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_ARGUMENTS_STYLE/@EntryValue" value="WRAP_IF_LONG" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_PARAMETERS_STYLE/@EntryValue" value="WRAP_IF_LONG" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BREAK_TEMPLATE_DECLARATION/@EntryValue" value="LINE_BREAK" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/NAMESPACE_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/FREE_BLOCK_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INVOCABLE_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ANONYMOUS_METHOD_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INITIALIZER_BRACES/@EntryValue" value="END_OF_LINE_NO_SPACE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_STYLE/@EntryValue" value="Space" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_SIZE/@EntryValue" value="4" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/CONTINUOUS_LINE_INDENT/@EntryValue" value="Double" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/TAB_WIDTH/@EntryValue" value="4" type="int" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/cg1_purple.iml" filepath="$PROJECT_DIR$/.idea/cg1_purple.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -5,7 +5,7 @@ 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,41 +18,49 @@ 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() {
vec4 color = vec4(texture(day, textureCoordinate).rgb, 1.0); if (isSkybox) {
// for skybox-texture
gl_FragColor = texture(skyboxSampler, skyboxCoord);
} else {
// for regular-textures
vec4 color = vec4(texture(day, textureCoordinate).rgb, 1.0);
float nightBrightness = texture(night, textureCoordinate).r; float nightBrightness = texture(night, textureCoordinate).r;
vec4 nightColor = vec4(nightBrightness, nightBrightness * 0.7, nightBrightness * 0.5, 1.0); vec4 nightColor = vec4(nightBrightness, nightBrightness * 0.7, nightBrightness * 0.5, 1.0);
float shininessMultiplier = texture(ocean, textureCoordinate).r; float shininessMultiplier = texture(ocean, textureCoordinate).r;
vec4 cloudColor = texture(clouds, textureCoordinate).rgba; vec4 cloudColor = texture(clouds, textureCoordinate).rgba;
vec3 norm = normalize(normal); vec3 norm = normalize(normal);
vec3 lightDir = normalize(lightPosition - fragmentPosition); vec3 lightDir = normalize(lightPosition - fragmentPosition);
vec3 eyeDir = (-normalize(fragmentPosition)); vec3 eyeDir = (-normalize(fragmentPosition));
float diff = max(dot(norm, lightDir), 0.0); float diff = max(dot(norm, lightDir), 0.0);
vec3 halfway = (lightDir + eyeDir) / length(lightDir + eyeDir); vec3 halfway = (lightDir + eyeDir) / length(lightDir + eyeDir);
float specular = pow(max(dot(halfway, norm), 0.0), shininess) * shininessMultiplier; float specular = pow(max(dot(halfway, norm), 0.0), shininess) * shininessMultiplier;
gl_FragColor = gl_FragColor =
// EMISSION // EMISSION
color * emissionStrength + color * emissionStrength +
// // AMBIENT // // AMBIENT
ambientLight * color + ambientLight * color +
// DIFFUSION // DIFFUSION
mix(nightColor * (vec4(1,1,1,1) - cloudColor) + (cloudColor * ambientLight), color + cloudColor, diff) + mix(nightColor * (vec4(1,1,1,1) - cloudColor) + (cloudColor * ambientLight), color + cloudColor, diff) +
// SPECULAR // SPECULAR
specular * lightColor * color; specular * lightColor * color;
}
} }

View File

@ -1,5 +1,4 @@
#include <stdio.h> #include <stdio.h>
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
@ -9,6 +8,7 @@
#include "matrixMath.h" #include "matrixMath.h"
#include "transformation.h" #include "transformation.h"
#include "wavefrontobj.h" #include "wavefrontobj.h"
#include "sceneGraph.h"
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "../lib/stb_image.h" #include "../lib/stb_image.h"
@ -61,6 +61,13 @@ vec3 cameraPosition = {0.0f, 3.0f, 5.5f};
char* defaultModel = "../obj/monkey.obj"; char* defaultModel = "../obj/monkey.obj";
char* model; char* model;
// Define a global scene graph root node
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) {
@ -92,6 +99,12 @@ void keyboardHandler(GLFWwindow* window, int key, int scancode, int action, int
} }
} }
// Define the function to render a node (e.g., rendering an object)
void renderNode(SceneNode* node) {
glUniformMatrix4fv(glGetUniformLocation(program, "modelView"), 1, GL_FALSE, (GLfloat*)&node->worldTransformation);
glDrawArrays(GL_TRIANGLES, 0, numFaces * 3);
}
void loadTexture(char* textureFile, GLuint* texture) { void loadTexture(char* textureFile, GLuint* texture) {
int width, height, nrChannels; int width, height, nrChannels;
unsigned char* image = stbi_load(textureFile, &width, &height, &nrChannels, 0); unsigned char* image = stbi_load(textureFile, &width, &height, &nrChannels, 0);
@ -120,6 +133,115 @@ void loadTexture(char* textureFile, GLuint* texture) {
stbi_image_free(image); stbi_image_free(image);
} }
void loadCubemap(const char* faces[6], GLuint* textureID) {
glGenTextures(1, textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, *textureID);
int width, height, nrChannels;
for (unsigned int i = 0; i < 6; i++) {
unsigned char* 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);
stbi_image_free(data);
} else {
printf("Cubemap texture failed to load at path: %s\n", faces[i]);
stbi_image_free(data);
}
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
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);
}
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,
-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
};
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);
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);
}
void renderSkybox(mat4* viewMatrix, mat4* projectionMatrix) {
glDepthFunc(GL_LEQUAL);
glUseProgram(program);
mat4 view = *viewMatrix;
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);
glBindVertexArray(skyboxVAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, skyboxTexture);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glDepthFunc(GL_LESS);
}
void init(void) { void init(void) {
// create and compile vertex shader // create and compile vertex shader
const GLchar *vertexTextConst = vertexShader_glsl; const GLchar *vertexTextConst = vertexShader_glsl;
@ -262,6 +384,17 @@ void init(void) {
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);
// Create the root scene node
rootNode = createSceneNode();
// Create a child node (e.g., for the model)
SceneNode* modelNode = createSceneNode();
modelNode->render = renderNode;
addChild(rootNode, modelNode);
// Set transformations for the modelNode (example)
rotateY(&modelNode->transformation, &modelNode->transformation, pi / 4);
// ENABLE BACKFACE CULLING // ENABLE BACKFACE CULLING
glFrontFace(GL_CCW); glFrontFace(GL_CCW);
@ -271,6 +404,9 @@ void init(void) {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
// initialize skybox
initSkybox();
} }
void updateStats() { void updateStats() {
@ -297,9 +433,15 @@ void draw(void) {
handleInputs(deltaTime); handleInputs(deltaTime);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program); glUseProgram(program);
glBindVertexArray(vao); glBindVertexArray(vao);
// Update and render the scene graph
mat4 identityMatrix;
identity(&identityMatrix);
updateSceneNode(rootNode, &identityMatrix);
renderSceneNode(rootNode);
// step for rotations // step for rotations
// counts up to 1.0 and then resets back to 0.0 forever // counts up to 1.0 and then resets back to 0.0 forever
step += deltaTime / 15; step += deltaTime / 15;
@ -408,6 +550,9 @@ void draw(void) {
// draw!!1 // draw!!1
glDrawArrays(GL_TRIANGLES, 0, numFaces * 3); glDrawArrays(GL_TRIANGLES, 0, numFaces * 3);
// draw skybox
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
@ -457,11 +602,10 @@ int main(int argc, char **argv) {
// exit when window should close or exit is requested (ESC) // exit when window should close or exit is requested (ESC)
while (!glfwWindowShouldClose(window) && !exitRequested) { while (!glfwWindowShouldClose(window) && !exitRequested) {
draw(); draw();
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
} }
freeSceneNode(rootNode);
glfwTerminate(); glfwTerminate();
return 0; return 0;

44
src/sceneGraph.c Normal file
View File

@ -0,0 +1,44 @@
// sceneGraph.c
#include "sceneGraph.h"
#include <stdlib.h>
SceneNode* createSceneNode() {
SceneNode* node = (SceneNode*)malloc(sizeof(SceneNode));
identity(&node->transformation);
identity(&node->worldTransformation);
node->render = NULL;
node->children = NULL;
node->numChildren = 0;
return node;
}
void addChild(SceneNode* parent, SceneNode* child) {
parent->children = (SceneNode**)realloc(parent->children, sizeof(SceneNode*) * (parent->numChildren + 1));
parent->children[parent->numChildren] = child;
parent->numChildren++;
}
void updateSceneNode(SceneNode* node, mat4* parentTransformation) {
multiply(&node->worldTransformation, parentTransformation, &node->transformation);
for (int i = 0; i < node->numChildren; i++) {
updateSceneNode(node->children[i], &node->worldTransformation);
}
}
void renderSceneNode(SceneNode* node) {
if (node->render) {
node->render(node);
}
for (int i = 0; i < node->numChildren; i++) {
renderSceneNode(node->children[i]);
}
}
void freeSceneNode(SceneNode* node) {
for (int i = 0; i < node->numChildren; i++) {
freeSceneNode(node->children[i]);
}
free(node->children);
free(node);
}

23
src/sceneGraph.h Normal file
View File

@ -0,0 +1,23 @@
// sceneGraph.h
#ifndef SCENE_GRAPH_H
#define SCENE_GRAPH_H
#include <GL/glew.h>
#include "matrixMath.h"
typedef struct SceneNode {
mat4 transformation; // Local transformation matrix
mat4 worldTransformation; // World transformation matrix
void (*render)(struct SceneNode*); // Function pointer to render this node
struct SceneNode** children; // Array of pointers to child nodes
int numChildren; // Number of child nodes
} SceneNode;
SceneNode* createSceneNode();
void addChild(SceneNode* parent, SceneNode* child);
void updateSceneNode(SceneNode* node, mat4* parentTransformation);
void renderSceneNode(SceneNode* node);
void freeSceneNode(SceneNode* node);
#endif

View File

@ -7,30 +7,45 @@ 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() {
textureCoordinate = aTextureCoordinate; if (isSkybox) {
// for skybox
skyboxCoord = aPosition;
vec3 tangent = normalize(normalModelView * aTangent); vec4 modelViewPos = modelView * vec4(aPosition, 1.0);
normal = normalize(normalModelView * aNormal);
vec3 bitangent = normalize(cross(normal, tangent)); gl_position = projection * vec4(aPosition, 1.0);
TBN = transpose(mat3( fragmentPosition = vec3(modelViewPos);
tangent, } else {
bitangent, // for regular objects
normal textureCoordinate = aTextureCoordinate;
));
vec3 tangent = normalize(normalModelView * aTangent);
normal = normalize(normalModelView * aNormal);
vec3 bitangent = normalize(cross(normal, tangent));
TBN = transpose(mat3(
tangent,
bitangent,
normal
));
vec4 modelViewPos = modelView * vec4(aPosition, 1.0); vec4 modelViewPos = modelView * vec4(aPosition, 1.0);
gl_Position = projection * modelViewPos; gl_Position = projection * modelViewPos;
fragmentPosition = vec3(modelViewPos); fragmentPosition = vec3(modelViewPos);
}
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 MiB