conworlds

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