Compare commits

...

4 Commits

Author SHA1 Message Date
David Vereb
94a7264386 Adjustments to follow mouse. 2022-06-16 15:11:58 -04:00
David Vereb
e5af7a1f30 Bugfix for debug info. Some cleanup. 2021-06-22 16:40:39 -04:00
ee44dfde4d Fixed issues with render. 2021-06-21 23:11:21 -04:00
af9286fbc9 Added missing camera directory. 2021-06-15 21:13:59 -04:00
5 changed files with 305 additions and 49 deletions

View File

@ -1,4 +1,5 @@
a.out: *.cpp
clang++ -std=c++17 -g -o a.out main.cpp \
a.out: *.cpp *.h camera/*.cpp camera/*.h
clang++ -std=c++14 -g -o a.out main.cpp \
camera/Camera.cpp Track.cpp Xml.cpp \
-lGL -lGLU -lglut -lstdc++fs
-lGL -lGLU -lglut -lstdc++fs \
#-fsanitize=address -fno-omit-frame-pointer

View File

@ -155,6 +155,16 @@ void draw(const Track &track)
// Draw bike path
glBegin(GL_LINE_STRIP);
srand((long long)(&track));
float r = rand() % 200 + 50;
float g = rand() % 200 + 50;
float b = rand() % 200 + 50;
r /= 255.0;
g /= 255.0;
b /= 255.0;
glColor3d(r, g, b);
for(auto p : track.points)
{
// auto val = (50 + (p.ele * 10)) / 180.0;
@ -166,12 +176,11 @@ void draw(const Track &track)
// glColor3d(val, val, val);
glColor3d(200, 200, 200);
glVertex3f(p.lat, p.ele, p.lon);
//glColor3d(200, 200, 200);
glVertex3f(p.lat,
p.ele,
p.lon);
}
glEnd();
// usleep(100000);
glutPostRedisplay();
}

141
camera/Camera.cpp Normal file
View File

@ -0,0 +1,141 @@
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include "Camera.h"
void Camera::Init()
{
m_yaw = 0.0;
m_pitch = 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/z for flat, y for height
m_lx = cos(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_ly = sin(m_yaw - M_PI_2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
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
2.0f, 0.0f, -4.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);
}
void Camera::SetPos(float x, float y, float z)
{
m_x = x;
m_y = y;
m_z = z;
// Refresh();
}
void Camera::GetPos(float &x, float &y, float &z)
{
x = m_x;
y = m_y;
z = m_z;
}
void Camera::GetDirectionVector(float &x, float &y, float &z)
{
x = m_lx;
y = m_ly;
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_yaw)*cos(m_pitch);
float lz = sin(m_pitch);
m_x = m_x + incr*lx;
m_y = m_y + incr*ly;
m_z = m_z + incr*lz;
// Refresh();
}
void Camera::Strafe(float incr)
{
m_x = m_x + incr*m_strafe_lx;
m_y = m_y + incr*m_strafe_ly;
// Refresh();
}
void Camera::Fly(float incr)
{
m_y = m_y + incr;
// Refresh();
}
void Camera::RotateYaw(float angle)
{
m_yaw += angle;
// Refresh();
}
void Camera::RotatePitch(float angle)
{
const float limit = 89.0 * M_PI / 180.0;
m_pitch += angle;
if(m_pitch < -limit)
m_pitch = -limit;
if(m_pitch > limit)
m_pitch = limit;
// Refresh();
}
void Camera::SetYaw(float angle)
{
m_yaw = angle;
// Refresh();
}
void Camera::SetPitch(float angle)
{
m_pitch = angle;
// Refresh();
}

47
camera/Camera.h Normal file
View File

@ -0,0 +1,47 @@
#ifndef __CAMERA_H__
#define __CAMERA_H__
/*
Generic camera class by Nghia Ho
*/
class Camera
{
public:
Camera() { Init(); }
~Camera(){}
enum class Target {
FORWARD,
POINT,
};
void Init();
void Refresh();
void SetPos(float x, float y, float z);
void GetPos(float &x, float &y, float &z);
void GetDirectionVector(float &x, float &y, float &z);
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);
void Fly(float incr);
void RotateYaw(float angle);
void RotatePitch(float angle);
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_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

