diff options
Diffstat (limited to 'src/redbook/src/glredbook11')
-rw-r--r-- | src/redbook/src/glredbook11/README.txt | 73 | ||||
-rw-r--r-- | src/redbook/src/glredbook11/aargb.java | 135 | ||||
-rw-r--r-- | src/redbook/src/glredbook11/hello.java | 121 | ||||
-rw-r--r-- | src/redbook/src/glredbook11/image.java | 206 | ||||
-rw-r--r-- | src/redbook/src/glredbook11/polyoff.java | 223 | ||||
-rw-r--r-- | src/redbook/src/glredbook11/tess.java | 263 | ||||
-rw-r--r-- | src/redbook/src/glredbook11/tesswind.java | 375 | ||||
-rw-r--r-- | src/redbook/src/glredbook11/texbind.java | 192 | ||||
-rw-r--r-- | src/redbook/src/glredbook11/texgen.java | 202 | ||||
-rw-r--r-- | src/redbook/src/glredbook11/texprox.java | 113 | ||||
-rw-r--r-- | src/redbook/src/glredbook11/texsub.java | 219 | ||||
-rw-r--r-- | src/redbook/src/glredbook11/torus.java | 150 | ||||
-rw-r--r-- | src/redbook/src/glredbook11/unproject.java | 165 | ||||
-rw-r--r-- | src/redbook/src/glredbook11/varray.java | 228 | ||||
-rw-r--r-- | src/redbook/src/glredbook11/wrap.java | 210 |
15 files changed, 2875 insertions, 0 deletions
diff --git a/src/redbook/src/glredbook11/README.txt b/src/redbook/src/glredbook11/README.txt new file mode 100644 index 0000000..8225c0b --- /dev/null +++ b/src/redbook/src/glredbook11/README.txt @@ -0,0 +1,73 @@ +This README is for the glredbook11 package.
+
+These are port of the examples programs which are featured
+in the OpenGL Programming Guide, 2nd Edition corresponding
+to OpenGL version 1.1.
+
+- All examples requires JDK 1.4.2 and javax.media.opengl (jogl)
+ and a system dependent jogl-natives-*.jar to compile.
+- No ant build file is yet available.
+
+New in the second edition demostrating new features of OpenGL 1.1:
+
+1. polyoff.c --> polyoff.java
+2. texbind.c --> texbind.java
+3. texprox.c --> texprox.java
+4. texsub.c --> texsub.java
+5. varray.c --> varray.java
+
+These may not be able to run in OpenGL 1.1, unless modified
+to support extensions.
+
+OpenGL 1.0 to 1.1 compatibility issues:
+There are nine programs that are modify for 1.1 features
+but they will also run in 1.0.
+
+Four programs--checker.c, mipmap.c, texgen.c, and wrap.c--using
+texture objects have been modified so that they will avoid
+the use of texture objects on OpenGL 1.0 machines.
+
+Best regards,
+
+Kiet Le (Java port 200604)
+ak.kiet.le '@' gmail.com
+http://ak.kiet.le.googlepages.com/theredbookinjava.html
+
+
+/* ALL PROGRAMS IN PACKAGE HAS THE FOLLOWING LICENSE.
+ *
+ * Copyright (c) 1993-1997, 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(R) is a registered trademark of Silicon Graphics, Inc.
+ */
diff --git a/src/redbook/src/glredbook11/aargb.java b/src/redbook/src/glredbook11/aargb.java new file mode 100644 index 0000000..71ae3b8 --- /dev/null +++ b/src/redbook/src/glredbook11/aargb.java @@ -0,0 +1,135 @@ +package glredbook11;
+
+import glredbook10.GLSkeleton;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import javax.media.opengl.*;
+ import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+import javax.swing.JFrame;
+
+/**
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+
+public class aargb//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private float rotAngle = 0f;
+ private boolean rotate = false;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setNumSamples(2);
+ caps.setSampleBuffers(true);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ aargb demo = new aargb();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("aargb");
+ frame.setSize(512, 512);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ float values[] = new float[2];
+ gl.glGetFloatv(GL2.GL_LINE_WIDTH_GRANULARITY, values, 0);
+ System.out
+ .println("GL.GL_LINE_WIDTH_GRANULARITY value is " + values[0]);
+ gl.glGetFloatv(GL2.GL_LINE_WIDTH_RANGE, values, 0);
+ System.out.println("GL.GL_LINE_WIDTH_RANGE values are " + values[0]
+ + ", " + values[1]);
+ gl.glEnable(GL2.GL_LINE_SMOOTH);
+ gl.glEnable(GL.GL_BLEND);
+ gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
+ gl.glHint(GL2.GL_LINE_SMOOTH_HINT, GL.GL_DONT_CARE);
+ gl.glLineWidth(1.5f);
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(0.0f, 1.0f, 0.0f);
+ gl.glPushMatrix();
+ gl.glRotatef(-rotAngle, 0.0f, 0.0f, 0.1f);
+ gl.glBegin(GL.GL_LINES);
+ gl.glVertex2f(-0.5f, 0.5f);
+ gl.glVertex2f(0.5f, -0.5f);
+ gl.glEnd();
+ gl.glPopMatrix();
+ gl.glColor3f(0.0f, 0.0f, 1.0f);
+ gl.glPushMatrix();
+ gl.glRotatef(rotAngle, 0.0f, 0.0f, 0.1f);
+ gl.glBegin(GL.GL_LINES);
+ gl.glVertex2f(0.5f, 0.5f);
+ gl.glVertex2f(-0.5f, -0.5f);
+ gl.glEnd();
+ gl.glPopMatrix();
+ gl.glFlush();
+ if (rotate)
+ rotAngle += 1f;
+ if (rotAngle >= 360f)
+ rotAngle = 0f;
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ GLU glu = new GLU();
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h) //
+ glu.gluOrtho2D(-1.0, 1.0, -1.0 * (float) h / (float) w, //
+ 1.0 * (float) h / (float) w);
+ else
+ glu.gluOrtho2D(-1.0 * (float) w / (float) h, //
+ 1.0 * (float) w / (float) h, -1.0, 1.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);
+ case KeyEvent.VK_R:
+ rotate = !rotate;
+ super.refresh();
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook11/hello.java b/src/redbook/src/glredbook11/hello.java new file mode 100644 index 0000000..f7ddfb1 --- /dev/null +++ b/src/redbook/src/glredbook11/hello.java @@ -0,0 +1,121 @@ +package glredbook11;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+
+import javax.media.opengl.*;import javax.swing.JFrame;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+
+
+/**
+ * This is a simple, introductory OpenGL program in Java using the
+ * javax.media.opengl extension library.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class hello //
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+
+ @Override
+ protected GLJPanel createDrawable() {
+ // 0. optionally set additional canvas capabilities
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setSampleBuffers(true);// enable sample buffers for aliasing
+ caps.setNumSamples(2);
+ //
+ // 1. create canvas with the desired optional capabilities above
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ hello demo = new hello();
+
+ /*
+ * 2. Declare initial window size, position, and set frame's close
+ * behavior. Open window with "hello" in its title bar. Call
+ * initialization routines. Register callback function to display
+ * graphics. Enter main loop and process events.
+ */
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("hello");
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ // 3. add canvas to frame, set it visible, and have input focus on the
+ // canvas
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ /* select clearing color (background) color */
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ /* initialize viewing values */
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ gl.glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ /* clear all pixels */
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ /*
+ * draw white polygon (rectangle) with corners at (0.25, 0.25, 0.0) and
+ * (0.75, 0.75, 0.0)
+ */
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glBegin(GL2.GL_POLYGON);
+ gl.glVertex3f(0.25f, 0.25f, 0.0f);
+ gl.glVertex3f(0.75f, 0.25f, 0.0f);
+ gl.glVertex3f(0.75f, 0.75f, 0.0f);
+ gl.glVertex3f(0.25f, 0.75f, 0.0f);
+ gl.glEnd();
+
+ /*
+ * don't wait! start processing buffered OpenGL routines
+ */
+ gl.glFlush();
+
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int width,
+ int height) {
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void keyPressed(KeyEvent e) {
+ if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
+ System.exit(0);
+
+ }
+
+ public void keyReleased(KeyEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook11/image.java b/src/redbook/src/glredbook11/image.java new file mode 100644 index 0000000..857c1b1 --- /dev/null +++ b/src/redbook/src/glredbook11/image.java @@ -0,0 +1,206 @@ +package glredbook11;
+
+/**
+ * This program demonstrates drawing pixels and shows the effect of
+ * glDrawPixels(), glCopyPixels(), and glPixelZoom(). Interaction: moving the
+ * mouse while pressing the mouse button will copy the image in the lower-left
+ * corner of the window to the mouse position, using the current pixel zoom
+ * factors. There is no attempt to prevent you from drawing over the original
+ * image. If you press the 'r' key, the original image and zoom factors are
+ * reset. If you press the 'z' or 'Z' keys, you change the zoom factors.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+
+import java.awt.Point;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseMotionListener;
+import java.nio.ByteBuffer;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.BufferUtil;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+
+/**
+ * This program demonstrates drawing pixels and shows the effect of
+ * glDrawPixels(), glCopyPixels(), and glPixelZoom(). Interaction: moving the
+ * mouse while pressing the mouse button will copy the image in the lower-left
+ * corner of the window to the mouse position, using the current pixel zoom
+ * factors. There is no attempt to prevent you from drawing over the original
+ * image. If you press the 'r' key, the original image and zoom factors are
+ * reset. If you press the 'z' or 'Z' keys, you change the zoom factors.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class image //
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener, MouseMotionListener {
+ private GLU glu;
+ //
+ private static final int checkImageWidth = 64;
+ private static final int checkImageHeight = 64;
+ private static final int rgb = 3;
+ // private byte checkImage[][][];
+ private ByteBuffer checkImageBuf = //
+ BufferUtil.newByteBuffer(checkImageHeight * checkImageWidth * rgb);
+
+ private static float zoomFactor = 1.0f;
+ private static int height;
+ private Point mousePoint;// = new Point();
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ panel.addMouseMotionListener(this);
+ return panel;
+ }
+ public static void main(String[] args) {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setSampleBuffers(true);// enable sample buffers for aliasing
+ caps.setNumSamples(caps.getNumSamples() * 2);
+
+ image demo = new image();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("image");
+ frame.setSize(250, 250);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+ this.makeCheckImage();
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ if (mousePoint != null) {
+ int screeny = height - (int) mousePoint.getY();
+ gl.glRasterPos2i(mousePoint.x, screeny);
+ gl.glPixelZoom(zoomFactor, zoomFactor);
+ gl.glCopyPixels(0, 0, checkImageWidth, checkImageHeight,
+ GL2.GL_COLOR);
+ // gl.glPixelZoom(1.0f, 1.0f);
+ // mousePoint = null;
+ } else
+ gl.glRasterPos2i(0, 0);
+
+ gl.glDrawPixels(checkImageWidth, checkImageHeight, GL2.GL_RGB,
+ GL.GL_UNSIGNED_BYTE, checkImageBuf);
+
+ 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);
+ height = h;
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluOrtho2D(0.0, (double) w, 0.0, (double) h);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ /*
+ * 3D array won't be used. I left it here for you to see.
+ */
+ private void makeCheckImage() {
+ byte c = (byte) 0xFF;
+
+ for (int i = 0; i < checkImageWidth; i++) {
+ for (int j = 0; j < checkImageHeight; j++) {
+ // c = ((((i & 0x8) == 0) ^ ((j & 0x8)) == 0)) * 255;
+ c = (byte) ((((byte) ((i & 0x8) == 0 ? 0x00 : 0xff)//
+ ^ (byte) ((j & 0x8) == 0 ? 0x00 : 0xff))));
+ // checkImage[i][j][0] = (byte) c;
+ // checkImage[i][j][1] = (byte) c;
+ // checkImage[i][j][2] = (byte) c;
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ }
+ }
+ checkImageBuf.rewind();
+ }//
+
+ public void keyTyped(KeyEvent e) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+ case 'r':
+ case 'R':
+ zoomFactor = 1.0f;
+ System.out.println("zoomFactor reset to 1.0\n");
+ break;
+ case 'z':
+ zoomFactor += 0.5;
+ if (zoomFactor >= 3.0)
+ zoomFactor = 3.0f;
+ System.out.println("zoomFactor is now " + zoomFactor);
+ break;
+ case 'Z':
+ zoomFactor -= 0.5;
+ if (zoomFactor <= 0.5)
+ zoomFactor = 0.5f;
+ System.out.println("zoomFactor is now " + zoomFactor);
+ break;
+
+ default:
+ break;
+ }
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent e) {
+ }
+
+ public void mouseDragged(MouseEvent e) {
+
+ }
+
+ public void mouseMoved(MouseEvent mouse) {
+ mousePoint = mouse.getPoint();
+ // screeny = height - (int) mouse.getY();
+ super.refresh();
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook11/polyoff.java b/src/redbook/src/glredbook11/polyoff.java new file mode 100644 index 0000000..d90edce --- /dev/null +++ b/src/redbook/src/glredbook11/polyoff.java @@ -0,0 +1,223 @@ +package glredbook11;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.gl2.GLUT;
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates polygon offset to draw a shaded polygon and its
+ * wireframe counterpart without ugly visual artifacts ("stitching").
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class polyoff//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener, MouseListener {
+ private GLU glu;
+ private GLUT glut;
+
+ private int list;
+ private int spinx = 0;
+ private int spiny = 0;
+ private float tdist = 0.0f;
+ private float polyfactor = 1.0f;
+ private float polyunits = 1.0f;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ panel.addMouseListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ polyoff demo = new polyoff();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("polyoff");
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ /*
+ * specify initial properties create display list with sphere initialize
+ * lighting and depth buffer
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ glut = new GLUT();
+ //
+
+ 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 };
+ float light_position[] = { 1.0f, 1.0f, 1.0f, 0.0f };
+
+ float global_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
+
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+
+ list = gl.glGenLists(1);
+ gl.glNewList(list, GL2.GL_COMPILE);
+ glut.glutSolidSphere(1.0, 20, 12);
+ gl.glEndList();
+
+ gl.glEnable(GL.GL_DEPTH_TEST);
+
+ 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, global_ambient, 0);
+ }
+
+ /*
+ * display() draws two spheres, one with a gray, diffuse material, the other
+ * sphere with a magenta material with a specular highlight.
+ */
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ /* clear all pixels */
+ // double mat_ambient[] = { 0.8, 0.8, 0.8, 1.0 };
+ // double mat_diffuse[] = { 1.0, 0.0, 0.5, 1.0 };
+ // double mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
+ float gray[] = { 0.8f, 0.8f, 0.8f, 1.0f };
+ float black[] = { 0.0f, 0.0f, 0.0f, 1.0f };
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glPushMatrix();
+ gl.glTranslated(0.0, 0.0, tdist);
+ gl.glRotated((double) spinx, 1.0, 0.0, 0.0);
+ gl.glRotated((double) spiny, 0.0, 1.0, 0.0);
+
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR & GL2.GL_DIFFUSE, gray, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, black, 0);
+ gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 0.0f);
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL2.GL_POLYGON_OFFSET_FILL);
+ gl.glPolygonOffset(polyfactor, polyunits);
+ gl.glCallList(list);
+ gl.glDisable(GL2.GL_POLYGON_OFFSET_FILL);
+
+ gl.glDisable(GL2.GL_LIGHTING);
+ gl.glDisable(GL2.GL_LIGHT0);
+ gl.glColor3d(1.0, 1.0, 1.0);
+ gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2.GL_LINE);
+ gl.glCallList(list);
+ gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2.GL_FILL);
+
+ gl.glPopMatrix();
+ gl.glFlush();
+
+ }
+
+ /* call when window is resized */
+ public void reshape(GLAutoDrawable drawable, int x, int y, int width,
+ int height) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, width, height);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(45.0, (double) width / (double) height, 1.0, 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ glu.gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case 't':
+ if (tdist < 4.0)
+ tdist = (tdist + 0.5f);
+ break;
+ case 'T':
+ if (tdist > -5.0)
+ tdist = (tdist - 0.5f);
+ break;
+ case 'F':
+ polyfactor = polyfactor + 0.1f;
+ System.out.println("polyfactor is " + polyfactor);
+ break;
+ case 'f':
+ polyfactor = polyfactor - 0.1f;
+ System.out.println("polyfactor is " + polyfactor);
+ break;
+ case 'U':
+ polyunits = polyunits + 1.0f;
+ System.out.println("polyunits is " + polyunits);
+ break;
+ case 'u':
+ polyunits = polyunits - 1.0f;
+ System.out.println("polyunits is " + polyunits);
+ break;
+ default:
+ break;
+ }
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void mouseClicked(MouseEvent mouse) {
+ }
+
+ public void mousePressed(MouseEvent mouse) {
+ switch (mouse.getButton()) {
+ case MouseEvent.BUTTON1:
+ spinx = (spinx + 5) % 360;
+ break;
+ case MouseEvent.BUTTON2:
+ spiny = (spiny + 5) % 360;
+ break;
+
+ default:
+ break;
+ }
+ super.refresh();
+ }
+
+ public void mouseReleased(MouseEvent mouse) {
+ }
+
+ public void mouseEntered(MouseEvent mouse) {
+ }
+
+ public void mouseExited(MouseEvent mouse) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook11/tess.java b/src/redbook/src/glredbook11/tess.java new file mode 100644 index 0000000..e2610fd --- /dev/null +++ b/src/redbook/src/glredbook11/tess.java @@ -0,0 +1,263 @@ +package glredbook11;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+
+import javax.media.opengl.*;
+
+import javax.swing.JFrame;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+import javax.media.opengl.glu.GLUtessellator;
+import javax.media.opengl.glu.GLUtessellatorCallback;
+
+/**
+ * This program demonstrates polygon tessellation. Two tesselated objects are
+ * drawn. The first is a rectangle with a triangular hole. The second is a
+ * smooth shaded, self-intersecting star. Note the exterior rectangle is drawn
+ * with its vertices in counter-clockwise order, but its interior clockwise.
+ * Note the combineCallback is needed for the self-intersecting star. Also note
+ * that removing the TessProperty for the star will make the interior unshaded
+ * (WINDING_ODD).
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ * @NOTE Java arrays are column major whereas C arrays are row major
+ */
+public class tess//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private int startList;
+
+ @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) {
+ tess demo = new tess();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("tess");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ /*
+ * jogl specific addition for tessellation
+ */
+ tessellCallBack tessCallback = new tessellCallBack(gl, glu);
+ //
+ double rect[][] = new double[][] { // [4][3] in C; reverse here
+ { 50.0, 50.0, 0.0 }, { 200.0, 50.0, 0.0 }, { 200.0, 200.0, 0.0 },
+ { 50.0, 200.0, 0.0 } };
+ double tri[][] = new double[][] {// [3][3]
+ { 75.0, 75.0, 0.0 }, { 125.0, 175.0, 0.0 }, { 175.0, 75.0, 0.0 } };
+ double star[][] = new double[][] {// [5][6]; 6x5 in java
+ { 250.0, 50.0, 0.0, 1.0, 0.0, 1.0 },
+ { 325.0, 200.0, 0.0, 1.0, 1.0, 0.0 },
+ { 400.0, 50.0, 0.0, 0.0, 1.0, 1.0 },
+ { 250.0, 150.0, 0.0, 1.0, 0.0, 0.0 },
+ { 400.0, 150.0, 0.0, 0.0, 1.0, 0.0 } };
+
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+
+ startList = gl.glGenLists(2);
+
+ GLUtessellator tobj = glu.gluNewTess();
+
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_VERTEX, tessCallback);// glVertex3dv);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_BEGIN, tessCallback);// beginCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_END, tessCallback);// endCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_ERROR, tessCallback);// errorCallback);
+
+ /* rectangle with triangular hole inside */
+ gl.glNewList(startList, GL2.GL_COMPILE);
+ gl.glShadeModel(GL2.GL_FLAT);
+ glu.gluTessBeginPolygon(tobj, null);
+ glu.gluTessBeginContour(tobj);
+ glu.gluTessVertex(tobj, rect[0], 0, rect[0]);
+ glu.gluTessVertex(tobj, rect[1], 0, rect[1]);
+ glu.gluTessVertex(tobj, rect[2], 0, rect[2]);
+ glu.gluTessVertex(tobj, rect[3], 0, rect[3]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessBeginContour(tobj);
+ glu.gluTessVertex(tobj, tri[0], 0, tri[0]);
+ glu.gluTessVertex(tobj, tri[1], 0, tri[1]);
+ glu.gluTessVertex(tobj, tri[2], 0, tri[2]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessEndPolygon(tobj);
+ gl.glEndList();
+
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_VERTEX, tessCallback);// vertexCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_BEGIN, tessCallback);// beginCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_END, tessCallback);// endCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_ERROR, tessCallback);// errorCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_COMBINE, tessCallback);// combineCallback);
+
+ /* smooth shaded, self-intersecting star */
+ gl.glNewList(startList + 1, GL2.GL_COMPILE);
+ gl.glShadeModel(GL2.GL_SMOOTH);
+ glu.gluTessProperty(tobj, //
+ GLU.GLU_TESS_WINDING_RULE, //
+ GLU.GLU_TESS_WINDING_POSITIVE);
+ glu.gluTessBeginPolygon(tobj, null);
+ glu.gluTessBeginContour(tobj);
+ glu.gluTessVertex(tobj, star[0], 0, star[0]);
+ glu.gluTessVertex(tobj, star[1], 0, star[1]);
+ glu.gluTessVertex(tobj, star[2], 0, star[2]);
+ glu.gluTessVertex(tobj, star[3], 0, star[3]);
+ glu.gluTessVertex(tobj, star[4], 0, star[4]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessEndPolygon(tobj);
+ gl.glEndList();
+ glu.gluDeleteTess(tobj);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glCallList(startList);
+ gl.glCallList(startList + 1);
+ 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();
+ glu.gluOrtho2D(0.0, (double) w, 0.0, (double) h);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent e) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent e) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+ /*
+ * Tessellator callback implemenation with all the callback routines. YOu
+ * could use GLUtesselatorCallBackAdapter instead. But
+ */
+ class tessellCallBack //
+ implements GLUtessellatorCallback //
+ {
+ private GL2 gl;
+ private GLU glu;
+
+ public tessellCallBack(GL2 gl, GLU glu) {
+ this.gl = gl;
+ this.glu = glu;
+ }
+
+ public void begin(int type) {
+ gl.glBegin(type);
+ }
+
+ public void end() {
+ gl.glEnd();
+ }
+
+ public void vertex(Object vertexData) {
+ double[] pointer;
+ if (vertexData instanceof double[]) {
+ pointer = (double[]) vertexData;
+ if (pointer.length == 6)
+ gl.glColor3dv(pointer, 3);
+ gl.glVertex3dv(pointer, 0);
+ }
+
+ }
+
+ public void vertexData(Object vertexData, Object polygonData) {
+ }
+
+ /*
+ * combineCallback is used to create a new vertex when edges intersect.
+ * coordinate location is trivial to calculate, but weight[4] may be
+ * used to average color, normal, or texture coordinate data. In this
+ * program, color is weighted.
+ */
+ public void combine(double[] coords, Object[] data, //
+ float[] weight, Object[] outData) {
+ double[] vertex = new double[6];
+ int i;
+
+ vertex[0] = coords[0];
+ vertex[1] = coords[1];
+ vertex[2] = coords[2];
+ for (i = 3; i < 6/* 7OutOfBounds from C! */; i++)
+ vertex[i] = weight[0] //
+ * ((double[]) data[0])[i] + weight[1]
+ * ((double[]) data[1])[i] + weight[2]
+ * ((double[]) data[2])[i] + weight[3]
+ * ((double[]) data[3])[i];
+ outData[0] = vertex;
+ }
+
+ public void combineData(double[] coords, Object[] data, //
+ float[] weight, Object[] outData, Object polygonData) {
+ }
+
+ public void error(int errnum) {
+ String estring;
+
+ estring = glu.gluErrorString(errnum);
+ System.err.println("Tessellation Error: " + estring);
+ System.exit(0);
+ }
+
+ public void beginData(int type, Object polygonData) {
+ }
+
+ public void endData(Object polygonData) {
+ }
+
+ public void edgeFlag(boolean boundaryEdge) {
+ }
+
+ public void edgeFlagData(boolean boundaryEdge, Object polygonData) {
+ }
+
+ public void errorData(int errnum, Object polygonData) {
+ }
+ }// tessellCallBack
+
+}// tess
diff --git a/src/redbook/src/glredbook11/tesswind.java b/src/redbook/src/glredbook11/tesswind.java new file mode 100644 index 0000000..548f519 --- /dev/null +++ b/src/redbook/src/glredbook11/tesswind.java @@ -0,0 +1,375 @@ +package glredbook11;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+import javax.media.opengl.glu.GLUtessellator;
+import javax.media.opengl.glu.GLUtessellatorCallback;
+
+/**
+ * This program demonstrates the winding rule polygon tessellation property.
+ * Four tessellated objects are drawn, each with very different contours. When
+ * the w key is pressed, the objects are drawn with a different winding rule.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ * @NOTE Java arrays are column major whereas C arrays are row major
+ */
+public class tesswind//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private GLUtessellator tobj;
+ private int list;
+// private int currentShape = 0;
+ private double currentWinding = GLU.GLU_TESS_WINDING_ODD;
+ private KeyEvent key;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setSampleBuffers(true);// enable sample buffers for aliasing
+ caps.setNumSamples(2);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args)
+ {
+ tesswind demo = new tesswind();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("tesswind");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable)
+ {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ /*
+ * jogl specific addition for tessellation
+ */
+ tessell tessCallback = new tessell(gl, glu);
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+
+ tobj = glu.gluNewTess();
+
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_VERTEX, tessCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_BEGIN, tessCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_END, tessCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_ERROR, tessCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_COMBINE, tessCallback);
+
+ list = gl.glGenLists(4);
+ makeNewLists(gl, glu);
+ }
+
+ public void display(GLAutoDrawable drawable)
+ {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ if (key != null)
+ {
+ makeNewLists(gl, glu);
+ key = null;// keyPressed re-reference again
+ }
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glPushMatrix();
+ gl.glCallList(list);
+ gl.glTranslatef(0.0f, 500.0f, 0.0f);
+ gl.glCallList(list + 1);
+ gl.glTranslatef(500.0f, -500.0f, 0.0f);
+ gl.glCallList(list + 2);
+ gl.glTranslatef(0.0f, 500.0f, 0.0f);
+ gl.glCallList(list + 3);
+ gl.glPopMatrix();
+ 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) glu.gluOrtho2D(0.0, 1000.0, //
+ 0.0, 1000.0 * (double) h / (double) w);
+ else glu.gluOrtho2D(0.0, 1000.0 * (double) w / (double) h,//
+ 0.0, 1000.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged)
+ {
+ }
+
+ /*
+ * Make four display lists, each with a different tessellated object.
+ */
+ private void makeNewLists(GL2 gl, GLU glu)
+ {
+ int i;
+ final/* static */double rects[][] = new double[][]
+ { // [12][3]
+ { 50.0, 50.0, 0.0 },
+ { 300.0, 50.0, 0.0 },
+ { 300.0, 300.0, 0.0 },
+ { 50.0, 300.0, 0.0 },
+ { 100.0, 100.0, 0.0 },
+ { 250.0, 100.0, 0.0 },
+ { 250.0, 250.0, 0.0 },
+ { 100.0, 250.0, 0.0 },
+ { 150.0, 150.0, 0.0 },
+ { 200.0, 150.0, 0.0 },
+ { 200.0, 200.0, 0.0 },
+ { 150.0, 200.0, 0.0 } };
+ final/* static */double spiral[][] = new double[][]
+ {// [16][3] =
+ { 400.0, 250.0, 0.0 },
+ { 400.0, 50.0, 0.0 },
+ { 50.0, 50.0, 0.0 },
+ { 50.0, 400.0, 0.0 },
+ { 350.0, 400.0, 0.0 },
+ { 350.0, 100.0, 0.0 },
+ { 100.0, 100.0, 0.0 },
+ { 100.0, 350.0, 0.0 },
+ { 300.0, 350.0, 0.0 },
+ { 300.0, 150.0, 0.0 },
+ { 150.0, 150.0, 0.0 },
+ { 150.0, 300.0, 0.0 },
+ { 250.0, 300.0, 0.0 },
+ { 250.0, 200.0, 0.0 },
+ { 200.0, 200.0, 0.0 },
+ { 200.0, 250.0, 0.0 } };
+ final/* static */double quad1[][] = new double[][]
+ {// [4][3] =
+ { 50.0, 150.0, 0.0 },
+ { 350.0, 150.0, 0.0 },
+ { 350.0, 200.0, 0.0 },
+ { 50.0, 200.0, 0.0 } };
+ final/* static */double quad2[][] = new double[][]
+ { // [4][3] =
+ { 100.0, 100.0, 0.0 },
+ { 300.0, 100.0, 0.0 },
+ { 300.0, 350.0, 0.0 },
+ { 100.0, 350.0, 0.0 } };
+ final/* static */double tri[][] = new double[][]
+ {// [3][3] =
+ { 200.0, 50.0, 0.0 },
+ { 250.0, 300.0, 0.0 },
+ { 150.0, 300.0, 0.0 } };
+
+ glu.gluTessProperty(tobj, //
+ GLU.GLU_TESS_WINDING_RULE, currentWinding);
+
+ gl.glNewList(list, GL2.GL_COMPILE);
+ glu.gluTessBeginPolygon(tobj, null);
+ glu.gluTessBeginContour(tobj);
+ for (i = 0; i < 4; i++)
+ glu.gluTessVertex(tobj, rects[i], 0, rects[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessBeginContour(tobj);
+ for (i = 4; i < 8; i++)
+ glu.gluTessVertex(tobj, rects[i], 0, rects[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessBeginContour(tobj);
+ for (i = 8; i < 12; i++)
+ glu.gluTessVertex(tobj, rects[i], 0, rects[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessEndPolygon(tobj);
+ gl.glEndList();
+
+ gl.glNewList(list + 1, GL2.GL_COMPILE);
+ glu.gluTessBeginPolygon(tobj, null);
+ glu.gluTessBeginContour(tobj);
+ for (i = 0; i < 4; i++)
+ glu.gluTessVertex(tobj, rects[i], 0, rects[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessBeginContour(tobj);
+ for (i = 7; i >= 4; i--)
+ glu.gluTessVertex(tobj, rects[i], 0, rects[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessBeginContour(tobj);
+ for (i = 11; i >= 8; i--)
+ glu.gluTessVertex(tobj, rects[i], 0, rects[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessEndPolygon(tobj);
+ gl.glEndList();
+
+ gl.glNewList(list + 2, GL2.GL_COMPILE);
+ glu.gluTessBeginPolygon(tobj, null);
+ glu.gluTessBeginContour(tobj);
+ for (i = 0; i < 16; i++)
+ glu.gluTessVertex(tobj, spiral[i], 0, spiral[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessEndPolygon(tobj);
+ gl.glEndList();
+
+ gl.glNewList(list + 3, GL2.GL_COMPILE);
+ glu.gluTessBeginPolygon(tobj, null);
+ glu.gluTessBeginContour(tobj);
+ for (i = 0; i < 4; i++)
+ glu.gluTessVertex(tobj, quad1[i], 0, quad1[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessBeginContour(tobj);
+ for (i = 0; i < 4; i++)
+ glu.gluTessVertex(tobj, quad2[i], 0, quad2[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessBeginContour(tobj);
+ for (i = 0; i < 3; i++)
+ glu.gluTessVertex(tobj, tri[i], 0, tri[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessEndPolygon(tobj);
+ gl.glEndList();
+ }
+
+ public void keyTyped(KeyEvent e)
+ {
+ }
+
+ public void keyPressed(KeyEvent key)
+ {
+ this.key = key;
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+
+ case KeyEvent.VK_W:
+ if (currentWinding == GLU.GLU_TESS_WINDING_ODD) currentWinding = GLU.GLU_TESS_WINDING_NONZERO;
+ else if (currentWinding == GLU.GLU_TESS_WINDING_NONZERO) currentWinding = GLU.GLU_TESS_WINDING_POSITIVE;
+ else if (currentWinding == GLU.GLU_TESS_WINDING_POSITIVE) currentWinding = GLU.GLU_TESS_WINDING_NEGATIVE;
+ else if (currentWinding == GLU.GLU_TESS_WINDING_NEGATIVE) currentWinding = GLU.GLU_TESS_WINDING_ABS_GEQ_TWO;
+ else if (currentWinding == GLU.GLU_TESS_WINDING_ABS_GEQ_TWO) currentWinding = GLU.GLU_TESS_WINDING_ODD;
+ drawable.display();
+ break;
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent e)
+ {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+ /*
+ * Tessellator callback implemenation with all the callback routines. YOu
+ * could use GLUtesselatorCallBackAdapter instead. But
+ */
+ class tessell
+ implements GLUtessellatorCallback
+ {
+ private GL2 gl;
+ private GLU glu;
+
+ public tessell(GL2 gl, GLU glu)
+ {
+ this.gl = gl;
+ this.glu = glu;
+ }
+
+ public void begin(int type)
+ {
+ gl.glBegin(type);
+ }
+
+ public void end()
+ {
+ gl.glEnd();
+ }
+
+ public void vertex(Object vertexData)
+ {
+ double[] pointer;
+ if (vertexData instanceof double[])
+ {
+ pointer = (double[]) vertexData;
+ if (pointer.length == 6) gl.glColor3dv(pointer, 3);
+ gl.glVertex3dv(pointer, 0);
+ }
+
+ }
+
+ public void vertexData(Object vertexData, Object polygonData)
+ {
+ }
+
+ /*
+ * combineCallback is used to create a new vertex when edges intersect.
+ * coordinate location is trivial to calculate, but weight[4] may be used to
+ * average color, normal, or texture coordinate data. In this program, color
+ * is weighted.
+ */
+ public void combine(double[] coords, Object[] data, //
+ float[] weight, Object[] outData)
+ {
+ double[] vertex = new double[3];
+
+ vertex[0] = coords[0];
+ vertex[1] = coords[1];
+ vertex[2] = coords[2];
+ outData[0] = vertex;
+ }
+
+ public void combineData(double[] coords, Object[] data, //
+ float[] weight, Object[] outData, Object polygonData)
+ {
+ }
+
+ public void error(int errnum)
+ {
+ String estring;
+
+ estring = glu.gluErrorString(errnum);
+ System.err.println("Tessellation Error: " + estring);
+ System.exit(0);
+ }
+
+ public void beginData(int type, Object polygonData)
+ {
+ }
+
+ public void endData(Object polygonData)
+ {
+ }
+
+ public void edgeFlag(boolean boundaryEdge)
+ {
+ }
+
+ public void edgeFlagData(boolean boundaryEdge, Object polygonData)
+ {
+ }
+
+ public void errorData(int errnum, Object polygonData)
+ {
+ }
+ }// tessellCallBack
+
+}// tess
diff --git a/src/redbook/src/glredbook11/texbind.java b/src/redbook/src/glredbook11/texbind.java new file mode 100644 index 0000000..9cbdd90 --- /dev/null +++ b/src/redbook/src/glredbook11/texbind.java @@ -0,0 +1,192 @@ +package glredbook11;
+
+import glredbook10.GLSkeleton;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.nio.ByteBuffer;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.BufferUtil;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates using glBindTexture() by creating and managing two
+ * textures.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class texbind //
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ //
+ private static final int rgba = 4;
+ private static final int checkImageWidth = 64;
+ private static final int checkImageHeight = 64;
+ // private byte checkImage[][][];
+ // private byte otherImage[][][];
+ private ByteBuffer checkImageBuf = //
+ BufferUtil.newByteBuffer(checkImageWidth * checkImageHeight * rgba);
+ private ByteBuffer otherImageBuf = //
+ BufferUtil.newByteBuffer(checkImageWidth * checkImageHeight * rgba);
+ private int[] texName = new int[2];
+
+ @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) {
+ texbind demo = new texbind();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("texbind");
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+
+ makeCheckImages();
+
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+
+ gl.glGenTextures(2, texName, 0);
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_CLAMP);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_CLAMP);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, checkImageWidth,
+ checkImageHeight, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE,
+ checkImageBuf);
+
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[1]);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_CLAMP);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_CLAMP);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_DECAL);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, checkImageWidth,
+ checkImageHeight, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE,
+ otherImageBuf);
+ gl.glEnable(GL2.GL_TEXTURE_2D);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+ gl.glBegin(GL2.GL_QUADS);
+ gl.glTexCoord2d(0.0, 0.0);
+ gl.glVertex3d(-2.0, -1.0, 0.0);
+ gl.glTexCoord2d(0.0, 1.0);
+ gl.glVertex3d(-2.0, 1.0, 0.0);
+ gl.glTexCoord2d(1.0, 1.0);
+ gl.glVertex3d(0.0, 1.0, 0.0);
+ gl.glTexCoord2d(1.0, 0.0);
+ gl.glVertex3d(0.0, -1.0, 0.0);
+ gl.glEnd();
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[1]);
+ gl.glBegin(GL2.GL_QUADS);
+ gl.glTexCoord2d(0.0, 0.0);
+ gl.glVertex3d(1.0, -1.0, 0.0);
+ gl.glTexCoord2d(0.0, 1.0);
+ gl.glVertex3d(1.0, 1.0, 0.0);
+ gl.glTexCoord2d(1.0, 1.0);
+ gl.glVertex3d(2.41421, 1.0, -1.41421);
+ gl.glTexCoord2d(1.0, 0.0);
+ gl.glVertex3d(2.41421, -1.0, -1.41421);
+ gl.glEnd();
+ 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();
+ glu.gluPerspective(60.0, (float) w / (float) h, 1.0, 30.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ gl.glTranslated(0.0, 0.0, -3.6);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void makeCheckImages() {
+ byte c = 0x00;
+
+ for (int i = 0; i < checkImageWidth; i++) {
+ for (int j = 0; j < checkImageHeight; j++) {
+
+ c = (byte) ((((byte) ((i & 0x8) == 0 ? 0x00 : 0xff)//
+ ^ (byte) ((j & 0x8) == 0 ? 0x00 : 0xff))));
+ // checkImage[i][j][0] = (byte) c;
+ // checkImage[i][j][1] = (byte) c;
+ // checkImage[i][j][2] = (byte) c;
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) 0xff);
+
+ c = (byte) ((((byte) ((i & 0x10) == 0 ? 0x00 : 0xff)//
+ ^ (byte) ((j & 0x10) == 0 ? 0x00 : 0xff))));
+
+ otherImageBuf.put((byte) c);
+ otherImageBuf.put((byte) 0);
+ otherImageBuf.put((byte) 0);
+ otherImageBuf.put((byte) 0xff);
+ }
+ }
+ checkImageBuf.rewind();
+ otherImageBuf.rewind();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook11/texgen.java b/src/redbook/src/glredbook11/texgen.java new file mode 100644 index 0000000..aabda01 --- /dev/null +++ b/src/redbook/src/glredbook11/texgen.java @@ -0,0 +1,202 @@ +package glredbook11;
+
+import glredbook10.GLSkeleton;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.nio.ByteBuffer;
+
+import javax.media.opengl.*;import javax.swing.JFrame;
+
+import com.sun.opengl.util.BufferUtil;
+
+import com.sun.opengl.util.gl2.GLUT;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program draws a texture mapped teapot with automatically generated
+ * texture coordinates. The texture is rendered as stripes on the teapot.
+ * Initially, the object is drawn with texture coordinates based upon the object
+ * coordinates of the vertex and distance from the plane x = 0. Pressing the 'e'
+ * key changes the coordinate generation to eye coordinates of the vertex.
+ * Pressing the 'o' key switches it back to the object coordinates. Pressing the
+ * 's' key changes the plane to a slanted one (x + y + z = 0). Pressing the 'x'
+ * key switches it back to x = 0.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class texgen//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLUT glut;
+ private static final int stripeImageWidth = 32;
+ private byte stripeImage[] = new byte[3 * stripeImageWidth];
+ private ByteBuffer stripeImageBuf = BufferUtil
+ .newByteBuffer(stripeImage.length);
+ /* glTexGen stuff: */
+ // private float sgenparams[] = { 1.0f, 1.0f, 1.0f, 0.0f };
+ private int texName[] = new int[1];
+ private static double xequalzero[] = { 1.0, 0.0, 0.0, 0.0 };
+ private static double slanted[] = { 1.0, 1.0, 1.0, 0.0 };
+ private static double currentCoeff[];
+ private static int currentPlane;
+ private static int currentGenMode;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setSampleBuffers(true);// enable sample buffers for aliasing
+ caps.setNumSamples(2);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ texgen demo = new texgen();
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("texgen");
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glut = new GLUT();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+
+ makeStripeImage();
+
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+
+ gl.glGenTextures(1, texName, 0);
+ gl.glBindTexture(GL2.GL_TEXTURE_1D, texName[0]);
+
+ gl
+ .glTexParameterf(GL2.GL_TEXTURE_1D, GL2.GL_TEXTURE_WRAP_S,
+ GL2.GL_REPEAT);
+ gl.glTexParameterf(GL2.GL_TEXTURE_1D, //
+ GL2.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
+ gl.glTexParameterf(GL2.GL_TEXTURE_1D, //
+ GL2.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
+ gl.glTexImage1D(GL2.GL_TEXTURE_1D, 0, GL2.GL_RGBA, stripeImageWidth, //
+ 0, GL2.GL_RGB, GL.GL_UNSIGNED_BYTE, stripeImageBuf);
+
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_MODULATE);
+ currentCoeff = xequalzero;
+ currentGenMode = GL2.GL_OBJECT_LINEAR;
+ currentPlane = GL2.GL_OBJECT_PLANE;
+ gl.glTexGeni(GL2.GL_S, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_OBJECT_LINEAR);
+ gl.glTexGendv(GL2.GL_S, GL2.GL_OBJECT_PLANE, currentCoeff, 0);
+
+ // gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL2.GL_TEXTURE_GEN_S);
+ gl.glEnable(GL2.GL_TEXTURE_1D);
+ gl.glEnable(GL.GL_CULL_FACE);
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL2.GL_AUTO_NORMAL);
+ gl.glEnable(GL2.GL_NORMALIZE);
+ gl.glFrontFace(GL.GL_CW);
+ gl.glCullFace(GL.GL_BACK);
+ gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 64.0f);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glPushMatrix();
+ gl.glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
+
+ gl.glTexGeni(GL2.GL_S, GL2.GL_TEXTURE_GEN_MODE, currentGenMode);
+ gl.glTexGendv(GL2.GL_S, currentPlane, currentCoeff, 0);
+
+ gl.glBindTexture(GL2.GL_TEXTURE_1D, texName[0]);
+ glut.glutSolidTeapot(2.0f);
+ gl.glPopMatrix();
+ 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(-3.5, 3.5, -3.5 * (float) h / (float) w, 3.5 * (float) h
+ / (float) w, -3.5, 3.5);
+ else
+ gl.glOrtho(-3.5 * (float) w / (float) h, //
+ 3.5 * (float) w / (float) h, -3.5, 3.5, -3.5, 3.5);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void makeStripeImage() {
+ for (int j = 0; j < stripeImageWidth; j++) {
+ // stripeImage[3 * j] = (j <= 4) ? 255 : 0;
+ // stripeImage[3 * j + 1] = (j > 4) ? 255 : 0;
+ // stripeImage[3 * j + 2] = 0;
+ stripeImageBuf.put(((j <= 4) ? (byte) 255 : (byte) 0));
+ stripeImageBuf.put(((j > 4) ? (byte) 255 : (byte) 0));
+ stripeImageBuf.put((byte) 0);
+ }
+ stripeImageBuf.rewind();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+ case 'e':
+ case 'E':
+ currentGenMode = GL2.GL_EYE_LINEAR;
+ currentPlane = GL2.GL_EYE_PLANE;
+ break;
+ case 'o':
+ case 'O':
+ currentGenMode = GL2.GL_OBJECT_LINEAR;
+ currentPlane = GL2.GL_OBJECT_PLANE;
+ break;
+ case 's':
+ case 'S':
+ currentCoeff = slanted;
+ break;
+ case 'x':
+ case 'X':
+ currentCoeff = xequalzero;
+ break;
+
+ default:
+ break;
+ }
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook11/texprox.java b/src/redbook/src/glredbook11/texprox.java new file mode 100644 index 0000000..2004645 --- /dev/null +++ b/src/redbook/src/glredbook11/texprox.java @@ -0,0 +1,113 @@ +package glredbook11;
+
+import glredbook10.GLSkeleton;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+
+import javax.media.opengl.*; import javax.media.opengl.awt.GLJPanel;
+import javax.swing.JFrame;
+
+/**
+ * The brief program illustrates use of texture proxies. This program only
+ * prints out some messages about whether certain size textures are supported
+ * and then exits.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class texprox//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+
+ @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) {
+ texprox demo = new texprox();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("texprox");
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+
+ System.out.println("This program demonstrates a feature "
+ + "which is not in OpenGL Version 1.0.");
+ System.out.println("If your implementation of OpenGL "
+ + "Version 1.0 has the right extensions,");
+ System.out.println("you may be able to modify this "
+ + "program to make it run.");
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ int proxyComponents[] = new int[1];
+
+ gl.glTexImage2D(GL2.GL_PROXY_TEXTURE_2D, 0, GL2.GL_RGBA8, 64, 64, 0,
+ GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE, null);
+ gl.glGetTexLevelParameteriv(GL2.GL_PROXY_TEXTURE_2D, 0,
+ GL2.GL_TEXTURE_COMPONENTS, proxyComponents, 0);
+ System.out.println("proxyComponents are " + proxyComponents[0]);
+ if (proxyComponents[0] == GL2.GL_RGBA8) //
+ System.out.println("proxy allocation succeeded");
+ else
+ System.out.println("proxy allocation failed");
+ //
+ gl.glTexImage2D(GL2.GL_PROXY_TEXTURE_2D, 0, GL2.GL_RGBA16, 2048, 2048, 0,
+ GL2.GL_RGBA, GL.GL_UNSIGNED_SHORT, null);
+ gl.glGetTexLevelParameteriv(GL2.GL_PROXY_TEXTURE_2D, 0,
+ GL2.GL_TEXTURE_COMPONENTS, proxyComponents, 0);
+ System.out.println("proxyComponents are " + proxyComponents[0]);
+ if (proxyComponents[0] == GL2.GL_RGBA16) //
+ System.out.println("proxy allocation succeeded");
+ else
+ System.out.println("proxy allocation failed");
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ drawable.getGL().glClear(GL.GL_COLOR_BUFFER_BIT);
+ }
+
+ 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();
+ }
+
+ 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);
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook11/texsub.java b/src/redbook/src/glredbook11/texsub.java new file mode 100644 index 0000000..93a2d21 --- /dev/null +++ b/src/redbook/src/glredbook11/texsub.java @@ -0,0 +1,219 @@ +package glredbook11;
+
+import glredbook10.GLSkeleton;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.nio.ByteBuffer;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.BufferUtil;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program texture maps a checkerboard image onto two rectangles. This
+ * program clamps the texture, if the texture coordinates fall outside 0.0 and
+ * 1.0. If the s key is pressed, a texture subimage is used to alter the
+ * original texture. If the r key is pressed, the original texture is restored.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class texsub
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private KeyEvent key;
+ //
+ private static final int rgba = 4;
+ private static final int checkImageWidth = 64;
+ private static final int checkImageHeight = 64;
+ private static final int subImageWidth = 16;
+ private static final int subImageHeight = 16;
+ // private byte checkImage[][][];
+ // private byte otherImage[][][];
+ private ByteBuffer checkImageBuf = //
+ BufferUtil.newByteBuffer(checkImageWidth * checkImageHeight * rgba);
+ private ByteBuffer subImageBuf = //
+ BufferUtil.newByteBuffer(subImageWidth * subImageHeight * rgba);
+ private int[] texName = new int[2];
+
+ @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) {
+
+ texsub demo = new texsub();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("texsub");
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+
+ makeCheckImages();
+
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+
+ gl.glGenTextures(2, texName, 0);
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+ gl
+ .glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S,
+ GL2.GL_REPEAT);
+ gl
+ .glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T,
+ GL2.GL_REPEAT);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, checkImageWidth,
+ checkImageHeight, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE,
+ checkImageBuf);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ if (key != null) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_S:
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+ gl.glTexSubImage2D(GL2.GL_TEXTURE_2D, 0, 12, 44, subImageWidth,
+ subImageHeight, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE,
+ subImageBuf);
+ break;
+ case KeyEvent.VK_R:
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA,
+ checkImageWidth, checkImageHeight, 0, GL2.GL_RGBA,
+ GL.GL_UNSIGNED_BYTE, checkImageBuf);
+ break;
+
+ default:
+ break;
+ }
+ key = null;
+ }
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ gl.glEnable(GL2.GL_TEXTURE_2D);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_DECAL);
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+
+ gl.glBegin(GL2.GL_QUADS);
+ gl.glTexCoord2d(0.0, 0.0);
+ gl.glVertex3d(-2.0, -1.0, 0.0);
+ gl.glTexCoord2d(0.0, 1.0);
+ gl.glVertex3d(-2.0, 1.0, 0.0);
+ gl.glTexCoord2d(1.0, 1.0);
+ gl.glVertex3d(0.0, 1.0, 0.0);
+ gl.glTexCoord2d(1.0, 0.0);
+ gl.glVertex3d(0.0, -1.0, 0.0);
+
+ gl.glTexCoord2d(0.0, 0.0);
+ gl.glVertex3d(1.0, -1.0, 0.0);
+ gl.glTexCoord2d(0.0, 1.0);
+ gl.glVertex3d(1.0, 1.0, 0.0);
+ gl.glTexCoord2d(1.0, 1.0);
+ gl.glVertex3d(2.41421, 1.0, -1.41421);
+ gl.glTexCoord2d(1.0, 0.0);
+ gl.glVertex3d(2.41421, -1.0, -1.41421);
+ gl.glEnd();
+ gl.glFlush();
+
+ gl.glDisable(GL2.GL_TEXTURE_2D);
+ }
+
+ 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();
+ glu.gluPerspective(60.0, (float) w / (float) h, 1.0, 30.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ gl.glTranslated(0.0, 0.0, -3.6);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void makeCheckImages() {
+ byte c = 0x00;
+
+ for (int i = 0; i < checkImageWidth; i++) {
+ for (int j = 0; j < checkImageHeight; j++) {
+
+ c = (byte) ((((byte) ((i & 0x8) == 0 ? 0x00 : 0xff)//
+ ^ (byte) ((j & 0x8) == 0 ? 0x00 : 0xff))));
+ // checkImage[i][j][0] = (byte) c;
+ // checkImage[i][j][1] = (byte) c;
+ // checkImage[i][j][2] = (byte) c;
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) 0xff);
+ }
+ }
+
+ for (int i = 0; i < subImageHeight; i++) {
+ for (int j = 0; j < subImageWidth; j++) {
+ c = (byte) ((((byte) ((i & 0x4) == 0 ? 0x00 : 0xff)//
+ ^ (byte) ((j & 0x4) == 0 ? 0x00 : 0xff))));
+ subImageBuf.put((byte) c);
+ subImageBuf.put((byte) 0);
+ subImageBuf.put((byte) 0);
+ subImageBuf.put((byte) 0xff);
+ }
+ }
+ checkImageBuf.rewind();
+ subImageBuf.rewind();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ this.key = key;
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ default:
+ break;
+ }
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook11/torus.java b/src/redbook/src/glredbook11/torus.java new file mode 100644 index 0000000..414d1f5 --- /dev/null +++ b/src/redbook/src/glredbook11/torus.java @@ -0,0 +1,150 @@ +package glredbook11;
+
+import glredbook10.GLSkeleton;
+
+import javax.swing.*;
+import javax.media.opengl.*;
+import java.awt.event.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates the creation of a display list.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class torus//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private int theTorus;
+ private KeyEvent key;
+ private float angleX = 30f;
+ private float angleY = 30f;
+
+ @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) {
+
+ torus demo = new torus();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("torus");
+ frame.setSize(512, 512);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ private void drawTorus(GL2 gl, int numc, int numt) {
+ double s, t, x, y, z, twopi = 2 * Math.PI;
+ for (int i = 0; i < numc; i++) {
+ gl.glBegin(GL2.GL_QUAD_STRIP);
+ for (int j = 0; j <= numt; j++) {
+ for (int k = 1; k >= 0; k--) {
+ s = (i + k) % numc + 0.5;
+ t = j % numt;
+ x = (1 + 0.1 * Math.cos(s * twopi / numc))
+ * Math.cos(t * twopi / numt);
+ y = (1 + 0.1 * Math.cos(s * twopi / numc))
+ * Math.sin(t * twopi / numt);
+ z = 0.1 * Math.sin(s * twopi / numc);
+ gl.glVertex3d(x, y, z);
+ }// k
+ }// j
+ gl.glEnd();
+ }// i
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ theTorus = gl.glGenLists(1);
+ gl.glNewList(theTorus, GL2.GL_COMPILE);
+ drawTorus(gl, 8, 25);
+ gl.glEndList();
+ //
+ gl.glShadeModel(GL2.GL_FLAT);
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ GLU glu = new GLU();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ //
+ if (key != null) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_X:
+ gl.glRotated(angleX, 1f, 0f, 0f);
+ break;
+ case KeyEvent.VK_Y:
+ gl.glRotated(angleY, 0f, 1f, 0f);
+ break;
+ case KeyEvent.VK_I:
+ gl.glLoadIdentity();
+ glu.gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
+ break;
+ default:
+ break;
+ }
+ }
+
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glCallList(theTorus);
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int width,
+ int height) {
+ GL2 gl = drawable.getGL().getGL2();
+ GLU glu = new GLU();
+ //
+ gl.glViewport(0, 0, width, height);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(30, (float) width / (float) height, 1.0, 100.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ glu.gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ this.key = key;
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook11/unproject.java b/src/redbook/src/glredbook11/unproject.java new file mode 100644 index 0000000..4f5514d --- /dev/null +++ b/src/redbook/src/glredbook11/unproject.java @@ -0,0 +1,165 @@ +package glredbook11;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+
+/**
+ * When the left mouse button is pressed, this program reads the mouse position
+ * and determines two 3D points from which it was transformed. Very little is
+ * displayed.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class unproject//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener, MouseListener {
+ private GLU glu;
+ private MouseEvent mouse;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ panel.addMouseListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ unproject demo = new unproject();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("unproject");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+// GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ int viewport[] = new int[4];
+ double mvmatrix[] = new double[16];
+ double projmatrix[] = new double[16];
+ int realy = 0;// GL y coord pos
+ double wcoord[] = new double[4];// wx, wy, wz;// returned xyz coords
+ if (mouse != null) {
+ int x = mouse.getX(), y = mouse.getY();
+ switch (mouse.getButton()) {
+ case MouseEvent.BUTTON1:
+ gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
+ gl.glGetDoublev(GL2.GL_MODELVIEW_MATRIX, mvmatrix, 0);
+ gl.glGetDoublev(GL2.GL_PROJECTION_MATRIX, projmatrix, 0);
+ /* note viewport[3] is height of window in pixels */
+ realy = viewport[3] - (int) y - 1;
+ System.out.println("Coordinates at cursor are (" + x + ", "
+ + realy);
+ glu.gluUnProject((double) x, (double) realy, 0.0, //
+ mvmatrix, 0,//
+ projmatrix, 0, //
+ viewport, 0, //
+ wcoord, 0);
+ System.out
+ .println("World coords at z=0.0 are ( " //
+ + wcoord[0] + ", " + wcoord[1] + ", "
+ + wcoord[2] + ")");
+ glu.gluUnProject((double) x, (double) realy, 1.0, //
+ mvmatrix, 0,//
+ projmatrix, 0,//
+ viewport, 0, //
+ wcoord, 0);
+ System.out
+ .println("World coords at z=1.0 are (" //
+ + wcoord[0] + ", " + wcoord[1] + ", "
+ + wcoord[2] + ")");
+ break;
+ case MouseEvent.BUTTON2:
+ break;
+ default:
+ break;
+ }
+ }
+
+ gl.glFlush();
+ }
+
+ /* Change these values for a different transformation */
+ 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();
+ glu.gluPerspective(45.0, (float) w / (float) h, 1.0, 100.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 mouseClicked(MouseEvent e) {
+ }
+
+ public void mousePressed(MouseEvent e) {
+ mouse = e;
+ super.refresh();
+ }
+
+ public void mouseReleased(MouseEvent e) {
+ }
+
+ public void mouseEntered(MouseEvent e) {
+ }
+
+ public void mouseExited(MouseEvent e) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook11/varray.java b/src/redbook/src/glredbook11/varray.java new file mode 100644 index 0000000..7e9379a --- /dev/null +++ b/src/redbook/src/glredbook11/varray.java @@ -0,0 +1,228 @@ +package glredbook11;
+
+import glredbook10.GLSkeleton;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+import java.nio.FloatBuffer;
+import java.nio.IntBuffer;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.BufferUtil;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates vertex arrays.
+ *
+ * @author Chris Brown (bug fix)
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+
+public class varray//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener, MouseListener {
+ private GLU glu;
+ private final int POINTER = 1;
+ private final int INTERLEAVED = 2;
+
+ private final int DRAWARRAY = 1;
+ private final int ARRAYELEMENT = 2;
+ private final int DRAWELEMENTS = 3;
+
+ private int setupMethod = POINTER;
+ private int derefMethod = DRAWARRAY;
+
+ private IntBuffer indicesBuf;
+ private IntBuffer verticesBuf;
+ private FloatBuffer colorsBuf;
+ private FloatBuffer intertwinedBuf;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ panel.addMouseListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setSampleBuffers(true);// enable sample buffers for aliasing
+ caps.setNumSamples(2);
+
+ varray demo = new varray();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("varray");
+ frame.setSize(512, 256);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_SMOOTH);
+ setupPointers(gl);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ if (derefMethod == DRAWARRAY) {
+ gl.glDrawArrays(GL2.GL_TRIANGLES, 0, 6);
+ } else if (derefMethod == ARRAYELEMENT) {
+ gl.glBegin(GL2.GL_TRIANGLES);
+ gl.glArrayElement(2);
+ gl.glArrayElement(3);
+ gl.glArrayElement(5);
+ gl.glEnd();
+ } else if (derefMethod == DRAWELEMENTS) {
+ int indices[] = new int[] { 0, 1, 3, 4 };
+ if (indicesBuf == null) {
+ indicesBuf = BufferUtil.newIntBuffer(indices.length);
+ indicesBuf.put(indices);
+ }
+ indicesBuf.rewind();
+ gl.glDrawElements(GL2.GL_POLYGON, 4, GL2.GL_UNSIGNED_INT, indicesBuf);
+ }
+ gl.glFlush();
+
+ // gl calls from C example's mouse routine are moved here
+ if (setupMethod == INTERLEAVED)
+ setupInterleave(gl);
+ else if (setupMethod == POINTER)
+ setupPointers(gl);
+
+ }
+
+ 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();
+ glu.gluOrtho2D(0.0, (double) w, 0.0, (double) h);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void setupPointers(GL2 gl) {
+ int vertices[] = new int[] { 25, 25, 100, 325, 175, 25, 175, 325, 250,
+ 25, 325, 325 };
+ float colors[] = new float[] { 1.0f, 0.2f, 0.2f, 0.2f, 0.2f, 1.0f,
+ 0.8f, 1.0f, 0.2f, 0.75f, 0.75f, 0.75f, 0.35f, 0.35f, 0.35f,
+ 0.5f, 0.5f, 0.5f };
+
+ if (verticesBuf == null) {// IntBuffer tmpVerticesBuf
+ verticesBuf = BufferUtil.newIntBuffer(vertices.length);
+ verticesBuf.put(vertices);
+ }
+ if (colorsBuf == null) {
+ colorsBuf = BufferUtil.newFloatBuffer(colors.length);
+ colorsBuf.put(colors);
+ }
+ verticesBuf.rewind();
+ colorsBuf.rewind();
+ //
+ gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
+ gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
+ //
+ gl.glVertexPointer(2, GL2.GL_INT, 0, verticesBuf);
+ gl.glColorPointer(3, GL.GL_FLOAT, 0, colorsBuf);
+
+ }
+
+ private void setupInterleave(GL2 gl) {
+ float intertwined[] = new float[] { 1.0f, 0.2f, 1.0f, 100.0f, 100.0f,
+ 0.0f, 1.0f, 0.2f, 0.2f, 0.0f, 200.0f, 0.0f, 1.0f, 1.0f, 0.2f,
+ 100.0f, 300.0f, 0.0f, 0.2f, 1.0f, 0.2f, 200.0f, 300.0f, 0.0f,
+ 0.2f, 1.0f, 1.0f, 300.0f, 200.0f, 0.0f, 0.2f, 0.2f, 1.0f,
+ 200.0f, 100.0f, 0.0f };
+ if (intertwinedBuf == null) {
+ intertwinedBuf = BufferUtil.newFloatBuffer(intertwined.length);
+ intertwinedBuf.put(intertwined);
+ }
+
+ intertwinedBuf.rewind();
+ gl.glInterleavedArrays(GL2.GL_C3F_V3F, 0, intertwinedBuf);
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void mouseClicked(MouseEvent mouse) {
+ }
+
+ public void mousePressed(MouseEvent mouse) {
+ if (mouse.getButton() == MouseEvent.BUTTON1) {
+ if (setupMethod == POINTER) {
+ setupMethod = INTERLEAVED;
+ // setupInterleave(gl);don't call
+ } else if (setupMethod == INTERLEAVED) {
+ setupMethod = POINTER;
+ // setupPointers(gl);
+ }
+ // validate();
+ }
+ if (mouse.getButton() == MouseEvent.BUTTON2
+ || mouse.getButton() == MouseEvent.BUTTON3) {
+ if (derefMethod == DRAWARRAY)
+ derefMethod = ARRAYELEMENT;
+ else if (derefMethod == ARRAYELEMENT)
+ derefMethod = DRAWELEMENTS;
+ else if (derefMethod == DRAWELEMENTS)
+ derefMethod = DRAWARRAY;
+ // validate();
+ }
+ super.refresh();
+ }
+
+ public void mouseReleased(MouseEvent mouse) {
+ }
+
+ public void mouseEntered(MouseEvent mouse) {
+ }
+
+ public void mouseExited(MouseEvent mouse) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook11/wrap.java b/src/redbook/src/glredbook11/wrap.java new file mode 100644 index 0000000..33e7281 --- /dev/null +++ b/src/redbook/src/glredbook11/wrap.java @@ -0,0 +1,210 @@ +package glredbook11;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.nio.ByteBuffer;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.BufferUtil;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program texture maps a checkerboard image onto two rectangles. This
+ * program demonstrates the wrapping modes, if the texture coordinates fall
+ * outside 0.0 and 1.0. Interaction: Pressing the 's' and 'S' keys switch the
+ * wrapping between clamping and repeating for the s parameter. The 't' and 'T'
+ * keys control the wrapping for the t parameter. If running this program on
+ * OpenGL 1.0, texture objects are not used.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class wrap//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+
+ private int texName[] = new int[1];
+ private static final int checkImageWidth = 64;
+ private static final int checkImageHeight = 64;
+ private static final int rgba = 4;
+ // private byte[][][] checkImage;
+ private ByteBuffer checkImageBuf = //
+ BufferUtil.newByteBuffer(checkImageHeight * checkImageWidth * rgba);
+ private KeyEvent key;
+
+ @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) {
+ wrap demo = new wrap();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("wrap");
+ frame.setSize(250, 250);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ System.out.println("" + GL.GL_VERSION);
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+
+ makeCheckImage();
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+
+ gl.glGenTextures(1, texName, 0);
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+
+ gl
+ .glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S,
+ GL2.GL_REPEAT);
+ gl
+ .glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T,
+ GL2.GL_REPEAT);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, checkImageWidth,
+ checkImageHeight, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE,
+ checkImageBuf);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ gl.glEnable(GL2.GL_TEXTURE_2D);
+ if (key != null) {
+ switch (key.getKeyChar()) {
+ case 's':
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S,
+ GL2.GL_CLAMP);
+ break;
+ case 'S':
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S,
+ GL2.GL_REPEAT);
+ break;
+ case 't':
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T,
+ GL2.GL_CLAMP);
+ break;
+ case 'T':
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T,
+ GL2.GL_REPEAT);
+ break;
+ default:
+ break;
+ }
+ }
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_DECAL);
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+
+ gl.glBegin(GL2.GL_QUADS);
+ gl.glTexCoord2d(0.0, 0.0);
+ gl.glVertex3d(-2.0, -1.0, 0.0);
+ gl.glTexCoord2d(0.0, 3.0);
+ gl.glVertex3d(-2.0, 1.0, 0.0);
+ gl.glTexCoord2d(3.0, 3.0);
+ gl.glVertex3d(0.0, 1.0, 0.0);
+ gl.glTexCoord2d(3.0, 0.0);
+ gl.glVertex3d(0.0, -1.0, 0.0);
+
+ gl.glTexCoord2d(0.0, 0.0);
+ gl.glVertex3d(1.0, -1.0, 0.0);
+ gl.glTexCoord2d(0.0, 3.0);
+ gl.glVertex3d(1.0, 1.0, 0.0);
+ gl.glTexCoord2d(3.0, 3.0);
+ gl.glVertex3d(2.41421, 1.0, -1.41421);
+ gl.glTexCoord2d(3.0, 0.0);
+ gl.glVertex3d(2.41421, -1.0, -1.41421);
+ gl.glEnd();
+ gl.glFlush();
+ gl.glDisable(GL2.GL_TEXTURE_2D);
+ }
+
+ 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();
+ glu.gluPerspective(60.0, (float) w / (float) h, 1.0, 30.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ gl.glTranslated(0.0, 0.0, -3.6);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void makeCheckImage() {
+ // byte c = (~(i & 0x8) ^ ~(j & 0x8)) * 255;
+ byte c = 0;
+ for (int i = 0; i < checkImageHeight; i++) {
+ for (int j = 0; j < checkImageWidth; j++) {
+ // c = ((((i&0x8)==0)^((j&0x8))==0))*255;C' version
+ c = (byte) ((((byte) ((i & 0x8) == 0 ? 0x00 : 0xff)//
+ ^ (byte) ((j & 0x8) == 0 ? 0x00 : 0xff))));
+ System.out.print("" + (byte) c + " ");
+ // checkImage[i][j][0] = ( byte) c;
+ // checkImage[i][j][1] = ( byte) c;
+ // checkImage[i][j][2] = ( byte) c;
+ // checkImage[i][j][3] = ( byte) 0xff;
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) 0xff);
+ }
+ System.out.println();
+ }
+ checkImageBuf.rewind();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ this.key = key;
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
|