dungeon_crawler

diff prototype/src/material.cc @ 11:e5567ddbf2ef

- Texture set (texture manager) - materials
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 19 Aug 2012 03:11:36 +0300
parents
children e95462632f9a
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/prototype/src/material.cc	Sun Aug 19 03:11:36 2012 +0300
     1.3 @@ -0,0 +1,71 @@
     1.4 +#include <assert.h>
     1.5 +#include <assimp/material.h>
     1.6 +#include "opengl.h"
     1.7 +#include "material.h"
     1.8 +
     1.9 +Material::Material()
    1.10 +	: kd(1.0, 1.0, 1.0), ks(0.0, 0.0, 0.0)
    1.11 +{
    1.12 +	shin = 1.0;
    1.13 +	memset(tex, 0, sizeof tex);
    1.14 +}
    1.15 +
    1.16 +void Material::load(const aiMaterial *assmat, TextureSet *texset)
    1.17 +{
    1.18 +	aiColor4D col;
    1.19 +	float val;
    1.20 +
    1.21 +	aiGetMaterialColor(assmat, AI_MATKEY_COLOR_DIFFUSE, &col);
    1.22 +	kd = Color(col[0], col[1], col[2]);
    1.23 +	aiGetMaterialColor(assmat, AI_MATKEY_COLOR_SPECULAR, &col);
    1.24 +	ks = Color(col[0], col[1], col[2]);
    1.25 +
    1.26 +	unsigned int sz = 1;
    1.27 +	val = 60.0;
    1.28 +	aiGetMaterialFloatArray(assmat, AI_MATKEY_SHININESS, &val, &sz);
    1.29 +	if(val > 127) {
    1.30 +		printf("GOT SHININESS: %f\n", val);
    1.31 +		val = 127;
    1.32 +	}
    1.33 +	shin = val;
    1.34 +
    1.35 +	sz = 1;
    1.36 +	val = 1.0;
    1.37 +	aiGetMaterialFloatArray(assmat, AI_MATKEY_SHININESS_STRENGTH, &val, &sz);
    1.38 +	ks *= val;
    1.39 +
    1.40 +	// must match TEXTYPE enum in material.h
    1.41 +	unsigned int asstypes[] = {
    1.42 +		aiTextureType_DIFFUSE,
    1.43 +		aiTextureType_NORMALS,
    1.44 +		aiTextureType_SPECULAR
    1.45 +	};
    1.46 +
    1.47 +	for(int i=0; i<NUM_TEXTURE_TYPES; i++) {
    1.48 +		aiString tex_name;
    1.49 +		if(aiGetMaterialString(assmat, AI_MATKEY_TEXTURE(asstypes[i], 0), &tex_name) == 0) {
    1.50 +			tex[i] = texset->get_texture(tex_name.data);
    1.51 +		} else {
    1.52 +			tex[i] = 0;
    1.53 +		}
    1.54 +	}
    1.55 +}
    1.56 +
    1.57 +void Material::setup() const
    1.58 +{
    1.59 +	float dcol[] = {kd.x, kd.y, kd.z, 1.0};
    1.60 +	float scol[] = {ks.x, ks.y, ks.z, 1.0};
    1.61 +
    1.62 +	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
    1.63 +	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
    1.64 +	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
    1.65 +
    1.66 +	if(tex[TEXTYPE_DIFFUSE]) {
    1.67 +		glActiveTextureARB(GL_TEXTURE0);
    1.68 +		glBindTexture(GL_TEXTURE_2D, tex[TEXTYPE_DIFFUSE]);
    1.69 +		glEnable(GL_TEXTURE_2D);
    1.70 +	} else {
    1.71 +		glDisable(GL_TEXTURE_2D);
    1.72 +	}
    1.73 +	glActiveTextureARB(GL_TEXTURE0);
    1.74 +}