-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCgDrawer.java
More file actions
executable file
·114 lines (84 loc) · 2.87 KB
/
Copy pathCgDrawer.java
File metadata and controls
executable file
·114 lines (84 loc) · 2.87 KB
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
/**
* CgDrawer
*/
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.glu.gl2.GLUgl2;
import com.jogamp.opengl.util.gl2.GLUT;
import javax.media.opengl.GL2;
import javax.media.opengl.GL;
import javax.media.opengl.GLEventListener;
public class CgDrawer implements GLEventListener {
GLAutoDrawable glAD;
static float light0pos[] = { 0.0f, 30.0f, 30.0f, 1.0f };
static float light1pos[] = { 0.0f, -30.0f, 0.0f, 1.0f };
public void init(GLAutoDrawable drawable) {
float silver[] = {0.5f, 0.5f, 0.5f, 1.0f};
this.glAD = drawable;
GL2 gl= drawable.getGL().getGL2();
// enable Z-buffering
gl.glEnable(GL.GL_RGBA);
gl.glEnable(GL2.GL_DEPTH);
gl.glEnable(GL2.GL_DOUBLE);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glEnable(GL2.GL_NORMALIZE);
gl.glEnable(GL.GL_CULL_FACE);
gl.glCullFace(GL.GL_BACK);
// enable two lights
gl.glEnable(GL2.GL_LIGHTING);
gl.glEnable(GL2.GL_LIGHT0);
gl.glEnable(GL2.GL_LIGHT1);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, silver, 0);
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_DIFFUSE, silver, 0);
// set background color
gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}
// called when window sizes are changed
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL2 gl = drawable.getGL().getGL2();
GLUgl2 glu = new GLUgl2();
if (height <= 0)
height = 1;
float h = (float) width / (float) height;
gl.glViewport(0, 0, width, height);
// projection transform
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(30.0, h, 1.0, 100.0);
// geometric transform
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
}
// !!! kept to be called while animator is running !!!
public void display(GLAutoDrawable drawable) {
draw(drawable);
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}
public void draw(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
GLUgl2 glu = new GLUgl2();
// clear window
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
// initialize for geometric transform
gl.glLoadIdentity();
// set bg color
float color[] = MyScene.getBGcolor();
gl.glClearColor(color[0], color[1], color[2], color[3]);
// set view point
float pos[] = MyScene.getCameraPos();
float angle[] = MyScene.getCameraAngle();
glu.gluLookAt(pos[0], pos[1], pos[2],
angle[0], angle[1], angle[2],
0.0, 0.0, 1.0);
// set light position
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light0pos, 0);
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, light1pos, 0);
MyScene.draw(drawable);
}
public GLAutoDrawable getGLAutoDrawable() {
return glAD;
}
@Override
public void dispose(GLAutoDrawable arg0) {
// TODO Auto-generated method stub
}
}