diff --git a/includes/learnopengl/camera.h b/includes/learnopengl/camera.h index 5c97bde..896e986 100644 --- a/includes/learnopengl/camera.h +++ b/includes/learnopengl/camera.h @@ -16,11 +16,11 @@ enum Camera_Movement { }; // Default camera values -const GLfloat YAW = -90.0f; -const GLfloat PITCH = 0.0f; -const GLfloat SPEED = 2.5f; -const GLfloat SENSITIVTY = 0.1f; -const GLfloat ZOOM = 45.0f; +const float YAW = -90.0f; +const float PITCH = 0.0f; +const float SPEED = 2.5f; +const float SENSITIVTY = 0.1f; +const float ZOOM = 45.0f; // An abstract camera class that processes input and calculates the corresponding Eular Angles, Vectors and Matrices for use in OpenGL @@ -34,83 +34,83 @@ public: glm::vec3 Right; glm::vec3 WorldUp; // Eular Angles - GLfloat Yaw; - GLfloat Pitch; + float Yaw; + float Pitch; // Camera options - GLfloat MovementSpeed; - GLfloat MouseSensitivity; - GLfloat Zoom; + float MovementSpeed; + float MouseSensitivity; + float Zoom; // Constructor with vectors - Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), GLfloat yaw = YAW, GLfloat pitch = PITCH) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVTY), Zoom(ZOOM) + Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), float yaw = YAW, float pitch = PITCH) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVTY), Zoom(ZOOM) { - this->Position = position; - this->WorldUp = up; - this->Yaw = yaw; - this->Pitch = pitch; - this->updateCameraVectors(); + Position = position; + WorldUp = up; + Yaw = yaw; + Pitch = pitch; + updateCameraVectors(); } // Constructor with scalar values - Camera(GLfloat posX, GLfloat posY, GLfloat posZ, GLfloat upX, GLfloat upY, GLfloat upZ, GLfloat yaw, GLfloat pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVTY), Zoom(ZOOM) + Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVTY), Zoom(ZOOM) { - this->Position = glm::vec3(posX, posY, posZ); - this->WorldUp = glm::vec3(upX, upY, upZ); - this->Yaw = yaw; - this->Pitch = pitch; - this->updateCameraVectors(); + Position = glm::vec3(posX, posY, posZ); + WorldUp = glm::vec3(upX, upY, upZ); + Yaw = yaw; + Pitch = pitch; + updateCameraVectors(); } // Returns the view matrix calculated using Eular Angles and the LookAt Matrix glm::mat4 GetViewMatrix() { - return glm::lookAt(this->Position, this->Position + this->Front, this->Up); + return glm::lookAt(Position, Position + Front, Up); } // Processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems) - void ProcessKeyboard(Camera_Movement direction, GLfloat deltaTime) + void ProcessKeyboard(Camera_Movement direction, float deltaTime) { - GLfloat velocity = this->MovementSpeed * deltaTime; + float velocity = MovementSpeed * deltaTime; if (direction == FORWARD) - this->Position += this->Front * velocity; + Position += Front * velocity; if (direction == BACKWARD) - this->Position -= this->Front * velocity; + Position -= Front * velocity; if (direction == LEFT) - this->Position -= this->Right * velocity; + Position -= Right * velocity; if (direction == RIGHT) - this->Position += this->Right * velocity; + Position += Right * velocity; } // Processes input received from a mouse input system. Expects the offset value in both the x and y direction. - void ProcessMouseMovement(GLfloat xoffset, GLfloat yoffset, GLboolean constrainPitch = true) + void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true) { - xoffset *= this->MouseSensitivity; - yoffset *= this->MouseSensitivity; + xoffset *= MouseSensitivity; + yoffset *= MouseSensitivity; - this->Yaw += xoffset; - this->Pitch += yoffset; + Yaw += xoffset; + Pitch += yoffset; // Make sure that when pitch is out of bounds, screen doesn't get flipped if (constrainPitch) { - if (this->Pitch > 89.0f) - this->Pitch = 89.0f; - if (this->Pitch < -89.0f) - this->Pitch = -89.0f; + if (Pitch > 89.0f) + Pitch = 89.0f; + if (Pitch < -89.0f) + Pitch = -89.0f; } // Update Front, Right and Up Vectors using the updated Eular angles - this->updateCameraVectors(); + updateCameraVectors(); } // Processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis - void ProcessMouseScroll(GLfloat yoffset) + void ProcessMouseScroll(float yoffset) { - if (this->Zoom >= 1.0f && this->Zoom <= 45.0f) - this->Zoom -= yoffset; - if (this->Zoom <= 1.0f) - this->Zoom = 1.0f; - if (this->Zoom >= 45.0f) - this->Zoom = 45.0f; + if (Zoom >= 1.0f && Zoom <= 45.0f) + Zoom -= yoffset; + if (Zoom <= 1.0f) + Zoom = 1.0f; + if (Zoom >= 45.0f) + Zoom = 45.0f; } private: @@ -119,13 +119,13 @@ private: { // Calculate the new Front vector glm::vec3 front; - front.x = cos(glm::radians(this->Yaw)) * cos(glm::radians(this->Pitch)); - front.y = sin(glm::radians(this->Pitch)); - front.z = sin(glm::radians(this->Yaw)) * cos(glm::radians(this->Pitch)); - this->Front = glm::normalize(front); + front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch)); + front.y = sin(glm::radians(Pitch)); + front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch)); + Front = glm::normalize(front); // Also re-calculate the Right and Up vector - this->Right = glm::normalize(glm::cross(this->Front, this->WorldUp)); // Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement. - this->Up = glm::normalize(glm::cross(this->Right, this->Front)); + Right = glm::normalize(glm::cross(Front, WorldUp)); // Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement. + Up = glm::normalize(glm::cross(Right, Front)); } }; #endif \ No newline at end of file diff --git a/src/1.getting_started/4.3.textures_exercise2/4.3.texture.fs b/src/1.getting_started/4.3.textures_exercise2/4.3.texture.fs new file mode 100644 index 0000000..9fef202 --- /dev/null +++ b/src/1.getting_started/4.3.textures_exercise2/4.3.texture.fs @@ -0,0 +1,15 @@ +#version 330 core +out vec4 FragColor; + +in vec3 ourColor; +in vec2 TexCoord; + +// texture samplers +uniform sampler2D texture1; +uniform sampler2D texture2; + +void main() +{ + // linearly interpolate between both textures (80% container, 20% awesomeface) + FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2); +} \ No newline at end of file diff --git a/src/1.getting_started/4.3.textures_exercise2/4.3.texture.vs b/src/1.getting_started/4.3.textures_exercise2/4.3.texture.vs new file mode 100644 index 0000000..4fd6603 --- /dev/null +++ b/src/1.getting_started/4.3.textures_exercise2/4.3.texture.vs @@ -0,0 +1,14 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aColor; +layout (location = 2) in vec2 aTexCoord; + +out vec3 ourColor; +out vec2 TexCoord; + +void main() +{ + gl_Position = vec4(aPos, 1.0); + ourColor = aColor; + TexCoord = vec2(aTexCoord.x, aTexCoord.y); +} \ No newline at end of file diff --git a/src/1.getting_started/4.3.textures_exercise2/textures_exercise2.cpp b/src/1.getting_started/4.3.textures_exercise2/textures_exercise2.cpp index 4c3936c..d4a5d2a 100644 --- a/src/1.getting_started/4.3.textures_exercise2/textures_exercise2.cpp +++ b/src/1.getting_started/4.3.textures_exercise2/textures_exercise2.cpp @@ -46,7 +46,7 @@ int main() // build and compile our shader zprogram // ------------------------------------ - Shader ourShader("4.2.texture_combined.vs", "4.2.texture_combined.fs"); + Shader ourShader("4.3.texture.vs", "4.3.texture.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ diff --git a/src/1.getting_started/4.4.textures_exercise3/4.4.texture.fs b/src/1.getting_started/4.4.textures_exercise3/4.4.texture.fs new file mode 100644 index 0000000..9fef202 --- /dev/null +++ b/src/1.getting_started/4.4.textures_exercise3/4.4.texture.fs @@ -0,0 +1,15 @@ +#version 330 core +out vec4 FragColor; + +in vec3 ourColor; +in vec2 TexCoord; + +// texture samplers +uniform sampler2D texture1; +uniform sampler2D texture2; + +void main() +{ + // linearly interpolate between both textures (80% container, 20% awesomeface) + FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2); +} \ No newline at end of file diff --git a/src/1.getting_started/4.4.textures_exercise3/4.4.texture.vs b/src/1.getting_started/4.4.textures_exercise3/4.4.texture.vs new file mode 100644 index 0000000..4fd6603 --- /dev/null +++ b/src/1.getting_started/4.4.textures_exercise3/4.4.texture.vs @@ -0,0 +1,14 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aColor; +layout (location = 2) in vec2 aTexCoord; + +out vec3 ourColor; +out vec2 TexCoord; + +void main() +{ + gl_Position = vec4(aPos, 1.0); + ourColor = aColor; + TexCoord = vec2(aTexCoord.x, aTexCoord.y); +} \ No newline at end of file diff --git a/src/1.getting_started/4.4.textures_exercise3/textures_exercise3.cpp b/src/1.getting_started/4.4.textures_exercise3/textures_exercise3.cpp index 419e716..53aeb85 100644 --- a/src/1.getting_started/4.4.textures_exercise3/textures_exercise3.cpp +++ b/src/1.getting_started/4.4.textures_exercise3/textures_exercise3.cpp @@ -46,7 +46,7 @@ int main() // build and compile our shader zprogram // ------------------------------------ - Shader ourShader("4.2.texture_combined.vs", "4.2.texture_combined.fs"); + Shader ourShader("4.4.texture.vs", "4.4.texture.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ diff --git a/src/1.getting_started/5.2.transformations_exercise2/5.2.transform.fs b/src/1.getting_started/5.2.transformations_exercise2/5.2.transform.fs new file mode 100644 index 0000000..44839bc --- /dev/null +++ b/src/1.getting_started/5.2.transformations_exercise2/5.2.transform.fs @@ -0,0 +1,14 @@ +#version 330 core +out vec4 FragColor; + +in vec2 TexCoord; + +// texture samplers +uniform sampler2D texture1; +uniform sampler2D texture2; + +void main() +{ + // linearly interpolate between both textures (80% container, 20% awesomeface) + FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2); +} \ No newline at end of file diff --git a/src/1.getting_started/5.2.transformations_exercise2/5.2.transform.vs b/src/1.getting_started/5.2.transformations_exercise2/5.2.transform.vs new file mode 100644 index 0000000..709bef9 --- /dev/null +++ b/src/1.getting_started/5.2.transformations_exercise2/5.2.transform.vs @@ -0,0 +1,13 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec2 aTexCoord; + +out vec2 TexCoord; + +uniform mat4 transform; + +void main() +{ + gl_Position = transform * vec4(aPos, 1.0); + TexCoord = vec2(aTexCoord.x, aTexCoord.y); +} \ No newline at end of file diff --git a/src/1.getting_started/5.2.transformations_exercise2/transformations_exercise2.cpp b/src/1.getting_started/5.2.transformations_exercise2/transformations_exercise2.cpp index ea4af65..e9c6d33 100644 --- a/src/1.getting_started/5.2.transformations_exercise2/transformations_exercise2.cpp +++ b/src/1.getting_started/5.2.transformations_exercise2/transformations_exercise2.cpp @@ -50,7 +50,7 @@ int main() // build and compile our shader zprogram // ------------------------------------ - Shader ourShader("5.1.transform.vs", "5.1.transform.fs"); + Shader ourShader("5.2.transform.vs", "5.2.transform.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ diff --git a/src/1.getting_started/6.1.coordinate_systems/coordinate_systems.cpp b/src/1.getting_started/6.1.coordinate_systems/coordinate_systems.cpp index 90fd2cd..cba3bd7 100644 --- a/src/1.getting_started/6.1.coordinate_systems/coordinate_systems.cpp +++ b/src/1.getting_started/6.1.coordinate_systems/coordinate_systems.cpp @@ -172,7 +172,7 @@ int main() glm::mat4 projection; model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f)); view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f)); - projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f); + projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); // retrieve the matrix uniform locations unsigned int modelLoc = glGetUniformLocation(ourShader.ID, "model"); unsigned int viewLoc = glGetUniformLocation(ourShader.ID, "view"); diff --git a/src/1.getting_started/6.2.coordinate_systems_depth/coordinate_systems_depth.cpp b/src/1.getting_started/6.2.coordinate_systems_depth/coordinate_systems_depth.cpp index ee2e08e..8af6c3f 100644 --- a/src/1.getting_started/6.2.coordinate_systems_depth/coordinate_systems_depth.cpp +++ b/src/1.getting_started/6.2.coordinate_systems_depth/coordinate_systems_depth.cpp @@ -204,7 +204,7 @@ int main() glm::mat4 projection; model = glm::rotate(model, (float)glfwGetTime(), glm::vec3(0.5f, 1.0f, 0.0f)); view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f)); - projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f); + projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); // retrieve the matrix uniform locations unsigned int modelLoc = glGetUniformLocation(ourShader.ID, "model"); unsigned int viewLoc = glGetUniformLocation(ourShader.ID, "view"); diff --git a/src/1.getting_started/6.3.coordinate_systems_multiple/coordinate_systems_multiple.cpp b/src/1.getting_started/6.3.coordinate_systems_multiple/coordinate_systems_multiple.cpp index 8ddb8b6..1a41613 100644 --- a/src/1.getting_started/6.3.coordinate_systems_multiple/coordinate_systems_multiple.cpp +++ b/src/1.getting_started/6.3.coordinate_systems_multiple/coordinate_systems_multiple.cpp @@ -214,7 +214,7 @@ int main() // create transformations glm::mat4 view; glm::mat4 projection; - projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f); + projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f)); // pass transformation matrices to the shader ourShader.setMat4("projection", projection); // note: currently we set the projection matrix each frame, but since the projection matrix rarely changes it's often best practice to set it outside the main loop only once. diff --git a/src/1.getting_started/7.1.camera_circle/7.1.camera.fs b/src/1.getting_started/7.1.camera_circle/7.1.camera.fs index 43d8561..44839bc 100644 --- a/src/1.getting_started/7.1.camera_circle/7.1.camera.fs +++ b/src/1.getting_started/7.1.camera_circle/7.1.camera.fs @@ -1,7 +1,7 @@ #version 330 core -out vec4 fragColor; +out vec4 FragColor; -in vec2 texCoord; +in vec2 TexCoord; // texture samplers uniform sampler2D texture1; @@ -10,5 +10,5 @@ uniform sampler2D texture2; void main() { // linearly interpolate between both textures (80% container, 20% awesomeface) - fragColor = mix(texture(texture1, texCoord), texture(texture2, texCoord), 0.2); + FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2); } \ No newline at end of file diff --git a/src/1.getting_started/7.1.camera_circle/7.1.camera.vs b/src/1.getting_started/7.1.camera_circle/7.1.camera.vs index 1e26e0d..e91af6d 100644 --- a/src/1.getting_started/7.1.camera_circle/7.1.camera.vs +++ b/src/1.getting_started/7.1.camera_circle/7.1.camera.vs @@ -2,7 +2,7 @@ layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; -out vec2 texCoord; +out vec2 TexCoord; uniform mat4 model; uniform mat4 view; @@ -11,5 +11,5 @@ uniform mat4 projection; void main() { gl_Position = projection * view * model * vec4(aPos, 1.0f); - texCoord = vec2(aTexCoord.x, aTexCoord.y); + TexCoord = vec2(aTexCoord.x, aTexCoord.y); } \ No newline at end of file diff --git a/src/1.getting_started/7.1.camera_circle/camera_circle.cpp b/src/1.getting_started/7.1.camera_circle/camera_circle.cpp index b6b36d9..60074b6 100644 --- a/src/1.getting_started/7.1.camera_circle/camera_circle.cpp +++ b/src/1.getting_started/7.1.camera_circle/camera_circle.cpp @@ -14,6 +14,10 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height); void processInput(GLFWwindow *window); +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + int main() { // glfw: initialize and configure @@ -26,14 +30,14 @@ int main() // glfw window creation // -------------------- - GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); - glfwMakeContextCurrent(window); + GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } + glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); // glad: load all OpenGL function pointers @@ -141,9 +145,9 @@ int main() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // load image, create texture and generate mipmaps - int width, height, nrComponents; + int width, height, nrChannels; stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis. - unsigned char *data = stbi_load(FileSystem::getPath("resources/textures/container.jpg").c_str(), &width, &height, &nrComponents, 0); + unsigned char *data = stbi_load(FileSystem::getPath("resources/textures/container.jpg").c_str(), &width, &height, &nrChannels, 0); if (data) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); @@ -165,7 +169,7 @@ int main() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // load image, create texture and generate mipmaps - data = stbi_load(FileSystem::getPath("resources/textures/awesomeface.png").c_str(), &width, &height, &nrComponents, 0); + data = stbi_load(FileSystem::getPath("resources/textures/awesomeface.png").c_str(), &width, &height, &nrChannels, 0); if (data) { // note that the awesomeface.png has transparency and thus an alpha channel, so make sure to tell OpenGL the data type is of GL_RGBA @@ -186,7 +190,7 @@ int main() // pass projection matrix to shader (as projection matrix rarely changes there's no need to do this per frame) // ----------------------------------------------------------------------------------------------------------- - glm::mat4 projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f); + glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); ourShader.setMat4("projection", projection); @@ -222,7 +226,7 @@ int main() // render boxes glBindVertexArray(VAO); - for (GLuint i = 0; i < 10; i++) + for (unsigned int i = 0; i < 10; i++) { // calculate the model matrix for each object and pass it to shader before drawing glm::mat4 model; diff --git a/src/1.getting_started/7.2.camera_keyboard_dt/7.2.camera.fs b/src/1.getting_started/7.2.camera_keyboard_dt/7.2.camera.fs new file mode 100644 index 0000000..44839bc --- /dev/null +++ b/src/1.getting_started/7.2.camera_keyboard_dt/7.2.camera.fs @@ -0,0 +1,14 @@ +#version 330 core +out vec4 FragColor; + +in vec2 TexCoord; + +// texture samplers +uniform sampler2D texture1; +uniform sampler2D texture2; + +void main() +{ + // linearly interpolate between both textures (80% container, 20% awesomeface) + FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2); +} \ No newline at end of file diff --git a/src/1.getting_started/7.2.camera_keyboard_dt/7.2.camera.vs b/src/1.getting_started/7.2.camera_keyboard_dt/7.2.camera.vs new file mode 100644 index 0000000..e91af6d --- /dev/null +++ b/src/1.getting_started/7.2.camera_keyboard_dt/7.2.camera.vs @@ -0,0 +1,15 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec2 aTexCoord; + +out vec2 TexCoord; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void main() +{ + gl_Position = projection * view * model * vec4(aPos, 1.0f); + TexCoord = vec2(aTexCoord.x, aTexCoord.y); +} \ No newline at end of file diff --git a/src/1.getting_started/7.2.camera_keyboard_dt/camera_keyboard_dt.cpp b/src/1.getting_started/7.2.camera_keyboard_dt/camera_keyboard_dt.cpp index c30340d..f8e322a 100644 --- a/src/1.getting_started/7.2.camera_keyboard_dt/camera_keyboard_dt.cpp +++ b/src/1.getting_started/7.2.camera_keyboard_dt/camera_keyboard_dt.cpp @@ -14,11 +14,16 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height); void processInput(GLFWwindow *window); +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + // camera glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f); glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f); +// timing float deltaTime = 0.0f; // time between current frame and last frame float lastFrame = 0.0f; @@ -34,14 +39,14 @@ int main() // glfw window creation // -------------------- - GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); - glfwMakeContextCurrent(window); + GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } + glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); // glad: load all OpenGL function pointers @@ -58,7 +63,7 @@ int main() // build and compile our shader zprogram // ------------------------------------ - Shader ourShader("7.1.camera.vs", "7.1.camera.fs"); + Shader ourShader("7.2.camera.vs", "7.2.camera.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ @@ -149,9 +154,9 @@ int main() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // load image, create texture and generate mipmaps - int width, height, nrComponents; + int width, height, nrChannels; stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis. - unsigned char *data = stbi_load(FileSystem::getPath("resources/textures/container.jpg").c_str(), &width, &height, &nrComponents, 0); + unsigned char *data = stbi_load(FileSystem::getPath("resources/textures/container.jpg").c_str(), &width, &height, &nrChannels, 0); if (data) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); @@ -173,7 +178,7 @@ int main() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // load image, create texture and generate mipmaps - data = stbi_load(FileSystem::getPath("resources/textures/awesomeface.png").c_str(), &width, &height, &nrComponents, 0); + data = stbi_load(FileSystem::getPath("resources/textures/awesomeface.png").c_str(), &width, &height, &nrChannels, 0); if (data) { // note that the awesomeface.png has transparency and thus an alpha channel, so make sure to tell OpenGL the data type is of GL_RGBA @@ -194,7 +199,7 @@ int main() // pass projection matrix to shader (as projection matrix rarely changes there's no need to do this per frame) // ----------------------------------------------------------------------------------------------------------- - glm::mat4 projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f); + glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); ourShader.setMat4("projection", projection); @@ -232,7 +237,7 @@ int main() // render boxes glBindVertexArray(VAO); - for (GLuint i = 0; i < 10; i++) + for (unsigned int i = 0; i < 10; i++) { // calculate the model matrix for each object and pass it to shader before drawing glm::mat4 model; diff --git a/src/1.getting_started/7.3.camera_mouse_zoom/7.3.camera.fs b/src/1.getting_started/7.3.camera_mouse_zoom/7.3.camera.fs new file mode 100644 index 0000000..44839bc --- /dev/null +++ b/src/1.getting_started/7.3.camera_mouse_zoom/7.3.camera.fs @@ -0,0 +1,14 @@ +#version 330 core +out vec4 FragColor; + +in vec2 TexCoord; + +// texture samplers +uniform sampler2D texture1; +uniform sampler2D texture2; + +void main() +{ + // linearly interpolate between both textures (80% container, 20% awesomeface) + FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2); +} \ No newline at end of file diff --git a/src/1.getting_started/7.3.camera_mouse_zoom/7.3.camera.vs b/src/1.getting_started/7.3.camera_mouse_zoom/7.3.camera.vs new file mode 100644 index 0000000..e91af6d --- /dev/null +++ b/src/1.getting_started/7.3.camera_mouse_zoom/7.3.camera.vs @@ -0,0 +1,15 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec2 aTexCoord; + +out vec2 TexCoord; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void main() +{ + gl_Position = projection * view * model * vec4(aPos, 1.0f); + TexCoord = vec2(aTexCoord.x, aTexCoord.y); +} \ No newline at end of file diff --git a/src/1.getting_started/7.3.camera_mouse_zoom/camera_mouse_zoom.cpp b/src/1.getting_started/7.3.camera_mouse_zoom/camera_mouse_zoom.cpp index 916b65c..c60c8e0 100644 --- a/src/1.getting_started/7.3.camera_mouse_zoom/camera_mouse_zoom.cpp +++ b/src/1.getting_started/7.3.camera_mouse_zoom/camera_mouse_zoom.cpp @@ -16,6 +16,10 @@ void mouse_callback(GLFWwindow* window, double xpos, double ypos); void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void processInput(GLFWwindow *window); +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + // camera glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f); glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); @@ -28,6 +32,7 @@ float lastX = 800.0f / 2.0; float lastY = 600.0 / 2.0; float fov = 45.0f; +// timing float deltaTime = 0.0f; // time between current frame and last frame float lastFrame = 0.0f; @@ -43,14 +48,14 @@ int main() // glfw window creation // -------------------- - GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); - glfwMakeContextCurrent(window); + GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } + glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); @@ -72,7 +77,7 @@ int main() // build and compile our shader zprogram // ------------------------------------ - Shader ourShader("7.1.camera.vs", "7.1.camera.fs"); + Shader ourShader("7.3.camera.vs", "7.3.camera.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ @@ -163,9 +168,9 @@ int main() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // load image, create texture and generate mipmaps - int width, height, nrComponents; + int width, height, nrChannels; stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis. - unsigned char *data = stbi_load(FileSystem::getPath("resources/textures/container.jpg").c_str(), &width, &height, &nrComponents, 0); + unsigned char *data = stbi_load(FileSystem::getPath("resources/textures/container.jpg").c_str(), &width, &height, &nrChannels, 0); if (data) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); @@ -187,7 +192,7 @@ int main() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // load image, create texture and generate mipmaps - data = stbi_load(FileSystem::getPath("resources/textures/awesomeface.png").c_str(), &width, &height, &nrComponents, 0); + data = stbi_load(FileSystem::getPath("resources/textures/awesomeface.png").c_str(), &width, &height, &nrChannels, 0); if (data) { // note that the awesomeface.png has transparency and thus an alpha channel, so make sure to tell OpenGL the data type is of GL_RGBA @@ -236,7 +241,7 @@ int main() ourShader.use(); // pass projection matrix to shader (note that in this case it could change every frame) - glm::mat4 projection = glm::perspective(glm::radians(fov), 800.0f / 600.0f, 0.1f, 100.0f); + glm::mat4 projection = glm::perspective(glm::radians(fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); ourShader.setMat4("projection", projection); // camera/view transformation @@ -245,7 +250,7 @@ int main() // render boxes glBindVertexArray(VAO); - for (GLuint i = 0; i < 10; i++) + for (unsigned int i = 0; i < 10; i++) { // calculate the model matrix for each object and pass it to shader before drawing glm::mat4 model; diff --git a/src/1.getting_started/7.4.camera_class/7.4.camera.fs b/src/1.getting_started/7.4.camera_class/7.4.camera.fs new file mode 100644 index 0000000..44839bc --- /dev/null +++ b/src/1.getting_started/7.4.camera_class/7.4.camera.fs @@ -0,0 +1,14 @@ +#version 330 core +out vec4 FragColor; + +in vec2 TexCoord; + +// texture samplers +uniform sampler2D texture1; +uniform sampler2D texture2; + +void main() +{ + // linearly interpolate between both textures (80% container, 20% awesomeface) + FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2); +} \ No newline at end of file diff --git a/src/1.getting_started/7.4.camera_class/7.4.camera.vs b/src/1.getting_started/7.4.camera_class/7.4.camera.vs new file mode 100644 index 0000000..e91af6d --- /dev/null +++ b/src/1.getting_started/7.4.camera_class/7.4.camera.vs @@ -0,0 +1,15 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec2 aTexCoord; + +out vec2 TexCoord; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void main() +{ + gl_Position = projection * view * model * vec4(aPos, 1.0f); + TexCoord = vec2(aTexCoord.x, aTexCoord.y); +} \ No newline at end of file diff --git a/src/1.getting_started/7.4.camera_class/camera_class.cpp b/src/1.getting_started/7.4.camera_class/camera_class.cpp index 71bde9d..f563cdf 100644 --- a/src/1.getting_started/7.4.camera_class/camera_class.cpp +++ b/src/1.getting_started/7.4.camera_class/camera_class.cpp @@ -17,12 +17,17 @@ void mouse_callback(GLFWwindow* window, double xpos, double ypos); void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void processInput(GLFWwindow *window); +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); float lastX = 800.0f / 2.0; float lastY = 600.0 / 2.0; bool firstMouse = true; +// timing float deltaTime = 0.0f; // time between current frame and last frame float lastFrame = 0.0f; @@ -38,14 +43,14 @@ int main() // glfw window creation // -------------------- - GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); - glfwMakeContextCurrent(window); + GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } + glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); @@ -67,7 +72,7 @@ int main() // build and compile our shader zprogram // ------------------------------------ - Shader ourShader("7.1.camera.vs", "7.1.camera.fs"); + Shader ourShader("7.4.camera.vs", "7.4.camera.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ @@ -158,9 +163,9 @@ int main() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // load image, create texture and generate mipmaps - int width, height, nrComponents; + int width, height, nrChannels; stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis. - unsigned char *data = stbi_load(FileSystem::getPath("resources/textures/container.jpg").c_str(), &width, &height, &nrComponents, 0); + unsigned char *data = stbi_load(FileSystem::getPath("resources/textures/container.jpg").c_str(), &width, &height, &nrChannels, 0); if (data) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); @@ -182,7 +187,7 @@ int main() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // load image, create texture and generate mipmaps - data = stbi_load(FileSystem::getPath("resources/textures/awesomeface.png").c_str(), &width, &height, &nrComponents, 0); + data = stbi_load(FileSystem::getPath("resources/textures/awesomeface.png").c_str(), &width, &height, &nrChannels, 0); if (data) { // note that the awesomeface.png has transparency and thus an alpha channel, so make sure to tell OpenGL the data type is of GL_RGBA @@ -231,7 +236,7 @@ int main() ourShader.use(); // pass projection matrix to shader (note that in this case it could change every frame) - glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), 800.0f / 600.0f, 0.1f, 100.0f); + glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); ourShader.setMat4("projection", projection); // camera/view transformation @@ -240,7 +245,7 @@ int main() // render boxes glBindVertexArray(VAO); - for (GLuint i = 0; i < 10; i++) + for (unsigned int i = 0; i < 10; i++) { // calculate the model matrix for each object and pass it to shader before drawing glm::mat4 model; diff --git a/src/6.pbr/1.1.lighting/lighting.cpp b/src/6.pbr/1.1.lighting/lighting.cpp index 8421c23..425379b 100644 --- a/src/6.pbr/1.1.lighting/lighting.cpp +++ b/src/6.pbr/1.1.lighting/lighting.cpp @@ -20,19 +20,20 @@ void processInput(GLFWwindow *window); unsigned int loadTexture(const char *path); void renderSphere(); +// settings +const unsigned int SCR_WIDTH = 1280; +const unsigned int SCR_HEIGHT = 720; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); float lastX = 800.0f / 2.0; float lastY = 600.0 / 2.0; bool firstMouse = true; +// timing float deltaTime = 0.0f; float lastFrame = 0.0f; -// settings -const unsigned int SCR_WIDTH = 1280; -const unsigned int SCR_HEIGHT = 720; - int main() { // glfw: initialize and configure diff --git a/src/6.pbr/1.2.lighting_textured/lighting_textured.cpp b/src/6.pbr/1.2.lighting_textured/lighting_textured.cpp index dc8e5b9..5ec9979 100644 --- a/src/6.pbr/1.2.lighting_textured/lighting_textured.cpp +++ b/src/6.pbr/1.2.lighting_textured/lighting_textured.cpp @@ -20,19 +20,20 @@ void processInput(GLFWwindow *window); unsigned int loadTexture(const char *path); void renderSphere(); +// settings +const unsigned int SCR_WIDTH = 1280; +const unsigned int SCR_HEIGHT = 720; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); float lastX = 800.0f / 2.0; float lastY = 600.0 / 2.0; bool firstMouse = true; +// timing float deltaTime = 0.0f; float lastFrame = 0.0f; -// settings -const unsigned int SCR_WIDTH = 1280; -const unsigned int SCR_HEIGHT = 720; - int main() { // glfw: initialize and configure diff --git a/src/6.pbr/2.1.1.ibl_irradiance_conversion/ibl_irradiance_conversion.cpp b/src/6.pbr/2.1.1.ibl_irradiance_conversion/ibl_irradiance_conversion.cpp index c1cc66f..bf527b9 100644 --- a/src/6.pbr/2.1.1.ibl_irradiance_conversion/ibl_irradiance_conversion.cpp +++ b/src/6.pbr/2.1.1.ibl_irradiance_conversion/ibl_irradiance_conversion.cpp @@ -20,19 +20,20 @@ void processInput(GLFWwindow *window); void renderSphere(); void renderCube(); +// settings +const unsigned int SCR_WIDTH = 1280; +const unsigned int SCR_HEIGHT = 720; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); float lastX = 800.0f / 2.0; float lastY = 600.0 / 2.0; bool firstMouse = true; +// timing float deltaTime = 0.0f; float lastFrame = 0.0f; -// settings -const unsigned int SCR_WIDTH = 1280; -const unsigned int SCR_HEIGHT = 720; - int main() { // glfw: initialize and configure diff --git a/src/6.pbr/2.1.2.ibl_irradiance/ibl_irradiance.cpp b/src/6.pbr/2.1.2.ibl_irradiance/ibl_irradiance.cpp index bfc1654..60cf22b 100644 --- a/src/6.pbr/2.1.2.ibl_irradiance/ibl_irradiance.cpp +++ b/src/6.pbr/2.1.2.ibl_irradiance/ibl_irradiance.cpp @@ -20,19 +20,20 @@ void processInput(GLFWwindow *window); void renderSphere(); void renderCube(); +// settings +const unsigned int SCR_WIDTH = 1280; +const unsigned int SCR_HEIGHT = 720; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); float lastX = 800.0f / 2.0; float lastY = 600.0 / 2.0; bool firstMouse = true; +// timing float deltaTime = 0.0f; float lastFrame = 0.0f; -// settings -const unsigned int SCR_WIDTH = 1280; -const unsigned int SCR_HEIGHT = 720; - int main() { // glfw: initialize and configure diff --git a/src/6.pbr/2.2.1.ibl_specular/ibl_specular.cpp b/src/6.pbr/2.2.1.ibl_specular/ibl_specular.cpp index 5b728bc..2795927 100644 --- a/src/6.pbr/2.2.1.ibl_specular/ibl_specular.cpp +++ b/src/6.pbr/2.2.1.ibl_specular/ibl_specular.cpp @@ -21,19 +21,20 @@ void renderSphere(); void renderCube(); void renderQuad(); +// settings +const unsigned int SCR_WIDTH = 1280; +const unsigned int SCR_HEIGHT = 720; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); float lastX = 800.0f / 2.0; float lastY = 600.0 / 2.0; bool firstMouse = true; +// timing float deltaTime = 0.0f; float lastFrame = 0.0f; -// settings -const unsigned int SCR_WIDTH = 1280; -const unsigned int SCR_HEIGHT = 720; - int main() { // glfw: initialize and configure diff --git a/src/6.pbr/2.2.2.ibl_specular_textured/ibl_specular_textured.cpp b/src/6.pbr/2.2.2.ibl_specular_textured/ibl_specular_textured.cpp index 7c12a5e..a9d6200 100644 --- a/src/6.pbr/2.2.2.ibl_specular_textured/ibl_specular_textured.cpp +++ b/src/6.pbr/2.2.2.ibl_specular_textured/ibl_specular_textured.cpp @@ -22,19 +22,20 @@ void renderSphere(); void renderCube(); void renderQuad(); +// settings +const unsigned int SCR_WIDTH = 1280; +const unsigned int SCR_HEIGHT = 720; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); float lastX = 800.0f / 2.0; float lastY = 600.0 / 2.0; bool firstMouse = true; +// timing float deltaTime = 0.0f; float lastFrame = 0.0f; -// settings -const unsigned int SCR_WIDTH = 1280; -const unsigned int SCR_HEIGHT = 720; - int main() { // glfw: initialize and configure