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