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