Adjustments to follow mouse.

This commit is contained in:
David Vereb 2022-06-16 15:11:58 -04:00
parent e5af7a1f30
commit 94a7264386
5 changed files with 100 additions and 61 deletions

View File

@ -1,4 +1,5 @@
a.out: *.cpp *.h camera/*.cpp camera/*.h
clang++ -std=c++17 -g -o a.out main.cpp \
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,11 +176,11 @@ void draw(const Track &track)
// glColor3d(val, val, val);
glColor3d(200, 200, 200);
//glColor3d(200, 200, 200);
glVertex3f(p.lat,
p.ele,
p.lon);
}
glEnd();
glutPostRedisplay();
}

View File

@ -33,7 +33,7 @@ void Camera::Refresh()
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
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);
@ -45,7 +45,7 @@ void Camera::SetPos(float x, float y, float z)
m_y = y;
m_z = z;
Refresh();
// Refresh();
}
void Camera::GetPos(float &x, float &y, float &z)
@ -73,7 +73,7 @@ void Camera::SetTarget(Target t, float x, float y, float z)
t_z = z;
}
Refresh();
// Refresh();
}
void Camera::Move(float incr)
@ -86,7 +86,7 @@ void Camera::Move(float incr)
m_y = m_y + incr*ly;
m_z = m_z + incr*lz;
Refresh();
// Refresh();
}
void Camera::Strafe(float incr)
@ -94,21 +94,21 @@ void Camera::Strafe(float incr)
m_x = m_x + incr*m_strafe_lx;
m_y = m_y + incr*m_strafe_ly;
Refresh();
// Refresh();
}
void Camera::Fly(float incr)
{
m_y = m_y + incr;
Refresh();
// Refresh();
}
void Camera::RotateYaw(float angle)
{
m_yaw += angle;
Refresh();
// Refresh();
}
void Camera::RotatePitch(float angle)
@ -123,19 +123,19 @@ void Camera::RotatePitch(float angle)
if(m_pitch > limit)
m_pitch = limit;
Refresh();
// Refresh();
}
void Camera::SetYaw(float angle)
{
m_yaw = angle;
Refresh();
// Refresh();
}
void Camera::SetPitch(float angle)
{
m_pitch = angle;
Refresh();
// Refresh();
}

104
main.cpp
View File

@ -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,45 +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 */
// 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);
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 + 2,
// distance * 12.5,
// distance * new_z - 4);
// }
camera.Refresh();
float new_x = cos(angle);
float new_z = sin(angle);
camera.SetPos(distance * new_x,
distance,
distance * new_z);
}
draw_origin();
// draw_origin();
// DRAW TRACKS:
for(auto track : track_groups[random_group].second)
draw(*track);
glutPostRedisplay();
glutSwapBuffers();
}
@ -119,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[])
@ -197,17 +224,17 @@ 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[i].first.min.lat = 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[i].first.min.lon = 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[i].first.min.ele = 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[i].first.max.lat = 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[i].first.max.lon = 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[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 << ", "
@ -308,7 +335,8 @@ int main(int argc, char *argv[])
p.lat *= 500;
p.lon *= 500;
p.ele *= 0.1;
// p.ele *= 0.001;
p.ele *= 0.01;
}
}
// }