138
main.cpp
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>
@ -18,6 +18,33 @@ GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; /* Infinite light location. */
Camera camera;
void (*passiveMouseFunc)(int x, int y) = [](int x, int y)
{
static int last_x = x;
static int last_y = y;
const int diff_x = x - last_x;
const int diff_y = y - last_y;
last_x = x;
last_y = y;
const int distance = 5;
static float angle_horizontal = 0.0;
static float angle_vertical = 0.0;
angle_horizontal += diff_x / 100.0f;
angle_vertical += diff_y / 100.0f;
angle_horizontal = fmod(angle_horizontal, 6.283185);
angle_vertical = fmod(angle_vertical, 6.283185);
float new_x = cos(angle_horizontal);
float new_y = sin(angle_vertical);
float new_z = sin(angle_horizontal);
camera.SetPos(distance * new_x,
distance * 12.5 * new_y,
distance * new_z);
};
// TODO(dev):
// As per https://www.opengl.org/resources/libraries/glut/spec3/node11.html
// "Therefore, GLUT programs should not assume the window was created at the specified size or position.
@ -62,25 +89,40 @@ void display()
glLineWidth(1.0f);
camera.SetTarget(Camera::Target::POINT); // center
//camera.RotateYaw(0.01);
static double asdf = -10.0;
asdf += 0.02;
if(asdf > 15.0)
asdf = -15.0;
// 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 if(a++ == 400) // ONCE
// {
// static float angle = 0;
// const int distance = 5;
// angle += 0.001f;
// angle = fmod(angle, 360.0);
draw_origin();
// float new_x = cos(angle);
// float new_z = sin(angle);
// camera.SetPos(distance * new_x + 2,
// distance * 12.5,
// distance * new_z - 4);
// }
camera.Refresh();
// draw_origin();
// DRAW TRACKS:
for(auto track : track_groups[random_group].second)
draw(*track);
glutPostRedisplay();
glutSwapBuffers();
}
@ -99,13 +141,18 @@ void init()
/* Setup the view of the cube. */
glMatrixMode(GL_PROJECTION);
gluPerspective( /* field of view in degree */ 70.0,
gluPerspective( /* field of view in degree */ 90.0,
/* aspect ratio */ 1.0,
/* Z near */ 1.0, /* Z far */ 1000.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(-10.0, 25.0, -10.0, /* eye is at */
0.0, 0.0, 0.0, /* center is at */
0.0, 1.0, 0.0); /* up is in positive Y direction */
camera.SetTarget(Camera::Target::POINT); // center
// handle mouse input
glutPassiveMotionFunc(passiveMouseFunc);
}
int main(int argc, char *argv[])
@ -138,7 +185,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;
@ -177,18 +224,26 @@ int main(int argc, char *argv[])
if(lat_overlap && lon_overlap)
{
no_changes = false;
track_groups[i].first.min.lat = std::fmin(track_groups[i].first.min.lat,
track_groups[j].first.min.lat);
track_groups[i].first.min.lon = std::fmin(track_groups[i].first.min.lon,
track_groups[j].first.min.lon);
track_groups[i].first.min.ele = std::fmin(track_groups[i].first.min.ele,
track_groups[j].first.min.ele);
track_groups[i].first.max.lat = std::fmax(track_groups[i].first.max.lat,
track_groups[j].first.max.lat);
track_groups[i].first.max.lon = std::fmax(track_groups[i].first.max.lon,
track_groups[j].first.max.lon);
track_groups[i].first.max.ele = std::fmax(track_groups[i].first.max.ele,
track_groups[j].first.max.ele);
track_groups[i].first.min.lat = fmin(track_groups[i].first.min.lat,
track_groups[j].first.min.lat);
track_groups[i].first.min.lon = fmin(track_groups[i].first.min.lon,
track_groups[j].first.min.lon);
track_groups[i].first.min.ele = fmin(track_groups[i].first.min.ele,
track_groups[j].first.min.ele);
track_groups[i].first.max.lat = fmax(track_groups[i].first.max.lat,
track_groups[j].first.max.lat);
track_groups[i].first.max.lon = fmax(track_groups[i].first.max.lon,
track_groups[j].first.max.lon);
track_groups[i].first.max.ele = 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(),
@ -216,16 +271,17 @@ int main(int argc, char *argv[])
<< " " << track_group.first.min.lat
<< "-" << track_group.first.max.lat << std::endl;
std::cout << " lon"
<< " " << track_group.first.min.lat
<< "-" << track_group.first.max.lat << std::endl;
<< " " << track_group.first.min.lon
<< "-" << track_group.first.max.lon << std::endl;
std::cout << " ele"
<< " " << track_group.first.min.lat
<< "-" << track_group.first.max.lat << std::endl;
<< " " << track_group.first.min.ele
<< "-" << track_group.first.max.ele << std::endl;
}
std::cout << std::endl;
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 +315,31 @@ 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.min.lat -= lat_diff;
track.min.lon -= lon_diff;
track.min.ele -= ele_diff;
for(auto &p : track.points)
{
p.lat -= lat_diff;
p.lon -= lon_diff;
p.ele -= ele_diff;
p.lat *= 500;
p.lon *= 500;
p.ele *= 0;
// p.ele *= 0.001;
p.ele *= 0.01;
}
}
}
// }
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_ALPHA | GLUT_MULTISAMPLE);
glutInit(&argc, argv);
@ -294,8 +352,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;