Subversion Repositories gelsvn

Rev

Rev 321 | Rev 339 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
299 jrf 1
#include "CGLA/Mat4x4f.h"
2
 
167 jab 3
#include "gel_gl.h"
4
#include "draw.h"
5
 
6
using namespace CGLA;
7
using namespace std;
8
 
9
namespace 
10
{
11
void set_material(const Geometry::Material& material)
12
{
13
    		if(material.tex_id >=0)
14
    			{
15
    				glEnable(GL_TEXTURE_2D);
16
    				glBindTexture(GL_TEXTURE_2D, material.tex_id);
17
    				glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
18
    			}
19
    		else
20
    			glDisable(GL_TEXTURE_2D);
21
 
22
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material.ambient);
23
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material.diffuse);
24
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material.specular);
25
    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, material.shininess);
26
}
27
 
28
}
29
 
299 jrf 30
namespace GLGraphics
31
{
32
 
167 jab 33
void draw(const Geometry::TriMesh& tm)
34
{
35
    int old_mat_idx = -1;
36
    glBegin(GL_TRIANGLES);
37
    for(int i=0;i<tm.geometry.no_faces();i++)
38
    {
39
        if(tm.mat_idx[i] != old_mat_idx)
40
        {
41
            glEnd();
42
            set_material(tm.materials[tm.mat_idx[i]]);
43
            glBegin(GL_TRIANGLES);
44
            old_mat_idx = tm.mat_idx[i];
45
        }
46
        Vec3i n_face = tm.normals.face(i);
47
        Vec3i g_face = tm.geometry.face(i);
48
        Vec3i t_face = tm.texcoords.face(i);
49
        for(int j=0;j<3;j++)
50
        {
51
            if(n_face != Geometry::NULL_FACE)
52
            {
53
                Vec3f norm = tm.normals.vertex(n_face[j]);
54
                glNormal3fv(norm.get());
55
            }
56
            if(t_face != Geometry::NULL_FACE)
57
            {
58
                Vec3f texc = tm.texcoords.vertex(t_face[j]);
59
                glTexCoord2fv(texc.get());
60
            }
61
            Vec3f vert = tm.geometry.vertex(g_face[j]);
62
            glVertex3fv(vert.get());
63
        }
64
    }
65
    glEnd();
66
    glDisable(GL_TEXTURE_2D);
67
}
299 jrf 68
 
69
void draw(const Geometry::AABox& box)
70
{
71
	glBegin(GL_QUADS);
72
	Vec3f norm_neg[] = {Vec3f(0,0,-1), Vec3f(-1,0,0), Vec3f(0,-1,0)};
73
	Vec3f norm_pos[] = {Vec3f(0,0, 1), Vec3f( 1,0,0), Vec3f(0, 1,0)};
74
	for(int j=0;j<3;++j)
75
		{
76
			glNormal3fv(norm_neg[j].get());
77
			Vec3f p = box.get_pmin();
78
			glVertex3f(p[0], p[1], p[2]);
79
			p[(j+1)%3] = box.get_pmax()[(j+1)%3];
80
			glVertex3f(p[0], p[1], p[2]);
81
			p[j] = box.get_pmax()[j];
82
			glVertex3f(p[0], p[1], p[2]);
83
			p[(j+1)%3] = box.get_pmin()[(j+1)%3];
84
			glVertex3f(p[0], p[1], p[2]);
85
		}
86
	glEnd();
87
	glBegin(GL_QUADS);
88
	for(int j=0;j<3;++j)
89
		{
90
			glNormal3fv(norm_pos[j].get());
91
			Vec3f p = box.get_pmax();
92
			glVertex3f(p[0], p[1], p[2]);
93
			p[j] = box.get_pmin()[j];
94
			glVertex3f(p[0], p[1], p[2]);
95
			p[(j+1)%3] = box.get_pmin()[(j+1)%3];
96
			glVertex3f(p[0], p[1], p[2]);
97
			p[j] = box.get_pmax()[j];
98
			glVertex3f(p[0], p[1], p[2]);
99
		}
100
	glEnd();
101
}
102
 
103
void draw(const Geometry::OBox& box)
104
{
105
	Mat4x4f m = identity_Mat4x4f();
106
	copy_matrix(box.get_rotation(), m);
107
	glPushMatrix();
108
	glMultMatrixf(m.get());
109
	draw(box.get_aabox());
110
	glPopMatrix();
111
}
112
 
113
/** Draw the tree. The first argument is the level counter, the second
114
	argument is the level at which to stop drawing. */
115
template <class BoxType>
116
void draw(const Geometry::BoundingINode<BoxType>& node, int level, int max_level)
117
{
118
  if(level == max_level)
119
  {
120
	draw(node); 
121
	return;
122
  }
321 jab 123
  node->left->draw(level + 1, max_level);
124
  node->right->draw(level + 1, max_level);  
299 jrf 125
}
126
 
127
template <class BoxType>
128
void draw(const Geometry::BoundingLNode<BoxType>& node, int level, int max_level)
129
{
130
#if USE_LEAF_BOXES
131
	draw(node); 
132
#endif
133
}
134
 
135
template <class BoxType>
136
void draw(const Geometry::BoundingTree<BoxType>& tree, int max_level)
137
{
138
  draw(*tree.root, 0, max_level);
139
}
140
 
324 jab 141
}