g2o
Loading...
Searching...
No Matches
opengl_primitives.cpp
Go to the documentation of this file.
1// g2o - General Graph Optimization
2// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3//
4// This file is part of g2o.
5//
6// g2o is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// g2o is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with g2o. If not, see <http://www.gnu.org/licenses/>.
18
19#include "opengl_primitives.h"
20
21#include <cmath>
22#include <cstdlib>
23
24#ifdef __APPLE__
25#include <OpenGL/glu.h>
26#else
27#include <GL/glu.h>
28#endif
29
30namespace g2o {
31namespace opengl {
32
37 public:
38 static GLUquadricObj* getQuadradic() {
39 static GLUWrapper inst;
40 return inst._quadratic;
41 }
42
43 protected:
46 gluNewQuadric(); // Create A Pointer To The Quadric Object ( NEW )
47 gluQuadricNormals(_quadratic, GLU_SMOOTH); // Create Smooth Normals ( NEW )
48 }
49 ~GLUWrapper() { gluDeleteQuadric(_quadratic); }
50 GLUquadricObj* _quadratic;
51};
52
53void drawArrow2D(float len, float head_width, float head_len) {
54 glBegin(GL_LINES);
55 glVertex2f(0.f, 0.f);
56 glVertex2f(len, 0.f);
57 glEnd();
58
59 glNormal3f(0.f, 0.f, 1.f);
60 glBegin(GL_TRIANGLES);
61 glVertex2f(len, 0.f);
62 glVertex2f(len - head_len, 0.5f * head_width);
63 glVertex2f(len - head_len, -0.5f * head_width);
64 glEnd();
65}
66
68 glPushMatrix();
69 glScalef(0.5f, 1.f, 1.f);
70 glPushMatrix();
71 glScalef(1.f, 0.25f, 0.5f);
72 glTranslatef(-0.5f, 0.5f, 0.f);
73 glColor3f(1.0f, 0.3f, 0.3f);
74 drawBox(1.f, 1.f, 1.f);
75 glPopMatrix();
76
77 glPushMatrix();
78 glScalef(1.f, 0.25f, 0.5f);
79 glTranslatef(-0.5f, -0.5f, 0.f);
80 glColor3f(1.0f, 0.1f, 0.1f);
81 drawBox(1.f, 1.f, 1.f);
82 glPopMatrix();
83
84 glPushMatrix();
85 glScalef(1.f, 0.25f, 0.5f);
86 glTranslatef(+0.5f, 0.5f, 0.f);
87 glColor3f(0.3f, 0.3f, 1.0f);
88 drawBox(1.f, 1.f, 1.f);
89 glPopMatrix();
90
91 glPushMatrix();
92 glScalef(1.f, 0.25f, 0.5f);
93 glTranslatef(+0.5f, -0.5f, 0.f);
94 glColor3f(0.1f, 0.1f, 1.f);
95 drawBox(1.f, 1.f, 1.f);
96 glPopMatrix();
97 glPopMatrix();
98}
99
100void drawBox(GLfloat l, GLfloat w, GLfloat h) {
101 GLfloat sx = l * 0.5f;
102 GLfloat sy = w * 0.5f;
103 GLfloat sz = h * 0.5f;
104
105 glBegin(GL_QUADS);
106 // bottom
107 glNormal3f(0.0f, 0.0f, -1.0f);
108 glVertex3f(-sx, -sy, -sz);
109 glVertex3f(-sx, sy, -sz);
110 glVertex3f(sx, sy, -sz);
111 glVertex3f(sx, -sy, -sz);
112 // top
113 glNormal3f(0.0f, 0.0f, 1.0f);
114 glVertex3f(-sx, -sy, sz);
115 glVertex3f(-sx, sy, sz);
116 glVertex3f(sx, sy, sz);
117 glVertex3f(sx, -sy, sz);
118 // back
119 glNormal3f(-1.0f, 0.0f, 0.0f);
120 glVertex3f(-sx, -sy, -sz);
121 glVertex3f(-sx, sy, -sz);
122 glVertex3f(-sx, sy, sz);
123 glVertex3f(-sx, -sy, sz);
124 // front
125 glNormal3f(1.0f, 0.0f, 0.0f);
126 glVertex3f(sx, -sy, -sz);
127 glVertex3f(sx, sy, -sz);
128 glVertex3f(sx, sy, sz);
129 glVertex3f(sx, -sy, sz);
130 // left
131 glNormal3f(0.0f, -1.0f, 0.0f);
132 glVertex3f(-sx, -sy, -sz);
133 glVertex3f(sx, -sy, -sz);
134 glVertex3f(sx, -sy, sz);
135 glVertex3f(-sx, -sy, sz);
136 // right
137 glNormal3f(0.0f, 1.0f, 0.0f);
138 glVertex3f(-sx, sy, -sz);
139 glVertex3f(sx, sy, -sz);
140 glVertex3f(sx, sy, sz);
141 glVertex3f(-sx, sy, sz);
142 glEnd();
143}
144
145void drawPlane(GLfloat l, GLfloat w) {
146 GLfloat sx = l * 0.5f;
147 GLfloat sy = w * 0.5f;
148
149 glBegin(GL_QUADS);
150 glNormal3f(0.0f, 0.0f, 1.0f);
151 glVertex3f(-sx, -sy, 0.f);
152 glVertex3f(-sx, sy, 0.f);
153 glVertex3f(sx, sy, 0.f);
154 glVertex3f(sx, -sy, 0.f);
155 glEnd();
156}
157
158void drawSphere(GLfloat radius) {
159 gluSphere(GLUWrapper::getQuadradic(), radius, 32, 32);
160}
161
162void drawEllipsoid(GLfloat r1, GLfloat r2, GLfloat r3) {
163 GLboolean hasNormalization = glIsEnabled(GL_NORMALIZE);
164 if (!hasNormalization) glEnable(GL_NORMALIZE);
165 glPushMatrix();
166 glScalef(r1, r2, r3);
167 gluSphere(GLUWrapper::getQuadradic(), 1.0f, 32, 32);
168 glPopMatrix();
169 if (!hasNormalization) glDisable(GL_NORMALIZE);
170}
171
172void drawCone(GLfloat radius, GLfloat height) {
173 glPushMatrix();
174 glRotatef(-90.f, 1.f, 0.f, 0.f);
175 glTranslatef(0.f, 0.f, -height / 2.0f);
176 gluCylinder(GLUWrapper::getQuadradic(), radius, 0.f, height, 32, 1);
177 gluDisk(GLUWrapper::getQuadradic(), 0, radius, 32, 1);
178 glPopMatrix();
179}
180
181void drawCylinder(GLfloat radius, GLfloat height) {
182 glPushMatrix();
183 glRotatef(-90, 1.f, 0.f, 0.f);
184 glTranslatef(0.f, 0.f, +height / 2.0f);
185 gluDisk(GLUWrapper::getQuadradic(), 0.f, radius, 32, 1);
186 glTranslatef(0, 0, -height);
187 gluCylinder(GLUWrapper::getQuadradic(), radius, radius, height, 32, 1);
188 glRotatef(180, 1.f, 0.f, 0.f);
189 gluDisk(GLUWrapper::getQuadradic(), 0, radius, 32, 1);
190 glPopMatrix();
191}
192
193void drawDisk(GLfloat radius) {
194 glRotatef(90, 0.f, 1.f, 0.f);
195 gluDisk(GLUWrapper::getQuadradic(), 0, radius, 32, 1);
196}
197
198void drawCircle(GLfloat radius, int segments) {
199 double angleStep = (2 * M_PI / (segments));
200 glBegin(GL_LINE_STRIP);
201 for (int i = 0; i <= segments; i++) {
202 double angle = i * angleStep;
203 float x = radius * cos(angle);
204 float y = radius * sin(angle);
205 glVertex3f(x, y, 0.f);
206 }
207 glEnd();
208}
209
210void drawPyramid(GLfloat length, GLfloat height) {
211 glPushMatrix();
212 glTranslatef(0.f, 0.f, -height / 2.0f);
213 glRotatef(45, 0.f, 0.f, 1.f);
214 gluCylinder(GLUWrapper::getQuadradic(), length, 0.f, height, 4, 1);
215 gluDisk(GLUWrapper::getQuadradic(), 0, length, 4, 1);
216 glPopMatrix();
217}
218
219void drawRangeRing(GLfloat range, GLfloat fov, GLfloat range_width) {
220 glPushMatrix();
221 glRotatef((fov / 2.0f) - 90, 0.f, 0.f, 1.f);
222 gluPartialDisk(GLUWrapper::getQuadradic(), range, range + range_width, 32, 1,
223 0.f, fov);
224 glPopMatrix();
225}
226
227void drawSlice(GLfloat radius, GLfloat height, GLfloat fov,
228 int slices_per_circle) {
229 double fov_rad = fov / 180. * M_PI; // convert to rad
230 int num_slices = int(slices_per_circle * (fov_rad / (2 * M_PI))) + 1;
231 double angle_step = fov_rad / num_slices;
232 double angle_step_half = angle_step * 0.5;
233
234 GLfloat height_half = height * 0.5f;
235 GLfloat lower_z = -height_half;
236 GLfloat upper_z = height_half;
237
238 GLfloat last_x = float(std::cos(-fov_rad * 0.5f) * radius);
239 GLfloat last_y = float(std::sin(-fov_rad * 0.5f) * radius);
240
241 glPushMatrix();
242 glBegin(GL_TRIANGLES);
243 glNormal3f((float)std::sin(-fov_rad * 0.5), (float)-std::cos(-fov_rad * 0.5),
244 0.f);
245 glVertex3f(0.f, 0.f, upper_z);
246 glVertex3f(0.f, 0.f, lower_z);
247 glVertex3f(last_x, last_y, upper_z);
248 glVertex3f(last_x, last_y, upper_z);
249 glVertex3f(last_x, last_y, lower_z);
250 glVertex3f(0.f, 0.f, lower_z);
251
252 double start_angle = -0.5 * fov_rad + angle_step;
253 double angle = start_angle;
254 for (int i = 0; i < num_slices; ++i) {
255 GLfloat x = float(std::cos(angle) * radius);
256 GLfloat y = float(std::sin(angle) * radius);
257 GLfloat front_normal_x = (float)std::cos(angle + angle_step_half);
258 GLfloat front_normal_y = (float)std::sin(angle + angle_step_half);
259
260 // lower triangle
261 glNormal3f(0.f, 0.f, -1.f);
262 glVertex3f(0.f, 0.f, lower_z);
263 glVertex3f(x, y, lower_z);
264 glVertex3f(last_x, last_y, lower_z);
265 // upper
266 glNormal3f(0.f, 0.f, 1.f);
267 glVertex3f(0.f, 0.f, upper_z);
268 glVertex3f(x, y, upper_z);
269 glVertex3f(last_x, last_y, upper_z);
270 // front rectangle (we use two triangles)
271 glNormal3f(front_normal_x, front_normal_y, 0.f);
272 glVertex3f(last_x, last_y, upper_z);
273 glVertex3f(last_x, last_y, lower_z);
274 glVertex3f(x, y, upper_z);
275 glVertex3f(x, y, upper_z);
276 glVertex3f(x, y, lower_z);
277 glVertex3f(last_x, last_y, lower_z);
278
279 last_x = x;
280 last_y = y;
281 angle += angle_step;
282 }
283
284 glNormal3f(float(-std::sin(fov_rad * 0.5)), float(std::cos(fov_rad * 0.5)),
285 -0.f);
286 glVertex3f(0.f, 0.f, upper_z);
287 glVertex3f(0.f, 0.f, lower_z);
288 glVertex3f(last_x, last_y, upper_z);
289 glVertex3f(last_x, last_y, upper_z);
290 glVertex3f(last_x, last_y, lower_z);
291 glVertex3f(0.f, 0.f, lower_z);
292
293 glEnd();
294 glPopMatrix();
295}
296
297void drawPoint(float pointSize) {
298 glPointSize(pointSize);
299 glBegin(GL_POINTS);
300 glVertex3f(0, 0, 0);
301 glEnd();
302}
303} // namespace opengl
304} // namespace g2o
handle the GLU quadratic
static GLUquadricObj * getQuadradic()
void drawCylinder(GLfloat radius, GLfloat height)
void drawSphere(GLfloat radius)
void drawSlice(GLfloat radius, GLfloat height, GLfloat fov, int slices_per_circle)
void drawPoint(float pointSize)
void drawCone(GLfloat radius, GLfloat height)
void drawPlane(GLfloat l, GLfloat w)
void drawDisk(GLfloat radius)
void drawBox(GLfloat l, GLfloat w, GLfloat h)
void drawCircle(GLfloat radius, int segments)
void drawRangeRing(GLfloat range, GLfloat fov, GLfloat range_width)
void drawArrow2D(float len, float head_width, float head_len)
void drawPyramid(GLfloat length, GLfloat height)
void drawEllipsoid(GLfloat r1, GLfloat r2, GLfloat r3)