1 /*
2  * NodePointLight.cpp
3  *
4  * Copyright (C) 1999 Stephen F. White
5  *
6  * This program 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.
10  *
11  * This program 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 this program (see the file "COPYING" for details); if
18  * not, write to the Free Software Foundation, Inc., 675 Mass Ave,
19  * Cambridge, MA 02139, USA.
20  */
21 
22 #include <stdio.h>
23 #include "stdafx.h"
24 
25 #include "NodePointLight.h"
26 #include "Scene.h"
27 #include "Proto.h"
28 #include "FieldValue.h"
29 #include "SFFloat.h"
30 #include "SFVec3f.h"
31 #include "SFColor.h"
32 #include "SFBool.h"
33 #include "Util.h"
34 #include "resource.h"
35 
36 enum {
37     LOCATION
38 };
39 
ProtoPointLight(Scene * scene)40 ProtoPointLight::ProtoPointLight(Scene *scene)
41   : Proto(scene, "PointLight")
42 {
43     ambientIntensity.set(
44           addExposedField(SFFLOAT, "ambientIntensity", new SFFloat(0.0f),
45                           new SFFloat(0.0f), new SFFloat(1.0f)));
46     attenuation.set(
47           addExposedField(SFVEC3F, "attenuation", new SFVec3f(1.0f, 0.0f, 0.0f),
48                           new SFFloat(0.0f)));
49     color.set(
50           addExposedField(SFCOLOR, "color", new SFColor(1.0f, 1.0f, 1.0f)));
51     global.set(
52           addExposedField(SFBOOL, "global", new SFBool(false)));
53     setFieldFlags(global, FF_X3D_ONLY);
54     intensity.set(
55           addExposedField(SFFLOAT, "intensity", new SFFloat(1.0f),
56                           new SFFloat(0.0f), new SFFloat(1.0f)));
57     location.set(
58           addExposedField(SFVEC3F, "location", new SFVec3f(0.0f, 0.0f, 0.0f)));
59     on.set(
60           addExposedField(SFBOOL, "on", new SFBool(true)));
61     radius.set(
62           addExposedField(SFFLOAT, "radius", new SFFloat(100.0f),
63                           new SFFloat(0.0f)));
64     kambiLightCommonFields()
65     x3domLightCommonFields()
66 }
67 
68 Node *
create(Scene * scene)69 ProtoPointLight::create(Scene *scene)
70 {
71     return new NodePointLight(scene, this);
72 }
73 
NodePointLight(Scene * scene,Proto * def)74 NodePointLight::NodePointLight(Scene *scene, Proto *def)
75   : Node(scene, def)
76 {
77 }
78 
79 void
preDraw()80 NodePointLight::preDraw()
81 {
82 //    float radius = ((SFFloat *) getField(radius_Field()))->getValue();
83 
84     if (color() == NULL)
85         return;
86 
87     if (on()->getValue()) {
88         float ambientColor[4], diffuseColor[4];
89         float pos[4];
90         for (int i = 0; i < 3; i++) {
91             ambientColor[i] = color()->getValue()[i] *
92                               ambientIntensity()->getValue();
93             diffuseColor[i] = color()->getValue()[i] *
94                               intensity()->getValue();
95             pos[i] = location()->getValue()[i];
96         }
97         ambientColor[3] = diffuseColor[3] = 1.0f;
98         pos[3] = 1.0f;
99 
100         m_light = (GLenum) m_scene->allocateLight();
101 
102         glLightfv(m_light, GL_AMBIENT, ambientColor);
103         glLightfv(m_light, GL_DIFFUSE, diffuseColor);
104         glLightfv(m_light, GL_SPECULAR, diffuseColor);
105         glLightfv(m_light, GL_POSITION, pos);
106         glLightf(m_light, GL_SPOT_CUTOFF, 180.0f);
107         glLightf(m_light, GL_SPOT_EXPONENT, 0.0f);
108         glLightf(m_light, GL_CONSTANT_ATTENUATION,
109                  attenuation()->getValue()[0]);
110         glLightf(m_light, GL_LINEAR_ATTENUATION,
111                  attenuation()->getValue()[1]);
112         glLightf(m_light, GL_QUADRATIC_ATTENUATION,
113                  attenuation()->getValue()[2]);
114         glEnable(m_light);
115     }
116 }
117 
118 void
drawHandles()119 NodePointLight::drawHandles()
120 {
121     const float *flocation = location()->getValue();
122 
123     glPushMatrix();
124     glTranslatef(flocation[0], flocation[1], flocation[2]);
125 
126     glPushName(LOCATION);
127 
128     GLUquadricObj *obj = gluNewQuadric();
129 
130     Util::myGlMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color()->getValue());
131     gluSphere(obj, 0.05f, 8, 4);
132 
133     glPopName();
134 
135     glPopMatrix();
136 
137     gluDeleteQuadric(obj);
138 }
139 
140 Vec3f
getHandle(int handle,int * constraint,int * field)141 NodePointLight::getHandle(int handle, int *constraint, int *field)
142 {
143     *field = location_Field();
144     return location()->getValue();
145 }
146 
147 void
setHandle(int handle,const Vec3f & v)148 NodePointLight::setHandle(int handle, const Vec3f &v)
149 {
150     m_scene->setField(this, location_Field(), new SFVec3f(v));
151 }
152 
153 int
getAnimationCommentID(void)154 NodePointLight::getAnimationCommentID(void)
155 {
156     return IDS_ANIMATION_HELP_POINTLIGHT + swGetLang();
157 }
158 
getProfile(void) const159 int NodePointLight::getProfile(void) const
160 {
161     return PROFILE_INTERCHANGE;
162 }
163 
164 const char*
getComponentName(void) const165 NodePointLight::getComponentName(void) const
166 {
167     static const char* name = "Lighting";
168     return name;
169 }
170 
171 int
getComponentLevel(void) const172 NodePointLight::getComponentLevel(void) const
173 {
174     if (!isDefault(radius_Field()))
175         return 3;
176     return -1;
177 }
178 
179 
180 int
writeRib(int f,int indent)181 NodePointLight::writeRib(int f, int indent)
182 {
183     RET_ONERROR( mywritestr(f, "LightSource \"pointlight\" 1 ") )
184     const float *p = location()->getValue();
185     RET_ONERROR( mywritef(f, "\"from\" [%f %f %f] ", p[0], p[1], -p[2]) )
186     RET_ONERROR( mywritef(f, "\"intensity\" [%f] ", intensity()->getValue()) )
187     const float *c = color()->getValue();
188     RET_ONERROR( mywritef(f, "\"lightcolor\" [%f %f %f]\n", c[0], c[1], c[2]) )
189 
190     return 0;
191 }
192 
193