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: 4095
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:
 
 
 
The program demonstrating the use of explicit fog coordinates E-mail
User Rating: / 3
PoorBest 

This program demonstrates the use of explicit fog coordinates. You can press the keyboard and change the fog coordinate value at any vertex. You can also switch between using explicit fog coordinates and the default fog generation mode.

Pressing the 'f' and 'b' keys move the viewer forward and backwards. Pressing 'c' initiates the default fog generation. Pressing capital 'C' restores explicit fog coordinates. Pressing '1', '2', '3', '8', '9', and '0' add or subtract from the fog coordinate values at one of the three vertices of the triangle.

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 demonstrates the use of explicit fog coordinates. You can press
 * the keyboard and change the fog coordinate value at any vertex. You can also
 * switch between using explicit fog coordinates and the default fog generation
 * mode. <br>
 * Pressing the 'f' and 'b' keys move the viewer forward and backwards. Pressing
 * 'c' initiates the default fog generation. Pressing capital 'C' restores
 * explicit fog coordinates. Pressing '1', '2', '3', '8', '9', and '0' add or
 * subtract from the fog coordinate values at one of the three vertices of the
 * triangle.
 
 @author Kiet Le (Java port)
 */
public class fogcoord
  extends JFrame
    implements GLEventListener
    , KeyListener  
{
  private GLCapabilities caps;
  private GLCanvas canvas;
  private GLU glu;
  private GLUT glut;
  private float f1, f2, f3;
  private KeyEvent key;

  
  public fogcoord()
  {
    super("fogcoord");

    caps = new GLCapabilities();
    canvas = new GLCanvas(caps);
    canvas.addGLEventListener(this);
    canvas.addKeyListener(this);
  
    add(canvas);
  }

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

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

  /*
   * Initialize fog
   */
  public void init(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
    glu = new GLU();
    glut = new GLUT();

    float fogColor[] =
    0.0f0.25f0.25f1.0f };
    f1 = 1.0f;
    f2 = 5.0f;
    f3 = 10.0f;

    gl.glEnable(GL.GL_FOG);
    gl.glFogi(GL.GL_FOG_MODE, GL.GL_EXP);
    gl.glFogfv(GL.GL_FOG_COLOR, fogColor, 0);
    gl.glFogf(GL.GL_FOG_DENSITY, 0.25f);
    gl.glHint(GL.GL_FOG_HINT, GL.GL_DONT_CARE);
    gl.glFogi(GL.GL_FOG_COORDINATE_SOURCE_EXT, GL.GL_FOG_COORDINATE_EXT);
    gl.glClearColor(0.0f0.25f0.25f1.0f)/* fog color */

  }

  /*
   * display() draws a triangle at an angle.
   */
  public void display(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();

    gl.glClear(GL.GL_COLOR_BUFFER_BIT);

    if (key != nullswitch (key.getKeyChar()) {
      case 'c':
        gl.glFogi(GL.GL_FOG_COORDINATE_SOURCE_EXT, GL.GL_FRAGMENT_DEPTH_EXT);
        break;
      case 'C':
        gl.glFogi(GL.GL_FOG_COORDINATE_SOURCE_EXT, GL.GL_FOG_COORDINATE_EXT);
        break;

      case 'b':
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glTranslatef(0.0f0.0f, -0.25f);
        break;
      case 'f':
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glTranslatef(0.0f0.0f0.25f);
        break;
      default:
        break;
    }

    gl.glColor3f(1.0f0.75f0.0f);
    gl.glBegin(GL.GL_TRIANGLES);
    gl.glFogCoordfEXT(f1);
    gl.glVertex3f(2.0f, -2.0f0.0f);
    gl.glFogCoordfEXT(f2);
    gl.glVertex3f(-2.0f0.0f, -5.0f);
    gl.glFogCoordfEXT(f3);
    gl.glVertex3f(0.0f2.0f, -10.0f);
    gl.glEnd();

    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();

    glu.gluPerspective(45.01.00.2525.0);
    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glTranslatef(0.0f0.0f, -5.0f);
  }

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

  public void keyTyped(KeyEvent key)
  {
  }

  public void keyPressed(KeyEvent key)
  {
    this.key = key;
    switch (key.getKeyChar()) {

      case KeyEvent.VK_ESCAPE:
        System.exit(0);
        break;

      case '1':
        f1 = f1 + 0.25f;
        break;
      case '2':
        f2 = f2 + 0.25f;
        break;
      case '3':
        f3 = f3 + 0.25f;
        break;
      case '8':
        if (f1 > 0.25f1 = f1 - 0.25f;
        break;
      case '9':
        if (f2 > 0.25f2 = f2 - 0.25f;

        break;
      case '0':
        if (f3 > 0.25f3 = f3 - 0.25f;
        break;

    }
    canvas.display();
  }

  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.