Fix for ghosts being rendered in correct order

This commit is contained in:
2023-10-20 21:46:03 -04:00
parent a40cef5e93
commit a25f8107ba
5 changed files with 39 additions and 17 deletions

View File

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

View File

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

View File

@@ -1,3 +1,4 @@
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#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);

View File

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

View File

@@ -6,6 +6,7 @@
#include <string>
#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;