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