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