added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / 3dengfx / material.cpp
1 /*
2 Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
3
4 This file is part of the 3dengfx, realtime visualization system.
5
6 3dengfxis free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 3dengfx is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with 3dengfx; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "3dengfx_config.h"
22
23 #include "opengl.h"
24 #include "material.hpp"
25
26 Material::Material() {
27         ambient_color = diffuse_color = Color(1.0f, 1.0f, 1.0f);
28         specular_color = emissive_color = Color(0.0f, 0.0f, 0.0f);
29         
30         specular_power = 1.0f;
31         env_intensity = 1.0f;
32         bump_intensity = 1.0f;
33         alpha = 1.0f;
34
35         wireframe = false;
36         shading = SHADING_GOURAUD;
37
38         auto_refl = true;
39         auto_refl_upd = 1;
40
41         two_sided = false;
42
43         tex_count = 0;
44         
45         memset(tex, 0, MAX_TEXTURES * sizeof(Texture*));
46 }
47
48 Material::Material(const Color &col) {
49         ambient_color = diffuse_color = col;
50         specular_color = emissive_color = Color(0.0f, 0.0f, 0.0f);
51         
52         specular_power = 1.0f;
53         env_intensity = 1.0f;
54         bump_intensity = 1.0f;
55         alpha = 1.0f;
56         
57         wireframe = false;
58         shading = SHADING_GOURAUD;
59
60         auto_refl = true;
61         auto_refl_upd = 1;
62
63         tex_count = 0;
64         
65         memset(tex, 0, MAX_TEXTURES * sizeof(Texture*));
66 }
67
68 void Material::set_texture(Texture *texture, TextureType type) {
69         if(texture) tex_count++;
70         tex[type] = texture;
71 }
72
73 Texture *Material::get_texture(TextureType type) {
74         return tex[type];
75 }
76
77 int Material::get_texture_count() const {
78         return tex_count;
79 }
80
81 void Material::set_glmaterial() const {
82         float amb[] = {ambient_color.r, ambient_color.g, ambient_color.b, ambient_color.a};
83         float dif[] = {diffuse_color.r, diffuse_color.g, diffuse_color.b, diffuse_color.a};
84         float spc[] = {specular_color.r, specular_color.g, specular_color.b, specular_color.a};
85         float ems[] = {emissive_color.r, emissive_color.g, emissive_color.b, emissive_color.a};
86
87         amb[3] *= alpha;
88         dif[3] *= alpha;
89         spc[3] *= alpha;
90         ems[3] *= alpha;
91         
92         glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
93         glMaterialfv(GL_FRONT, GL_DIFFUSE, dif);
94         glMaterialfv(GL_FRONT, GL_SPECULAR, spc);
95         glMaterialfv(GL_FRONT, GL_EMISSION, ems);
96         glMaterialf(GL_FRONT, GL_SHININESS, specular_power);
97 }