Subversion Repositories gelsvn

Rev

Rev 656 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 656 Rev 657
1
/* ----------------------------------------------------------------------- *
1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
5
 * ----------------------------------------------------------------------- */
6
 
6
 
7
#include "../GL/glew.h"
7
#include "../GL/glew.h"
8
 
8
 
9
#include "../CGLA/Mat4x4f.h"
9
#include "../CGLA/Mat4x4f.h"
10
#include "../CGLA/Vec3d.h"
10
#include "../CGLA/Vec3d.h"
11
#include "../HMesh/Manifold.h"
11
#include "../HMesh/Manifold.h"
-
 
12
#include "../Geometry/TriMesh.h"
12
 
13
 
13
#include "draw.h"
14
#include "draw.h"
14
#include "SinglePassWireframeRenderer.h"
15
#include "SinglePassWireframeRenderer.h"
15
#include "IDBufferWireFrameRenderer.h"
16
#include "IDBufferWireFrameRenderer.h"
16
#include "SOIL.h"
17
#include "SOIL.h"
17
 
18
 
18
namespace GLGraphics
19
namespace GLGraphics
19
{
20
{
20
    using namespace CGLA;
21
    using namespace CGLA;
-
 
22
    using namespace Geometry;
21
    using namespace HMesh;
23
    using namespace HMesh;
22
    using namespace std;
24
    using namespace std;
23
    
25
    
24
    namespace
26
    namespace
25
    {
27
    {
26
        void set_material(const Geometry::Material& material)
28
        void set_material(const Geometry::Material& material)
27
        {
29
        {
28
            if(material.has_texture && material.tex_id >=0)
30
            if(material.has_texture && material.tex_id >=0)
29
            {
31
            {
30
                glEnable(GL_TEXTURE_2D);
32
                glEnable(GL_TEXTURE_2D);
31
                glBindTexture(GL_TEXTURE_2D, material.tex_id);
33
                glBindTexture(GL_TEXTURE_2D, material.tex_id);
32
                glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
34
                glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
33
            }
35
            }
34
            else
36
            else
35
                glDisable(GL_TEXTURE_2D);
37
                glDisable(GL_TEXTURE_2D);
36
            
38
            
37
            glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material.ambient);
39
            glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material.ambient);
38
            glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material.diffuse);
40
            glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material.diffuse);
39
            glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material.specular);
41
            glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material.specular);
40
            glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, material.shininess);
42
            glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, material.shininess);
41
        }
43
        }
42
    }
44
    }
43
    
45
    
-
 
