dungeon_crawler

view prototype/src/material.cc @ 70:f55ddab0f3a4

"fixed" the disappearing lights issue
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 09 Oct 2012 02:54:01 +0300
parents acfe0c0110fc
children
line source
1 #include <stdio.h>
2 #include <assert.h>
3 #include <assimp/material.h>
4 #include "opengl.h"
5 #include "material.h"
6 #include "renderer.h"
8 extern bool ass_obj_hack;
10 Material::Material()
11 : kd(1.0, 1.0, 1.0), ks(0.0, 0.0, 0.0)
12 {
13 shin = 1.0;
14 memset(tex, 0, sizeof tex);
15 }
17 void Material::load(const aiMaterial *assmat, TextureSet *texset)
18 {
19 aiColor4D col;
20 float val;
22 aiGetMaterialColor(assmat, AI_MATKEY_COLOR_DIFFUSE, &col);
23 kd = Color(col[0], col[1], col[2]);
24 aiGetMaterialColor(assmat, AI_MATKEY_COLOR_SPECULAR, &col);
25 ks = Color(col[0], col[1], col[2]);
27 unsigned int sz = 1;
28 val = 60.0;
29 aiGetMaterialFloatArray(assmat, AI_MATKEY_SHININESS, &val, &sz);
30 if(ass_obj_hack) {
31 val /= 4.0;
32 }
33 shin = val > 127 ? 127 : val;
35 sz = 1;
36 val = 1.0;
37 aiGetMaterialFloatArray(assmat, AI_MATKEY_SHININESS_STRENGTH, &val, &sz);
38 ks *= val;
40 // must match TEXTYPE enum in material.h
41 unsigned int asstypes[][2] = {
42 {aiTextureType_DIFFUSE, 0},
43 {aiTextureType_NORMALS, aiTextureType_HEIGHT},
44 {aiTextureType_SPECULAR, 0}
45 };
47 for(int i=0; i<NUM_TEXTURE_TYPES; i++) {
48 for(int j=0; j<2; j++) {
49 aiString tex_name;
50 if(asstypes[i][j] > 0 && aiGetMaterialString(assmat, AI_MATKEY_TEXTURE(asstypes[i][j], 0), &tex_name) == 0) {
51 tex[i] = texset->get(tex_name.data);
52 break;
53 } else {
54 tex[i] = 0;
55 }
56 }
57 }
58 }
60 void Material::setup() const
61 {
62 float dcol[] = {kd.x, kd.y, kd.z, 1.0};
63 float scol[] = {ks.x, ks.y, ks.z, 1.0};
65 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
66 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
67 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
69 if(tex[TEXTYPE_DIFFUSE]) {
70 glActiveTextureARB(GL_TEXTURE0);
71 glBindTexture(GL_TEXTURE_2D, tex[TEXTYPE_DIFFUSE]);
72 glEnable(GL_TEXTURE_2D);
73 } else {
74 glActiveTextureARB(GL_TEXTURE0);
75 glDisable(GL_TEXTURE_2D);
76 }
78 if(rend->get_current_program() && tex[TEXTYPE_NORMAL]) {
79 glActiveTextureARB(GL_TEXTURE1);
80 glBindTexture(GL_TEXTURE_2D, tex[TEXTYPE_NORMAL]);
81 glEnable(GL_TEXTURE_2D);
82 } else {
83 glActiveTextureARB(GL_TEXTURE1);
84 glDisable(GL_TEXTURE_2D);
85 }
87 glActiveTextureARB(GL_TEXTURE0);
88 }