Skip to content

Commit 9e7faa3

Browse files
committed
Added borders to cubes using stencil buffer
1 parent eb7c310 commit 9e7faa3

File tree

18 files changed

+9534
-0
lines changed

18 files changed

+9534
-0
lines changed

10-depth-and-stencil/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
3+
project(depth)
4+
5+
add_executable(main.o main.cpp glWindow.cpp camera.cpp mesh.cpp model.cpp shader.cpp stb_image.cpp)
6+
7+
find_package(GLEW REQUIRED)
8+
target_link_libraries(main.o GLEW::GLEW)
9+
10+
find_package(glfw3 REQUIRED)
11+
target_link_libraries(main.o glfw)
12+
13+
find_package(OpenGL REQUIRED)
14+
target_link_libraries(main.o OpenGL::GL)
15+
16+
find_package(assimp REQUIRED)
17+
target_link_libraries(main.o assimp)

10-depth-and-stencil/camera.cpp

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#include "camera.h"
2+
3+
Camera::Camera(glm::vec3 position, glm::vec3 up, float yaw, float pitch) :
4+
front(FRONT),
5+
movementSpeed(SPEED),
6+
mouseSensitivity(SENSITIVITY),
7+
fov(FOV)
8+
{
9+
this->position = position;
10+
worldUp = up;
11+
this->yaw = yaw;
12+
this->pitch = pitch;
13+
}
14+
15+
Camera::Camera(float xPos, float yPos, float zPos, float xUp, float yUp, float zUp, float yaw, float pitch) :
16+
front(FRONT),
17+
movementSpeed(SPEED),
18+
mouseSensitivity(SENSITIVITY),
19+
fov(FOV)
20+
{
21+
position = glm::vec3(xPos, yPos, zPos);
22+
worldUp = glm::vec3(xUp, yUp, zUp);
23+
this->yaw = yaw;
24+
this->pitch = pitch;
25+
}
26+
27+
Camera::~Camera()
28+
{
29+
30+
}
31+
32+
// glm::mat4 Camera::getViewMatrix(glm::vec3 position, glm::vec3 target, glm::vec3 up)
33+
// {
34+
// return glm::lookAt(position, position + front, up);
35+
// }
36+
37+
glm::mat4 Camera::calculateLookAtMatrix(glm::vec3 position, glm::vec3 target, glm::vec3 up)
38+
{
39+
// Create Z-axis by subtracting "target" vector from "position" vector
40+
glm::vec3 zaxis = glm::normalize(position - target);
41+
// Create X-axis from cross-product of world up and Z-axis
42+
glm::vec3 xaxis = glm::normalize(glm::cross(glm::normalize(worldUp), zaxis));
43+
// Create Y-axis from cross-product of Z and X axes
44+
glm::vec3 yaxis = glm::cross(zaxis, xaxis);
45+
46+
// Create translation matrix for movement
47+
// We decrease the position because in OpenGL and generally we move the whole world around the camera instead of moving the camera itself
48+
glm::mat4 translation(1.0f);
49+
translation[3][0] = -position.x;
50+
translation[3][1] = -position.y;
51+
translation[3][2] = -position.z;
52+
53+
// Create rotation matrix to calculate rotation in all axes
54+
glm::mat4 rotation(1.0f);
55+
rotation[0][0] = xaxis.x;
56+
rotation[1][0] = xaxis.y;
57+
rotation[2][0] = xaxis.z;
58+
rotation[0][1] = yaxis.x;
59+
rotation[1][1] = yaxis.y;
60+
rotation[2][1] = yaxis.z;
61+
rotation[0][2] = zaxis.x;
62+
rotation[1][2] = zaxis.y;
63+
rotation[2][2] = zaxis.z;
64+
65+
// Return lookAt matrix by multiplying "translation" and "rotation" matrices
66+
// Remember that matrix multiplication is from right to left
67+
return rotation * translation;
68+
}
69+
70+
void Camera::processKeyboard(CameraMovement direction, float deltaTime)
71+
{
72+
// Velocity equals to defined movement speed multiplied by delta time (time between frames)
73+
float velocity = movementSpeed * deltaTime;
74+
75+
// Switch case to add velocity to specified direction
76+
switch(direction)
77+
{
78+
case FORWARD:
79+
position += front * velocity;
80+
break;
81+
case BACKWARD:
82+
position -= front * velocity;
83+
break;
84+
case RIGHT:
85+
position += right * velocity;
86+
break;
87+
case LEFT:
88+
position -= right * velocity;
89+
break;
90+
}
91+
92+
// std::cout << "POSITION (" << position.x << ", " << position.y << ", " << position.z << ")" << std::endl;
93+
}
94+
95+
void Camera::processMouseMovement(float xOffset, float yOffset, GLboolean constrainPitch)
96+
{
97+
xOffset *= mouseSensitivity;
98+
yOffset *= mouseSensitivity;
99+
100+
yaw += xOffset;
101+
pitch += yOffset;
102+
103+
if(constrainPitch)
104+
{
105+
if(pitch > 89.0f)
106+
pitch = 89.0f;
107+
if(pitch < -89.0f)
108+
pitch = -89.0f;
109+
}
110+
111+
// std::cout << "PITCH " << pitch << std::endl;
112+
updateCameraVectors();
113+
}
114+
115+
void Camera::processMouseScroll(float yOffset)
116+
{
117+
fov -= yOffset;
118+
119+
if(fov < 1.0f)
120+
fov = 1.0f;
121+
if(fov > 90.0f)
122+
fov = 90.0f;
123+
124+
// std::cout << "FOV " << fov << std::endl;
125+
}
126+
127+
void Camera::updateCameraVectors()
128+
{
129+
glm::vec3 front;
130+
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
131+
front.y = sin(glm::radians(pitch));
132+
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
133+
134+
this->front = glm::normalize(front);
135+
right = glm::cross(front, worldUp);
136+
up = glm::cross(right, front);
137+
138+
// std::cout << "FRONT (" << front.x << ", " << front.y << ", " << front.z << ")" << std::endl;
139+
// std::cout << "RIGHT (" << right.x << ", " << right.y << ", " << right.z << ")" << std::endl;
140+
// std::cout << "UP (" << up.x << ", " << up.y << ", " << up.z << ")" << std::endl;
141+
}