46
    void draw_ball() {
-
 
47
        static TriMesh mesh;
-
 
48
        if(mesh.geometry.no_vertices()==0)
-
 
49
        {
-
 
50
            GLfloat sphere_verts[] = {0.91358,0,0,-0.91358,0,0,0,0.91358,0,0,-0.91358,0,0,0,0.91358,0,0,-0.91358,0.54321,-0.54321,0.54321,0.54321,0.54321,-0.54321,-0.54321,0.54321,0.54321,-0.54321,-0.54321,-0.54321,0.54321,0.54321,0.54321,-0.54321,0.54321,-0.54321,-0.54321,-0.54321,0.54321,0.54321,-0.54321,-0.54321,0.444444,0,0.814815,0,-0.814815,0.444444,0.814815,-0.444444,0,0.444444,0,-0.814815,0,0.814815,-0.444444,0.814815,0.444444,0,-0.444444,0,0.814815,0,0.814815,0.444444,-0.814815,0.444444,0,-0.444444,0,-0.814815,0,-0.814815,-0.444444,-0.814815,-0.444444,0,0,0.444444,0.814815,0.814815,0,0.444444,0.444444,0.814815,0,0,0.444444,-0.814815,-0.814815,0,-0.444444,-0.444444,0.814815,0,0,-0.444444,0.814815,-0.814815,0,0.444444,-0.444444,-0.814815,0,0,-0.444444,-0.814815,0.814815,0,-0.444444,0.444444,-0.814815,0};
-
 
51
            GLuint sphere_indices[] = {28,11,15,27,5,15,33,7,15,33,13,16,35,4,16,38,7,16,38,14,17,37,1,17,28,7,17,37,14,18,36,6,18,30,8,18,30,12,19,32,3,19,29,8,19,29,11,20,28,1,20,37,8,20,34,13,21,33,5,21,27,9,21,27,11,22,29,3,22,32,9,22,32,12,23,31,2,23,34,9,23,31,12,24,30,6,24,36,10,24,36,14,25,38,4,25,35,10,25,35,13,26,34,2,26,31,10,26,22,9,27,21,5,27,15,11,27,15,7,28,17,1,28,20,11,28,20,8,29,19,3,29,22,11,29,19,8,30,18,6,30,24,12,30,24,10,31,26,2,31,23,12,31,23,9,32,22,3,32,19,12,32,16,7,33,15,5,33,21,13,33,21,9,34,23,2,34,26,13,34,26,10,35,25,4,35,16,13,35,25,10,36,24,6,36,18,14,36,18,8,37,20,1,37,17,14,37,17,7,38,16,4,38,25,14,38};
-
 
52
            
-
 
53
            for(int i=0;i<38;++i) {
-
 
54
                int idx0 = 3*i;
-
 
55
                mesh.geometry.add_vertex(Vec3f(sphere_verts[idx0],sphere_verts[idx0+1],sphere_verts[idx0+2]));
-
 
56
            }
-
 
57
            for(int i=0;i<72;++i) {
-
 
58
                int idx0 = 3*i;
-
 
59
                mesh.geometry.add_face(Vec3i(sphere_indices[idx0]-1,sphere_indices[idx0+1]-1,sphere_indices[idx0+2]-1));
-
 
60
            }
-
 
61
            mesh.compute_normals();
-
 
62
        }
-
 
63
        draw(mesh);
-
 
64
    }
-
 
65
 
-
 
66
    
44
    void draw(const Geometry::IndexedFaceSet& geometry)
67
    void draw(const Geometry::IndexedFaceSet& geometry)
45
    {
68
    {
46
        glBegin(GL_TRIANGLES);
69
        glBegin(GL_TRIANGLES);
47
        for(int i=0;i<geometry.no_faces();i++)
70
        for(int i=0;i<geometry.no_faces();i++)
48
        {
71
        {
49
            Vec3i g_face = geometry.face(i);
72
            Vec3i g_face = geometry.face(i);
50
            Vec3f vert0 = geometry.vertex(g_face[0]);
73
            Vec3f vert0 = geometry.vertex(g_face[0]);
51
            Vec3f vert1 = geometry.vertex(g_face[1]);
74
            Vec3f vert1 = geometry.vertex(g_face[1]);
52
            Vec3f vert2 = geometry.vertex(g_face[2]);
75
            Vec3f vert2 = geometry.vertex(g_face[2]);
53
            Vec3f norm = normalize(cross(vert1-vert0, vert2-vert0));
76
            Vec3f norm = normalize(cross(vert1-vert0, vert2-vert0));
54
            glNormal3fv(norm.get());
77
            glNormal3fv(norm.get());
55
            glVertex3fv(vert0.get());
78
            glVertex3fv(vert0.get());
56
            glVertex3fv(vert1.get());
79
            glVertex3fv(vert1.get());
57
            glVertex3fv(vert2.get());
80
            glVertex3fv(vert2.get());
58
        }
81
        }
59
        glEnd();
82
        glEnd();
60
    }
83
    }
61
    
84
    
62
    void draw(const Geometry::TriMesh& tm, bool per_vertex_norms)
85
    void draw(const Geometry::TriMesh& tm, bool per_vertex_norms)
