Fixed issues with render.

This commit is contained in:
David Vereb 2021-06-21 23:11:21 -04:00
parent af9286fbc9
commit ee44dfde4d
5 changed files with 116 additions and 45 deletions

View File

@ -1,4 +1,4 @@
a.out: *.cpp
a.out: *.cpp *.h camera/*.cpp camera/*.h
clang++ -std=c++17 -g -o a.out main.cpp \
camera/Camera.cpp Track.cpp Xml.cpp \
-lGL -lGLU -lglut -lstdc++fs

View File

@ -167,7 +167,10 @@ void draw(const Track &track)
// glColor3d(val, val, val);
glColor3d(200, 200, 200);
glVertex3f(p.lat, p.ele, p.lon);
glVertex3f(p.lat,// - track.min.lat,
p.ele,// - track.min.ele,
p.lon);// - track.min.lon);
static bool asdf = true;
}
glEnd();

View File

@ -8,25 +8,35 @@ void Camera::Init()
m_yaw = 0.0;
m_pitch = 0.0;
SetPos(0, 0, 0);
target = Target::FORWARD;
t_x = t_y = t_z = 0.0f;
SetPos(0, 20, 0);
}
void Camera::Refresh()
{
// Camera parameter according to Riegl's co-ordinate system
// x/y for flat, z for height
// x/z for flat, y for height
m_lx = cos(m_yaw) * cos(m_pitch);
m_ly = sin(m_pitch);
m_lz = sin(m_yaw) * cos(m_pitch);
m_ly = sin(m_yaw) * cos(m_pitch);
m_lz = sin(m_pitch);
m_strafe_lx = cos(m_yaw - M_PI_2);
m_strafe_lz = sin(m_yaw - M_PI_2);
m_strafe_ly = sin(m_yaw - M_PI_2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(m_x, m_y, m_z, m_x + m_lx, m_y + m_ly, m_z + m_lz, 0.0,1.0,0.0);
if(target == Target::FORWARD)
gluLookAt(m_x, m_y, m_z, // eye at
m_x + m_lx, m_y + m_ly, m_z + m_lz, // look at
0.0,1.0,0.0); // up
else
gluLookAt(m_x, m_y, m_z, // eye at
0.0f, 0.0f, 0.0f, // look at
0.0f, 1.0f, 0.0f); // up
printf("Camera: %f %f %f Direction vector: %f %f %f\n", m_x, m_y, m_z, m_lx, m_ly, m_lz);
// printf("Camera: %f %f %f Direction vector: %f %f %f\n", m_x, m_y, m_z, m_lx, m_ly, m_lz);
}
void Camera::SetPos(float x, float y, float z)
@ -52,11 +62,25 @@ void Camera::GetDirectionVector(float &x, float &y, float &z)
z = m_lz;
}
void Camera::SetTarget(Target t, float x, float y, float z)
{
target = t;
if(target == Target::POINT)
{
t_x = x;
t_y = y;
t_z = z;
}
Refresh();
}
void Camera::Move(float incr)
{
float lx = cos(m_yaw)*cos(m_pitch);
float ly = sin(m_pitch);
float lz = sin(m_yaw)*cos(m_pitch);
float ly = sin(m_yaw)*cos(m_pitch);
float lz = sin(m_pitch);
m_x = m_x + incr*lx;
m_y = m_y + incr*ly;
@ -68,7 +92,7 @@ void Camera::Move(float incr)
void Camera::Strafe(float incr)
{
m_x = m_x + incr*m_strafe_lx;
m_z = m_z + incr*m_strafe_lz;
m_y = m_y + incr*m_strafe_ly;
Refresh();
}

View File

@ -11,6 +11,11 @@ public:
Camera() { Init(); }
~Camera(){}
enum class Target {
FORWARD,
POINT,
};
void Init();
void Refresh();
void SetPos(float x, float y, float z);
@ -19,6 +24,9 @@ public:
void SetYaw(float angle);
void SetPitch(float angle);
// NOTE(dev): x,y,z is only set if target is POINT
void SetTarget(Target target, float x = 0.0f, float y = 0.0f, float z = 0.0f);
// Navigation
void Move(float incr);
void Strafe(float incr);
@ -30,7 +38,10 @@ private:
float m_x, m_y, m_z; // Position
float m_lx, m_ly, m_lz; // Direction vector of where we are looking at
float m_yaw, m_pitch; // Various rotation angles
float m_strafe_lx, m_strafe_lz; // Always 90 degree to direction vector
float m_strafe_lx, m_strafe_ly; // Always 90 degree to direction vector
float t_x, t_y, t_z; // when in TARGET mode, look at this x,y,z coordinate
Target target;
};
#endif

View File

@ -9,7 +9,7 @@
#include <syslog.h>
#include <cstdlib>
#include <iostream>
#include <math.h> // fmod
#include <cmath> // fmod
#include <unistd.h>
#include <vector>
@ -71,9 +71,29 @@ void display()
// gluLookAt(-15.0, 30.0, -asdf, /* eye is at */
// 0.0, 5.0, 0.0, /* center is at */
// 0.0, 1.0, 0.0); /* up is in positive Y direction */
gluLookAt(-20.0, 20.0, 8.0, /* eye is at */
-5.0, 2.5, 0.0, /* center is at */
0.0, 1.0, 0.0); /* up is in positive Y direction */
static int a = 0;
if(a < 400)
{
camera.RotatePitch(0.03f);
camera.RotateYaw(0.01f);
camera.Move(-0.1f);
if(++a == 200)
camera.SetTarget(Camera::Target::POINT); // center
}
else
{
static float angle = 0;
const int distance = 50;
angle += 0.01f;
angle = fmod(angle, 360.0);
float new_x = cos(angle);
float new_z = sin(angle);
camera.SetPos(distance * new_x,
distance,
distance * new_z);
}
draw_origin();
@ -138,7 +158,7 @@ int main(int argc, char *argv[])
// First, put all the tracks in their own group
// Second, keep checking to see if any groups overlap until there are no changes
// std::vector<std::pair<Track, std::vector<const Track*>>> track_groups;
for(const auto &track : tracks)
for(auto &track : tracks)
{
// Build a header-only copy of each track
Track header;
@ -190,6 +210,14 @@ int main(int argc, char *argv[])
track_groups[i].first.max.ele = std::fmax(track_groups[i].first.max.ele,
track_groups[j].first.max.ele);
// std::cout << "first.min.lat: " << track_groups[i].first.min.lat << ", "
// << "first.min.lon: " << track_groups[i].first.min.lon << ", "
// << "first.min.lon: " << track_groups[i].first.min.ele << ", "
// << "first.min.lon: " << track_groups[i].first.max.lat << ", "
// << "first.min.lon: " << track_groups[i].first.max.lon << ", "
// << "first.min.lon: " << track_groups[i].first.max.ele << ", "
// << std::endl;
// Add tracks vector to make one big one
track_groups[i].second.insert(track_groups[i].second.end(),
track_groups[j].second.begin(),
@ -226,6 +254,7 @@ int main(int argc, char *argv[])
random_group = 0;
float min_lat, max_lat, min_lon, max_lon, min_ele, max_ele;
first = true; // reset!
for(auto &track : track_groups[random_group].second)
{
for(auto &p : track->points)
@ -259,29 +288,33 @@ int main(int argc, char *argv[])
<< "min_ele: " << min_ele << " max_ele: " << max_ele << std::endl;
auto lat_diff = min_lat + ((max_lat - min_lat) / 2.0);
auto lon_diff = min_lon + ((max_lon - min_lon) / 2.0);
auto ele_diff = min_ele;
for(auto &track : track_groups[random_group].second)
{
// auto ele_diff = min_ele;
std::cout << "lat_diff: " << lat_diff << ", "
<< "lon_diff: " << lon_diff << ", "
// << "ele_diff: " << ele_diff << ", "
<< std::endl;
// for(Track &track : track_groups[random_group].second)
// {
for(auto &track : tracks)
{
track.max.lat -= lat_diff;
track.max.lon -= lon_diff;
track.max.ele -= ele_diff;
// track.max.ele -= ele_diff;
track.min.lat -= lat_diff;
track.min.lon -= lon_diff;
track.min.ele -= ele_diff;
// track.min.ele -= ele_diff;
for(auto &p : track.points)
{
p.lat -= lat_diff;
p.lon -= lon_diff;
p.ele -= ele_diff;
// p.ele -= ele_diff;
p.lat *= 500;
p.lon *= 500;
p.ele *= 0;
}
p.ele *= 0.1;
}
}
// }
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_ALPHA | GLUT_MULTISAMPLE);
glutInit(&argc, argv);
@ -294,8 +327,8 @@ int main(int argc, char *argv[])
init();
camera.Init();
camera.Fly(50);
camera.RotatePitch(-89 * M_PI / 180.0);
// camera.Strafe(-15);
// camera.RotatePitch(-89 * M_PI / 180.0);
glutMainLoop();
return 0;