Fix for ghosts being rendered in correct order
This commit is contained in:
@@ -107,3 +107,7 @@ float rand_float(float rmin, float rmax) {
|
|||||||
float res = (float)rand()/(float)RAND_MAX;
|
float res = (float)rand()/(float)RAND_MAX;
|
||||||
return res * rdiff + rmin;
|
return res * rdiff + rmin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glm::vec3 Ghost::get_pos() const {
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|||||||
1
ghost.h
1
ghost.h
@@ -8,6 +8,7 @@ class Ghost {
|
|||||||
Ghost(float xmin, float xmax, float zmin, float zmax);
|
Ghost(float xmin, float xmax, float zmin, float zmax);
|
||||||
void apply_movement(float curr_time, float timed);
|
void apply_movement(float curr_time, float timed);
|
||||||
glm::mat4 get_model(glm::vec3 &camera_pos);
|
glm::mat4 get_model(glm::vec3 &camera_pos);
|
||||||
|
glm::vec3 get_pos() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int ghost_id;
|
int ghost_id;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#include <algorithm>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "glad/glad.h"
|
#include "glad/glad.h"
|
||||||
@@ -60,8 +61,7 @@ float *wall_vertices;
|
|||||||
|
|
||||||
int trailmax = 1800;
|
int trailmax = 1800;
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[]) {
|
||||||
{
|
|
||||||
|
|
||||||
int success;
|
int success;
|
||||||
glm::vec3 position;
|
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));
|
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;
|
int FPS = -1;
|
||||||
|
|
||||||
@@ -353,7 +353,7 @@ int main(int argc, char *argv[])
|
|||||||
int current_frameI = (int)current_frame;
|
int current_frameI = (int)current_frame;
|
||||||
if (current_frameI != time_secI) { // show stats every second
|
if (current_frameI != time_secI) { // show stats every second
|
||||||
FPS = num_frames;
|
FPS = num_frames;
|
||||||
glm::vec3 player_pos = player->get_position();
|
glm::vec3 player_pos = player->get_pos();
|
||||||
float yaw, pitch;
|
float yaw, pitch;
|
||||||
yaw = player->get_yaw();
|
yaw = player->get_yaw();
|
||||||
pitch = player->get_pitch();
|
pitch = player->get_pitch();
|
||||||
@@ -468,7 +468,20 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
set_uniform(ghost_program, projectionC, projection);
|
set_uniform(ghost_program, projectionC, projection);
|
||||||
set_uniform(ghost_program, viewC, view);
|
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);
|
glm::mat4 ghost_model = ghosts[i]->get_model(camera_pos);
|
||||||
ghosts[i]->apply_movement(current_frame, timed);
|
ghosts[i]->apply_movement(current_frame, timed);
|
||||||
set_uniform(ghost_program, modelC, ghost_model);
|
set_uniform(ghost_program, modelC, ghost_model);
|
||||||
|
|||||||
13
player.cpp
13
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;
|
return position;
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec3 Player::get_camera_front() {
|
glm::vec3 Player::get_camera_front() const {
|
||||||
return camera_front;
|
return camera_front;
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec3 Player::get_camera_up() {
|
glm::vec3 Player::get_camera_up() const {
|
||||||
return camera_up;
|
return camera_up;
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec3 Player::get_camera_pos() {
|
glm::vec3 Player::get_camera_pos() const {
|
||||||
return position + camera_offset;
|
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;
|
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 yawp = yaw + light_xpersist;
|
||||||
float pitchp = pitch + light_ypersist;
|
float pitchp = pitch + light_ypersist;
|
||||||
glm::vec3 front;
|
glm::vec3 front;
|
||||||
@@ -318,3 +318,4 @@ void Player::scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
|||||||
fov = 90.0f;
|
fov = 90.0f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
15
player.h
15
player.h
@@ -6,6 +6,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "collisions.h"
|
#include "collisions.h"
|
||||||
|
#include "ghost.h"
|
||||||
|
|
||||||
const float lightoffsetmax = 15.0;
|
const float lightoffsetmax = 15.0;
|
||||||
const float light_persist_factor = 0.99;
|
const float light_persist_factor = 0.99;
|
||||||
@@ -24,12 +25,12 @@ class Player {
|
|||||||
int num_walls;
|
int num_walls;
|
||||||
float *wall_vertices;
|
float *wall_vertices;
|
||||||
|
|
||||||
glm::vec3 get_position();
|
glm::vec3 get_pos() const;
|
||||||
glm::vec3 get_camera_front();
|
glm::vec3 get_camera_front() const;
|
||||||
glm::vec3 get_camera_up();
|
glm::vec3 get_camera_up() const;
|
||||||
glm::vec3 get_camera_pos();
|
glm::vec3 get_camera_pos() const;
|
||||||
glm::vec3 get_light_pos();
|
glm::vec3 get_light_pos() const;
|
||||||
glm::vec3 get_light_front();
|
glm::vec3 get_light_front() const;
|
||||||
float get_yaw();
|
float get_yaw();
|
||||||
float get_pitch();
|
float get_pitch();
|
||||||
float get_fov();
|
float get_fov();
|
||||||
@@ -40,6 +41,8 @@ class Player {
|
|||||||
bool is_in_air();
|
bool is_in_air();
|
||||||
void apply_movement(float timed);
|
void apply_movement(float timed);
|
||||||
|
|
||||||
|
bool operator()(const Ghost &a, const Ghost &b) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float yaw;
|
float yaw;
|
||||||
float pitch;
|
float pitch;
|
||||||
|
|||||||
Reference in New Issue
Block a user