63
    {
86
    {
64
        int old_mat_idx = -1;
87
        int old_mat_idx = -1;
65
        glBegin(GL_TRIANGLES);
88
        glBegin(GL_TRIANGLES);
66
        for(int i=0;i<tm.geometry.no_faces();i++)
89
        for(int i=0;i<tm.geometry.no_faces();i++)
67
        {
90
        {
68
            int new_mat_idx = i<static_cast<int>(tm.mat_idx.size()) ? tm.mat_idx[i] : -1;
91
            int new_mat_idx = i<static_cast<int>(tm.mat_idx.size()) ? tm.mat_idx[i] : -1;
69
            if(new_mat_idx != old_mat_idx)
92
            if(new_mat_idx != old_mat_idx)
70
            {
93
            {
71
                glEnd();
94
                glEnd();
72
                set_material(tm.materials[tm.mat_idx[i]]);
95
                set_material(tm.materials[tm.mat_idx[i]]);
73
                glBegin(GL_TRIANGLES);
96
                glBegin(GL_TRIANGLES);
74
                old_mat_idx = new_mat_idx;
97
                old_mat_idx = new_mat_idx;
75
            }
98
            }
76
            Vec3i n_face = tm.normals.face(i);
99
            Vec3i n_face = tm.normals.face(i);
77
            Vec3i g_face = tm.geometry.face(i);
100
            Vec3i g_face = tm.geometry.face(i);
78
            Vec3i t_face = tm.texcoords.face(i);
101
            Vec3i t_face = tm.texcoords.face(i);
79
            
102
            
80
            if(!per_vertex_norms)
103
            if(!per_vertex_norms)
81
            {
104
            {
82
                Vec3f vert0 = tm.geometry.vertex(g_face[0]);
105
                Vec3f vert0 = tm.geometry.vertex(g_face[0]);
83
                Vec3f vert1 = tm.geometry.vertex(g_face[1]);
106
                Vec3f vert1 = tm.geometry.vertex(g_face[1]);
84
                Vec3f vert2 = tm.geometry.vertex(g_face[2]);
107
                Vec3f vert2 = tm.geometry.vertex(g_face[2]);
85
                Vec3f norm = normalize(cross(vert1-vert0, vert2-vert0));
108
                Vec3f norm = normalize(cross(vert1-vert0, vert2-vert0));
86
                glNormal3fv(norm.get());
109
                glNormal3fv(norm.get());
87
            }
110
            }
88
            for(int j=0;j<3;j++)
111
            for(int j=0;j<3;j++)
89
            {
112
            {
90
                if(per_vertex_norms && n_face != Geometry::NULL_FACE)
113
                if(per_vertex_norms && n_face != Geometry::NULL_FACE)
91
                {
114
                {
92
                    Vec3f norm = tm.normals.vertex(n_face[j]);
115
                    Vec3f norm = tm.normals.vertex(n_face[j]);
93
                    glNormal3fv(norm.get());
116
                    glNormal3fv(norm.get());
94
                }
117
                }
95
                if(t_face != Geometry::NULL_FACE)
118
                if(t_face != Geometry::NULL_FACE)
96
                {
119
                {
97
                    Vec3f texc = tm.texcoords.vertex(t_face[j]);
120
                    Vec3f texc = tm.texcoords.vertex(t_face[j]);
98
                    glTexCoord2fv(texc.get());
121
                    glTexCoord2fv(texc.get());
99
                }
122
                }
100
                Vec3f vert = tm.geometry.vertex(g_face[j]);
123
                Vec3f vert = tm.geometry.vertex(g_face[j]);
101
                glVertex3fv(vert.get());
124
                glVertex3fv(vert.get());
102
            }
125
            }
103
        }
126
        }
104
        glEnd();
127
        glEnd();
105
        glDisable(GL_TEXTURE_2D);
128
        glDisable(GL_TEXTURE_2D);
106
    }
129
    }
107
    
130
    
108
    void load_textures(Geometry::TriMesh& tm)
131
    void load_textures(Geometry::TriMesh& tm)
109
    {
132
    {
110
        for(unsigned int i=0;i<tm.materials.size(); ++i)
133
        for(unsigned int i=0;i<tm.materials.size(); ++i)
111
        {
134
        {
112
            Geometry::Material& mat = tm.materials[i];
135
            Geometry::Material& mat = tm.materials[i];
113
            if(mat.tex_name != "")
136
            if(mat.tex_name != "")
114
            {
137
            {
115
                string name = mat.tex_path + mat.tex_name;
138
                string name = mat.tex_path + mat.tex_name;
116
                mat.tex_id = SOIL_load_OGL_texture(name.data(), 0, 0, SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_POWER_OF_TWO);
139
                mat.tex_id = SOIL_load_OGL_texture(name.data(), 0, 0, SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_POWER_OF_TWO);
117
            }
140
            }
118
        }
141
        }
119
    }
142
    }
