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