simple ubershader system, reflection debugging
[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         /*
41         if(stdtex[MTL_TEX_LIGHTMAP]) {
42                 bind_program(stdtex[MTL_TEX_DIFFUSE] ? sdr_ltmap : sdr_ltmap_notex);
43         }
44         */
45 }
46
47 void Material::add_texture(Texture *tex, int type)
48 {
49         if(std::find(textures.begin(), textures.end(), tex) == textures.end()) {
50                 textures.push_back(tex);
51         }
52
53         if(type != MTL_TEX_UNKNOWN) {
54                 stdtex[type] = tex;
55         }
56 }
57
58 void Material::remove_texture(Texture *tex)
59 {
60         std::vector<Texture*>::iterator it = std::find(textures.begin(), textures.end(), tex);
61         if(it != textures.end()) {
62                 textures.erase(it);
63         }
64
65         for(int i=0; i<NUM_MTL_TEXTURES; i++) {
66                 if(stdtex[i] == tex) {
67                         stdtex[i] = 0;
68                 }
69         }
70 }
71
72 int mtl_parse_type(const char *str)
73 {
74         if(strcmp(str, "diffuse") == 0) {
75                 return MTL_TEX_DIFFUSE;
76         } else if(strcmp(str, "specular") == 0) {
77                 return MTL_TEX_SPECULAR;
78         } else if(strcmp(str, "normalmap") == 0) {
79                 return MTL_TEX_NORMALMAP;
80         } else if(strcmp(str, "lightmap") == 0) {
81                 return MTL_TEX_LIGHTMAP;
82         } else if(strcmp(str, "envmap") == 0) {
83                 return MTL_TEX_REFLECT;
84         }
85         return MTL_TEX_UNKNOWN;
86 }
87
88 const char *mtl_type_string(int type)
89 {
90         switch(type) {
91         case MTL_TEX_DIFFUSE:
92                 return "diffuse";
93         case MTL_TEX_SPECULAR:
94                 return "specular";
95         case MTL_TEX_NORMALMAP:
96                 return "normalmap";
97         case MTL_TEX_LIGHTMAP:
98                 return "lightmap";
99         case MTL_TEX_REFLECT:
100                 return "envmap";
101         default:
102                 break;
103         }
104         return "unknown";
105 }