Code/content re-work: getting started.

This commit is contained in:
Joey de Vries
2017-05-29 23:47:28 +02:00
parent 79e22d716e
commit 18e5fdb443
31 changed files with 315 additions and 118 deletions

View File

@@ -16,11 +16,11 @@ enum Camera_Movement {
}; };
// Default camera values // Default camera values
const GLfloat YAW = -90.0f; const float YAW = -90.0f;
const GLfloat PITCH = 0.0f; const float PITCH = 0.0f;
const GLfloat SPEED = 2.5f; const float SPEED = 2.5f;
const GLfloat SENSITIVTY = 0.1f; const float SENSITIVTY = 0.1f;
const GLfloat ZOOM = 45.0f; 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 // 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 Right;
glm::vec3 WorldUp; glm::vec3 WorldUp;
// Eular Angles // Eular Angles
GLfloat Yaw; float Yaw;
GLfloat Pitch; float Pitch;
// Camera options // Camera options
GLfloat MovementSpeed; float MovementSpeed;
GLfloat MouseSensitivity; float MouseSensitivity;
GLfloat Zoom; float Zoom;
// Constructor with vectors // 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; Position = position;
this->WorldUp = up; WorldUp = up;
this->Yaw = yaw; Yaw = yaw;
this->Pitch = pitch; Pitch = pitch;
this->updateCameraVectors(); updateCameraVectors();
} }
// Constructor with scalar values // 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); Position = glm::vec3(posX, posY, posZ);
this->WorldUp = glm::vec3(upX, upY, upZ); WorldUp = glm::vec3(upX, upY, upZ);
this->Yaw = yaw; Yaw = yaw;
this->Pitch = pitch; Pitch = pitch;
this->updateCameraVectors(); updateCameraVectors();
} }
// Returns the view matrix calculated using Eular Angles and the LookAt Matrix // Returns the view matrix calculated using Eular Angles and the LookAt Matrix
glm::mat4 GetViewMatrix() 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) // 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) if (direction == FORWARD)
this->Position += this->Front * velocity; Position += Front * velocity;
if (direction == BACKWARD) if (direction == BACKWARD)
this->Position -= this->Front * velocity; Position -= Front * velocity;
if (direction == LEFT) if (direction == LEFT)
this->Position -= this->Right * velocity; Position -= Right * velocity;
if (direction == RIGHT) 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. // 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; xoffset *= MouseSensitivity;
yoffset *= this->MouseSensitivity; yoffset *= MouseSensitivity;
this->Yaw += xoffset; Yaw += xoffset;
this->Pitch += yoffset; Pitch += yoffset;
// Make sure that when pitch is out of bounds, screen doesn't get flipped // Make sure that when pitch is out of bounds, screen doesn't get flipped
if (constrainPitch) if (constrainPitch)
{ {
if (this->Pitch > 89.0f) if (Pitch > 89.0f)
this->Pitch = 89.0f; Pitch = 89.0f;
if (this->Pitch < -89.0f) if (Pitch < -89.0f)
this->Pitch = -89.0f; Pitch = -89.0f;
} }
// Update Front, Right and Up Vectors using the updated Eular angles // 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 // 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) if (Zoom >= 1.0f && Zoom <= 45.0f)
this->Zoom -= yoffset; Zoom -= yoffset;
if (this->Zoom <= 1.0f) if (Zoom <= 1.0f)
this->Zoom = 1.0f; Zoom = 1.0f;
if (this->Zoom >= 45.0f) if (Zoom >= 45.0f)
this->Zoom = 45.0f; Zoom = 45.0f;
} }
private: private:
@@ -119,13 +119,13 @@ private:
{ {
// Calculate the new Front vector // Calculate the new Front vector
glm::vec3 front; glm::vec3 front;
front.x = cos(glm::radians(this->Yaw)) * cos(glm::radians(this->Pitch)); front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
front.y = sin(glm::radians(this->Pitch)); front.y = sin(glm::radians(Pitch));
front.z = sin(glm::radians(this->Yaw)) * cos(glm::radians(this->Pitch)); front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
this->Front = glm::normalize(front); Front = glm::normalize(front);
// Also re-calculate the Right and Up vector // 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. 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.
this->Up = glm::normalize(glm::cross(this->Right, this->Front)); Up = glm::normalize(glm::cross(Right, Front));
} }
}; };
#endif #endif

View File

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

View File

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

View File