10-depth-and-stencil/camera.h

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#pragma once
2+
3+
#include <iostream>
4+
#include <GL/glew.h>
5+
6+
#include <glm/glm.hpp>
7+
#include <glm/gtc/matrix_transform.hpp>
8+
#include <glm/gtc/type_ptr.hpp>
9+
10+
/**
11+
* @brief Enum for camera movement direction
12+
*/
13+
enum CameraMovement
14+
{
15+
FORWARD,
16+
BACKWARD,
17+
LEFT,
18+
RIGHT
19+
};
20+
21+
// Default camera values
22+
const glm::vec3 POSITION(0.0f, 0.0f, 0.0f);
23+
const glm::vec3 UP(0.0f, 1.0f, 0.0f);
24+
const glm::vec3 FRONT(0.0f, 0.0f, -1.0f);
25+
const float YAW = -90.0f;
26+
const float PITCH = 0.0f;
27+
const float SPEED = 1.0f;
28+
const float SENSITIVITY = 0.2f;
29+
const float FOV = 45.0f;
30+
31+
/**
32+
* @brief Class Camera to handle all camera operations and logic.
33+
*/
34+
class Camera
35+
{
36+
private:
37+
glm::vec3 position;
38+
glm::vec3 front;
39+
glm::vec3 up;
40+
glm::vec3 right;
41+
glm::vec3 worldUp;
42+
// Euler angles for camera
43+
float yaw;
44+
float pitch;
45+
// Camera options
46+
float movementSpeed;
47+
float mouseSensitivity;
48+
float fov;
49+
50+
public:
51+
/**
52+
* @brief Constructor to initialise camera with vectors.
53+
* @param position Vector3 (x,y,z) to define camera's starting position in world space.
54+
* @param up Vector3 (x,y,z) to define camera's up direction.
55+
* @param yaw Starting YAW (rotation around Y axis) value, should be -90.0f.
56+
* @param pitch Starting PITCH (rotation around X axis) value, should be 0.0f.
57+
*/
58+
Camera(glm::vec3 position = POSITION, glm::vec3 up = UP, float yaw = YAW, float pitch = PITCH);
59+
60+
/**
61+
* @brief Constructor to initialise camera with scalar values.
62+
* @param xPos Scalar value on X-axis for position vector.
63+
* @param yPos Scalar value on Y-axis for position vector.
64+
* @param zPos Scalar value on Z-axis for position vector.
65+
* @param xUp Scalar value on X-axis for up vector.
66+
* @param yUp Scalar value on Y-axis for up vector.
67+
* @param zUp Scalar value on Z-axis for up vector.
68+
* @param yaw Starting YAW (rotation around Y axis) value, should be -90.0f.
69+
* @param pitch Starting PITCH (rotation around X axis) value, should be 0.0f.
70+
*/
71+
Camera(float xPos, float yPos, float zPos, float xUp, float yUp, float zUp, float yaw = YAW, float pitch = PITCH);
72+
~Camera();
73+
74+
// Get view matrix using GLM "lookAt" function
75+
// glm::mat4 getViewMatrix(glm::vec3 position, glm::vec3 target, glm::vec3 up);
76+
77+
/**
78+
* @brief Calculates camera's translation (movement) and rotation so it will look towards target.
79+
* @param position Position of camera.
80+
* @param target Target's position.
81+
* @param up Camera's up direction.
82+
*/
83+
glm::mat4 calculateLookAtMatrix(glm::vec3 position, glm::vec3 target, glm::vec3 up);
84+
85+
/**
86+
* @brief Process keyboard inputs to ouput movement.
87+
* @param direction Direction which camera should move.
88+
* @param deltaTime Time between frames.
89+
*/
90+
void processKeyboard(CameraMovement direction, float deltaTime);
91+
92+
/**
93+
* @brief Process mouse movement to move camera's view.
94+
* @param xOffset Offset on X-axis.
95+
* @param yOffset Offset on Y-axis.
96+
* @param constrainPitch Whether camera's pitch is contraint between values or not.
97+
*/
98+
void processMouseMovement(float xOffset, float yOffset, GLboolean constrainPitch = true);
99+
100+
/**
101+
* @brief Process mouse scroll to update camera's FOV.
102+
* @param yOffset Offset on Y-axis.
103+
*/
104+
void processMouseScroll(float yOffset);
105+
106+
void setPosition(glm::vec3 position) { this->position = position; };
107+
glm::vec3 getPosition() const { return position; };
108+
109+
void setFront(glm::vec3 front) { this->front = front; };
110+
glm::vec3 getFront() const { return front; };
111+
112+
void setUp(glm::vec3 up) { this->up = up; };
113+
glm::vec3 getUp() const { return up; };
114+
115+
void setYaw(float yaw) { this->yaw = yaw; };
116+
float getYaw() { return yaw; };
117+
118+
void setPitch(float pitch) { this->pitch = pitch; };
119+
float getPitch() { return pitch; };
120+
121+
void setMovementSpeed(float movementSpeed) { this->movementSpeed = movementSpeed; };
122+
float getMovementSpeed() const { return movementSpeed; };
123+
124+
void setMouseSensitivity(float mouseSensitivity) { this->mouseSensitivity = mouseSensitivity; };
125+
float getMouseSensitivity() const { return mouseSensitivity; };
126+
127+
void setFov(float fov) { this->fov = fov; };
128+
float getFov() { return fov; };
129+
130+
private:
131+
/**
132+
* @brief Calculate camera's front direction with updated Euler angles.
133+
*/
134+
void updateCameraVectors();
135+
};

