/* * 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. */ /* * Created on 24/02/2006 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ /** * * Vrray.java * This program demonstrates vertex arrays. * * @converted to Java by Wu Shin - Ting * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ import java.nio.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.media.opengl.*; import javax.media.opengl.glu.*; public class Varray extends JFrame implements GLEventListener, MouseListener, KeyListener { public static final Dimension PREFERRED_FRAME_SIZE = new Dimension(350, 350); private IntBuffer vb, ib; private FloatBuffer cb, wb; private int vertices[] = {25, 25, 100, 325, 175, 25, 175, 325, 250, 25, 325, 325}; private float colors[] = {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}; private float intertwined [] = {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}; private int indices[] = {0, 1, 3, 4}; private int SIZEOF_FLOAT = 4; private int SIZEOF_INT = 4; private int POINTER = 1; private int INTERLEAVED = 2; private int DRAWARRAY = 1; private int ARRAYELEMENT = 2; private int DRAWELEMENTS = 3; private int setupMethod = POINTER; private int derefMethod = DRAWARRAY; /** Constructor. */ public Varray() { // init JFrame super ("Varray - JMDM version "); System.out.println("Constructor"); } /** We'd like to be 350x350, please. */ public Dimension getPreferredSize(){ return PREFERRED_FRAME_SIZE; } /* * METHODS DEFINED BY GLEventListener */ public void display(GLAutoDrawable drawable) { System.out.println("Display"); GL gl = drawable.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT); if (setupMethod == POINTER) { gl.glVertexPointer(2, GL.GL_INT, 0, vb); gl.glColorPointer (3, GL.GL_FLOAT, 0, cb); } else if (setupMethod == INTERLEAVED) { gl.glInterleavedArrays (GL.GL_C3F_V3F, 0, wb); } if (derefMethod == DRAWARRAY) { System.out.println("DRAWARRAY"); gl.glDrawArrays (GL.GL_TRIANGLES, 0, 6); } else if (derefMethod == ARRAYELEMENT) { System.out.println("ARRAYELEMENT"); gl.glBegin (GL.GL_TRIANGLES); gl.glArrayElement (2); gl.glArrayElement (3); gl.glArrayElement (5); gl.glEnd (); } else if (derefMethod == DRAWELEMENTS) { System.out.println("DRAWELEMENTS"); ib.rewind(); for (int i = 0; i < indices.length; i++) ib.put(indices[i]); ib.rewind(); gl.glDrawElements (GL.GL_POLYGON, 4, GL.GL_UNSIGNED_INT, ib); } gl.glFlush (); } /** Called by drawable to indicate mode or device has changed. */ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged){} /** Called after OpenGL is init'ed */ public void init(GLAutoDrawable drawable) { GL gl = drawable.getGL(); System.out.println("init()"); System.err.println("INIT GL IS: " + gl.getClass().getName()); System.err.println("GL_VENDOR: " + gl.glGetString(GL.GL_VENDOR)); System.err.println("GL_RENDERER: " + gl.glGetString(GL.GL_RENDERER)); System.err.println("GL_VERSION: " + gl.glGetString(GL.GL_VERSION)); gl.glClearColor (0.0f, 0.0f, 0.0f, 0.0f); gl.glShadeModel (GL.GL_SMOOTH); //Allocate a set of direct buffers for processing // GL vertex arrays cb = setupFloat(ByteBuffer.allocateDirect(6 * 3 * (SIZEOF_FLOAT))); wb = setupFloat(ByteBuffer.allocateDirect(6 * 6 * (SIZEOF_FLOAT))); vb = setupInt(ByteBuffer.allocateDirect(6 * 2 * (SIZEOF_INT))); ib = setupInt(ByteBuffer.allocateDirect(4 * (SIZEOF_INT))); //Activate the vertex and color arrays gl.glEnableClientState(GL.GL_VERTEX_ARRAY); gl.glEnableClientState(GL.GL_COLOR_ARRAY); // Initialize with the setup method = POINTER setupPointers(); //Add the key listener drawable.addKeyListener(this); //Add the mouse listener drawable.addMouseListener(this); } /** Called to indicate the drawing surface has been moved and/or resized. */ public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height){ System.out.println("reshape()"); GL gl = drawable.getGL(); gl.glViewport (0, 0, width, height); gl.glMatrixMode (GL.GL_PROJECTION); gl.glLoadIdentity (); (new GLU()).gluOrtho2D (0.0f, (double) width, 0.0f, (double) height); } /* * OUR HELPER METHODS */ private FloatBuffer setupFloat (ByteBuffer buffer) { buffer.order(ByteOrder.nativeOrder()); return buffer.asFloatBuffer(); } private IntBuffer setupInt (ByteBuffer buffer) { buffer.order(ByteOrder.nativeOrder()); return buffer.asIntBuffer(); } private void setupPointers(){ vb.position(0); for (int i = 0; i < vertices.length; i++) { vb.put(vertices[i]); } cb.position(0); for (int i = 0; i < colors.length; i++) cb.put(colors[i]); vb.position(0); cb.position(0); } private void setupInterleaved() { wb.position(0); for (int i = 0; i < intertwined.length; i++) wb.put(intertwined[i]); wb.position(0); } // Methods required for the implementation of MouseListener public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e){ System.out.println("Mouse Pressed"); switch (e.getButton()){ case MouseEvent.BUTTON1: if (setupMethod == POINTER) { setupMethod = INTERLEAVED; setupInterleaved(); } else if (setupMethod == INTERLEAVED) { setupMethod = POINTER; setupPointers(); } ((GLCanvas)(this.getContentPane().getComponentAt(0,0))).display(); break; case MouseEvent.BUTTON2: case MouseEvent.BUTTON3: if (derefMethod == DRAWARRAY) derefMethod = ARRAYELEMENT; else if (derefMethod == ARRAYELEMENT) derefMethod = DRAWELEMENTS; else if (derefMethod == DRAWELEMENTS) derefMethod = DRAWARRAY; ((GLCanvas)(this.getContentPane().getComponentAt(0,0))).display(); break; } } public void mouseReleased(MouseEvent e){} public void mouseClicked(MouseEvent e){} // Methods required for the implementation of MouseMotionListener public void mouseDragged(MouseEvent e){} public void mouseMoved(MouseEvent e){} // Methods required for the implementation of KeyListener public void keyPressed(KeyEvent e){ System.out.println("Key pressed"); if (e.getKeyChar() == 27) { /* Escape Key */ System.exit(0); } } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} /** main creates and shows a Varray-JFrame */ public static void main(String[] args){ System.err.println("This program demonstrates a feature which is not in OpenGL Version 1.0."); System.err.println("If your implementation of OpenGL Version 1.0 has the right extensions,"); System.err.println("you may be able to modify this program to make it run."); Varray g = new Varray(); //Set frame location g.setLocation(100,100); GLCapabilities gl_c = new GLCapabilities(); // Disable double buffer gl_c.setDoubleBuffered(false); // get a GLCanvas GLCanvas canvas = new GLCanvas(gl_c); // add a GLEventListener, which will get called when the // canvas is resized or needs a repaint canvas.addGLEventListener(g); // now add the canvas to the JFrame. Note we use BorderLayout.CENTER // to make the canvas stretch to fill the container (ie, the frame) g.getContentPane().add(canvas, BorderLayout.CENTER); g.pack(); g.setVisible(true); } }