/* * Copyright (c) 1993-1997, Silicon Graphics, Inc. * ALL RIGHTS RESERVED * Permission to use, copy, modify, and distribute this software for * any purpose and without fee is hereby granted, provided that the above * copyright notice appear in all copies and that both the copyright notice * and this permission notice appear in supporting documentation, and that * the name of Silicon Graphics, Inc. not be used in advertising * or publicity pertaining to distribution of the software without specific, * written prior permission. */ /* * Created on 29/07/2006 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ /* * colormat.c * After initialization, the program will be in * ColorMaterial mode. Interaction: pressing the * mouse buttons will change the diffuse reflection values. */ /** * @ported to java by Wu Shin - Ting * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.media.opengl.*; 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// { public static final Dimension PREFERRED_FRAME_SIZE = new Dimension(500, 500); private GLUT glut; private float diffuseMaterial[] = { 0.5f, 0.5f, 0.5f, 1.0f }; private boolean diffuseColorChanged = false; // /** Constructor */ public Colormat() { // init JFrame super ("Colormat - JMDM version "); System.out.println("Constructor"); } /** We'd like to be 500x500, please. */ public Dimension getPreferredSize(){ return PREFERRED_FRAME_SIZE; } public static void main(String[] args) { Colormat g = new Colormat(); //Set frame location g.setLocation(100,100); GLCapabilities gl_c = new GLCapabilities(); // Disable double buffer gl_c.setDoubleBuffered(true); // get a GLCanvas GLCanvas canvas = new GLCanvas(gl_c); // add a GLEventListener, which will get called when the // canvas is resized or needs a repaint canvas.addGLEventListener(g); //Add the key listener canvas.addKeyListener(g); //Add the mouse listener canvas.addMouseListener(g); // now add the canvas to the JFrame. Note we use BorderLayout.CENTER // to make the canvas stretch to fill the container (ie, the frame) g.getContentPane().add(canvas, BorderLayout.CENTER); g.pack(); g.setVisible(true); }// /* * Initialize values for material property, light source, lighting model, * and depth buffer. */ public void init(GLAutoDrawable drawable) { GL gl = drawable.getGL(); glut = new GLUT(); // float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; float light_position[] = { 1.0f, 1.0f, 1.0f, 0.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.0f, 10, 10); gl.glFlush(); } public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) { GL gl = drawable.getGL(); // gl.glViewport(0, 0, w, h); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); if (w <= h) gl.glOrtho(-1.5f, 1.5f, -1.5f * (float) h / (float) w, 1.5f * (float) h / (float) w, -10.0f, 10.0f); else gl.glOrtho(-1.5f * (float) w / (float) h, 1.5f * (float) w / (float) h, -1.5f, 1.5f, -10.0f, 10.0f); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); } public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { } private void changeRedDiffuse() { diffuseMaterial[0] += 0.1f; if (diffuseMaterial[0] > 1.0f) diffuseMaterial[0] = 0.0f; // gl.glColor4fv(diffuseMaterial, 0); } private void changeGreenDiffuse() { diffuseMaterial[1] += 0.1f; if (diffuseMaterial[1] > 1.0f) diffuseMaterial[1] = 0.0f; // gl.glColor4fv(diffuseMaterial, 0); } private void changeBlueDiffuse() { diffuseMaterial[2] += 0.1f; if (diffuseMaterial[2] > 1.0f) diffuseMaterial[2] = 0.0f; // gl.glColor4fv(diffuseMaterial, 0); } public void keyTyped(KeyEvent key) { } public void keyPressed(KeyEvent key) { switch (key.getKeyCode()) { 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; } ((GLCanvas)(this.getContentPane().getComponentAt(0,0))).display(); } public void mouseReleased(MouseEvent mouse) { } public void mouseEntered(MouseEvent mouse) { } public void mouseExited(MouseEvent mouse) { } }