-
Notifications
You must be signed in to change notification settings - Fork 2
/
openglutils.cpp
286 lines (249 loc) · 7.7 KB
/
openglutils.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "openglutils.h"
#ifdef __APPLE__
#include <GLUT/glut.h> // why does Apple have to put glut.h here...
#else
#include <GL/glut.h> // ...when everyone else puts it here?
#endif
#include "vec.h"
#include <cfloat>
void draw_circle2d(const Vec2f& centre, float rad, int segs)
{
glBegin(GL_POLYGON);
for(int i=0;i<segs;i++){
float cosine=rad*cos(i*2*M_PI/(float)(segs));
float sine=rad* sin(i*2*M_PI/(float)(segs));
glVertex2fv((Vec2f(cosine,sine) + centre).v);
}
glEnd();
}
void draw_grid2d(const Vec2f& origin, float dx, int nx, int ny) {
float width = nx*dx;
float height = ny*dx;
glBegin(GL_LINES);
for(int i = 0; i <= nx; i++) {
Vec2f a(i*dx, 0);
Vec2f b(i*dx, height);
glVertex2fv((origin+a).v);
glVertex2fv((origin+b).v);
}
for(int j = 0; j <= ny; ++j) {
Vec2f a(0,j*dx);
Vec2f b(width,j*dx);
glVertex2fv((origin + a).v);
glVertex2fv((origin + b).v);
}
glEnd();
}
void draw_box2d(const Vec2f& origin, float width, float height) {
glBegin(GL_POLYGON);
glVertex2fv(origin.v);
glVertex2fv((origin + Vec2f(0, height)).v);
glVertex2fv((origin + Vec2f(width, height)).v);
glVertex2fv((origin + Vec2f(width, 0)).v);
glEnd();
}
void draw_segmentset2d(const std::vector<Vec2f>& vertices, const std::vector<Vec2ui>& edges) {
glBegin(GL_LINES);
for(unsigned int i = 0; i < edges.size(); ++i) {
glVertex2fv(vertices[edges[i][0]].v);
glVertex2fv(vertices[edges[i][1]].v);
}
glEnd();
}
void draw_segmentset2d(const std::vector<Vec2f>& vertices, const std::vector<Vec2i>& edges) {
glBegin(GL_LINES);
for(unsigned int i = 0; i < edges.size(); ++i) {
glVertex2fv(vertices[edges[i][0]].v);
glVertex2fv(vertices[edges[i][1]].v);
}
glEnd();
}
void draw_points2d(const std::vector<Vec2f>& points) {
glBegin(GL_POINTS);
for(unsigned int i = 0; i < points.size(); ++i) {
glVertex2fv(points[i].v);
}
glEnd();
}
void draw_polygon2d(const std::vector<Vec2f>& vertices) {
glBegin(GL_POLYGON);
for(unsigned int i = 0; i < vertices.size(); ++i)
glVertex2fv(vertices[i].v);
glEnd();
}
void draw_polygon2d(const std::vector<Vec2f>& vertices, const std::vector<int>& order) {
glBegin(GL_POLYGON);
for(unsigned int i = 0; i < order.size(); ++i)
glVertex2fv(vertices[order[i]].v);
glEnd();
}
void draw_segment2d(const Vec2f& start, const Vec2f& end) {
glBegin(GL_LINES);
glVertex2fv(start.v);
glVertex2fv(end.v);
glEnd();
}
void draw_arrow2d(const Vec2f& start, const Vec2f& end, float arrow_head_len)
{
Vec2f direction = end - start;
Vec2f dir_norm = direction;
//TODO Possibly automatically scale arrowhead length based on vector magnitude
if(mag(dir_norm) < 1e-14)
return;
normalize(dir_norm);
Vec2f perp(dir_norm[1],-dir_norm[0]);
Vec2f tip_left = end + arrow_head_len/(float)sqrt(2.0)*(-dir_norm + perp);
Vec2f tip_right = end + arrow_head_len/(float)sqrt(2.0)*(-dir_norm - perp);
glBegin(GL_LINES);
glVertex2fv(start.v);
glVertex2fv(end.v);
glVertex2fv(end.v);
glVertex2fv(tip_left.v);
glVertex2fv(end.v);
glVertex2fv(tip_right.v);
glEnd();
}
void draw_trimesh2d(const std::vector<Vec2f>& vertices, const std::vector<Vec3ui>& tris) {
glBegin(GL_TRIANGLES);
for(unsigned int i = 0; i < tris.size(); ++i) {
glVertex2fv(vertices[tris[i][0]].v);
glVertex2fv(vertices[tris[i][1]].v);
glVertex2fv(vertices[tris[i][2]].v);
}
glEnd();
}
void hueToRGB(float hue, float sat, float val, float &r, float &g, float &b) {
//compute hue (adapted from an older Wikipedia article)
int Hi = (int)(floor(hue / 60.0f)) % 6;
float f = hue / 60 - Hi;
float p = val * (1 - sat);
float q = val * (1- f * sat);
float t = val * (1 - (1 - f) * sat);
switch(Hi) {
case 0:
r=val;
g=t;
b=p;
break;
case 1:
r=q;
g=val;
b=p;
break;
case 2:
r=p;
g=val;
b=t;
break;
case 3:
r=p;
g=q;
b=val;
break;
case 4:
r=t;
g=p;
b=val;
break;
case 5:
r=val;
g=p;
b=q;
break;
}
}
void draw_grid_data2d(Array2f& data, Vec2f origin, float dx, bool color) {
float max_val = FLT_MIN;
float min_val = FLT_MAX;
for(int j = 0; j < data.nj; ++j) for(int i = 0; i < data.ni; ++i) {
max_val = max(data(i,j),max_val);
min_val = min(data(i,j),min_val);
}
for(int j = 0; j < data.nj; ++j) {
for(int i = 0; i < data.ni; ++i) {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
Vec2f bl = origin + Vec2f(i*dx,j*dx);
float r,g,b;
if(color) {
hueToRGB(240*(data(i,j) - min_val)/(max_val-min_val), 1, 1, r,g,b);
}
else {
float gray = (data(i,j) - min_val)/(max_val-min_val);
r = g = b = gray;
}
//TODO Black body colormap, if I can find it.
glColor3f(r,g,b);
draw_box2d(bl, dx, dx);
}
}
}
void draw_trimesh3d(const std::vector<Vec3f>& vertices, const std::vector<Vec3ui>& tris) {
glBegin(GL_TRIANGLES);
for(unsigned int i = 0; i < tris.size(); ++i) {
glVertex3fv(vertices[tris[i][0]].v);
glVertex3fv(vertices[tris[i][1]].v);
glVertex3fv(vertices[tris[i][2]].v);
}
glEnd();
}
void draw_trimesh3d(const std::vector<Vec3f>& vertices, const std::vector<Vec3ui>& tris, const std::vector<Vec3f> & normals) {
glBegin(GL_TRIANGLES);
for(unsigned int i = 0; i < tris.size(); ++i) {
glNormal3fv(normals[tris[i][0]].v);
glVertex3fv(vertices[tris[i][0]].v);
glNormal3fv(normals[tris[i][1]].v);
glVertex3fv(vertices[tris[i][1]].v);
glNormal3fv(normals[tris[i][2]].v);
glVertex3fv(vertices[tris[i][2]].v);
}
glEnd();
}
void draw_box3d(const Vec3f& dimensions) {
//Draw an axis-aligned box with specified dimensions,
//where the midpoint of the box is at the origin
float width = dimensions[0];
float height = dimensions[1];
float depth = dimensions[2];
glBegin(GL_POLYGON);
glNormal3f(-1,0,0);
glVertex3f(-0.5*width, -0.5*height, 0.5*depth);
glVertex3f(-0.5*width, 0.5*height, 0.5*depth);
glVertex3f(-0.5*width, 0.5*height, -0.5*depth);
glVertex3f(-0.5*width, -0.5*height, -0.5*depth);
glEnd();
glBegin(GL_POLYGON);
glNormal3f(1,0,0);
glVertex3f(0.5*width, -0.5*height, 0.5*depth);
glVertex3f(0.5*width, 0.5*height, 0.5*depth);
glVertex3f(0.5*width, 0.5*height, -0.5*depth);
glVertex3f(0.5*width, -0.5*height, -0.5*depth);
glEnd();
glBegin(GL_POLYGON);
glNormal3f(0,0,-1);
glVertex3f(-0.5*width, -0.5*height, -0.5*depth);
glVertex3f(0.5*width, -0.5*height, -0.5*depth);
glVertex3f(0.5*width, 0.5*height, -0.5*depth);
glVertex3f(-0.5*width, 0.5*height, -0.5*depth);
glEnd();
glBegin(GL_POLYGON);
glNormal3f(0,0,1);
glVertex3f(-0.5*width, -0.5*height, 0.5*depth);
glVertex3f(0.5*width, -0.5*height, 0.5*depth);
glVertex3f(0.5*width, 0.5*height, 0.5*depth);
glVertex3f(-0.5*width, 0.5*height, 0.5*depth);
glEnd();
glBegin(GL_POLYGON);
glNormal3f(0,-1,0);
glVertex3f(-0.5*width, -0.5*height, 0.5*depth);
glVertex3f(0.5*width, -0.5*height, 0.5*depth);
glVertex3f(0.5*width, -0.5*height, -0.5*depth);
glVertex3f(-0.5*width, -0.5*height, -0.5*depth);
glEnd();
glBegin(GL_POLYGON);
glNormal3f(0,1,0);
glVertex3f(-0.5*width, 0.5*height, 0.5*depth);
glVertex3f(0.5*width, 0.5*height, 0.5*depth);
glVertex3f(0.5*width, 0.5*height, -0.5*depth);
glVertex3f(-0.5*width, 0.5*height, -0.5*depth);
glEnd();
}