java left logo
java middle logo
java right logo
 

Home arrow Other API Tips arrow JOGL arrow Demonstration of polygon offset to draw a shaded polygon and its wireframe counterpart
 
 
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: 4092
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:
 
 
 
Demonstration of polygon offset to draw a shaded polygon and its wireframe counterpart E-mail
User Rating: / 7
PoorBest 

This program demonstrates polygon offset to draw a shaded polygon and its wireframe counterpart without ugly visual artifacts ("stitching"). The example is ported from C examples in the OpenGL Programming Guide (known as the "red book").


Image

-> Copyright and Permission Notice

package glredbook11;

import java.awt.event.*;

import javax.swing.*;

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

/**
 * This program demonstrates polygon offset to draw a shaded polygon and its
 * wireframe counterpart without ugly visual artifacts ("stitching").
 
 @author Kiet Le (java port)
 */
public class polyoff
  extends JFrame
    implements GLEventListener, KeyListener, MouseListener
{
  private GLCapabilities caps;
  private GLCanvas canvas;
  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;


  public polyoff()
  {
    super("polyoff");// name of file as title in window bar
    /*
     * display mode (single buffer and RGBA)
     */
    caps = new GLCapabilities();
    caps.setDoubleBuffered(false);
    System.out.println(caps.toString());
    canvas = new GLCanvas(caps);
    canvas.addGLEventListener(this);
    canvas.addKeyListener(this);
    canvas.addMouseListener(this);

    getContentPane().add(canvas);
  }

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

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

  /*
   * specify initial properties create display list with sphere initialize
   * lighting and depth buffer
   */
  public void init(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
    glu = new GLU();
    glut = new GLUT();
    //

    float light_ambient[] =
    0.0f0.0f0.0f1.0f };
    float light_diffuse[] =
    1.0f1.0f1.0f1.0f };
    float light_specular[] =
    1.0f1.0f1.0f1.0f };
    float light_position[] =
    1.0f1.0f1.0f0.0f };

    float global_ambient[] =
    0.2f0.2f0.2f1.0f };

    gl.glClearColor(0.0f0.0f0.0f1.0f);

    list = gl.glGenLists(1);
    gl.glNewList(list, GL.GL_COMPILE);
    glut.glutSolidSphere(1.02012);
    gl.glEndList();

    gl.glEnable(GL.GL_DEPTH_TEST);

    gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, light_ambient, 0);
    gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, light_diffuse, 0);
    gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, light_specular, 0);
    gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_position, 0);
    gl.glLightModelfv(GL.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)
  {
    GL gl = drawable.getGL();

    /* clear all pixels */
    double mat_ambient[] =
    0.80.80.81.0 };
    double mat_diffuse[] =
    1.00.00.51.0 };
    double mat_specular[] =
    1.01.01.01.0 };
    float gray[] =
    0.8f0.8f0.8f1.0f };
    float black[] =
    0.0f0.0f0.0f1.0f };

    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
    gl.glPushMatrix();
    gl.glTranslated(0.00.0, tdist);
    gl.glRotated((doublespinx, 1.00.00.0);
    gl.glRotated((doublespiny, 0.01.00.0);

    gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE, gray, 0);
    gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, black, 0);
    gl.glMaterialf(GL.GL_FRONT, GL.GL_SHININESS, 0.0f);
    gl.glEnable(GL.GL_LIGHTING);
    gl.glEnable(GL.GL_LIGHT0);
    gl.glEnable(GL.GL_POLYGON_OFFSET_FILL);
    gl.glPolygonOffset(polyfactor, polyunits);
    gl.glCallList(list);
    gl.glDisable(GL.GL_POLYGON_OFFSET_FILL);

    gl.glDisable(GL.GL_LIGHTING);
    gl.glDisable(GL.GL_LIGHT0);
    gl.glColor3d(1.01.01.0);
    gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
    gl.glCallList(list);
    gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_FILL);

    gl.glPopMatrix();
    gl.glFlush();

  }

  /* call when window is resized */
  public void reshape(GLAutoDrawable drawable, int x, int y, int width,
      int height)
  {
    GL gl = drawable.getGL();
    //
    gl.glViewport(00, width, height);
    gl.glMatrixMode(GL.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluPerspective(45.0(doublewidth / (doubleheight, 1.010.0);
    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
    glu.gluLookAt(0.00.05.00.00.00.00.01.00.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.0tdist = (tdist + 0.5f);
        break;
      case 'T':
        if (tdist > -5.0tdist = (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;
    }
    canvas.display();
  }

  public void keyReleased(KeyEvent key)
  {
  }

  public void mouseClicked(MouseEvent mouse)
  {
  }

  public void mousePressed(MouseEvent mouse)
  {
    switch (mouse.getButton()) {
      case MouseEvent.BUTTON1:
        spinx = (spinx + 5360;
        break;
      case MouseEvent.BUTTON2:
        spiny = (spiny + 5360;
        break;

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

  public void mouseReleased(MouseEvent mouse)
  {
  }

  public void mouseEntered(MouseEvent mouse)
  {
  }

  public void mouseExited(MouseEvent mouse)
  {
  }

}

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.