mirror mtledit
[laserbrain_demo] / src / material.cc
index 962b10c..26ab63d 100644 (file)
@@ -1,11 +1,19 @@
+#include <algorithm>
+#include <string.h>
 #include "opengl.h"
 #include "material.h"
+#include "sdr.h"
+#include "app.h"
 
 Material::Material()
        : diffuse(1.0f, 1.0f, 1.0f)
 {
        shininess = 0.0f;
        alpha = 1.0f;
+       memset(stdtex, 0, sizeof stdtex);
+
+       reflect = 0.0f;
+       flat_mirror = false;
 }
 
 void Material::setup() const
@@ -16,4 +24,80 @@ void Material::setup() const
        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, kd);
        glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, ks);
        glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
+
+       /*
+       int ntex = std::min((int)textures.size(), 8);   // TODO: use max texture units
+       for(int i=0; i<ntex; i++) {
+               bind_texture(textures[i], i);
+       }
+       */
+       for(int i=0; i<NUM_MTL_TEXTURES; i++) {
+               if(stdtex[i]) {
+                       bind_texture(stdtex[i], i);
+               }
+       }
+
+       if(stdtex[MTL_TEX_LIGHTMAP]) {
+               bind_program(stdtex[MTL_TEX_DIFFUSE] ? sdr_ltmap : sdr_ltmap_notex);
+       }
+}
+
+void Material::add_texture(Texture *tex, int type)
+{
+       if(std::find(textures.begin(), textures.end(), tex) == textures.end()) {
+               textures.push_back(tex);
+       }
+
+       if(type != MTL_TEX_UNKNOWN) {
+               stdtex[type] = tex;
+       }
+}
+
+void Material::remove_texture(Texture *tex)
+{
+       std::vector<Texture*>::iterator it = std::find(textures.begin(), textures.end(), tex);
+       if(it != textures.end()) {
+               textures.erase(it);
+       }
+
+       for(int i=0; i<NUM_MTL_TEXTURES; i++) {
+               if(stdtex[i] == tex) {
+                       stdtex[i] = 0;
+               }
+       }
+}
+
+int mtl_parse_type(const char *str)
+{
+       if(strcmp(str, "diffuse") == 0) {
+               return MTL_TEX_DIFFUSE;
+       } else if(strcmp(str, "specular") == 0) {
+               return MTL_TEX_SPECULAR;
+       } else if(strcmp(str, "normalmap") == 0) {
+               return MTL_TEX_NORMALMAP;
+       } else if(strcmp(str, "lightmap") == 0) {
+               return MTL_TEX_LIGHTMAP;
+       } else if(strcmp(str, "envmap") == 0) {
+               return MTL_TEX_ENVMAP;
+       }
+       return MTL_TEX_UNKNOWN;
+}
+
+const char *mtl_type_string(int type)
+{
+       switch(type) {
+       case MTL_TEX_DIFFUSE:
+               return "diffuse";
+       case MTL_TEX_SPECULAR:
+               return "specular";
+       case MTL_TEX_NORMALMAP:
+               return "normalmap";
+       case MTL_TEX_LIGHTMAP:
+               return "lightmap";
+       case MTL_TEX_ENVMAP:
+               return "envmap";
+       default:
+               break;
+       }
+       return "unknown";
 }