playing with another shader for a sun type thing and WAY better camera movement
This commit is contained in:
parent
9eb8d55630
commit
b299a8d8a2
38
Camera.h
38
Camera.h
@ -11,15 +11,25 @@ class Camera
|
|||||||
glm::vec3 target;
|
glm::vec3 target;
|
||||||
glm::vec3 direction;
|
glm::vec3 direction;
|
||||||
glm::vec3 up;
|
glm::vec3 up;
|
||||||
|
|
||||||
|
// Movement
|
||||||
|
glm::vec3 horizontal_velocity;
|
||||||
|
glm::vec3 forward_velocity;
|
||||||
|
glm::vec3 vertical_velocity;
|
||||||
|
|
||||||
float speed;
|
float speed;
|
||||||
|
|
||||||
glm::vec3 worldUp;
|
glm::vec3 worldUp;
|
||||||
|
|
||||||
Camera(float speed)
|
Camera(float speed, glm::vec3 pos)
|
||||||
{
|
{
|
||||||
this->speed = speed;
|
this->speed = speed;
|
||||||
|
this->vertical_velocity = glm::vec3(0.0f);
|
||||||
|
|
||||||
this->position = glm::vec3(0.0f, 0.0f, 3.0f);
|
this->horizontal_velocity = glm::vec3(0.0f);
|
||||||
|
this->forward_velocity = glm::vec3(0.0f);
|
||||||
|
|
||||||
|
this->position = pos;
|
||||||
this->target = glm::vec3(0.0f, 0.0f, -1.0f);
|
this->target = glm::vec3(0.0f, 0.0f, -1.0f);
|
||||||
|
|
||||||
this->direction = glm::normalize(this->position);
|
this->direction = glm::normalize(this->position);
|
||||||
@ -32,6 +42,10 @@ class Camera
|
|||||||
Camera(glm::vec3 pos, glm::vec3 target, float speed)
|
Camera(glm::vec3 pos, glm::vec3 target, float speed)
|
||||||
{
|
{
|
||||||
this->speed = speed;
|
this->speed = speed;
|
||||||
|
this->vertical_velocity = glm::vec3(0.0f);
|
||||||
|
|
||||||
|
this->horizontal_velocity = glm::vec3(0.0f);
|
||||||
|
this->forward_velocity = glm::vec3(0.0f);
|
||||||
|
|
||||||
this->position = pos;
|
this->position = pos;
|
||||||
this->target = target;
|
this->target = target;
|
||||||
@ -50,6 +64,26 @@ class Camera
|
|||||||
this->worldUp);
|
this->worldUp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void move(float time) // time passed in seconds since last move
|
||||||
|
{
|
||||||
|
if (this->position.y > 0)
|
||||||
|
this->vertical_velocity.y -= (9.8 * time); // gravity is 9.8m/s^2 no way! physics reference?
|
||||||
|
|
||||||
|
std::cout << "pos: " << this->position.x << " " << this->position.y << " " << this->position.z << std::endl;
|
||||||
|
|
||||||
|
glm::vec3 velocity = this->forward_velocity + this->horizontal_velocity + this->vertical_velocity;
|
||||||
|
|
||||||
|
glm::vec3 movement = (velocity * time);
|
||||||
|
this->position += movement;
|
||||||
|
|
||||||
|
// Don't want to clip into the ground right
|
||||||
|
if (this->position.y < 0)
|
||||||
|
{
|
||||||
|
this->position.y = 0;
|
||||||
|
this->vertical_velocity.y = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
146
main.cpp
146
main.cpp
@ -31,8 +31,14 @@ int flipped = 1; // 1 for normal, -1 for flipped
|
|||||||
bool flipping = false;
|
bool flipping = false;
|
||||||
glm::vec3 flip_target = glm::vec3(0.0f);
|
glm::vec3 flip_target = glm::vec3(0.0f);
|
||||||
|
|
||||||
|
// Jump
|
||||||
|
const float jump_cooldown = 1.0f;
|
||||||
|
float last_jump = 0.0f;
|
||||||
|
|
||||||
// Various other settings
|
// Various other settings
|
||||||
const float camera_speed = 0.05f;
|
const float camera_speed = 1.0f;
|
||||||
|
const float max_camera_speed = 2.0f;
|
||||||
|
|
||||||
const float look_sensitivity = 0.1f;
|
const float look_sensitivity = 0.1f;
|
||||||
const float Z_FAR = 1000.0f;
|
const float Z_FAR = 1000.0f;
|
||||||
const float Z_NEAR = 0.1f;
|
const float Z_NEAR = 0.1f;
|
||||||
@ -41,31 +47,24 @@ const float TARGET_FPS = 60.0f;
|
|||||||
const float FOV = 90.0f;
|
const float FOV = 90.0f;
|
||||||
float sleep_time = 15.0f;
|
float sleep_time = 15.0f;
|
||||||
|
|
||||||
|
bool W_pressed, S_pressed, A_pressed, D_pressed = false;
|
||||||
|
|
||||||
glm::mat4 projection = glm::mat4(1.0f);
|
glm::mat4 projection = glm::mat4(1.0f);
|
||||||
|
|
||||||
float yaw, pitch = 0;
|
float yaw, pitch = 0;
|
||||||
|
|
||||||
// Camera
|
// Camera
|
||||||
Camera camera = Camera(camera_speed);
|
Camera camera = Camera(camera_speed, glm::vec3(0.0f, 0.0f, 115.0f));
|
||||||
|
|
||||||
|
|
||||||
// Lighting
|
// Lighting
|
||||||
glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
|
glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
|
||||||
glm::vec3 lightColor(0.557, 0.482, 0.6);
|
glm::vec3 lightColor(1.0f, 1.0f, 0.949f);
|
||||||
|
|
||||||
glm::vec3 objectColor(0.671f, 0.671f, 0.671f);
|
glm::vec3 objectColor(0.0f, 1.0f, 0.0f);
|
||||||
|
|
||||||
glm::vec3 cubePositions[] = {
|
float planets[][2] = {
|
||||||
glm::vec3( 0.0f, 0.0f, 0.0f),
|
{140.0f, 1.0f}
|
||||||
glm::vec3( 2.0f, 5.0f, -15.0f),
|
|
||||||
glm::vec3(-1.5f, -2.2f, -2.5f),
|
|
||||||
glm::vec3(-3.8f, -2.0f, -12.3f),
|
|
||||||
glm::vec3 (2.4f, -0.4f, -3.5f),
|
|
||||||
glm::vec3(-1.7f, 3.0f, -7.5f),
|
|
||||||
glm::vec3( 1.3f, -2.0f, -2.5f),
|
|
||||||
glm::vec3( 1.5f, 2.0f, -2.5f),
|
|
||||||
glm::vec3( 1.5f, 0.2f, -1.5f),
|
|
||||||
glm::vec3(-1.3f, 1.0f, -1.5f)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Mouse Look
|
// Mouse Look
|
||||||
@ -135,6 +134,7 @@ int main()
|
|||||||
|
|
||||||
// ------ Setup Shader ------
|
// ------ Setup Shader ------
|
||||||
Shader shader("shaders/shader.vert", "shaders/shader.frag");
|
Shader shader("shaders/shader.vert", "shaders/shader.frag");
|
||||||
|
Shader sun_shader("shaders/sun.vert", "shaders/sun.frag");
|
||||||
|
|
||||||
// ------ World Objects ------
|
// ------ World Objects ------
|
||||||
std::vector<Object3D> world_objects;
|
std::vector<Object3D> world_objects;
|
||||||
@ -185,6 +185,7 @@ int main()
|
|||||||
glEnable(GL_DEPTH_TEST); // Z-Buffer
|
glEnable(GL_DEPTH_TEST); // Z-Buffer
|
||||||
|
|
||||||
double start_time = glfwGetTime();
|
double start_time = glfwGetTime();
|
||||||
|
double move_time = glfwGetTime();
|
||||||
|
|
||||||
// Setup mouse look
|
// Setup mouse look
|
||||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||||
@ -201,24 +202,36 @@ int main()
|
|||||||
processInput(window);
|
processInput(window);
|
||||||
|
|
||||||
// ------ SETUP Matrices ------
|
// ------ SETUP Matrices ------
|
||||||
shader.use();
|
glm::mat4 view = camera.getView();
|
||||||
|
|
||||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Z-Buffer
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Z-Buffer
|
||||||
|
|
||||||
|
|
||||||
// Camera Rotate around origin
|
// Orbitting sun thing?
|
||||||
// const float radius = 10.0f;
|
float t = glfwGetTime() * 10.0f;
|
||||||
// camera.position.x = sin(glfwGetTime()) * radius;
|
float radius = 15.0f;
|
||||||
// camera.position.z = cos(glfwGetTime()) * radius;
|
glm::mat4 model = glm::mat4(1.0f);
|
||||||
|
model = glm::scale(model, glm::vec3(109)); // The sun should be BIG
|
||||||
|
glm::vec3 sun_pos = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||||
|
|
||||||
glm::mat4 view = camera.getView();
|
model = glm::translate(model, sun_pos);
|
||||||
|
sun_shader.use();
|
||||||
|
sun_shader.setMat4("model", model);
|
||||||
|
sun_shader.setMat4("view", view);
|
||||||
|
sun_shader.setMat4("projection", projection);
|
||||||
|
sun_shader.setVec3("lightColor", lightColor);
|
||||||
|
|
||||||
|
glDrawElements(GL_TRIANGLES, cube.EBO_buffer.size(), GL_UNSIGNED_INT, nullptr);
|
||||||
|
|
||||||
|
// Switch to normal shader
|
||||||
|
shader.use();
|
||||||
shader.setMat4("projection", projection);
|
shader.setMat4("projection", projection);
|
||||||
shader.setMat4("view", view);
|
shader.setMat4("view", view);
|
||||||
|
|
||||||
// Fragment shader things
|
// Fragment shader things
|
||||||
shader.setVec3("lightPos", lightPos);
|
shader.setFloat("light_power", 40.0f);
|
||||||
|
shader.setVec3("lightPos", sun_pos);
|
||||||
shader.setVec3("viewPos", camera.position);
|
shader.setVec3("viewPos", camera.position);
|
||||||
shader.setVec3("lightColor", lightColor);
|
shader.setVec3("lightColor", lightColor);
|
||||||
shader.setVec3("objectColor", objectColor);
|
shader.setVec3("objectColor", objectColor);
|
||||||
@ -234,13 +247,20 @@ int main()
|
|||||||
// model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.5f, 0.0f));
|
// model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.5f, 0.0f));
|
||||||
// shader.setMat4("model", model);
|
// shader.setMat4("model", model);
|
||||||
|
|
||||||
for (unsigned int i = 0; i < 10; i++)
|
for (unsigned int i = 0; i < sizeof(planets) / sizeof(float[2]); 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(1.0f);
|
glm::mat4 model = glm::mat4(1.0f);
|
||||||
model = glm::translate(model, cubePositions[i]);
|
|
||||||
float angle = 20.0f * i;
|
float radius = planets[i][0];
|
||||||
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
|
model = glm::scale(model, glm::vec3(planets[i][1]));
|
||||||
|
|
||||||
|
float x = sin(glm::radians(0.0f)) * radius;
|
||||||
|
float z = cos(glm::radians(0.0f)) * radius;
|
||||||
|
|
||||||
|
model = glm::translate(model, glm::vec3(x, 0.0f, z));
|
||||||
|
|
||||||
|
|
||||||
shader.setMat4("model", model);
|
shader.setMat4("model", model);
|
||||||
|
|
||||||
// glDrawArrays(GL_TRIANGLES, 0, 36);
|
// glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||||
@ -272,6 +292,13 @@ int main()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float time_since_last_move = end_time - move_time;
|
||||||
|
if (time_since_last_move >= 0.017)
|
||||||
|
{
|
||||||
|
camera.move(time_since_last_move);
|
||||||
|
move_time = end_time;
|
||||||
|
}
|
||||||
|
|
||||||
// ------ FPS output ------
|
// ------ FPS output ------
|
||||||
if (delta_time >= 1)
|
if (delta_time >= 1)
|
||||||
{
|
{
|
||||||
@ -306,6 +333,8 @@ int main()
|
|||||||
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
|
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
|
||||||
void processInput(GLFWwindow *window)
|
void processInput(GLFWwindow *window)
|
||||||
{
|
{
|
||||||
|
float current_time = glfwGetTime();
|
||||||
|
|
||||||
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||||
// if (mouse_lock) {
|
// if (mouse_lock) {
|
||||||
// glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
// glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
||||||
@ -320,32 +349,75 @@ void processInput(GLFWwindow *window)
|
|||||||
// ---- Movement ----
|
// ---- Movement ----
|
||||||
// Forward / Backward
|
// Forward / Backward
|
||||||
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||||
camera.position += camera.speed * camera.target;
|
{
|
||||||
|
camera.forward_velocity = camera.speed * glm::vec3(camera.target.x, 0.0f, camera.target.z);
|
||||||
|
W_pressed = true;
|
||||||
|
}
|
||||||
|
|
||||||
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||||
camera.position -= camera.speed * camera.target;
|
{
|
||||||
|
camera.forward_velocity = -camera.speed * glm::vec3(camera.target.x, 0.0f, camera.target.z);
|
||||||
|
S_pressed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_RELEASE && S_pressed)
|
||||||
|
{
|
||||||
|
camera.forward_velocity = glm::vec3(0.0f);
|
||||||
|
S_pressed = false;
|
||||||
|
}
|
||||||
|
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_RELEASE && W_pressed)
|
||||||
|
{
|
||||||
|
camera.forward_velocity = glm::vec3(0.0f);
|
||||||
|
W_pressed = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Horizontal
|
// Horizontal
|
||||||
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||||
camera.position -= glm::normalize(glm::cross(camera.target, camera.worldUp)) * camera.speed;
|
{
|
||||||
|
camera.horizontal_velocity = -glm::normalize(glm::cross(glm::vec3(camera.target.x, 0.0f, camera.target.z), camera.worldUp)) * camera.speed;
|
||||||
|
A_pressed = true;
|
||||||
|
}
|
||||||
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||||
camera.position += glm::normalize(glm::cross(camera.target, camera.worldUp)) * camera.speed;
|
{
|
||||||
|
camera.horizontal_velocity = glm::normalize(glm::cross(glm::vec3(camera.target.x, 0.0f, camera.target.z), camera.worldUp)) * camera.speed;
|
||||||
|
D_pressed = true;
|
||||||
|
}
|
||||||
|
|
||||||
// Vertical
|
|
||||||
|
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_RELEASE && A_pressed)
|
||||||
|
{
|
||||||
|
camera.horizontal_velocity = glm::vec3(0.0f);
|
||||||
|
A_pressed = false;
|
||||||
|
}
|
||||||
|
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_RELEASE && D_pressed)
|
||||||
|
{
|
||||||
|
camera.horizontal_velocity = glm::vec3(0.0f);
|
||||||
|
D_pressed = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Jump
|
||||||
if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
||||||
camera.position -= glm::normalize(glm::cross(camera.target, glm::normalize(glm::cross(camera.target, camera.worldUp)))) * camera.speed * 0.5f;
|
// camera.velocity -= glm::normalize(glm::cross(camera.target, glm::normalize(glm::cross(camera.target, camera.worldUp)))) * camera.speed * 0.5f;
|
||||||
if(glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
|
{
|
||||||
camera.position += glm::normalize(glm::cross(camera.target, glm::normalize(glm::cross(camera.target, camera.worldUp)))) * camera.speed * 0.5f;
|
if (current_time - last_jump > jump_cooldown && camera.position.y == 0)
|
||||||
|
{
|
||||||
|
camera.vertical_velocity.y += 10.0f;
|
||||||
|
last_jump = current_time;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Sprint
|
// Sprint
|
||||||
if(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
|
if(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
|
||||||
camera.speed = camera_speed;
|
camera.speed = camera_speed * 7.5;
|
||||||
if(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_RELEASE)
|
if(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_RELEASE)
|
||||||
camera.speed = camera_speed * 5;
|
camera.speed = camera_speed * 20;
|
||||||
|
|
||||||
// World Flip
|
// World Flip
|
||||||
if(glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS)
|
if(glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS)
|
||||||
{
|
{
|
||||||
float current_time = glfwGetTime();
|
|
||||||
if (current_time - last_flip > flip_cooldown && !flipping)
|
if (current_time - last_flip > flip_cooldown && !flipping)
|
||||||
{
|
{
|
||||||
// flip_target = camera.worldUp;
|
// flip_target = camera.worldUp;
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
|
||||||
|
uniform float light_power;
|
||||||
uniform vec3 lightColor;
|
uniform vec3 lightColor;
|
||||||
uniform vec3 objectColor;
|
uniform vec3 objectColor;
|
||||||
uniform vec3 viewPos;
|
uniform vec3 viewPos;
|
||||||
@ -10,8 +11,9 @@ in vec3 FragPos;
|
|||||||
in vec3 Normal;
|
in vec3 Normal;
|
||||||
in vec3 LightPos;
|
in vec3 LightPos;
|
||||||
|
|
||||||
|
// const float light_power = 12.0;
|
||||||
const float ambientStrength = 0.2;
|
const float ambientStrength = 0.2;
|
||||||
const float shininess = 32.0;
|
const float shininess = 64.0;
|
||||||
const float screenGamma = 2.2; // Assume the monitor is calibrated to the sRGB color space
|
const float screenGamma = 2.2; // Assume the monitor is calibrated to the sRGB color space
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
@ -39,7 +41,7 @@ void main()
|
|||||||
specular = pow(specAngle, shininess);
|
specular = pow(specAngle, shininess);
|
||||||
}
|
}
|
||||||
|
|
||||||
vec3 colorLinear = (ambient + diffuse + specular) * objectColor;
|
vec3 colorLinear = (ambient + (diffuse * light_power) + (specular * light_power)) * objectColor;
|
||||||
|
|
||||||
// vec3 colorGammaCorrected = pow(colorLinear, vec3(1.0 / screenGamma));
|
// vec3 colorGammaCorrected = pow(colorLinear, vec3(1.0 / screenGamma));
|
||||||
|
|
||||||
|
|||||||
11
shaders/sun.frag
Normal file
11
shaders/sun.frag
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
uniform vec3 lightColor;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor = vec4(lightColor, 1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
15
shaders/sun.vert
Normal file
15
shaders/sun.vert
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec3 aNormal;
|
||||||
|
|
||||||
|
uniform mat4 model;
|
||||||
|
uniform mat4 view;
|
||||||
|
uniform mat4 projection;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user