java left logo
java middle logo
java right logo
 

Home arrow Other API Tips arrow JOGL arrow Demonstration of different types of fog in JOGL
 
 
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: 4093
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 different types of fog in JOGL E-mail
User Rating: / 8
PoorBest 

This program draws 5 red teapots, each at a different z distance from the eye, in different types of fog. Pressing the left mouse button chooses between 3 types of fog: exponential, exponential squared, and linear. In this program, there is a fixed density value, as well as fixed start and end values for the linear fog.

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


Image

-> Copyright and Permission Notice

package glredbook10;

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

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

/**
 * This program draws 5 red teapots, each at a different z distance from the
 * eye, in different types of fog. Pressing the left mouse button chooses
 * between 3 types of fog: exponential, exponential squared, and linear. In this
 * program, there is a fixed density value, as well as fixed start and end
 * values for the linear fog.
 
 @author Kiet Le (Java conversion)
 */
public class fog
  extends JFrame
    implements GLEventListener, KeyListener, MouseListener
{
  private GLU glu;
  private GLUT glut;
  private GLCapabilities caps;
  private GLCanvas canvas;
  private int fogMode;

  
  public fog()
  {
    super("fog");

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

    getContentPane().add(canvas);
  }

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

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

  /*
   * Initialize z-buffer, projection matrix, light source, and lighting model.
   * Do not specify a material property here.
   */
  public void init(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
    glu = new GLU();
    glut = new GLUT();
    //
    float position[] =
    0.0f3.0f3.0f0.0f };
    float local_view[] =
    0.0f };

    gl.glEnable(GL.GL_DEPTH_TEST);
    gl.glDepthFunc(GL.GL_LESS);

    gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, position, 0);
    gl.glLightModelfv(GL.GL_LIGHT_MODEL_LOCAL_VIEWER, local_view, 0);

    gl.glFrontFace(GL.GL_CW);
    gl.glEnable(GL.GL_LIGHTING);
    gl.glEnable(GL.GL_LIGHT0);
    gl.glEnable(GL.GL_AUTO_NORMAL);
    gl.glEnable(GL.GL_NORMALIZE);
    gl.glEnable(GL.GL_FOG);
    {
      float fogColor[] =
      0.5f0.5f0.5f1.0f };

      fogMode = GL.GL_EXP;
      gl.glFogi(GL.GL_FOG_MODE, fogMode);
      gl.glFogfv(GL.GL_FOG_COLOR, fogColor, 0);
      gl.glFogf(GL.GL_FOG_DENSITY, 0.35f);
      gl.glHint(GL.GL_FOG_HINT, GL.GL_DONT_CARE);

      gl.glClearColor(0.5f0.5f0.5f1.0f);
    }
  }

  /*
   * display() draws 5 teapots at different z positions.
   */
  public void display(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
 
    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
   
    if (fogMode == GL.GL_EXP2)
    {
      gl.glFogf(GL.GL_FOG_START, 1.0f);
      gl.glFogf(GL.GL_FOG_END, 5.0f);
    }
    gl.glFogi(GL.GL_FOG_MODE, fogMode);

    renderRedTeapot(gl, -4.0f, -0.5f, -1.0f);
    renderRedTeapot(gl, -2.0f, -0.5f, -2.0f);
    renderRedTeapot(gl, 0.0f, -0.5f, -3.0f);
    renderRedTeapot(gl, 2.0f, -0.5f, -4.0f);
    renderRedTeapot(gl, 4.0f, -0.5f, -5.0f);
    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 * 3)) gl.glOrtho(-6.06.0, -2.0 ((floath * 3(floatw,
        2.0 ((floath * 3(floatw, 0.010.0);
    else gl.glOrtho(-6.0 (floatw / ((floath * 3)//
        6.0 (floatw / ((floath * 3), -2.02.00.010.0);
    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
  }

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

  private void cycleFog()
  {
    if (fogMode == GL.GL_EXP)
    {
      fogMode = GL.GL_EXP2;
      System.out.println("Fog mode is GL.GL_EXP2");
    }
    else if (fogMode == GL.GL_EXP2)
    {
      fogMode = GL.GL_LINEAR;
      System.out.println("Fog mode is GL.GL_LINEAR\n");
    }
    else if (fogMode == GL.GL_LINEAR)
    {
      fogMode = GL.GL_EXP;
      System.out.println("Fog mode is GL.GL_EXP\n");
    }
  }

  private void renderRedTeapot(GL gl, float x, float y, float z)
  {
    float mat[] new float[4];

    gl.glPushMatrix();
    gl.glTranslatef(x, y, z);
    mat[00.1745f;
    mat[10.01175f;
    mat[20.01175f;
    mat[31.0f;
    gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, mat, 0);
    mat[00.61424f;
    mat[10.04136f;
    mat[20.04136f;
    gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, mat, 0);
    mat[00.727811f;
    mat[10.626959f;
    mat[20.626959f;
    gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat, 0);
    gl.glMaterialf(GL.GL_FRONT, GL.GL_SHININESS, 0.6f 128.0f);
    glut.glutSolidTeapot(1.0);
    gl.glPopMatrix();
  }

  public void keyTyped(KeyEvent key)
  {
  }

  public void keyPressed(KeyEvent key)
  {
    switch (key.getKeyChar()) {
      case KeyEvent.VK_ESCAPE:
        System.exit(0);
        break;

      default:
        break;
    }
  }

  public void keyReleased(KeyEvent key)
  {
  }

  public void mouseClicked(MouseEvent mouse)
  {
  }

  public void mousePressed(MouseEvent mouse)
  {
    cycleFog();
    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.