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