120
 
143
 
121
	/// Draw an object of type T which contains only triangles as wireframe. In practice T = Manifold or TriMesh.
144
	/// Draw an object of type T which contains only triangles as wireframe. In practice T = Manifold or TriMesh.
122
	template<typename T>
145
	template<typename T>
123
    void draw_triangles_in_wireframe(T& m, bool per_vertex_norms, const CGLA::Vec3f& line_color)
146
    void draw_triangles_in_wireframe(T& m, bool per_vertex_norms, const CGLA::Vec3f& line_color)
124
	{
147
	{
125
		static SinglePassWireframeRenderer swr;
148
		static SinglePassWireframeRenderer swr;
126
		swr.enable(line_color);
149
		swr.enable(line_color);
127
		draw(m, per_vertex_norms);
150
		draw(m, per_vertex_norms);
128
		swr.disable();
151
		swr.disable();
129
	}
152
	}
130
    
153
    
131
	template
154
	template
132
    void draw_triangles_in_wireframe(HMesh::Manifold& m, bool per_vertex_norms, const CGLA::Vec3f& line_color);
155
    void draw_triangles_in_wireframe(HMesh::Manifold& m, bool per_vertex_norms, const CGLA::Vec3f& line_color);
133
    
156
    
134
	template
157
	template
135
    void draw_triangles_in_wireframe(Geometry::TriMesh& m, bool per_vertex_norms, const CGLA::Vec3f& line_color);
158
    void draw_triangles_in_wireframe(Geometry::TriMesh& m, bool per_vertex_norms, const CGLA::Vec3f& line_color);
136
    
159
    
137
    template<class T>
160
    template<class T>
138
    void draw_wireframe_oldfashioned(const T& m, bool per_vertex_norms, const Vec3f& line_color)
161
    void draw_wireframe_oldfashioned(const T& m, bool per_vertex_norms, const Vec3f& line_color)
139
    {
162
    {
140
        // Store state that we change
163
        // Store state that we change
141
        glPushAttrib(GL_POLYGON_BIT);
164
        glPushAttrib(GL_POLYGON_BIT);
142
        GLboolean lights_on;
165
        GLboolean lights_on;
143
        glGetBooleanv(GL_LIGHTING, &lights_on);
166
        glGetBooleanv(GL_LIGHTING, &lights_on);
144
        Vec4f current_color;
167
        Vec4f current_color;
145
        glGetFloatv(GL_CURRENT_COLOR, &current_color[0]);
168
        glGetFloatv(GL_CURRENT_COLOR, &current_color[0]);
146
        
169
        
147
        // Draw filled
170
        // Draw filled
148
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
171
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
149
        draw(m, per_vertex_norms);
172
        draw(m, per_vertex_norms);
150
        
173
        
151
        // Draw lines
174
        // Draw lines
152
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
175
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
153
        glDisable(GL_LIGHTING);
176
        glDisable(GL_LIGHTING);
154
        glEnable(GL_POLYGON_OFFSET_LINE);
177
        glEnable(GL_POLYGON_OFFSET_LINE);
155
        glPolygonOffset(0,-5);
178
        glPolygonOffset(0,-5);
156
        glColor3fv(line_color.get());
179
        glColor3fv(line_color.get());
157
        draw(m, per_vertex_norms);
180
        draw(m, per_vertex_norms);
158
        
181
        
159
        // Put back old state
182
        // Put back old state
160
        glColor3fv(current_color.get());
183
        glColor3fv(current_color.get());
161
        if(lights_on) glEnable(GL_LIGHTING);
184
        if(lights_on) glEnable(GL_LIGHTING);
162
        glPopAttrib();
185
        glPopAttrib();
163
    }
186
    }
164
    template
187
    template
