goat3dgfx

annotate src/material.cc @ 34:3eb6c8f89fe1

merge
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Mar 2014 17:41:10 +0200
parents 1873dfd13f2d
children
rev   line source
nuclear@0 1 #include "material.h"
nuclear@0 2 #include "unistate.h"
nuclear@0 3
nuclear@15 4 using namespace goatgfx;
nuclear@15 5
nuclear@0 6 Material::Material()
nuclear@0 7 : diffuse(1, 1, 1), specular(0, 0, 0)
nuclear@0 8 {
nuclear@0 9 alpha = 1.0;
nuclear@0 10 shininess = 1.0;
nuclear@0 11
nuclear@0 12 for(int i=0; i<MAX_MTL_TEXTURES; i++) {
nuclear@0 13 tex[i] = 0;
nuclear@0 14 }
nuclear@0 15 }
nuclear@0 16
nuclear@0 17
nuclear@0 18 void Material::setup(bool use_textures) const
nuclear@0 19 {
nuclear@0 20 static bool done_init;
nuclear@0 21 static int st_diffuse_idx, st_specular_idx, st_shininess_idx, st_alpha_idx;
nuclear@0 22 static int st_tex_diffuse_idx, st_tex_specular_idx, st_tex_normal_idx;
nuclear@0 23 static int st_tex_idx[MAX_MTL_TEXTURES];
nuclear@0 24
nuclear@0 25 if(!done_init) {
nuclear@0 26 st_diffuse_idx = add_unistate("st_mtl_diffuse", ST_FLOAT3);
nuclear@0 27 st_specular_idx = add_unistate("st_mtl_specular", ST_FLOAT3);
nuclear@0 28 st_shininess_idx = add_unistate("st_mtl_shininess", ST_FLOAT);
nuclear@0 29 st_alpha_idx = add_unistate("st_mtl_alpha", ST_FLOAT);
nuclear@0 30 st_tex_diffuse_idx = add_unistate("st_tex_diffuse", ST_INT);
nuclear@0 31 st_tex_specular_idx = add_unistate("st_tex_specular", ST_INT);
nuclear@0 32 st_tex_normal_idx = add_unistate("st_tex_normal", ST_INT);
nuclear@0 33
nuclear@0 34 for(int i=0; i<MAX_MTL_TEXTURES; i++) {
nuclear@0 35 char name[32];
nuclear@0 36 sprintf(name, "st_tex%d", i);
nuclear@0 37 st_tex_idx[i] = add_unistate(name, ST_INT);
nuclear@0 38 }
nuclear@0 39 done_init = true;
nuclear@0 40 }
nuclear@0 41
nuclear@0 42 set_unistate(st_diffuse_idx, diffuse);
nuclear@0 43 set_unistate(st_specular_idx, specular);
nuclear@0 44 set_unistate(st_shininess_idx, shininess);
nuclear@0 45 set_unistate(st_alpha_idx, alpha);
nuclear@0 46
nuclear@0 47 if(!use_textures) {
nuclear@0 48 return; // we're done
nuclear@0 49 }
nuclear@0 50
nuclear@0 51 const int tex_named_idx[] = {
nuclear@0 52 st_tex_diffuse_idx, st_tex_specular_idx, st_tex_normal_idx, -1
nuclear@0 53 };
nuclear@0 54
nuclear@0 55 int tex_unit = 0;
nuclear@0 56 for(int i=0; i<MAX_MTL_TEXTURES; i++) {
nuclear@0 57 if(tex[i]) {
nuclear@0 58 tex[i]->bind(tex_unit);
nuclear@0 59
nuclear@0 60 if(tex_named_idx[i]) {
nuclear@0 61 set_unistate(tex_named_idx[i], tex_unit);
nuclear@0 62 }
nuclear@0 63 set_unistate(st_tex_idx[i], tex_unit);
nuclear@0 64 tex_unit++;
nuclear@0 65 }
nuclear@0 66 }
nuclear@0 67 }