ac8c11247909590c776e2f6fcacacd28fb93daa2
[laserbrain_demo] / src / material.cc
1 #include <algorithm>
2 #include <string.h>
3 #include "opengl.h"
4 #include "material.h"
5
6 Material::Material()
7         : diffuse(1.0f, 1.0f, 1.0f)
8 {
9         shininess = 0.0f;
10         alpha = 1.0f;
11         memset(stdtex, 0, sizeof stdtex);
12 }
13
14 void Material::setup() const
15 {
16         float kd[] = {diffuse.x, diffuse.y, diffuse.z, alpha};
17         float ks[] = {specular.x, specular.y, specular.z, 1.0f};
18
19         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, kd);
20         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, ks);
21         glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
22
23         int ntex = std::min((int)textures.size(), 8);   // TODO: use max texture units
24         for(int i=0; i<ntex; i++) {
25                 bind_texture(textures[i], i);
26         }
27 }
28
29 void Material::add_texture(Texture *tex, int type)
30 {
31         textures.push_back(tex);
32
33         if(type != MTL_TEX_UNKNOWN) {
34                 stdtex[type] = tex;
35         }
36 }