2 Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
4 This file is part of 3dengfx, realtime visualization system.
6 3dengfx is 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.
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.
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
21 #include "3dengfx_config.h"
23 #include "3denginefx.hpp"
28 ambient_color = Color(0, 0, 0);
29 diffuse_color = Color(1.0f, 1.0f, 1.0f);
30 specular_color = Color(1.0f, 1.0f, 1.0f);
32 attenuation[0] = 1.0f;
33 attenuation[1] = 0.0f;
34 attenuation[2] = 0.0f;
40 void Light::set_color(const Color &c, unsigned short color_flags) {
41 if(color_flags & LIGHTCOL_AMBIENT) {
45 if(color_flags & LIGHTCOL_DIFFUSE) {
49 if(color_flags & LIGHTCOL_SPECULAR) {
55 void Light::set_color(const Color &amb, const Color &diff, const Color &spec) {
58 specular_color = spec;
61 Color Light::get_color(unsigned short which) const {
63 case LIGHTCOL_AMBIENT:
66 case LIGHTCOL_SPECULAR:
67 return specular_color;
69 case LIGHTCOL_DIFFUSE:
75 void Light::set_intensity(scalar_t intensity) {
76 this->intensity = intensity;
79 scalar_t Light::get_intensity() const {
83 void Light::set_attenuation(scalar_t att0, scalar_t att1, scalar_t att2) {
84 attenuation[0] = att0;
85 attenuation[1] = att1;
86 attenuation[2] = att2;
89 scalar_t Light::get_attenuation(int which) const {
90 return attenuation[which];
93 Vector3 Light::get_attenuation_vector() const {
94 return Vector3(attenuation[0], attenuation[1], attenuation[2]);
97 void Light::set_shadow_casting(bool shd) {
101 bool Light::casts_shadows() const {
105 // ------- point lights -------
107 PointLight::PointLight(const Vector3 &pos, const Color &col) {
109 diffuse_color = specular_color = col;
112 PointLight::~PointLight() {}
114 void PointLight::set_gl_light(int n, unsigned long time) const {
115 int light_num = GL_LIGHT0 + n;
118 if(time == XFORM_LOCAL_PRS) {
119 pos = (Vector4)local_prs.position;
121 pos = (Vector4)get_prs(time).position;
124 glMatrixMode(GL_MODELVIEW);
126 Matrix4x4 test = engfx_state::view_matrix;
128 load_matrix_gl(test);
130 Color amb = ambient_color * intensity;
131 Color dif = diffuse_color * intensity;
132 Color spec = specular_color * intensity;
134 float position[] = {0.0f, 0.0f, 0.0f, 1.0f};
135 float gl_amb[] = {amb.r, amb.g, amb.b, ambient_color.a};
136 float gl_dif[] = {dif.r, dif.g, dif.b, diffuse_color.a};
137 float gl_spec[] = {spec.r, spec.g, spec.b, specular_color.a};
139 glLightfv(light_num, GL_POSITION, position);
140 glLightfv(light_num, GL_AMBIENT, gl_amb);
141 glLightfv(light_num, GL_DIFFUSE, gl_dif);
142 glLightfv(light_num, GL_SPECULAR, gl_spec);
143 glLightf(light_num, GL_CONSTANT_ATTENUATION, (float)attenuation[0]);
144 glLightf(light_num, GL_LINEAR_ATTENUATION, (float)attenuation[1]);
145 glLightf(light_num, GL_QUADRATIC_ATTENUATION, (float)attenuation[2]);
151 if(!engfx_state::bump_light) set_bump_light(this);
156 // ------- directional lights -------
158 DirLight::DirLight(const Vector3 &dir, const Color &col) {
160 diffuse_color = specular_color = col;
163 DirLight::~DirLight() {}
165 Vector3 DirLight::get_direction()
170 void DirLight::set_gl_light(int n, unsigned long time) const {
171 int light_num = GL_LIGHT0 + n;
173 Vector3 ldir = dir.transformed(get_prs(time).rotation);
175 glMatrixMode(GL_MODELVIEW);
177 Matrix4x4 test = engfx_state::view_matrix;
178 load_matrix_gl(test);
180 Color amb = ambient_color * intensity;
181 Color dif = diffuse_color * intensity;
182 Color spec = specular_color * intensity;
184 float position[] = {-ldir.x, -ldir.y, -ldir.z, 0.0f};
185 float gl_amb[] = {amb.r, amb.g, amb.b, ambient_color.a};
186 float gl_dif[] = {dif.r, dif.g, dif.b, diffuse_color.a};
187 float gl_spec[] = {spec.r, spec.g, spec.b, specular_color.a};
189 glLightfv(light_num, GL_POSITION, position);
190 glLightfv(light_num, GL_AMBIENT, gl_amb);
191 glLightfv(light_num, GL_DIFFUSE, gl_dif);
192 glLightfv(light_num, GL_SPECULAR, gl_spec);
198 if(!engfx_state::bump_light) set_bump_light(this);