Subversion Repositories gelsvn

Rev

Rev 178 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
167 jab 1
#include "gel_gl.h"
2
#include "draw.h"
3
 
4
using namespace CGLA;
5
using namespace std;
6
 
7
namespace 
8
{
9
void set_material(const Geometry::Material& material)
10
{
11
    		if(material.tex_id >=0)
12
    			{
13
    				glEnable(GL_TEXTURE_2D);
14
    				glBindTexture(GL_TEXTURE_2D, material.tex_id);
15
    				glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
16
    			}
17
    		else
18
    			glDisable(GL_TEXTURE_2D);
19
 
20
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material.ambient);
21
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material.diffuse);
22
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material.specular);
23
    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, material.shininess);
24
}
25
 
26
}
27
 
28
void draw(const Geometry::TriMesh& tm)
29
{
30
    int old_mat_idx = -1;
31
    glBegin(GL_TRIANGLES);
32
    for(int i=0;i<tm.geometry.no_faces();i++)
33
    {
34
        if(tm.mat_idx[i] != old_mat_idx)
35
        {
36
            glEnd();
37
            set_material(tm.materials[tm.mat_idx[i]]);
38
            glBegin(GL_TRIANGLES);
39
            old_mat_idx = tm.mat_idx[i];
40
        }
41
        Vec3i n_face = tm.normals.face(i);
42
        Vec3i g_face = tm.geometry.face(i);
43
        Vec3i t_face = tm.texcoords.face(i);
44
        for(int j=0;j<3;j++)
45
        {
46
            if(n_face != Geometry::NULL_FACE)
47
            {
48
                Vec3f norm = tm.normals.vertex(n_face[j]);
49
                glNormal3fv(norm.get());
50
            }
51
            if(t_face != Geometry::NULL_FACE)
52
            {
53
                Vec3f texc = tm.texcoords.vertex(t_face[j]);
54
                glTexCoord2fv(texc.get());
55
            }
56
            Vec3f vert = tm.geometry.vertex(g_face[j]);
57
            glVertex3fv(vert.get());
58
        }
59
    }
60
    glEnd();
61
    glDisable(GL_TEXTURE_2D);
62
}