1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/**
* @(#) [place applet name here].java
* @(#) author: [place author here] (converted to Java by Ron Cemer)
*/
/* Copyright (c) Mark J. Kilgard, 1994. */
/**
* (c) Copyright 1993, 1994, Silicon Graphics, Inc.
* ALL RIGHTS RESERVED
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the above
* copyright notice appear in all copies and that both the copyright notice
* and this permission notice appear in supporting documentation, and that
* the name of Silicon Graphics, Inc. not be used in advertising
* or publicity pertaining to distribution of the software without specific,
* written prior permission.
*
* THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
* AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
* GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
* SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
* KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
* LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
* THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
* POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
*
* US Government Users Restricted Rights
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
* (c)(1)(ii) of the Rights in Technical Data and Computer Software
* clause at DFARS 252.227-7013 and/or in similar or successor
* clauses in the FAR or the DOD or NASA FAR Supplement.
* Unpublished-- rights reserved under the copyright laws of the
* United States. Contractor/manufacturer is Silicon Graphics,
* Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
*
* OpenGL(TM) is a trademark of Silicon Graphics, Inc.
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;
import java.io.*;
import java.util.*;
import gl4java.GLContext;
import gl4java.awt.GLAnimCanvas;
import gl4java.applet.SimpleGLAnimApplet1;
public class spots extends SimpleGLAnimApplet1
{
/* Initialize the applet */
public void init()
{
super.init();
Dimension d = getSize();
canvas = new spotsCanvas(d.width, d.height);
add("Center", canvas);
}
private class Light
{
public float amb[] = new float[4];
public float diff[] = new float[4];
public float spec[] = new float[4];
public float pos[] = new float[4];
public float spotDir[] = new float[3];
public float spotExp;
public float spotCutoff;
public float atten[] = new float[3];
public float trans[] = new float[3];
public float rot[] = new float[3];
public float swing[] = new float[3];
public float arc[] = new float[3];
public float arcIncr[] = new float[3];
public Light
(float ambr, float ambg, float ambb, float amba,
float diffr, float diffg, float diffb, float diffa,
float specr, float specg, float specb, float speca,
float posx, float posy, float posz, float posw,
float dirx, float diry, float dirz,
float exp,
float cutoff,
float attenr, float atteng, float attenb,
float transx, float transy, float transz,
float rotx, float roty, float rotz,
float swingx, float swingy, float swingz,
float arcx, float arcy, float arcz,
float arcincrx, float arcincry, float arcincrz)
{
amb[0] = ambr;
amb[1] = ambg;
amb[2] = ambb;
amb[3] = amba;
diff[0] = diffr;
diff[1] = diffg;
diff[2] = diffb;
diff[3] = diffa;
spec[0] = specr;
spec[1] = specg;
spec[2] = specb;
spec[3] = speca;
pos[0] = posx;
pos[1] = posy;
pos[2] = posz;
pos[3] = posw;
spotDir[0] = dirx;
spotDir[1] = diry;
spotDir[2] = dirz;
spotExp = exp;
spotCutoff = cutoff;
atten[0] = attenr;
atten[1] = atteng;
atten[2] = attenb;
trans[0] = transx;
trans[1] = transy;
trans[2] = transz;
rot[0] = rotx;
rot[1] = roty;
rot[2] = rotz;
swing[0] = swingx;
swing[1] = swingy;
swing[2] = swingz;
arc[0] = arcx;
arc[1] = arcy;
arc[2] = arcz;
arcIncr[0] = arcincrx;
arcIncr[1] = arcincry;
arcIncr[2] = arcincrz;
}
}
/* Local GLAnimCanvas extension class */
private class spotsCanvas extends GLAnimCanvas
{
private final double M_PI = 3.14159265;
private final double TWO_PI = (2.0*M_PI);
private final int NUM_LIGHTS = 3;
private boolean useSAME_AMB_SPEC = true;
private float modelAmb[] = {0.2f, 0.2f, 0.2f, 1.0f};
private float matAmb[] = {0.2f, 0.2f, 0.2f, 1.0f};
private float matDiff[] = {0.8f, 0.8f, 0.8f, 1.0f};
private float matSpec[] = {0.4f, 0.4f, 0.4f, 1.0f};
private float matEmission[] = {0.0f, 0.0f, 0.0f, 1.0f};
private Light[] spots =
{
new Light
(0.2f, 0.0f, 0.0f, 1.0f, /* ambient */
0.8f, 0.0f, 0.0f, 1.0f, /* diffuse */
0.4f, 0.0f, 0.0f, 1.0f, /* specular */
0.0f, 0.0f, 0.0f, 1.0f, /* position */
0.0f, -1.0f, 0.0f, /* direction */
20.0f,
60.0f, /* exponent, cutoff */
1.0f, 0.0f, 0.0f, /* attenuation */
0.0f, 1.25f, 0.0f, /* translation */
0.0f, 0.0f, 0.0f, /* rotation */
20.0f, 0.0f, 40.0f, /* swing */
0.0f, 0.0f, 0.0f, /* arc */
(float)(TWO_PI / 70.0), 0.0f, (float)(TWO_PI / 140.0)), /* arc increment */
new Light
(0.0f, 0.2f, 0.0f, 1.0f, /* ambient */
0.0f, 0.8f, 0.0f, 1.0f, /* diffuse */
0.0f, 0.4f, 0.0f, 1.0f, /* specular */
0.0f, 0.0f, 0.0f, 1.0f, /* position */
0.0f, -1.0f, 0.0f, /* direction */
20.0f,
60.0f, /* exponent, cutoff */
1.0f, 0.0f, 0.0f, /* attenuation */
0.0f, 1.25f, 0.0f, /* translation */
0.0f, 0.0f, 0.0f, /* rotation */
20.0f, 0.0f, 40.0f, /* swing */
0.0f, 0.0f, 0.0f, /* arc */
(float)(TWO_PI / 120.0), 0.0f, (float)(TWO_PI / 60.0)), /* arc increment */
new Light
(0.0f, 0.0f, 0.2f, 1.0f, /* ambient */
0.0f, 0.0f, 0.8f, 1.0f, /* diffuse */
0.0f, 0.0f, 0.4f, 1.0f, /* specular */
0.0f, 0.0f, 0.0f, 1.0f, /* position */
0.0f, -1.0f, 0.0f, /* direction */
20.0f,
60.0f, /* exponent, cutoff */
1.0f, 0.0f, 0.0f, /* attenuation */
0.0f, 1.25f, 0.0f, /* translation */
0.0f, 0.0f, 0.0f, /* rotation */
20.0f, 0.0f, 40.0f, /* swing */
0.0f, 0.0f, 0.0f, /* arc */
(float)(TWO_PI / 50.0), 0.0f, (float)(TWO_PI / 100.0)), /* arc increment */
};
float spin = 0.0f;
public spotsCanvas(int w, int h)
{
super(w, h);
GLContext.gljNativeDebug = false;
GLContext.gljClassDebug = false;
setAnimateFps(30.0);
}
public void preInit()
{
doubleBuffer = true;
stereoView = false;
}
public void init()
{
System.out.println("init(): " + this);
reshape(getSize().width, getSize().height);
gl.glMatrixMode(GL_PROJECTION);
gl.glFrustum(-1.0f, 1.0f, -1.0f, 1.0f, 2.0f, 6.0f);
gl.glMatrixMode(GL_MODELVIEW);
gl.glTranslatef(0.0f, 0.0f, -3.0f);
gl.glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
gl.glEnable(GL_LIGHTING);
gl.glEnable(GL_NORMALIZE);
gl.glLightModelfv(GL_LIGHT_MODEL_AMBIENT, modelAmb);
gl.glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 1.0f);
gl.glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 0.0f);
gl.glMaterialfv(GL_FRONT, GL_AMBIENT, matAmb);
gl.glMaterialfv(GL_FRONT, GL_DIFFUSE, matDiff);
gl.glMaterialfv(GL_FRONT, GL_SPECULAR, matSpec);
gl.glMaterialfv(GL_FRONT, GL_EMISSION, matEmission);
gl.glMaterialf(GL_FRONT, GL_SHININESS, 10.0f);
initLights();
glj.gljCheckGL();
}
public void reshape(int width, int height)
{
gl.glViewport(0,0,width,height);
}
public void display()
{
if (glj.gljMakeCurrent() == false) return;
spin += 0.5f;
if (spin > 360.0f) spin -= 360.0f;
gl.glClear(GL_COLOR_BUFFER_BIT);
gl.glPushMatrix();
gl.glRotatef(spin, 0.0f, 1.0f, 0.0f);
aimLights();
setLights();
gl.glPushMatrix();
gl.glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
gl.glScalef(1.9f, 1.9f, 1.0f);
gl.glTranslatef(-0.5f, -0.5f, 0.0f);
drawPlane(16, 16);
gl.glPopMatrix();
drawLights();
gl.glPopMatrix();
glj.gljSwap();
glj.gljCheckGL();
glj.gljFree();
//if (!isSuspended()) repaint(); // Animate at full speed.
}
private void initLights()
{
for (int k = 0; k < NUM_LIGHTS; ++k)
{
int lt = GL_LIGHT0 + k;
Light light = spots[k];
gl.glEnable(lt);
gl.glLightfv(lt, GL_AMBIENT, light.amb);
gl.glLightfv(lt, GL_DIFFUSE, light.diff);
if (useSAME_AMB_SPEC)
gl.glLightfv(lt, GL_SPECULAR, light.amb);
else
gl.glLightfv(lt, GL_SPECULAR, light.spec);
gl.glLightf(lt, GL_SPOT_EXPONENT, light.spotExp);
gl.glLightf(lt, GL_SPOT_CUTOFF, light.spotCutoff);
gl.glLightf(lt, GL_CONSTANT_ATTENUATION, light.atten[0]);
gl.glLightf(lt, GL_LINEAR_ATTENUATION, light.atten[1]);
gl.glLightf(lt, GL_QUADRATIC_ATTENUATION, light.atten[2]);
}
}
private void aimLights()
{
for (int k = 0; k < NUM_LIGHTS; ++k)
{
Light light = spots[k];
light.rot[0] = light.swing[0] * (float)Math.sin(light.arc[0]);
light.arc[0] += light.arcIncr[0];
if (light.arc[0] > TWO_PI)
light.arc[0] -= TWO_PI;
light.rot[1] = light.swing[1] * (float)Math.sin(light.arc[1]);
light.arc[1] += light.arcIncr[1];
if (light.arc[1] > TWO_PI)
light.arc[1] -= TWO_PI;
light.rot[2] = light.swing[2] * (float)Math.sin(light.arc[2]);
light.arc[2] += light.arcIncr[2];
if (light.arc[2] > TWO_PI)
light.arc[2] -= TWO_PI;
}
}
private void setLights()
{
for (int k = 0; k < NUM_LIGHTS; ++k)
{
int lt = GL_LIGHT0 + k;
Light light = spots[k];
gl.glPushMatrix();
gl.glTranslatef(light.trans[0], light.trans[1], light.trans[2]);
gl.glRotatef(light.rot[0], 1, 0, 0);
gl.glRotatef(light.rot[1], 0, 1, 0);
gl.glRotatef(light.rot[2], 0, 0, 1);
gl.glLightfv(lt, GL_POSITION, light.pos);
gl.glLightfv(lt, GL_SPOT_DIRECTION, light.spotDir);
gl.glPopMatrix();
}
}
private void drawLights()
{
gl.glDisable(GL_LIGHTING);
for (int k = 0; k < NUM_LIGHTS; ++k)
{
Light light = spots[k];
gl.glColor4fv(light.diff);
gl.glPushMatrix();
gl.glTranslatef(light.trans[0], light.trans[1], light.trans[2]);
gl.glRotatef(light.rot[0], 1, 0, 0);
gl.glRotatef(light.rot[1], 0, 1, 0);
gl.glRotatef(light.rot[2], 0, 0, 1);
gl.glBegin(GL_LINES);
gl.glVertex3f(light.pos[0], light.pos[1], light.pos[2]);
gl.glVertex3f(light.spotDir[0], light.spotDir[1], light.spotDir[2]);
gl.glEnd();
gl.glPopMatrix();
}
gl.glEnable(GL_LIGHTING);
}
private void drawPlane(int w, int h)
{
int i, j;
float dw = 1.0f / (float)w;
float dh = 1.0f / (float)h;
gl.glNormal3f(0.0f, 0.0f, 1.0f);
for (j = 0; j < h; ++j)
{
gl.glBegin(GL_TRIANGLE_STRIP);
for (i = 0; i <= w; ++i)
{
gl.glVertex2f(dw * i, dh * (j + 1));
gl.glVertex2f(dw * i, dh * j);
}
gl.glEnd();
}
}
}
}
|