goat3dgfx

diff src/material.cc @ 0:1873dfd13f2d

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 14 Nov 2013 05:27:09 +0200
parents
children 7d6b667821cf
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/material.cc	Thu Nov 14 05:27:09 2013 +0200
     1.3 @@ -0,0 +1,65 @@
     1.4 +#include "material.h"
     1.5 +#include "unistate.h"
     1.6 +
     1.7 +Material::Material()
     1.8 +	: diffuse(1, 1, 1), specular(0, 0, 0)
     1.9 +{
    1.10 +	alpha = 1.0;
    1.11 +	shininess = 1.0;
    1.12 +
    1.13 +	for(int i=0; i<MAX_MTL_TEXTURES; i++) {
    1.14 +		tex[i] = 0;
    1.15 +	}
    1.16 +}
    1.17 +
    1.18 +
    1.19 +void Material::setup(bool use_textures) const
    1.20 +{
    1.21 +	static bool done_init;
    1.22 +	static int st_diffuse_idx, st_specular_idx, st_shininess_idx, st_alpha_idx;
    1.23 +	static int st_tex_diffuse_idx, st_tex_specular_idx, st_tex_normal_idx;
    1.24 +	static int st_tex_idx[MAX_MTL_TEXTURES];
    1.25 +
    1.26 +	if(!done_init) {
    1.27 +		st_diffuse_idx = add_unistate("st_mtl_diffuse", ST_FLOAT3);
    1.28 +		st_specular_idx = add_unistate("st_mtl_specular", ST_FLOAT3);
    1.29 +		st_shininess_idx = add_unistate("st_mtl_shininess", ST_FLOAT);
    1.30 +		st_alpha_idx = add_unistate("st_mtl_alpha", ST_FLOAT);
    1.31 +		st_tex_diffuse_idx = add_unistate("st_tex_diffuse", ST_INT);
    1.32 +		st_tex_specular_idx = add_unistate("st_tex_specular", ST_INT);
    1.33 +		st_tex_normal_idx = add_unistate("st_tex_normal", ST_INT);
    1.34 +
    1.35 +		for(int i=0; i<MAX_MTL_TEXTURES; i++) {
    1.36 +			char name[32];
    1.37 +			sprintf(name, "st_tex%d", i);
    1.38 +			st_tex_idx[i] = add_unistate(name, ST_INT);
    1.39 +		}
    1.40 +		done_init = true;
    1.41 +	}
    1.42 +
    1.43 +	set_unistate(st_diffuse_idx, diffuse);
    1.44 +	set_unistate(st_specular_idx, specular);
    1.45 +	set_unistate(st_shininess_idx, shininess);
    1.46 +	set_unistate(st_alpha_idx, alpha);
    1.47 +
    1.48 +	if(!use_textures) {
    1.49 +		return;	// we're done
    1.50 +	}
    1.51 +
    1.52 +	const int tex_named_idx[] = {
    1.53 +		st_tex_diffuse_idx, st_tex_specular_idx, st_tex_normal_idx, -1
    1.54 +	};
    1.55 +
    1.56 +	int tex_unit = 0;
    1.57 +	for(int i=0; i<MAX_MTL_TEXTURES; i++) {
    1.58 +		if(tex[i]) {
    1.59 +			tex[i]->bind(tex_unit);
    1.60 +
    1.61 +			if(tex_named_idx[i]) {
    1.62 +				set_unistate(tex_named_idx[i], tex_unit);
    1.63 +			}
    1.64 +			set_unistate(st_tex_idx[i], tex_unit);
    1.65 +			tex_unit++;
    1.66 +		}
    1.67 +	}
    1.68 +}