/* * 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 23/02/2006 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ /* Aaccanti.java * Use the accumulation buffer to do full-scene antialiasing * on a scene with orthographic parallel projection. * * * @converted to Java by Wu Shin - Ting * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import com.sun.opengl.util.*; import javax.media.opengl.*; public class Accanti extends JFrame implements GLEventListener, KeyListener { public static final Dimension PREFERRED_FRAME_SIZE = new Dimension(250, 250); private static int ACSIZE = 8; float j8[] = { -0.334818f, 0.435331f, 0.286438f, -0.393495f, 0.459462f, 0.141540f, -0.414498f, -0.192829f, -0.183790f, 0.082102f, -0.079263f, -0.317383f, 0.102254f, 0.299133f, 0.164216f, -0.054399f }; /** Constructor. */ public Accanti() { // init JFrame super ("Accanti - JMDM version "); System.out.println("Constructor"); } /** We'd like to be 250x250, please. */ public Dimension getPreferredSize(){ return PREFERRED_FRAME_SIZE; } /* * METHODS DEFINED BY GLEventListener */ /** Called by drawable to initiate drawing. */ public void display(GLAutoDrawable drawable) { GL gl = drawable.getGL(); int[] viewport = new int [4]; gl.glGetIntegerv (GL.GL_VIEWPORT, viewport, 0); gl.glClear(GL.GL_ACCUM_BUFFER_BIT); for (int jitter = 0; jitter < ACSIZE; jitter++) { gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glPushMatrix (); /* Note that 4.5 is the distance in world space between * left and right and bottom and top. * This formula converts fractional pixel movement to * world coordinates. */ gl.glTranslatef(j8[jitter*2]*4.5f/viewport[2], j8[jitter*2+1]*4.5f/viewport[3], 0.0f); displayObjects (gl); gl.glPopMatrix (); gl.glAccum(GL.GL_ACCUM, 1.0f/ACSIZE); } gl.glAccum (GL.GL_RETURN, 1.0f); 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) { System.out.println("init()"); GL gl = drawable.getGL(); 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)); float mat_ambient[] = { 1.0f, 1.0f, 1.0f, 1.0f }; float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; float light_position[] = { 0.0f, 0.0f, 10.0f, 1.0f }; float lm_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f }; gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, mat_ambient, 0); gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular, 0); gl.glMaterialf(GL.GL_FRONT, GL.GL_SHININESS, 50.0f); gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_position, 0); gl.glLightModelfv(GL.GL_LIGHT_MODEL_AMBIENT, lm_ambient, 0); gl.glEnable(GL.GL_LIGHTING); gl.glEnable(GL.GL_LIGHT0); gl.glEnable(GL.GL_DEPTH_TEST); gl.glShadeModel (GL.GL_FLAT); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glClearAccum(0.0f, 0.0f, 0.0f, 0.0f); // add a GLEventListener, which will get called when a // key is pressed drawable.addKeyListener(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(); if (width <= height) gl.glOrtho (-2.25f, 2.25f, -2.25f*height/width, 2.25f*height/width, -10.0f, 10.0f); else gl.glOrtho (-2.25f*width/height, 2.25f*width/height, -2.25f, 2.25f, -10.0f, 10.0f); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); } /* * OUR HELPER METHODS */ // Draw the scene objects public void displayObjects (GL gl) { float torus_diffuse[] = { 0.7f, 0.7f, 0.0f, 1.0f }; float cube_diffuse[] = { 0.0f, 0.7f, 0.7f, 1.0f }; float sphere_diffuse[] = { 0.7f, 0.0f, 0.7f, 1.0f }; float octa_diffuse[] = { 0.7f, 0.4f, 0.4f, 1.0f }; gl.glPushMatrix (); gl.glRotatef (30.0f, 1.0f, 0.0f, 0.0f); gl.glPushMatrix (); gl.glTranslatef (-0.80f, 0.35f, 0.0f); gl.glRotatef (100.0f, 1.0f, 0.0f, 0.0f); gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, torus_diffuse, 0); GLUT glut = new GLUT(); glut.glutSolidTorus (0.275f, 0.85f, 16, 16); gl.glPopMatrix (); gl.glPushMatrix (); gl.glTranslatef (-0.75f, -0.50f, 0.0f); gl.glRotatef (45.0f, 0.0f, 0.0f, 1.0f); gl.glRotatef (45.0f, 1.0f, 0.0f, 0.0f); gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, cube_diffuse, 0); glut.glutSolidCube (1.5f); gl.glPopMatrix (); gl.glPushMatrix (); gl.glTranslatef (0.75f, 0.60f, 0.0f); gl.glRotatef (30.0f, 1.0f, 0.0f, 0.0f); gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, sphere_diffuse, 0); glut.glutSolidSphere (1.0f, 16, 16); gl.glPopMatrix (); gl.glPushMatrix (); gl.glTranslatef (0.70f, -0.90f, 0.25f); gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, octa_diffuse, 0); glut.glutSolidOctahedron (); gl.glPopMatrix (); gl.glPopMatrix (); } // Methods required for the implementation of KeyListener public void keyPressed(KeyEvent e){ System.out.println("Key pressed"); if (e.getKeyChar() == 27){ System.exit(0); } } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} /** main creates and shows a Cube-JFrame */ public static void main(String[] args){ Accanti g = new Accanti(); //Set frame location g.setLocation(100,100); GLCapabilities gl_c = new GLCapabilities(); // Disable double buffer gl_c.setDoubleBuffered(false); // Set depth bits gl_c.setDepthBits(8); // Set accumulation bits gl_c.setAccumAlphaBits(8); gl_c.setAccumRedBits(8); gl_c.setAccumGreenBits(8); gl_c.setAccumBlueBits(8); // 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); } }