From a25f8107ba6947b142de40f3ecddeff6da68423b Mon Sep 17 00:00:00 2001 From: Erik Cowley Date: Fri, 20 Oct 2023 21:46:03 -0400 Subject: [PATCH] Fix for ghosts being rendered in correct order --- ghost.cpp | 4 ++++ ghost.h | 1 + ghostland.cpp | 23 ++++++++++++++++++----- player.cpp | 13 +++++++------ player.h | 15 +++++++++------ 5 files changed, 39 insertions(+), 17 deletions(-) diff --git a/ghost.cpp b/ghost.cpp index 985cb27..95780d6 100644 --- a/ghost.cpp +++ b/ghost.cpp @@ -107,3 +107,7 @@ float rand_float(float rmin, float rmax) { float res = (float)rand()/(float)RAND_MAX; return res * rdiff + rmin; } + +glm::vec3 Ghost::get_pos() const { + return pos; +} diff --git a/ghost.h b/ghost.h index 05873a2..96b32a1 100644 --- a/ghost.h +++ b/ghost.h @@ -8,6 +8,7 @@ class Ghost { Ghost(float xmin, float xmax, float zmin, float zmax); void apply_movement(float curr_time, float timed); glm::mat4 get_model(glm::vec3 &camera_pos); + glm::vec3 get_pos() const; private: int ghost_id; diff --git a/ghostland.cpp b/ghostland.cpp index 01cd03a..5ef80f4 100644 --- a/ghostland.cpp +++ b/ghostland.cpp @@ -1,3 +1,4 @@ +#include #include #include #include "glad/glad.h" @@ -60,8 +61,7 @@ float *wall_vertices; int trailmax = 1800; -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { int success; glm::vec3 position; @@ -343,7 +343,7 @@ int main(int argc, char *argv[]) ghosts.push_back(new Ghost(xmin_wall, xmax_wall, zmin_wall, zmax_wall)); } - player->mouse_callback(window, 0.0f, WINDOWHEIGHT); + player->mouse_callback(window, WINDOWWIDTH/2, WINDOWHEIGHT); int FPS = -1; @@ -353,7 +353,7 @@ int main(int argc, char *argv[]) int current_frameI = (int)current_frame; if (current_frameI != time_secI) { // show stats every second FPS = num_frames; - glm::vec3 player_pos = player->get_position(); + glm::vec3 player_pos = player->get_pos(); float yaw, pitch; yaw = player->get_yaw(); pitch = player->get_pitch(); @@ -468,7 +468,20 @@ int main(int argc, char *argv[]) set_uniform(ghost_program, projectionC, projection); set_uniform(ghost_program, viewC, view); - for (int i = 0; i < ghosts.size(); i++) { + + struct { + bool operator()(const Ghost *a, const Ghost *b) const { + glm::vec3 a_diff, b_diff; + a_diff = player->get_pos() - a->get_pos(); + b_diff = player->get_pos() - b->get_pos(); + float a_dist2, b_dist2; + a_dist2 = a_diff.x * a_diff.x + a_diff.y * a_diff.y + a_diff.z * a_diff.z; + b_dist2 = b_diff.x * b_diff.x + b_diff.y * b_diff.y + b_diff.z * b_diff.z; + return a_dist2 < b_dist2; + } + } ghost_compare; + std::sort(ghosts.begin(), ghosts.end(), ghost_compare); + for (int i = ghosts.size()/5; i >= 0; i--) { glm::mat4 ghost_model = ghosts[i]->get_model(camera_pos); ghosts[i]->apply_movement(current_frame, timed); set_uniform(ghost_program, modelC, ghost_model); diff --git a/player.cpp b/player.cpp index f6f2024..a378d6a 100644 --- a/player.cpp +++ b/player.cpp @@ -22,27 +22,27 @@ Player::Player(glm::vec3 startpos, float startyaw) { } -glm::vec3 Player::get_position() { +glm::vec3 Player::get_pos() const { return position; } -glm::vec3 Player::get_camera_front() { +glm::vec3 Player::get_camera_front() const { return camera_front; } -glm::vec3 Player::get_camera_up() { +glm::vec3 Player::get_camera_up() const { return camera_up; } -glm::vec3 Player::get_camera_pos() { +glm::vec3 Player::get_camera_pos() const { return position + camera_offset; } -glm::vec3 Player::get_light_pos() { +glm::vec3 Player::get_light_pos() const { return get_camera_pos() + get_light_front() * armlength; } -glm::vec3 Player::get_light_front() { +glm::vec3 Player::get_light_front() const { float yawp = yaw + light_xpersist; float pitchp = pitch + light_ypersist; glm::vec3 front; @@ -318,3 +318,4 @@ void Player::scroll_callback(GLFWwindow* window, double xoffset, double yoffset) fov = 90.0f; } } + diff --git a/player.h b/player.h index 5a7b9d5..9e33b24 100644 --- a/player.h +++ b/player.h @@ -6,6 +6,7 @@ #include #include "collisions.h" +#include "ghost.h" const float lightoffsetmax = 15.0; const float light_persist_factor = 0.99; @@ -24,12 +25,12 @@ class Player { int num_walls; float *wall_vertices; - glm::vec3 get_position(); - glm::vec3 get_camera_front(); - glm::vec3 get_camera_up(); - glm::vec3 get_camera_pos(); - glm::vec3 get_light_pos(); - glm::vec3 get_light_front(); + glm::vec3 get_pos() const; + glm::vec3 get_camera_front() const; + glm::vec3 get_camera_up() const; + glm::vec3 get_camera_pos() const; + glm::vec3 get_light_pos() const; + glm::vec3 get_light_front() const; float get_yaw(); float get_pitch(); float get_fov(); @@ -40,6 +41,8 @@ class Player { bool is_in_air(); void apply_movement(float timed); + bool operator()(const Ghost &a, const Ghost &b) const; + private: float yaw; float pitch;