-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.cpp
161 lines (138 loc) · 3.74 KB
/
display.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "display.hpp"
using namespace std;
void drawCursor(glm::vec4 focus)
{
float x = focus.x;
float y = focus.y;
float z = focus.z;
// spacing between cursor and line inner point
float in = 0.02;
// line length
float len = 0.04;
// vertical
glBegin(GL_LINES);
// upper
glColor3f(0.0, 0.0, 1.0);
glVertex3f(x, y, z + in);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(x, y, z + in + len);
// lower
glColor3f(0.3, 0.3, 0.3);
glVertex3f(x, y, z - in);
glColor3f(0.3, 0.3, 0.3);
glVertex3f(x, y, z - in - len);
glEnd();
// x
glBegin(GL_LINES);
// positive
glColor3f(1.0, 0.0, 0.0);
glVertex3f(x + in, y, z);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(x + in + len, y, z);
// negative
glColor3f(0.3, 0.3, 0.3);
glVertex3f(x - in, y, z);
glColor3f(0.3, 0.3, 0.3);
glVertex3f(x - in - len, y, z);
glEnd();
// y
glBegin(GL_LINES);
// positive
glColor3f(0.0, 1.0, 0.0);
glVertex3f(x , y + in, z);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(x, y + in + len, z);
// negative
glColor3f(0.3, 0.3, 0.3);
glVertex3f(x , y - in, z);
glColor3f(0.3, 0.3, 0.3);
glVertex3f(x, y - in - len, z);
glEnd();
}
// callback for displaying to the screen
void display()
{
// moderate framerate and keep track of dt
static float lastTime = 0.0f;
float dt = glutGet(GLUT_ELAPSED_TIME) - lastTime;
float framerate = 60.0f;
if (dt < 1000/(framerate))
{
return;
}
lastTime = glutGet(GLUT_ELAPSED_TIME);
// RigidBody assumes that time is in seconds
body.sim_step(dt/1000.0f);
State s = body.getState();
//cout << s.x.x << " " << s.x.y << " " << s.x.z << endl;
// clear screen before drawing
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawGround(30.0);
drawAxes();
mat4 mv = cam.getModelview()*body.getTransformation();
glPushMatrix();
glLoadMatrixf(&mv[0][0]);
glutSolidTeapot(1.0);
glPopMatrix();
drawCursor(cam.focus);
// swap buffers to display image
glutSwapBuffers();
}
void drawAxes()
{
// coordinate axes
glDisable(GL_DEPTH_TEST);
glBegin(GL_LINES); // X
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glEnd();
glBegin(GL_LINES); // Y
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();
glBegin(GL_LINES); // Z
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 1.0);
glEnd();
glEnable(GL_DEPTH_TEST);
}
void drawGround(float r)
{
// ground plane
glBegin(GL_POLYGON);
glColor4f(0.5, 0.5, 0.5, 0.5);
glVertex3f(r, r, 0.0);
glColor4f(0.5, 0.5, 0.5, 0.5);
glVertex3f(-r, r, 0.0);
glColor4f(0.5, 0.5, 0.5, 0.5);
glVertex3f(-r, -r, 0.0);
glColor4f(0.5, 0.5, 0.5, 0.5);
glVertex3f(r, -r, 0.0);
glEnd();
// ground grid
glDisable(GL_DEPTH_TEST);
for (float i = -r; i <= r; i+=1.0)
{
// vertical
glBegin(GL_LINES);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(i, -r, 0);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(i, r, 0);
glEnd();
// horizontal
glBegin(GL_LINES);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(-r, i, 0);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(r, i, 0);
glEnd();
}
glEnable(GL_DEPTH_TEST);
}