dungeon_crawler

annotate prototype/src/material.cc @ 80:a373b36ffc17

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