-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.cpp
More file actions
142 lines (102 loc) · 2.75 KB
/
Window.cpp
File metadata and controls
142 lines (102 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "Camera.h"
#include "Window.h"
//////////////////////////////////////////////////////////////////////////
extern USHORT WINDOW_W = 800;
extern USHORT WINDOW_H = 600;
extern std::vector<float3> G_RESULT_POINTS(0);
void DrawPoints(const std::vector<float3>& a_points)
{
glBegin(GL_POINTS);
glColor3f(1.0F, 1.0F, 1.0F);
for (auto& point : a_points)
glVertex3f(point.x, point.y, point.z);
glEnd();
}
void DrawObjects(const std::vector<float3>& points)
{
// stars: to simulate infinite distance,
// translate the sphere of stars to the eye
// and perform the arcball rotation around the eye
// (also disable depth test so they become the background)
//glPushMatrix();
//glDisable( GL_DEPTH_TEST );
//glTranslatef( eye.x, eye.y, eye.z );
//arcball_rotate();
//draw_stars();
//glEnable( GL_DEPTH_TEST );
//glPopMatrix();
// now render the regular scene under the arcball rotation about 0,0,0
// (generally you would want to render everything here)
//m_scene.m_camera.arcball_rotate();
G_CAMERA.arcball_rotate();
DrawPoints(points);
}
void reset_view(int w, int h)
{
const float aspectRatio = (float)(w) / (float)(h);
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(20.0f, aspectRatio, 1.0f, 100.0f);
gluLookAt(
G_CAMERA.m_eye.x, G_CAMERA.m_eye.y, G_CAMERA.m_eye.z,
G_CAMERA.m_centre.x, G_CAMERA.m_centre.y, G_CAMERA.m_centre.z,
G_CAMERA.m_up.x, G_CAMERA.m_up.y, G_CAMERA.m_up.z);
// set up the arcball using the current projection matrix
G_CAMERA.arcball_setzoom(G_CAMERA.SPHERE_RADIUS, G_CAMERA.m_eye, G_CAMERA.m_up);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//////////////////////////////////////////////////////
// GLUT Callbacks
//////////////////////////////////////////////////////
void resize(int w, int h)
{
reset_view(w, h);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
DrawObjects(G_RESULT_POINTS);
glutSwapBuffers();
}
void idle()
{
glutPostRedisplay();
}
// Mouse and keyboard
void key(unsigned char key, int x, int y)
{
switch (key)
{
case 27: // Esc
case 'q':
exit(0);
//break;
default:
break;
}
glutPostRedisplay();
}
void click_scene(const int x, const int y)
{
int invert_y = (WINDOW_H - y) - 1; // OpenGL viewport coordinates are Cartesian
G_CAMERA.arcball_start(x, invert_y);
}
void drag_scene(const int x, const int y)
{
int invert_y = (WINDOW_H - y) - 1;
G_CAMERA.arcball_move(x, invert_y);
}
void mouse_button(const int button, const int state, const int x, const int y)
{
if (state == GLUT_DOWN)
click_scene(x, y);
}
void mouse_motion(const int x, const int y)
{
// glutMotionFunc only called when a mouse button is held down
drag_scene(x, y);
}