dungeon_crawler

annotate prototype/src/material.cc @ 20:48cfd351a9be

mrt going on nicely
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 21 Aug 2012 04:57:33 +0300
parents e95462632f9a
children ddb68dc4ba07
rev   line source
nuclear@12 1 #include <stdio.h>
nuclear@11 2 #include <assert.h>
nuclear@11 3 #include <assimp/material.h>
nuclear@11 4 #include "opengl.h"
nuclear@11 5 #include "material.h"
nuclear@11 6
nuclear@11 7 Material::Material()
nuclear@11 8 : kd(1.0, 1.0, 1.0), ks(0.0, 0.0, 0.0)
nuclear@11 9 {
nuclear@11 10 shin = 1.0;
nuclear@11 11 memset(tex, 0, sizeof tex);
nuclear@11 12 }
nuclear@11 13
nuclear@11 14 void Material::load(const aiMaterial *assmat, TextureSet *texset)
nuclear@11 15 {
nuclear@11 16 aiColor4D col;
nuclear@11 17 float val;
nuclear@11 18
nuclear@11 19 aiGetMaterialColor(assmat, AI_MATKEY_COLOR_DIFFUSE, &col);
nuclear@11 20 kd = Color(col[0], col[1], col[2]);
nuclear@11 21 aiGetMaterialColor(assmat, AI_MATKEY_COLOR_SPECULAR, &col);
nuclear@11 22 ks = Color(col[0], col[1], col[2]);
nuclear@11 23
nuclear@11 24 unsigned int sz = 1;
nuclear@11 25 val = 60.0;
nuclear@11 26 aiGetMaterialFloatArray(assmat, AI_MATKEY_SHININESS, &val, &sz);
nuclear@11 27 if(val > 127) {
nuclear@12 28 fprintf(stderr, "Warning: shininess %f... setting to 127\n", val);
nuclear@11 29 val = 127;
nuclear@11 30 }
nuclear@11 31 shin = val;
nuclear@11 32
nuclear@11 33 sz = 1;
nuclear@11 34 val = 1.0;
nuclear@11 35 aiGetMaterialFloatArray(assmat, AI_MATKEY_SHININESS_STRENGTH, &val, &sz);
nuclear@11 36 ks *= val;
nuclear@11 37
nuclear@11 38 // must match TEXTYPE enum in material.h
nuclear@14 39 unsigned int asstypes[][2] = {
nuclear@14 40 {aiTextureType_DIFFUSE, 0},
nuclear@14 41 {aiTextureType_NORMALS, aiTextureType_HEIGHT},
nuclear@14 42 {aiTextureType_SPECULAR, 0}
nuclear@11 43 };
nuclear@11 44
nuclear@11 45 for(int i=0; i<NUM_TEXTURE_TYPES; i++) {
nuclear@14 46 for(int j=0; j<2; j++) {
nuclear@14 47 aiString tex_name;
nuclear@14 48 if(asstypes[i][j] > 0 && aiGetMaterialString(assmat, AI_MATKEY_TEXTURE(asstypes[i][j], 0), &tex_name) == 0) {
nuclear@14 49 tex[i] = texset->get_texture(tex_name.data);
nuclear@14 50 break;
nuclear@14 51 } else {
nuclear@14 52 tex[i] = 0;
nuclear@14 53 }
nuclear@11 54 }
nuclear@11 55 }
nuclear@11 56 }
nuclear@11 57
nuclear@11 58 void Material::setup() const
nuclear@11 59 {
nuclear@11 60 float dcol[] = {kd.x, kd.y, kd.z, 1.0};
nuclear@11 61 float scol[] = {ks.x, ks.y, ks.z, 1.0};
nuclear@11 62
nuclear@11 63 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
nuclear@11 64 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
nuclear@11 65 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
nuclear@11 66
nuclear@11 67 if(tex[TEXTYPE_DIFFUSE]) {
nuclear@11 68 glActiveTextureARB(GL_TEXTURE0);
nuclear@11 69 glBindTexture(GL_TEXTURE_2D, tex[TEXTYPE_DIFFUSE]);
nuclear@11 70 glEnable(GL_TEXTURE_2D);
nuclear@11 71 } else {
nuclear@11 72 glDisable(GL_TEXTURE_2D);
nuclear@11 73 }
nuclear@11 74 glActiveTextureARB(GL_TEXTURE0);
nuclear@11 75 }