dungeon_crawler

annotate prototype/src/material.cc @ 12:e95462632f9a

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 19 Aug 2012 05:35:00 +0300
parents e5567ddbf2ef
children 67ae9fcb802c
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@11 39 unsigned int asstypes[] = {
nuclear@11 40 aiTextureType_DIFFUSE,
nuclear@11 41 aiTextureType_NORMALS,
nuclear@11 42 aiTextureType_SPECULAR
nuclear@11 43 };
nuclear@11 44
nuclear@11 45 for(int i=0; i<NUM_TEXTURE_TYPES; i++) {
nuclear@11 46 aiString tex_name;
nuclear@11 47 if(aiGetMaterialString(assmat, AI_MATKEY_TEXTURE(asstypes[i], 0), &tex_name) == 0) {
nuclear@11 48 tex[i] = texset->get_texture(tex_name.data);
nuclear@11 49 } else {
nuclear@11 50 tex[i] = 0;
nuclear@11 51 }
nuclear@11 52 }
nuclear@11 53 }
nuclear@11 54
nuclear@11 55 void Material::setup() const
nuclear@11 56 {
nuclear@11 57 float dcol[] = {kd.x, kd.y, kd.z, 1.0};
nuclear@11 58 float scol[] = {ks.x, ks.y, ks.z, 1.0};
nuclear@11 59
nuclear@11 60 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
nuclear@11 61 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
nuclear@11 62 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
nuclear@11 63
nuclear@11 64 if(tex[TEXTYPE_DIFFUSE]) {
nuclear@11 65 glActiveTextureARB(GL_TEXTURE0);
nuclear@11 66 glBindTexture(GL_TEXTURE_2D, tex[TEXTYPE_DIFFUSE]);
nuclear@11 67 glEnable(GL_TEXTURE_2D);
nuclear@11 68 } else {
nuclear@11 69 glDisable(GL_TEXTURE_2D);
nuclear@11 70 }
nuclear@11 71 glActiveTextureARB(GL_TEXTURE0);
nuclear@11 72 }