java left logo
java middle logo
java right logo
 

Home arrow Other API Tips arrow JOGL arrow A model with a light which is moved by a modeling transformation
 
 
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: 4089
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:
 
 
 
A model with a light which is moved by a modeling transformation E-mail
User Rating: / 8
PoorBest 

This program demonstrates when to issue lighting and transformation commands to render a model with a light which is moved by a modeling transformation (rotate or translate). The light position is reset after the modeling transformation is called. The eye position does not change.

A sphere is drawn using a grey material characteristic. A single light source illuminates the object.

Interaction: pressing the left or middle mouse button alters the modeling transformation (x rotation) by 30 degrees. The scene is then redrawn with the light in a new position.

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.*;
import java.awt.event.*;

import javax.swing.*;

import java.nio.*;

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

/**
 * This program demonstrates when to issue lighting and transformation commands
 * to render a model with a light which is moved by a modeling transformation
 * (rotate or translate). The light position is reset after the modeling
 * transformation is called. The eye position does not change. <br>
 <br>
 * A sphere is drawn using a grey material characteristic. A single light source
 * illuminates the object. <br>
 <br>
 * Interaction: pressing the left or middle mouse button alters the modeling
 * transformation (x rotation) by 30 degrees. The scene is then redrawn with the
 * light in a new position.
 
 @author Kiet Le (Java port)
 */
public class movelight
  extends JFrame
    implements GLEventListener, KeyListener, MouseListener,
    MouseMotionListener, MouseWheelListener
{
  private GLU glu;
  private GLUT glut;
  private GLCapabilities caps;
  private GLCanvas canvas;
  private FPSAnimator animator;

  private static int spin = 0;


  public movelight()
  {
    super("movelight");
   
    caps = new GLCapabilities();
    canvas = new GLCanvas(caps);
    canvas.addGLEventListener(this);
    canvas.addKeyListener(this);
    canvas.addMouseListener(this);
    
    getContentPane().add(canvas);
  }

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

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

  public void init(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
    glu = new GLU();
    glut = new GLUT();
   
    gl.glEnable(GL.GL_LIGHTING);
    gl.glEnable(GL.GL_LIGHT0);
    gl.glDepthFunc(GL.GL_LESS);
    gl.glEnable(GL.GL_DEPTH_TEST);
  }

  /*
   * Here is where the light position is reset after the modeling transformation
   * (glRotated) is called. This places the light at a new position in world
   * coordinates. The cube represents the position of the light.
   */
  public void display(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
    
    float position[] =
    0.0f0.0f1.5f1.0f };

    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
    gl.glPushMatrix();
    gl.glTranslatef(0.0f0.0f, -5.0f);

    gl.glPushMatrix();
    gl.glRotated((doublespin, 1.00.00.0);
    gl.glRotated(0.01.00.00.0);
    gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, position, 0);

    gl.glTranslated(0.00.01.5);
    gl.glDisable(GL.GL_LIGHTING);
    gl.glColor3f(0.0f1.0f1.0f);
    glut.glutWireCube(0.1f);
    gl.glEnable(GL.GL_LIGHTING);
    gl.glPopMatrix();

    glut.glutSolidTorus(0.275f0.85f2020);
    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();
    glu.gluPerspective(40.0(floatw / (floath, 1.020.0);
    gl.glMatrixMode(GL.GL_MODELVIEW);
  }

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

  private void movelight()
  {
    spin = (spin + 30360;
  }

  public void keyTyped(KeyEvent key)
  {
  }

  public void keyPressed(KeyEvent key)
  {
    char ch = key.getKeyChar();
    switch (ch) {
      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)
  {
    if (mouse.getButton() == MouseEvent.BUTTON1//
    movelight();
    canvas.display();
  }

  public void mouseReleased(MouseEvent mouse)
  {
  }

  public void mouseEntered(MouseEvent mouse)
  {
  }

  public void mouseExited(MouseEvent mouse)
  {
  }

  public void mouseDragged(MouseEvent mouse)
  {
  }

  public void mouseMoved(MouseEvent mouse)
  {
  }

  public void mouseWheelMoved(MouseWheelEvent wheel)
  {
  }
}

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.