java left logo
java middle logo
java right logo
 

Home arrow Other API Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4097
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to use multisampling to draw anti-aliased geometric primitives E-mail
User Rating: / 5
PoorBest 

This program draws shows how to use multisampling to draw anti-aliased geometric primitives. The same display list, a pinwheel of triangles and lines of varying widths, is rendered twice. Multisampling is enabled when the left side is drawn. Multisampling is disabled when the right side is drawn.

Pressing the 'b' key toggles drawing of the checkerboard background. Antialiasing is sometimes easier to see when objects are rendered over a contrasting background.

The example is ported from C examples in the OpenGL Programming Guide (known as the "red book").


Image

-> Copyright and Permission Notice

package glredbook1314;

import java.awt.event.*;
import javax.swing.*;

import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*;

/**
 * This program draws shows how to use multisampling to draw anti-aliased
 * geometric primitives. The same display list, a pinwheel of triangles and
 * lines of varying widths, is rendered twice. Multisampling is enabled when the
 * left side is drawn. Multisampling is disabled when the right side is drawn.
 <br>
 * Pressing the 'b' key toggles drawing of the checkerboard background.
 * Antialiasing is sometimes easier to see when objects are rendered over a
 * contrasting background.
 */
public class multisamp
  extends JFrame
    implements GLEventListener
    , KeyListener 
{
  private GLCapabilities caps;
  private GLCanvas canvas;
  private GLU glu;
  private GLUT glut;
  private boolean bgtoggle = true;


  public multisamp()
  {
    super("mulisamp");

    caps = new GLCapabilities();
    caps.setSampleBuffers(true);
    caps.setNumSamples(2);
    canvas = new GLCanvas(caps);
    canvas.addGLEventListener(this);
    canvas.addKeyListener(this);

    add(canvas);
  }

  public void run()
  {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(600300);
    setLocationRelativeTo(null);
    setVisible(true);
    canvas.requestFocusInWindow();
  }

  public static void main(String[] args)
  {
    new multisamp().run();
  }

  /*
   * Print out state values related to multisampling. Create display list with
   * "pinwheel" of lines and triangles.
   */
  public void init(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
    glu = new GLU();
    glut = new GLUT();
    //
    int buf[] new int[1];
    int sbuf[] new int[1];

    gl.glClearColor(0.0f0.0f0.0f0.0f);
    gl.glGetIntegerv(GL.GL_SAMPLE_BUFFERS, buf, 0);
    System.out.println("number of sample buffers is " + buf[0]);
    gl.glGetIntegerv(GL.GL_SAMPLES, sbuf, 0);
    System.out.println("number of samples is " + sbuf[0]);

    gl.glNewList(1, GL.GL_COMPILE);
    for (int i = 0; i < 19; i++)
    {
      gl.glPushMatrix();
      gl.glRotatef(360.0f (floati / 19.0f0.0f0.0f1.0f);
      gl.glColor3f(1.0f1.0f1.0f);
      gl.glLineWidth((i % 31.0f);
      gl.glBegin(GL.GL_LINES);
      gl.glVertex2f(0.25f0.05f);
      gl.glVertex2f(0.9f0.2f);
      gl.glEnd();
      gl.glColor3f(0.0f1.0f1.0f);
      gl.glBegin(GL.GL_TRIANGLES);
      gl.glVertex2f(0.25f0.0f);
      gl.glVertex2f(0.9f0.0f);
      gl.glVertex2f(0.875f0.10f);
      gl.glEnd();
      gl.glPopMatrix();
    }
    gl.glEndList();

    gl.glNewList(2, GL.GL_COMPILE);
    gl.glColor3f(1.0f0.5f0.0f);
    gl.glBegin(GL.GL_QUADS);
    for (int i = 0; i < 16; i++)
      for (int j = 0; j < 16; j++)
        if (((i + j2== 0)
        {
          gl.glVertex2f(-2.0f (i * 0.25f), -2.0f (j * 0.25f));
          gl.glVertex2f(-2.0f (i * 0.25f), -1.75f (j * 0.25f));
          gl.glVertex2f(-1.75f (i * 0.25f), -1.75f (j * 0.25f));
          gl.glVertex2f(-1.75f (i * 0.25f), -2.0f (j * 0.25f));
        }

    gl.glEnd();
    gl.glEndList();
  }

  /*
   * Draw two sets of primitives, so that you can compare the user of
   * multisampling against its absence. This code enables antialiasing and draws
   * one display list and disables and draws the other display list
   */
  public void display(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
    gl.glClear(GL.GL_COLOR_BUFFER_BIT);
    //
    if (bgtogglegl.glCallList(2);

    gl.glEnable(GL.GL_MULTISAMPLE);
    gl.glPushMatrix();
    gl.glTranslatef(-1.0f0.0f0.0f);
    gl.glCallList(1);
    gl.glPopMatrix();

    gl.glDisable(GL.GL_MULTISAMPLE);
    gl.glPushMatrix();
    gl.glTranslatef(1.0f0.0f0.0f);
    gl.glCallList(1);
    gl.glPopMatrix();

    gl.glFlush();
  }

  public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h)
  {
    GL gl = drawable.getGL();
    //
    gl.glViewport(00, w, h);
    gl.glMatrixMode(GL.GL_PROJECTION);
    gl.glLoadIdentity();
    if (w <= (* h)) glu.gluOrtho2D(-2.02.0//
        -2.0 (floath / (floatw, 2.0 (floath / (floatw);
    else glu.gluOrtho2D(-2.0 (floatw / (floath, //
        2.0 (floatw / (floath, -2.02.0);
    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
  }

  public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
      boolean deviceChanged)
  {
  }

  public void keyPressed(KeyEvent key)
  {
    switch (key.getKeyChar()) {

      case KeyEvent.VK_ESCAPE:
        System.exit(0);
        break;
      case 'b':
      case 'B':
        bgtoggle = !bgtoggle;
        break;

      default:
        break;
    }
    canvas.display();
  }

  public void keyTyped(KeyEvent key)
  {
  }

  public void keyReleased(KeyEvent key)
  {
  }

}

Source: Kiet Le's The Red Book Examples using JOGL


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.