playing with another shader for a sun type thing and WAY better camera movement

This commit is contained in:
Jackson H committed 2024-06-04 15:25:14 -05:00
1 parent 9eb8d55630
commit b299a8d8a2
5 files changed
+175 -41

No files matched your search

+109 -37
View File
@@ -31,8 +31,14 @@ int flipped = 1; // 1 for normal, -1 for flipped
bool flipping = false;
glm::vec3 flip_target = glm::vec3(0.0f);
// Jump
const float jump_cooldown = 1.0f;
float last_jump = 0.0f;
// 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 Z_FAR = 1000.0f;
const float Z_NEAR = 0.1f;
@@ -41,31 +47,24 @@ const float TARGET_FPS = 60.0f;
const float FOV = 90.0f;
float sleep_time = 15.0f;
bool W_pressed, S_pressed, A_pressed, D_pressed = false;
glm::mat4 projection = glm::mat4(1.0f);
float yaw, pitch = 0;
// Camera
Camera camera = Camera(camera_speed);
Camera camera = Camera(camera_speed, glm::vec3(0.0f, 0.0f, 115.0f));
// Lighting
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[] = {
glm::vec3( 0.0f, 0.0f, 0.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)
float planets[][2] = {
{140.0f, 1.0f}
};
// Mouse Look
@@ -135,6 +134,7 @@ int main()
// ------ Setup Shader ------
Shader shader("shaders/shader.vert", "shaders/shader.frag");
Shader sun_shader("shaders/sun.vert", "shaders/sun.frag");
// ------ World Objects ------
std::vector<Object3D> world_objects;
@@ -185,6 +185,7 @@ int main()
glEnable(GL_DEPTH_TEST); // Z-Buffer
double start_time = glfwGetTime();
double move_time = glfwGetTime();
// Setup mouse look
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
@@ -201,24 +202,36 @@ int main()
processInput(window);
// ------ SETUP Matrices ------
shader.use();
glm::mat4 view = camera.getView();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Z-Buffer
// Camera Rotate around origin
// const float radius = 10.0f;
// camera.position.x = sin(glfwGetTime()) * radius;
// camera.position.z = cos(glfwGetTime()) * radius;
// Orbitting sun thing?
float t = glfwGetTime() * 10.0f;
float radius = 15.0f;
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("view", view);
// 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("lightColor", lightColor);
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));
// 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
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, cubePositions[i]);
float angle = 20.0f * i;
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
float radius = planets[i][0];
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);
// 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 ------
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
void processInput(GLFWwindow *window)
{
float current_time = glfwGetTime();
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
// if (mouse_lock) {
// glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
@@ -320,32 +349,75 @@ void processInput(GLFWwindow *window)
// ---- Movement ----
// Forward / Backward
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)
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
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)
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)
camera.position -= 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;
// camera.velocity -= 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
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)
camera.speed = camera_speed * 5;
camera.speed = camera_speed * 20;
// World Flip
if(glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS)
{
float current_time = glfwGetTime();
if (current_time - last_flip > flip_cooldown && !flipping)
{
// flip_target = camera.worldUp;