conworlds

annotate src/material.cc @ 17:c814f77d177e

moved to SDL2
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 25 Aug 2014 22:02:08 +0300
parents
children
rev   line source
nuclear@13 1 #include "opengl.h"
nuclear@13 2 #include "material.h"
nuclear@13 3 #include "unistate.h"
nuclear@13 4
nuclear@13 5 Material::Material()
nuclear@13 6 : diffuse(1, 1, 1), specular(0, 0, 0)
nuclear@13 7 {
nuclear@13 8 alpha = 1.0;
nuclear@13 9 shininess = 1.0;
nuclear@13 10
nuclear@13 11 for(int i=0; i<MAX_MTL_TEXTURES; i++) {
nuclear@13 12 tex[i] = 0;
nuclear@13 13 }
nuclear@13 14 }
nuclear@13 15
nuclear@13 16
nuclear@13 17 void Material::setup(bool use_textures) const
nuclear@13 18 {
nuclear@13 19 static bool done_init;
nuclear@13 20 static int st_diffuse_idx, st_specular_idx, st_shininess_idx, st_alpha_idx;
nuclear@13 21 static int st_tex_diffuse_idx, st_tex_specular_idx, st_tex_normal_idx;
nuclear@13 22 static int st_tex_idx[MAX_MTL_TEXTURES];
nuclear@13 23
nuclear@13 24 if(!done_init) {
nuclear@13 25 st_diffuse_idx = add_unistate("st_mtl_diffuse", ST_FLOAT3);
nuclear@13 26 st_specular_idx = add_unistate("st_mtl_specular", ST_FLOAT3);
nuclear@13 27 st_shininess_idx = add_unistate("st_mtl_shininess", ST_FLOAT);
nuclear@13 28 st_alpha_idx = add_unistate("st_mtl_alpha", ST_FLOAT);
nuclear@13 29 st_tex_diffuse_idx = add_unistate("st_tex_diffuse", ST_INT);
nuclear@13 30 st_tex_specular_idx = add_unistate("st_tex_specular", ST_INT);
nuclear@13 31 st_tex_normal_idx = add_unistate("st_tex_normal", ST_INT);
nuclear@13 32
nuclear@13 33 for(int i=0; i<MAX_MTL_TEXTURES; i++) {
nuclear@13 34 char name[32];
nuclear@13 35 sprintf(name, "st_tex%d", i);
nuclear@13 36 st_tex_idx[i] = add_unistate(name, ST_INT);
nuclear@13 37 }
nuclear@13 38 done_init = true;
nuclear@13 39 }
nuclear@13 40
nuclear@13 41 set_unistate(st_diffuse_idx, diffuse);
nuclear@13 42 set_unistate(st_specular_idx, specular);
nuclear@13 43 set_unistate(st_shininess_idx, shininess);
nuclear@13 44 set_unistate(st_alpha_idx, alpha);
nuclear@13 45
nuclear@13 46 if(!use_textures) {
nuclear@13 47 return; // we're done
nuclear@13 48 }
nuclear@13 49
nuclear@13 50 const int tex_named_idx[] = {
nuclear@13 51 st_tex_diffuse_idx, st_tex_specular_idx, st_tex_normal_idx, -1
nuclear@13 52 };
nuclear@13 53
nuclear@13 54 int tex_unit = 0;
nuclear@13 55 for(int i=0; i<MAX_MTL_TEXTURES; i++) {
nuclear@13 56 if(tex[i]) {
nuclear@13 57 bind_texture(tex[i], tex_unit);
nuclear@13 58
nuclear@13 59 if(tex_named_idx[i] != -1) {
nuclear@13 60 set_unistate(tex_named_idx[i], tex_unit);
nuclear@13 61 }
nuclear@13 62 set_unistate(st_tex_idx[i], tex_unit);
nuclear@13 63 tex_unit++;
nuclear@13 64 }
nuclear@13 65 }
nuclear@13 66
nuclear@13 67 // also do fixed-function setup
nuclear@13 68 float dcol[] = {diffuse.x, diffuse.y, diffuse.z, alpha};
nuclear@13 69 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
nuclear@13 70
nuclear@13 71 float scol[] = {specular.x, specular.y, specular.z, 1.0f};
nuclear@13 72 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
nuclear@13 73
nuclear@13 74 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
nuclear@13 75 }