diff options
Diffstat (limited to 'src/redbook/src/glredbook10/cone.java')
-rw-r--r-- | src/redbook/src/glredbook10/cone.java | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/src/redbook/src/glredbook10/cone.java b/src/redbook/src/glredbook10/cone.java new file mode 100644 index 0000000..bc7a003 --- /dev/null +++ b/src/redbook/src/glredbook10/cone.java @@ -0,0 +1,139 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program demonstrates the use of the GL lighting model. A sphere is drawn
+ * using a grey material characteristic. A single light source illuminates the
+ * object.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class cone//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+
+ private GLUT glut;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ cone demo = new cone();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("cone");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }//
+
+ /*
+ * Initialize material property, light source, and lighting model.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+
+ glut = new GLUT();
+ //
+ float mat_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
+ float mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
+ /* mat_specular and mat_shininess are NOT default values */
+ float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float mat_shininess[] = { 50.0f };
+
+ float light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
+ float light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ /* light_position is NOT default value */
+ float light_position[] = { 1.0f, 1.0f, 1.0f, 0.0f };
+
+ float lmodel_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
+
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_ambient, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, mat_shininess, 0);
+
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_ambient, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, light_diffuse, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_specular, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0);
+
+ gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient, 0);
+
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glTranslatef(0.0f, -1.0f, 0.0f);
+ gl.glRotatef(250.0f, 1.0f, 0.0f, 0.0f);
+ glut.glutSolidCone(1.0, 2.0, 20, 20);
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(-1.5, 1.5, -1.5 * (float) h / (float) w, 1.5 * (float) h
+ / (float) w, -10.0, 10.0);
+ else
+ gl.glOrtho(-1.5 * (float) w / (float) h, 1.5 * (float) w
+ / (float) h, -1.5, 1.5, -10.0, 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
|