165
    void draw_wireframe_oldfashioned(const HMesh::Manifold& m, bool per_vertex_norms, const Vec3f& line_color);
188
    void draw_wireframe_oldfashioned(const HMesh::Manifold& m, bool per_vertex_norms, const Vec3f& line_color);
166
    
189
    
167
    template
190
    template
168
    void draw_wireframe_oldfashioned(const Geometry::TriMesh& m, bool per_vertex_norms, const Vec3f& line_color);
191
    void draw_wireframe_oldfashioned(const Geometry::TriMesh& m, bool per_vertex_norms, const Vec3f& line_color);
169
    
192
    
170
    
193
    
171
    void draw(const Manifold& m, bool per_vertex_norms)
194
    void draw(const Manifold& m, bool per_vertex_norms)
172
    {
195
    {
173
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){
196
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){
174
            if(!per_vertex_norms)
197
            if(!per_vertex_norms)
175
                glNormal3dv(normal(m, *f).get());
198
                glNormal3dv(normal(m, *f).get());
176
            if(no_edges(m, *f)== 3)
199
            if(no_edges(m, *f)== 3)
177
                glBegin(GL_TRIANGLES);
200
                glBegin(GL_TRIANGLES);
178
            else
201
            else
179
                glBegin(GL_POLYGON);
202
                glBegin(GL_POLYGON);
180
            
203
            
181
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
204
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
182
                Vec3d n = normal(m, w.vertex());
205
                Vec3d n = normal(m, w.vertex());
183
                if(per_vertex_norms)
206
                if(per_vertex_norms)
184
                    glNormal3dv(n.get());
207
                    glNormal3dv(n.get());
185
                glVertex3dv(m.pos(w.vertex()).get());
208
                glVertex3dv(m.pos(w.vertex()).get());
186
            }
209
            }
187
            glEnd();
210
            glEnd();
188
        }
211
        }
189
    }
212
    }
190
    
213
    
191
    
214
    
192
    bool depth_pick(int x, int y, float& depth)
215
    bool depth_pick(int x, int y, float& depth)
193
    {
216
    {
194
        float new_depth;
217
        float new_depth;
195
        
218
        
196
        // Get the minimum and maximum depth values.
219
        // Get the minimum and maximum depth values.
197
        float minmax_depth[2];
220
        float minmax_depth[2];
198
        glGetFloatv(GL_DEPTH_RANGE, minmax_depth);
221
        glGetFloatv(GL_DEPTH_RANGE, minmax_depth);
199
        glPushAttrib(GL_PIXEL_MODE_BIT);
222
        glPushAttrib(GL_PIXEL_MODE_BIT);
200
        glReadBuffer(GL_FRONT);
223
        glReadBuffer(GL_FRONT);
201
        // Read a single pixel at the position of the mouse cursor.
224
        // Read a single pixel at the position of the mouse cursor.
202
        glReadPixels(x, y, 1,1, GL_DEPTH_COMPONENT,
225
        glReadPixels(x, y, 1,1, GL_DEPTH_COMPONENT,
203
                     GL_FLOAT, (void*) &new_depth);
226
                     GL_FLOAT, (void*) &new_depth);
204
        glPopAttrib();
227
        glPopAttrib();
205
        
228
        
206
        // If the depth corresponds to the far plane, we clicked on the
229
        // If the depth corresponds to the far plane, we clicked on the
207
        // background.
230
        // background.
208
        if(new_depth == minmax_depth[1])
231
        if(new_depth == minmax_depth[1])
209
            return false;
232
            return false;
210
        
233
        
211
        depth = new_depth;
234
        depth = new_depth;
212
        return true;
235
        return true;
213
    }
236
    }
214
    
237
    
215
    Vec3d screen2world(int x, int y, float depth)
238
    Vec3d screen2world(int x, int y, float depth)
