java left logo
java middle logo
java right logo
 

Home arrow Other API Tips arrow JOGL arrow How to use glColorMaterial
 
 
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:
 
 
 
How to use glColorMaterial E-mail
User Rating: / 2
PoorBest 

glColorMaterial causes a material color to track the current color. After initialization, this program will be in ColorMaterial mode. Pressing the mouse buttons will change the color of the diffuse reflection. 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 javax.swing.text.ChangedCharSetException;

import java.nio.*;

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

/**
 * After initialization, the program will be in ColorMaterial mode. Pressing the
 * mouse buttons will change the color of the diffuse reflection.
 
 @author Kiet Le (Java conversion)
 */
public class colormat
  extends JFrame
    implements GLEventListener, KeyListener, MouseListener
{
  private GLU glu;
  private GLUT glut;
  private GLCapabilities caps;
  private GLCanvas canvas; 
  private float diffuseMaterial[] =
  0.5f0.5f0.5f1.0f };
  private boolean diffuseColorChanged = false;

 
  public colormat()
  {
    super("colormat");
    
    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 colormat().run();
  }

  /*
   * Initialize values for material property, light source, lighting model, and
   * depth buffer.
   */
  public void init(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
    glu = new GLU();
    glut = new GLUT();
  
    float mat_specular[] =
    1.0f1.0f1.0f1.0f };
    float light_position[] =
    1.0f1.0f1.0f0.0f };

    gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, diffuseMaterial, 0);
    gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular, 0);
    gl.glMaterialf(GL.GL_FRONT, GL.GL_SHININESS, 25.0f);
    gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_position, 0);

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

    gl.glColorMaterial(GL.GL_FRONT, GL.GL_DIFFUSE);
    gl.glEnable(GL.GL_COLOR_MATERIAL);
  }

  public void display(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
 
    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

    if (diffuseColorChanged)
    {
      gl.glColor4fv(diffuseMaterial, 0);
      diffuseColorChanged = !diffuseColorChanged;
    }

    glut.glutSolidSphere(1.0f1010);
    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 <= hgl.glOrtho(-1.51.5, -1.5 (floath / (floatw,
        1.5 (floath / (floatw, -10.010.0);
    else gl.glOrtho(-1.5 (floatw / (floath, 1.5 (floatw / (floath,
        -1.51.5, -10.010.0);
    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
  }

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

  private void changeRedDiffuse()
  {
    diffuseMaterial[0+= 0.1;
    if (diffuseMaterial[01.0diffuseMaterial[00.0f;
    // gl.glColor4fv(diffuseMaterial, 0);
  }

  private void changeGreenDiffuse()
  {
    diffuseMaterial[1+= 0.1;
    if (diffuseMaterial[11.0diffuseMaterial[10.0f;
    // gl.glColor4fv(diffuseMaterial, 0);
  }

  private void changeBlueDiffuse()
  {
    diffuseMaterial[2+= 0.1;
    if (diffuseMaterial[21.0diffuseMaterial[20.0f;
    // gl.glColor4fv(diffuseMaterial, 0);
  }

  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)
  {
    switch (mouse.getButton()) {
      case MouseEvent.BUTTON1:
        changeRedDiffuse();
        diffuseColorChanged = true;
        break;
      case MouseEvent.BUTTON2:
        changeGreenDiffuse();
        diffuseColorChanged = true;
        break;
      case MouseEvent.BUTTON3:
        changeBlueDiffuse();
        diffuseColorChanged = true;
        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.