6288ef4640dcee291a78c33c6534cb54b0f61370
[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         /*
26         int ntex = std::min((int)textures.size(), 8);   // TODO: use max texture units
27         for(int i=0; i<ntex; i++) {
28                 bind_texture(textures[i], i);
29         }
30         */
31         for(int i=0; i<NUM_MTL_TEXTURES; i++) {
32                 if(stdtex[i]) {
33                         bind_texture(stdtex[i], i);
34                 }
35         }
36
37         if(stdtex[MTL_TEX_LIGHTMAP]) {
38                 bind_program(stdtex[MTL_TEX_DIFFUSE] ? sdr_ltmap : sdr_ltmap_notex);
39         }
40 }
41
42 void Material::add_texture(Texture *tex, int type)
43 {
44         if(std::find(textures.begin(), textures.end(), tex) == textures.end()) {
45                 textures.push_back(tex);
46         }
47
48         if(type != MTL_TEX_UNKNOWN) {
49                 stdtex[type] = tex;
50         }
51 }
52
53 void Material::remove_texture(Texture *tex)
54 {
55         std::vector<Texture*>::iterator it = std::find(textures.begin(), textures.end(), tex);
56         if(it != textures.end()) {
57                 textures.erase(it);
58         }
59
60         for(int i=0; i<NUM_MTL_TEXTURES; i++) {
61                 if(stdtex[i] == tex) {
62                         stdtex[i] = 0;
63                 }
64         }
65 }
66
67 int mtl_parse_type(const char *str)
68 {
69         if(strcmp(str, "diffuse") == 0) {
70                 return MTL_TEX_DIFFUSE;
71         } else if(strcmp(str, "specular") == 0) {
72                 return MTL_TEX_SPECULAR;
73         } else if(strcmp(str, "normalmap") == 0) {
74                 return MTL_TEX_NORMALMAP;
75         } else if(strcmp(str, "lightmap") == 0) {
76                 return MTL_TEX_LIGHTMAP;
77         } else if(strcmp(str, "envmap") == 0) {
78                 return MTL_TEX_ENVMAP;
79         }
80         return MTL_TEX_UNKNOWN;
81 }
82
83 const char *mtl_type_string(int type)
84 {
85         switch(type) {
86         case MTL_TEX_DIFFUSE:
87                 return "diffuse";
88         case MTL_TEX_SPECULAR:
89                 return "specular";
90         case MTL_TEX_NORMALMAP:
91                 return "normalmap";
92         case MTL_TEX_LIGHTMAP:
93                 return "lightmap";
94         case MTL_TEX_ENVMAP:
95                 return "envmap";
96         default:
97                 break;
98         }
99         return "unknown";
100 }