@@ -46,7 +46,7 @@ int main()
// build and compile our shader zprogram // 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 // set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------ // ------------------------------------------------------------------

View File

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

View File

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

View File

@@ -46,7 +46,7 @@ int main()
// build and compile our shader zprogram // 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 // set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------ // ------------------------------------------------------------------

View File

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

View File

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

View File

@@ -50,7 +50,7 @@ int main()
// build and compile our shader zprogram // 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 // set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------ // ------------------------------------------------------------------

View File

@@ -172,7 +172,7 @@ int main()
glm::mat4 projection; glm::mat4 projection;
model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f)); 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)); 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 // retrieve the matrix uniform locations
unsigned int modelLoc = glGetUniformLocation(ourShader.ID, "model"); unsigned int modelLoc = glGetUniformLocation(ourShader.ID, "model");
unsigned int viewLoc = glGetUniformLocation(ourShader.ID, "view"); unsigned int viewLoc = glGetUniformLocation(ourShader.ID, "view");

View File

@@ -204,7 +204,7 @@ int main()
glm::mat4 projection; glm::mat4 projection;
model = glm::rotate(model, (float)glfwGetTime(), glm::vec3(0.5f, 1.0f, 0.0f)); 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)); 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 // retrieve the matrix uniform locations
unsigned int modelLoc = glGetUniformLocation(ourShader.ID, "model"); unsigned int modelLoc = glGetUniformLocation(ourShader.ID, "model");
unsigned int viewLoc = glGetUniformLocation(ourShader.ID, "view"); unsigned int viewLoc = glGetUniformLocation(ourShader.ID, "view");

View File

@@ -214,7 +214,7 @@ int main()
// create transformations // create transformations
glm::mat4 view; glm::mat4 view;
glm::mat4 projection; 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)); view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
// pass transformation matrices to the shader // 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. 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.

View File

@@ -1,7 +1,7 @@
#version 330 core #version 330 core
out vec4 fragColor; out vec4 FragColor;
in vec2 texCoord; in vec2 TexCoord;
// texture samplers // texture samplers
uniform sampler2D texture1; uniform sampler2D texture1;
@@ -10,5 +10,5 @@ uniform sampler2D texture2;
void main() void main()
{ {
// linearly interpolate between both textures (80% container, 20% awesomeface) // 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);
} }

View File

@@ -2,7 +2,7 @@
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord; layout (location = 1) in vec2 aTexCoord;
out vec2 texCoord; out vec2 TexCoord;
uniform mat4 model; uniform mat4 model;
uniform mat4 view; uniform mat4 view;
@@ -11,5 +11,5 @@ uniform mat4 projection;
void main() void main()
{ {
gl_Position = projection * view * model * vec4(aPos, 1.0f); gl_Position = projection * view * model * vec4(aPos, 1.0f);
texCoord = vec2(aTexCoord.x, aTexCoord.y); TexCoord = vec2(aTexCoord.x, aTexCoord.y);
} }

View File

