Jogamp
Bug 682 - Relocating javax.media.opengl.* -> com.jogamp.opengl.* (Part 1)
[jogl-demos.git] / src / redbook / src / glredbook10 / antipoly.java
CommitLineData
85bd6a6e
MB
1package glredbook10;\r
2\r
0d46fa30 3import com.jogamp.opengl.util.gl2.GLUT;\r
85bd6a6e
MB
4import java.awt.event.*;\r
5\r
6import javax.swing.*;\r
7\r
b32c5de0
SG
8import com.jogamp.opengl.*;\r
9import com.jogamp.opengl.awt.GLJPanel;\r
10import com.jogamp.opengl.glu.GLU;\r
85bd6a6e
MB
11\r
12/**\r
13 * This program draws filled polygons with antialiased edges. The special\r
14 * GL_SRC_ALPHA_SATURATE blending function is used. Pressing the left mouse\r
15 * button turns the antialiasing on and off.\r
16 * \r
17 * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes\r
18 */\r
19public class antipoly //\r
20 extends GLSkeleton<GLJPanel>\r
21 implements GLEventListener, KeyListener, MouseListener {\r
22 private GLU glu;\r
23 private GLUT glut;\r
24 private boolean polySmooth;\r
25\r
26\r
27 @Override\r
28 protected GLJPanel createDrawable() {\r
29 GLCapabilities caps = new GLCapabilities(null);\r
30 //\r
31 GLJPanel panel = new GLJPanel(caps);\r
32 panel.addGLEventListener(this);\r
33 panel.addKeyListener(this);\r
34 panel.addMouseListener(this);\r
35 return panel;\r
36 }\r
37\r
38 public static void main(String[] args) {\r
39\r
40 antipoly demo = new antipoly();\r
41\r
42 JFrame.setDefaultLookAndFeelDecorated(true);\r
43 JFrame frame = new JFrame("antipoly");\r
44 frame.setSize(200, 200);\r
45 frame.setLocationRelativeTo(null);\r
46 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\r
47\r
48 frame.getContentPane().add(demo.drawable);\r
49 frame.setVisible(true);\r
50 demo.drawable.requestFocusInWindow();\r
51 }\r
52\r
53 public void init(GLAutoDrawable drawable) {\r
54 GL2 gl = drawable.getGL().getGL2();\r
55 glu = new GLU();\r
56 glut = new GLUT();\r
57 //\r
58 float mat_ambient[] = { 0.0f, 0.0f, 0.0f, 1.00f };\r
59 float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.00f };\r
60 float mat_shininess[] = { 15.0f };\r
61\r
62 gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_ambient, 0);\r
63 gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);\r
64 gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, mat_shininess, 0);\r
65\r
66 gl.glEnable(GL2.GL_LIGHTING);\r
67 gl.glEnable(GL2.GL_LIGHT0);\r
68 gl.glEnable(GL.GL_BLEND);\r
69 gl.glCullFace(GL.GL_BACK);\r
70 gl.glEnable(GL.GL_CULL_FACE);\r
71 gl.glEnable(GL2.GL_POLYGON_SMOOTH);\r
72 polySmooth = true;\r
73\r
74 gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);\r
75 }\r
76\r
77 /*\r
78 * Note: polygons must be drawn from back to front for proper blending.\r
79 */\r
80 public void display(GLAutoDrawable drawable) {\r
81 GL2 gl = drawable.getGL().getGL2();\r
82 //\r
83 float position[] = { 0.0f, 0.0f, 1.0f, 0.0f };\r
84 float mat_cube1[] = { 0.75f, 0.75f, 0.0f, 1.0f };\r
85 float mat_cube2[] = { 0.0f, 0.75f, 0.75f, 1.0f };\r
86\r
87 if (polySmooth)\r
88 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);\r
89 else\r
90 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);\r
91\r
92 toggleSmooth(gl);\r
93\r
94 gl.glPushMatrix();\r
95\r
96 gl.glTranslatef(0.0f, 0.0f, -8.0f);\r
97 gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, position, 0);\r
98\r
99 gl.glBlendFunc(GL2.GL_SRC_ALPHA_SATURATE, GL.GL_ONE);\r
100\r
101 gl.glPushMatrix();\r
102 gl.glRotatef(30.0f, 1.0f, 0.0f, 0.0f);\r
103 gl.glRotatef(60.0f, 0.0f, 1.0f, 0.0f);\r
104 gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_cube1, 0);\r
105 glut.glutSolidCube(1.0f);\r
106 gl.glPopMatrix();\r
107\r
108 gl.glTranslatef(0.0f, 0.0f, -2.0f);\r
109 gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_cube2, 0);\r
110 gl.glRotatef(30.0f, 0.0f, 1.0f, 0.0f);\r
111 gl.glRotatef(60.0f, 1.0f, 0.0f, 0.0f);\r
112 glut.glutSolidCube(1.0f);\r
113 gl.glPopMatrix();\r
114\r
115 gl.glFlush();\r
116 }\r
117\r
118 public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {\r
119 GL2 gl = drawable.getGL().getGL2();\r
120 //\r
121 gl.glViewport(0, 0, w, h);\r
122 gl.glMatrixMode(GL2.GL_PROJECTION);\r
123 gl.glLoadIdentity();\r
124 glu.gluPerspective(30.0, (float) w / (float) h, 1.0, 20.0);\r
125 gl.glMatrixMode(GL2.GL_MODELVIEW);\r
126 gl.glLoadIdentity();\r
127 }\r
128\r
129 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,\r
130 boolean deviceChanged) {\r
131 }\r
132\r
133 private void toggleSmooth(GL2 gl) {\r
134 if (polySmooth) {\r
135 gl.glDisable(GL.GL_BLEND);\r
136 gl.glDisable(GL2.GL_POLYGON_SMOOTH);\r
137 gl.glEnable(GL.GL_DEPTH_TEST);\r
138 } else {\r
139 gl.glEnable(GL.GL_BLEND);\r
140 gl.glEnable(GL2.GL_POLYGON_SMOOTH);\r
141 gl.glDisable(GL.GL_DEPTH_TEST);\r
142 }\r
143 }\r
144\r
145 public void keyTyped(KeyEvent key) {\r
146 }\r
147\r
148 public void keyPressed(KeyEvent key) {\r
149 switch (key.getKeyCode()) {\r
150 case KeyEvent.VK_ESCAPE:\r
151 System.exit(0);\r
152 break;\r
153\r
154 default:\r
155 break;\r
156 }\r
157 }\r
158\r
159 public void keyReleased(KeyEvent key) {\r
160 }\r
161\r
162 public void mouseClicked(MouseEvent mouse) {\r
163 }\r
164\r
165 public void mousePressed(MouseEvent mouse) {\r
166 if (mouse.getButton() == MouseEvent.BUTTON1) {\r
167 polySmooth = !polySmooth;\r
168 System.out.println(polySmooth);\r
169 }\r
170\r
171 super.refresh();\r
172 }\r
173\r
174 public void mouseReleased(MouseEvent mouse) {\r
175 }\r
176\r
177 public void mouseEntered(MouseEvent mouse) {\r
178 }\r
179\r
180 public void mouseExited(MouseEvent mouse) {\r
181 }\r
182\r
183 public void dispose(GLAutoDrawable arg0) {\r
184 \r
185 }\r
186\r
187}\r
http://JogAmp.org git info: FAQ, tutorial and man pages.