dungeon_crawler

view prototype/src/material.cc @ 11:e5567ddbf2ef

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