dungeon_crawler

view prototype/src/material.cc @ 14:67ae9fcb802c

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