Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

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