216
    {
239
    {
217
        // Enquire about the viewport dimensions
240
        // Enquire about the viewport dimensions
218
        GLint viewport[4];
241
        GLint viewport[4];
219
        glGetIntegerv(GL_VIEWPORT, viewport);
242
        glGetIntegerv(GL_VIEWPORT, viewport);
220
        // Copy modelview matrix.
243
        // Copy modelview matrix.
221
        double mvmat[16];
244
        double mvmat[16];
222
        glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
245
        glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
223
        
246
        
224
        // Copy the projection matrix. We assume it is unchanged.
247
        // Copy the projection matrix. We assume it is unchanged.
225
        double prjmat[16];
248
        double prjmat[16];
226
        glGetDoublev(GL_PROJECTION_MATRIX, prjmat);
249
        glGetDoublev(GL_PROJECTION_MATRIX, prjmat);
227
        
250
        
228
        // Now unproject the point from screen to world coordinates.
251
        // Now unproject the point from screen to world coordinates.
229
        double ox, oy, oz;
252
        double ox, oy, oz;
230
        gluUnProject(x, y,depth,
253
        gluUnProject(x, y,depth,
231
                     mvmat,prjmat,viewport,
254
                     mvmat,prjmat,viewport,
232
                     &ox, &oy, &oz);
255
                     &ox, &oy, &oz);
233
        
256
        
234
        return Vec3d(ox,oy,oz);
257
        return Vec3d(ox,oy,oz);
235
    }
258
    }
-
 
259
    
-
 
260
    Vec3d world2screen(const Vec3d& p)
-
 
261
    {
-
 
262
        // Enquire about the viewport dimensions
-
 
263
        GLint viewport[4];
-
 
264
        glGetIntegerv(GL_VIEWPORT, viewport);
-
 
265
        // Copy modelview matrix.
-
 
266
        double mvmat[16];
-
 
267
        glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
-
 
268
        
-
 
269
        // Copy the projection matrix. We assume it is unchanged.
-
 
270
        double prjmat[16];
-
 
271
        glGetDoublev(GL_PROJECTION_MATRIX, prjmat);
-
 
272
        
-
 
273
        // Now unproject the point from screen to world coordinates.
-
 
274
        double ox, oy, oz;
-
 
275
        gluProject(p[0], p[1], p[2],
-
 
276
                   mvmat,prjmat,viewport,
-
 
277
                     &ox, &oy, &oz);
-
 
278
        
-
 
279
        return Vec3d(ox,oy,oz);
-
 
280
    }
-
 
281
 
236
 
282
 
237
    
283
    
238
    
284
    
239
    void draw(const Geometry::AABox& box)
285
    void draw(const Geometry::AABox& box)
240
    {
286
    {
241
        glBegin(GL_QUADS);
287
        glBegin(GL_QUADS);
242
        Vec3f norm_neg[] = {Vec3f(0,0,-1), Vec3f(-1,0,0), Vec3f(0,-1,0)};
288
        Vec3f norm_neg[] = {Vec3f(0,0,-1), Vec3f(-1,0,0), Vec3f(0,-1,0)};
243
        Vec3f norm_pos[] = {Vec3f(0,0, 1), Vec3f( 1,0,0), Vec3f(0, 1,0)};
289
        Vec3f norm_pos[] = {Vec3f(0,0, 1), Vec3f( 1,0,0), Vec3f(0, 1,0)};
244
        for(int j=0;j<3;++j)
290
        for(int j=0;j<3;++j)
245
        {
291
        {
246
            glNormal3fv(norm_neg[j].get());
292
            glNormal3fv(norm_neg[j].get());
247
            Vec3f p = box.get_pmin();
293
            Vec3f p = box.get_pmin();
248
            glVertex3f(p[0], p[1], p[2]);
294
            glVertex3f(p[0], p[1], p[2]);
249
            p[(j+1)%3] = box.get_pmax()[(j+1)%3];
295
            p[(j+1)%3] = box.get_pmax()[(j+1)%3];
250
            glVertex3f(p[0], p[1], p[2]);
296
            glVertex3f(p[0], p[1], p[2]);
251
            p[j] = box.get_pmax()[j];
297
            p[j] = box.get_pmax()[j];
252
            glVertex3f(p[0], p[1], p[2]);
298
            glVertex3f(p[0], p[1], p[2]);
253
            p[(j+1)%3] = box.get_pmin()[(j+1)%3];
299
            p[(j+1)%3] = box.get_pmin()[(j+1)%3];
254
            glVertex3f(p[0], p[1], p[2]);
300
            glVertex3f(p[0], p[1], p[2]);
255
        }
301
        }
256
        glEnd();
302
        glEnd();
257
        glBegin(GL_QUADS);
303
        glBegin(GL_QUADS);
258
        for(int j=0;j<3;++j)
304
        for(int j=0;j<3;++j)
259
        {
305
        {
260
            glNormal3fv(norm_pos[j].get());
306
            glNormal3fv(norm_pos[j].get());
261
            Vec3f p = box.get_pmax();
307
            Vec3f p = box.get_pmax();
262
            glVertex3f(p[0], p[1], p[2]);
308
            glVertex3f(p[0], p[1], p[2]);
263
            p[j] = box.get_pmin()[j];
309
            p[j] = box.get_pmin()[j];
264
            glVertex3f(p[0], p[1], p[2]);
310
            glVertex3f(p[0], p[1], p[2]);
265
            p[(j+1)%3] = box.get_pmin()[(j+1)%3];
311
            p[(j+1)%3] = box.get_pmin()[(j+1)%3];
266
            glVertex3f(p[0], p[1], p[2]);
312
            glVertex3f(p[0], p[1], p[2]);
267
            p[j] = box.get_pmax()[j];
313
            p[j] = box.get_pmax()[j];
268
            glVertex3f(p[0], p[1], p[2]);
314
            glVertex3f(p[0], p[1], p[2]);
269
        }
315
        }
270
        glEnd();
316
        glEnd();
271
    }
317
    }
