conworlds
diff src/material.cc @ 13:283cdfa7dda2
added a crapload of code from goat3dgfx
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 24 Aug 2014 09:41:24 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/material.cc Sun Aug 24 09:41:24 2014 +0300 1.3 @@ -0,0 +1,75 @@ 1.4 +#include "opengl.h" 1.5 +#include "material.h" 1.6 +#include "unistate.h" 1.7 + 1.8 +Material::Material() 1.9 + : diffuse(1, 1, 1), specular(0, 0, 0) 1.10 +{ 1.11 + alpha = 1.0; 1.12 + shininess = 1.0; 1.13 + 1.14 + for(int i=0; i<MAX_MTL_TEXTURES; i++) { 1.15 + tex[i] = 0; 1.16 + } 1.17 +} 1.18 + 1.19 + 1.20 +void Material::setup(bool use_textures) const 1.21 +{ 1.22 + static bool done_init; 1.23 + static int st_diffuse_idx, st_specular_idx, st_shininess_idx, st_alpha_idx; 1.24 + static int st_tex_diffuse_idx, st_tex_specular_idx, st_tex_normal_idx; 1.25 + static int st_tex_idx[MAX_MTL_TEXTURES]; 1.26 + 1.27 + if(!done_init) { 1.28 + st_diffuse_idx = add_unistate("st_mtl_diffuse", ST_FLOAT3); 1.29 + st_specular_idx = add_unistate("st_mtl_specular", ST_FLOAT3); 1.30 + st_shininess_idx = add_unistate("st_mtl_shininess", ST_FLOAT); 1.31 + st_alpha_idx = add_unistate("st_mtl_alpha", ST_FLOAT); 1.32 + st_tex_diffuse_idx = add_unistate("st_tex_diffuse", ST_INT); 1.33 + st_tex_specular_idx = add_unistate("st_tex_specular", ST_INT); 1.34 + st_tex_normal_idx = add_unistate("st_tex_normal", ST_INT); 1.35 + 1.36 + for(int i=0; i<MAX_MTL_TEXTURES; i++) { 1.37 + char name[32]; 1.38 + sprintf(name, "st_tex%d", i); 1.39 + st_tex_idx[i] = add_unistate(name, ST_INT); 1.40 + } 1.41 + done_init = true; 1.42 + } 1.43 + 1.44 + set_unistate(st_diffuse_idx, diffuse); 1.45 + set_unistate(st_specular_idx, specular); 1.46 + set_unistate(st_shininess_idx, shininess); 1.47 + set_unistate(st_alpha_idx, alpha); 1.48 + 1.49 + if(!use_textures) { 1.50 + return; // we're done 1.51 + } 1.52 + 1.53 + const int tex_named_idx[] = { 1.54 + st_tex_diffuse_idx, st_tex_specular_idx, st_tex_normal_idx, -1 1.55 + }; 1.56 + 1.57 + int tex_unit = 0; 1.58 + for(int i=0; i<MAX_MTL_TEXTURES; i++) { 1.59 + if(tex[i]) { 1.60 + bind_texture(tex[i], tex_unit); 1.61 + 1.62 + if(tex_named_idx[i] != -1) { 1.63 + set_unistate(tex_named_idx[i], tex_unit); 1.64 + } 1.65 + set_unistate(st_tex_idx[i], tex_unit); 1.66 + tex_unit++; 1.67 + } 1.68 + } 1.69 + 1.70 + // also do fixed-function setup 1.71 + float dcol[] = {diffuse.x, diffuse.y, diffuse.z, alpha}; 1.72 + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol); 1.73 + 1.74 + float scol[] = {specular.x, specular.y, specular.z, 1.0f}; 1.75 + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol); 1.76 + 1.77 + glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess); 1.78 +}