/* * Wave.java */ import com.sun.opengl.util.FPSAnimator; import com.sun.opengl.util.GLUT; import java.awt.BorderLayout; import javax.media.opengl.GL; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCanvas; import javax.media.opengl.GLEventListener; import javax.media.opengl.glu.GLU; import javax.swing.JFrame; /** * Demonstrates how to plot a 2D surface as a wireframe. */ public class Wave implements GLEventListener { private GLU glu = new GLU(); // OpenGL Utility Library - used to set camera view private FPSAnimator anim; // object used to do the animation private double angle = 0.0; // angle of teapot at each frame private int fps = 10; // frames per second /** * Creates a new instance of Wave */ public Wave() { } /** * Initializes parameters. * @param d Object that provides support for OpenGL rendering. **/ public void init( GLAutoDrawable d ) { GL gl = d.getGL(); gl.glClearColor( .2f,.2f,.5f,0 ); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(20.0f, 1.0f, 1.0f, 200.0f); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glEnable(GL.GL_DEPTH_TEST); anim = new FPSAnimator(d, fps); anim.start(); } /** * Draws what is on the canvas. Here we are drawing a 2D function * computed at points on a grid where the value of the function is * represented by the z component. The resulting surface is drawn * as a wire frame. The function is a radial cos. * @param d Object that provides support for OpenGL rendering. **/ public void display(GLAutoDrawable d) { angle += 1; if( angle > 360. ) angle -= 360.0; GL gl = d.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); glu.gluLookAt(0,8,4,0,0,0,0,0,1); gl.glRotated(angle,0,0,1); gl.glColor3d(1,1,0); double range = 4; // side length = 2*range int n = 60; // number of grids along edge double grid = 2*range/n; //size of one grid for (int i = 0; i < n; i++) { for (int j=0; j < n; j++) { double x = i*grid - range; double y = j*grid - range; double xx = x + grid, yy = y + grid; gl.glBegin(GL.GL_LINE_STRIP); gl.glVertex3d(x, y, f(x,y)); gl.glVertex3d(x, yy, f(x, yy)); gl.glVertex3d(xx, yy, f(xx, yy)); gl.glVertex3d(xx, y, f(xx,y)); gl.glEnd(); // This will render surface as checkerboard solid, not wireframe. // if ((i+j)%2==0) gl.glColor3d(1,0,0); // else gl.glColor3d(0,1,0); // gl.glBegin(GL.GL_QUADS); // gl.glVertex3d(x, y, f(x,y)); // gl.glVertex3d(x, yy, f(x, yy)); // gl.glVertex3d(xx, yy, f(xx, yy)); // gl.glVertex3d(xx, y, f(xx,y)); // gl.glEnd(); } } } /** * Compute the value of function being plotted, f(x,y) = .5*cos(x^2+y^2) * @param x x value where function is computed. * @param y y value where function is computed. */ public double f(double x, double y) { return .5*Math.cos(x*x + y*y); } /** * Called when the window is resized. * @param d Object that provides support for OpenGL rendering. * @param x horizontal location of upper left corner of window. * @param y vertical location of upper left corner of window. * @param width width of window. * @param height height of window. **/ public void reshape(GLAutoDrawable d, int x, int y, int width, int height ){ GL gl = d.getGL(); gl.glViewport(0,0, width, height); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(60.,(double)width/height, 1.0, 300.); gl.glMatrixMode(GL.GL_MODELVIEW); } /** * Starting point of execution. * @param args command line arguments. Not used here. **/ public static void main( String[] args ) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); GLCanvas canvas = new GLCanvas(); canvas.setSize(400,400); canvas.addGLEventListener( new Wave() ); frame.getContentPane().add( canvas, BorderLayout.CENTER ); frame.pack(); frame.setVisible( true ); } /** * Called by the drawable when the display mode or the display device associated * with the GLAutoDrawable has changed. Not used here but must be included * for interface. **/ public void displayChanged( GLAutoDrawable d, boolean modeChanged, boolean deviceChanged ) { } }