10-depth-and-stencil/glWindow.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include "glWindow.h"
2+
3+
glWindow::glWindow()
4+
{
5+
window = 0;
6+
title = "Main Window";
7+
width = 800;
8+
height = 600;
9+
10+
initialise();
11+
}
12+
13+
glWindow::glWindow(std::string title, GLint width, GLint height)
14+
{
15+
this->title = title;
16+
this->width = width;
17+
this->height = height;
18+
19+
initialise();
20+
}
21+
22+
glWindow::~glWindow()
23+
{
24+
glfwDestroyWindow(window);
25+
glfwTerminate();
26+
}
27+
28+
int glWindow::initialise()
29+
{
30+
// Initialize GLFW
31+
if(!glfwInit())
32+
{
33+
std::cerr << "Failed to initialize GLFW" << std::endl;
34+
glfwTerminate();
35+
return GL_FALSE;
36+
}
37+
38+
// Set GLFW window hints
39+
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
40+
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
41+
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
42+
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
43+
44+
// Create the main window
45+
window = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr);
46+
if(!window)
47+
{
48+
std::cerr << "Failed to create main window" << std::endl;
49+
glfwTerminate();
50+
return GL_FALSE;
51+
}
52+
53+
// Get framebuffer size
54+
glfwGetFramebufferSize(window, &bufferWidth, &bufferHeight);
55+
56+
// Make the context of the main window current
57+
glfwMakeContextCurrent(window);
58+
59+
// Initialize GLEW
60+
glewExperimental = GL_TRUE;
61+
if(glewInit() != GLEW_OK)
62+
{
63+
std::cerr << "Failed to initialize GLEW" << std::endl;
64+
glfwDestroyWindow(window);
65+
glfwTerminate();
66+
return GL_FALSE;
67+
}
68+
69+
// Set the viewport size
70+
glViewport(0, 0, bufferWidth, bufferHeight);
71+
glfwSetFramebufferSizeCallback(window, staticFrameBufferSizeCallback);
72+
73+
return GL_TRUE;
74+
}
75+
76+
// Define the static member function for the callback
77+
void glWindow::staticFrameBufferSizeCallback(GLFWwindow* window, int width, int height)
78+
{
79+
// Forward the call to the non-static member function
80+
static_cast<glWindow*>(glfwGetWindowUserPointer(window))->frameBufferSizeCallback(window, width, height);
81+
}
82+
83+
// Define the non-static member function for the callback
84+
void glWindow::frameBufferSizeCallback(GLFWwindow* window, int width, int height)
85+
{
86+
glViewport(0, 0, width, height);
87+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy