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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
import gl4java.*;
import gl4java.awt.*;
import gl4java.utils.textures.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class GLImageWorld1 extends GLCanvas
implements MouseListener,MouseMotionListener
{
float []mPosObjTrans;
float []mPosObjRot;
Point mousePoint;
Point oldMousePoint;
boolean mouseMoveFlag;
TextureGrabber textGrab4Snapshot = null;
String textGrab4SnapshotFName = null;
TextureLoader txtLoader = null;
int texName[] = {0};
public GLImageWorld1 (int w, int h)
{
super(w, h);
}
/**
* Creates a snapshot (save texture/image) of the current
* GL-Context !
*
* The snapshot itself is created delayed,
* so no return value is avaiable.
* Because this is a non critical path, I hope its enough !
*
* @param tg The TextureGrabber
* @param fname The filename
* @see TextureGrabber
*/
public void snapshot(TextureGrabber tg, String fname)
{
textGrab4Snapshot=tg;
textGrab4SnapshotFName=fname;
repaint();
}
public void setOriginalPerspective()
{
setOriginalPerspective(true);
}
private void setOriginalPerspective(boolean fetchGL)
{
for(int i=0;i<16;i++)
mPosObjTrans[i]=0f;
mPosObjTrans[0]=mPosObjTrans[5]=mPosObjTrans[10]=mPosObjTrans[15]=1f;
for(int i=0;i<16;i++)
mPosObjRot[i]=0f;
mPosObjRot[0]=mPosObjRot[5]=mPosObjRot[10]=mPosObjRot[15]=1f;
if(fetchGL)
{
if( glj.gljMakeCurrent() == false )
{
System.out.println("problem in use() method");
return;
}
}
TranlateObj(0f,0f,-10f);
SetCamera();
if(fetchGL)
{
glj.gljCheckGL();
glj.gljFree();
repaint();
}
}
public void preInit()
{
// createOwnWindow = true;
}
public void init()
{
gl.glEnable(GL_TEXTURE_2D);
gl.glGenTextures(1,texName);
gl.glBindTexture(GL_TEXTURE_2D,texName[0]);
//gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
//gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
//gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
//gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
gl.glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
// cameraMatrix init
mPosObjTrans=new float[16];
mPosObjRot=new float[16];
setOriginalPerspective(false);
gl.glShadeModel (GL_SMOOTH);
gl.glEnable(GL_DEPTH_TEST);
gl.glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
gl.glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glj.gljCheckGL();
addMouseListener(this);
addMouseMotionListener(this);
mouseMoveFlag=false;
reshape(getSize().width, getSize().height);
}
/**
* These variables are very important
* to respect the texture-size ratio !
*/
float texMaxPosX = 1f;
float texMaxPosY = 1f;
public boolean loadTexture(String name, String type)
{
boolean ok = true;
if( glj.gljMakeCurrent() == false )
{
System.out.println("problem in use() method");
return false;
}
// texture laden
if(type.equals("png"))
txtLoader = new PngTextureLoader(gl, glu);
else if(type.equals("ppm"))
txtLoader = new PPMAsciiTextureLoader(gl, glu);
else if(type.equals("tga"))
txtLoader = new TGATextureLoader(gl, glu);
else if(type.equals("any"))
txtLoader = new AWTTextureLoader(this, gl, glu);
else {
System.out.println("Imagetype: "+type+" is currently not supported !");
ok = false;
}
if(ok)
{
try {
txtLoader.readTexture(name);
} catch (Exception ex) {
ex.printStackTrace();
}
}
if(ok && txtLoader.isOk())
{
gl.glEnable(GL_TEXTURE_2D);
txtLoader.texImage2DScaled4BestSize();
texMaxPosX = 1f;
texMaxPosY = 1f;
float texAspect = txtLoader.getTextureWidth()/
txtLoader.getTextureHeight();
if(txtLoader.getTextureWidth()>txtLoader.getTextureHeight())
texMaxPosY = 1f/texAspect;
else
texMaxPosX = texAspect;
System.out.println("texture succesfully loaded !");
System.out.println("texture: "+txtLoader);
}
glj.gljCheckGL();
glj.gljFree();
if(ok)
repaint();
return ok;
}
public void display()
{
int i;
/* Standard GL4Java Init */
if( glj.gljMakeCurrent() == false )
{
System.out.println("problem in use() method");
return;
}
// just render it
gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SetCamera();
DrawScene();
if(textGrab4Snapshot!=null)
{
textGrab4Snapshot.grabPixels(GL_BACK,
0, 0, cvsGetWidth(), cvsGetHeight());
if(textGrab4SnapshotFName!=null)
textGrab4Snapshot.write2File(textGrab4SnapshotFName);
textGrab4Snapshot =null;
textGrab4SnapshotFName=null;
}
/* For your animation dutys ;-) */
glj.gljSwap();
glj.gljCheckGL();
glj.gljFree();
}
public void reshape(int w, int h)
{
gl.glMatrixMode (GL_MODELVIEW);
gl.glViewport (0, 0, w, h);
gl.glLoadIdentity();
SetCamera();
}
public void drawGrid(float x0, float y0,
float width, float height, float step)
{
float i,j;
/* draw grid */
gl.glBegin(GL_LINES);
for(i=x0;i<width;i+=step)
for(j=y0;j<height;j+=step)
{
if(i==0f && j==0f)
gl.glColor3f (1f,0f,0f);
else
gl.glColor3f (0.6f,0.5f,0.5f);
gl.glVertex2f(0f,j);
gl.glVertex2f(width,j);
gl.glVertex2f(i,height);
gl.glVertex2f(i,0f);
}
gl.glEnd();
}
void DrawScene()
{
gl.glMatrixMode (GL_MODELVIEW);
gl.glLoadIdentity ();
// kamera setzen
gl.glLoadMatrixf(mPosObjTrans);
gl.glMultMatrixf(mPosObjRot);
gl.glPushMatrix();
gl.glDisable(GL_TEXTURE_2D);
drawGrid(0f, 0f, 10f, 10f, 0.5f);
gl.glPopMatrix();
// obj zeichnen
gl.glPushMatrix();
gl.glScalef(2f,2f,2f);
gl.glColor3f(1f,0f,0f);
gl.glDisable(GL_TEXTURE_2D);
DrawObj();
gl.glPopMatrix();
if(texName[0]!=0)
gl.glEnable(GL_TEXTURE_2D);
gl.glEnable(GL_BLEND);
gl.glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
gl.glColor3f(1f,1f,1f);
gl.glPushMatrix();
gl.glScalef(3f,3f,3f);
gl.glTranslatef(-1f,0f,1f);
DrawObj();
gl.glPopMatrix();
gl.glPushMatrix();
gl.glScalef(3f,3f,3f);
gl.glTranslatef(1f,0f,1f);
DrawObj();
gl.glPopMatrix();
gl.glPushMatrix();
gl.glScalef(3f,3f,3f);
gl.glTranslatef(-2f,1f,1f);
Billboard();
DrawObj();
gl.glPopMatrix();
gl.glDisable(GL_BLEND);
}
void DrawObj()
{
gl.glPushMatrix();
gl.glBegin(GL_QUADS);
gl.glNormal3f(0f,0f,1f);
gl.glTexCoord2f(0f,0f);
gl.glVertex3f(0f,0f,0f);
gl.glNormal3f(0f,0f,1f);
gl.glTexCoord2f(0f,1f);
gl.glVertex3f(0f,texMaxPosY,0f);
gl.glNormal3f(0f,0f,1f);
gl.glTexCoord2f(1f,1f);
gl.glVertex3f(texMaxPosX,texMaxPosY,0f);
gl.glNormal3f(0f,0f,1f);
gl.glTexCoord2f(1f,0f);
gl.glVertex3f(texMaxPosX,0f,0f);
gl.glEnd();
gl.glPopMatrix();
}
float winaspect = 1f;
float imgaspect = 1f;
float imgwidth = 1f;
float imgheight = 1f;
void SetCamera()
{
Dimension dim=getSize();
float aspect=(float)dim.width/(float)dim.height;
gl.glMatrixMode (GL_PROJECTION);
gl.glLoadIdentity ();
glu.gluPerspective(60f,aspect,.01,100);
}
void RotateObj(float degree,
float axisX,
float axisY,
float axisZ)
{
gl.glMatrixMode (GL_MODELVIEW);
gl.glLoadIdentity ();
// kamera setzen
gl.glRotatef(degree,axisX,axisY,axisZ);
gl.glMultMatrixf(mPosObjRot);
gl.glGetFloatv(GL_MODELVIEW_MATRIX,mPosObjRot);
}
void TranlateObj(float x,float y,float z)
{
gl.glMatrixMode (GL_MODELVIEW);
gl.glLoadIdentity ();
// kamera setzen
gl.glLoadMatrixf(mPosObjTrans);
gl.glTranslatef(x,y,z);
gl.glGetFloatv(GL_MODELVIEW_MATRIX,mPosObjTrans);
}
// entfernt rotationen aus aktueller matrix
double Billboard()
{
float[] mat=new float[16];
gl.glGetFloatv(GL_MODELVIEW_MATRIX,mat);
mat[0] = mat[5] = mat[10] = 1;
mat[1] = mat[2] = mat[4] = mat[6] = mat[8] = mat[9] = 0;
gl.glLoadMatrixf(mat);
return mat[14];
}
// Methods required for the implementation of MouseListener
public void mouseEntered( MouseEvent evt )
{
}
public void mouseExited( MouseEvent evt )
{
}
public void mousePressed( MouseEvent evt )
{
if(mouseMoveFlag==false)
{ // start drag
mouseMoveFlag=true;
mousePoint=evt.getPoint();
}
}
public void mouseReleased( MouseEvent evt )
{
mouseMoveFlag=false;
}
public void mouseClicked( MouseEvent evt )
{
Component comp = evt.getComponent();
}
public void mouseDragged(MouseEvent e)
{
if(mouseMoveFlag==true)
{
oldMousePoint=new Point(mousePoint);
mousePoint=e.getPoint();
Point dif=new Point(mousePoint.x-oldMousePoint.x,
mousePoint.y-oldMousePoint.y);
if( glj.gljMakeCurrent() == false )
{
System.out.println("problem in use() method");
return;
}
if(e.isShiftDown()==true)
TranlateObj((float)dif.x/6.0f,(float)dif.y/-6.0f,0f);
else if(e.isAltDown()==true)
{
TranlateObj(0f,0f,(float)dif.y/6.0f);
RotateObj(dif.x,0f,0f,1f);
}
else
{
RotateObj(dif.x,0f,1f,0f);
RotateObj(dif.y,1f,0f,0f);
}
glj.gljFree();
repaint();
}
}
public void mouseMoved(MouseEvent e)
{
}
}
|