@@ -14,6 +14,10 @@
void framebuffer_size_callback(GLFWwindow* window, int width, int height); void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window); void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
int main() int main()
{ {
// glfw: initialize and configure // glfw: initialize and configure
@@ -26,14 +30,14 @@ int main()
// glfw window creation // glfw window creation
// -------------------- // --------------------
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
glfwMakeContextCurrent(window);
if (window == NULL) if (window == NULL)
{ {
std::cout << "Failed to create GLFW window" << std::endl; std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate(); glfwTerminate();
return -1; return -1;
} }
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// glad: load all OpenGL function pointers // 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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps // 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. 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) if (data)
{ {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps // 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) 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 // 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) // 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); ourShader.setMat4("projection", projection);
@@ -222,7 +226,7 @@ int main()
// render boxes // render boxes
glBindVertexArray(VAO); 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 // calculate the model matrix for each object and pass it to shader before drawing
glm::mat4 model; glm::mat4 model;

View File

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

View File

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

View File

@@ -14,11 +14,16 @@
void framebuffer_size_callback(GLFWwindow* window, int width, int height); void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window); void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
// camera // camera
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f); glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.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 deltaTime = 0.0f; // time between current frame and last frame
float lastFrame = 0.0f; float lastFrame = 0.0f;
@@ -34,14 +39,14 @@ int main()
// glfw window creation // glfw window creation
// -------------------- // --------------------
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
glfwMakeContextCurrent(window);
if (window == NULL) if (window == NULL)
{ {
std::cout << "Failed to create GLFW window" << std::endl; std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate(); glfwTerminate();
return -1; return -1;
} }
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// glad: load all OpenGL function pointers // glad: load all OpenGL function pointers
@@ -58,7 +63,7 @@ int main()
// build and compile our shader zprogram // 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 // 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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps // 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. 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) if (data)
{ {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps // 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) 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 // 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) // 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); ourShader.setMat4("projection", projection);
@@ -232,7 +237,7 @@ int main()
// render boxes // render boxes
glBindVertexArray(VAO); 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 // calculate the model matrix for each object and pass it to shader before drawing
glm::mat4 model; glm::mat4 model;

View File

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

View File

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

View File

@@ -16,6 +16,10 @@ void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void processInput(GLFWwindow *window); void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
// camera // camera
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f); glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.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 lastY = 600.0 / 2.0;
float fov = 45.0f; float fov = 45.0f;
// timing
float deltaTime = 0.0f; // time between current frame and last frame float deltaTime = 0.0f; // time between current frame and last frame
float lastFrame = 0.0f; float lastFrame = 0.0f;
@@ -43,14 +48,14 @@ int main()
// glfw window creation // glfw window creation
// -------------------- // --------------------
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
glfwMakeContextCurrent(window);
if (window == NULL) if (window == NULL)
{ {
std::cout << "Failed to create GLFW window" << std::endl; std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate(); glfwTerminate();
return -1; return -1;
} }
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback); glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback); glfwSetScrollCallback(window, scroll_callback);
@@ -72,7 +77,7 @@ int main()
// build and compile our shader zprogram // 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 // 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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps // 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. 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) if (data)
{ {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps // 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) 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 // 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(); ourShader.use();
// pass projection matrix to shader (note that in this case it could change every frame) // 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); ourShader.setMat4("projection", projection);
// camera/view transformation // camera/view transformation
@@ -245,7 +250,7 @@ int main()
// render boxes // render boxes
glBindVertexArray(VAO); 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 // calculate the model matrix for each object and pass it to shader before drawing
glm::mat4 model; glm::mat4 model;

View File

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

View File

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

View File

@@ -17,12 +17,17 @@ void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void processInput(GLFWwindow *window); void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
// camera // camera
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = 800.0f / 2.0; float lastX = 800.0f / 2.0;
float lastY = 600.0 / 2.0; float lastY = 600.0 / 2.0;
bool firstMouse = true; bool firstMouse = true;
// timing
float deltaTime = 0.0f; // time between current frame and last frame float deltaTime = 0.0f; // time between current frame and last frame
float lastFrame = 0.0f; float lastFrame = 0.0f;
@@ -38,14 +43,14 @@ int main()
// glfw window creation // glfw window creation
// -------------------- // --------------------
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
glfwMakeContextCurrent(window);
if (window == NULL) if (window == NULL)
{ {
std::cout << "Failed to create GLFW window" << std::endl; std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate(); glfwTerminate();
return -1; return -1;
} }
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback); glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback); glfwSetScrollCallback(window, scroll_callback);
@@ -67,7 +72,7 @@ int main()
// build and compile our shader zprogram // 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 // 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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps // 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. 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) if (data)
{ {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps // 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) 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 // 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(); ourShader.use();
// pass projection matrix to shader (note that in this case it could change every frame) // 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); ourShader.setMat4("projection", projection);
// camera/view transformation // camera/view transformation
@@ -240,7 +245,7 @@ int main()
// render boxes // render boxes
glBindVertexArray(VAO); 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 // calculate the model matrix for each object and pass it to shader before drawing
glm::mat4 model; glm::mat4 model;

View File

@@ -20,19 +20,20 @@ void processInput(GLFWwindow *window);
unsigned int loadTexture(const char *path); unsigned int loadTexture(const char *path);
void renderSphere(); void renderSphere();
// settings
const unsigned int SCR_WIDTH = 1280;
const unsigned int SCR_HEIGHT = 720;
// camera // camera
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = 800.0f / 2.0; float lastX = 800.0f / 2.0;
float lastY = 600.0 / 2.0; float lastY = 600.0 / 2.0;
bool firstMouse = true; bool firstMouse = true;
// timing
float deltaTime = 0.0f; float deltaTime = 0.0f;
float lastFrame = 0.0f; float lastFrame = 0.0f;
// settings
const unsigned int SCR_WIDTH = 1280;
const unsigned int SCR_HEIGHT = 720;
int main() int main()
{ {
// glfw: initialize and configure // glfw: initialize and configure

View File

@@ -20,19 +20,20 @@ void processInput(GLFWwindow *window);
unsigned int loadTexture(const char *path); unsigned int loadTexture(const char *path);
void renderSphere(); void renderSphere();
// settings
const unsigned int SCR_WIDTH = 1280;
const unsigned int SCR_HEIGHT = 720;
// camera // camera
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = 800.0f / 2.0; float lastX = 800.0f / 2.0;
float lastY = 600.0 / 2.0; float lastY = 600.0 / 2.0;
bool firstMouse = true; bool firstMouse = true;
// timing
float deltaTime = 0.0f; float deltaTime = 0.0f;
float lastFrame = 0.0f; float lastFrame = 0.0f;
// settings
const unsigned int SCR_WIDTH = 1280;
const unsigned int SCR_HEIGHT = 720;
int main() int main()
{ {
// glfw: initialize and configure // glfw: initialize and configure

View File

@@ -20,19 +20,20 @@ void processInput(GLFWwindow *window);
void renderSphere(); void renderSphere();
void renderCube(); void renderCube();
// settings
const unsigned int SCR_WIDTH = 1280;
const unsigned int SCR_HEIGHT = 720;
// camera // camera
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = 800.0f / 2.0; float lastX = 800.0f / 2.0;
float lastY = 600.0 / 2.0; float lastY = 600.0 / 2.0;
bool firstMouse = true; bool firstMouse = true;
// timing
float deltaTime = 0.0f; float deltaTime = 0.0f;
float lastFrame = 0.0f; float lastFrame = 0.0f;
// settings
const unsigned int SCR_WIDTH = 1280;
const unsigned int SCR_HEIGHT = 720;
int main() int main()
{ {
// glfw: initialize and configure // glfw: initialize and configure

View File

@@ -20,19 +20,20 @@ void processInput(GLFWwindow *window);
void renderSphere(); void renderSphere();
void renderCube(); void renderCube();
// settings
const unsigned int SCR_WIDTH = 1280;
const unsigned int SCR_HEIGHT = 720;
// camera // camera
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = 800.0f / 2.0; float lastX = 800.0f / 2.0;
float lastY = 600.0 / 2.0; float lastY = 600.0 / 2.0;
bool firstMouse = true; bool firstMouse = true;
// timing
float deltaTime = 0.0f; float deltaTime = 0.0f;
float lastFrame = 0.0f; float lastFrame = 0.0f;
// settings
const unsigned int SCR_WIDTH = 1280;
const unsigned int SCR_HEIGHT = 720;
int main() int main()
{ {
// glfw: initialize and configure // glfw: initialize and configure

View File

@@ -21,19 +21,20 @@ void renderSphere();
void renderCube(); void renderCube();
void renderQuad(); void renderQuad();
// settings
const unsigned int SCR_WIDTH = 1280;
const unsigned int SCR_HEIGHT = 720;
// camera // camera
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = 800.0f / 2.0; float lastX = 800.0f / 2.0;
float lastY = 600.0 / 2.0; float lastY = 600.0 / 2.0;
bool firstMouse = true; bool firstMouse = true;
// timing
float deltaTime = 0.0f; float deltaTime = 0.0f;
float lastFrame = 0.0f; float lastFrame = 0.0f;
// settings
const unsigned int SCR_WIDTH = 1280;
const unsigned int SCR_HEIGHT = 720;
int main() int main()
{ {
// glfw: initialize and configure // glfw: initialize and configure

View File

@@ -22,19 +22,20 @@ void renderSphere();
void renderCube(); void renderCube();
void renderQuad(); void renderQuad();
// settings
const unsigned int SCR_WIDTH = 1280;
const unsigned int SCR_HEIGHT = 720;
// camera // camera
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = 800.0f / 2.0; float lastX = 800.0f / 2.0;
float lastY = 600.0 / 2.0; float lastY = 600.0 / 2.0;
bool firstMouse = true; bool firstMouse = true;
// timing
float deltaTime = 0.0f; float deltaTime = 0.0f;
float lastFrame = 0.0f; float lastFrame = 0.0f;
// settings
const unsigned int SCR_WIDTH = 1280;
const unsigned int SCR_HEIGHT = 720;
int main() int main()
{ {
// glfw: initialize and configure // glfw: initialize and configure