material editing in scene metafile
[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 }
43
44 int mtl_parse_type(const char *str)
45 {
46         if(strcmp(str, "diffuse") == 0) {
47                 return MTL_TEX_DIFFUSE;
48         } else if(strcmp(str, "specular") == 0) {
49                 return MTL_TEX_SPECULAR;
50         } else if(strcmp(str, "normalmap") == 0) {
51                 return MTL_TEX_NORMALMAP;
52         } else if(strcmp(str, "lightmap") == 0) {
53                 return MTL_TEX_LIGHTMAP;
54         } else if(strcmp(str, "envmap") == 0) {
55                 return MTL_TEX_ENVMAP;
56         }
57         return MTL_TEX_UNKNOWN;
58 }
59
60 const char *mtl_type_string(int type)
61 {
62         switch(type) {
63         case MTL_TEX_DIFFUSE:
64                 return "diffuse";
65         case MTL_TEX_SPECULAR:
66                 return "specular";
67         case MTL_TEX_NORMALMAP:
68                 return "normalmap";
69         case MTL_TEX_LIGHTMAP:
70                 return "lightmap";
71         case MTL_TEX_ENVMAP:
72                 return "envmap";
73         default:
74                 break;
75         }
76         return "unknown";
77 }