dungeon_crawler

view 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
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[] = {
40 aiTextureType_DIFFUSE,
41 aiTextureType_NORMALS,
42 aiTextureType_SPECULAR
43 };
45 for(int i=0; i<NUM_TEXTURE_TYPES; i++) {
46 aiString tex_name;
47 if(aiGetMaterialString(assmat, AI_MATKEY_TEXTURE(asstypes[i], 0), &tex_name) == 0) {
48 tex[i] = texset->get_texture(tex_name.data);
49 } else {
50 tex[i] = 0;
51 }
52 }
53 }
55 void Material::setup() const
56 {
57 float dcol[] = {kd.x, kd.y, kd.z, 1.0};
58 float scol[] = {ks.x, ks.y, ks.z, 1.0};
60 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
61 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
62 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
64 if(tex[TEXTYPE_DIFFUSE]) {
65 glActiveTextureARB(GL_TEXTURE0);
66 glBindTexture(GL_TEXTURE_2D, tex[TEXTYPE_DIFFUSE]);
67 glEnable(GL_TEXTURE_2D);
68 } else {
69 glDisable(GL_TEXTURE_2D);
70 }
71 glActiveTextureARB(GL_TEXTURE0);
72 }