272
    
318
    
273
    void draw(const Geometry::OBox& box)
319
    void draw(const Geometry::OBox& box)
274
    {
320
    {
275
        Mat4x4f m = identity_Mat4x4f();
321
        Mat4x4f m = identity_Mat4x4f();
276
        copy_matrix(box.get_rotation(), m);
322
        copy_matrix(box.get_rotation(), m);
277
        glPushMatrix();
323
        glPushMatrix();
278
        glMultMatrixf(m.get());
324
        glMultMatrixf(m.get());
279
        draw(box.get_aabox());
325
        draw(box.get_aabox());
280
        glPopMatrix();
326
        glPopMatrix();
281
    }
327
    }
282
    
328
    
283
    /** Draw the tree. The first argument is the level counter, the second
329
    /** Draw the tree. The first argument is the level counter, the second
284
     argument is the level at which to stop drawing. */
330
     argument is the level at which to stop drawing. */
285
    template <class BoxType>
331
    template <class BoxType>
286
    void draw(const Geometry::BoundingINode<BoxType>& node, int level, int max_level)
332
    void draw(const Geometry::BoundingINode<BoxType>& node, int level, int max_level)
287
    {
333
    {
288
        if(level == max_level)
334
        if(level == max_level)
289
        {
335
        {
290
            draw(node);
336
            draw(node);
291
            return;
337
            return;
292
        }
338
        }
293
        node->left->draw(level + 1, max_level);
339
        node->left->draw(level + 1, max_level);
294
        node->right->draw(level + 1, max_level);
340
        node->right->draw(level + 1, max_level);
295
    }
341
    }
296
    
342
    
297
    template <class BoxType>
343
    template <class BoxType>
298
    void draw(const Geometry::BoundingLNode<BoxType>& node, int level, int max_level)
344
    void draw(const Geometry::BoundingLNode<BoxType>& node, int level, int max_level)
299
    {
345
    {
300
#if USE_LEAF_BOXES
346
#if USE_LEAF_BOXES
301
        draw(node); 
347
        draw(node); 
302
#endif
348
#endif
303
    }
349
    }
304
    
350
    
305
    template <class BoxType>
351
    template <class BoxType>
306
    void draw(const Geometry::BoundingTree<BoxType>& tree, int max_level)
352
    void draw(const Geometry::BoundingTree<BoxType>& tree, int max_level)
307
    {
353
    {
308
        draw(*tree.root, 0, max_level);
354
        draw(*tree.root, 0, max_level);
309
    }
355
    }
310
}
356
}
311
 
357