Hi, I’m new to OpenGL and have a problem, my cube is not shaded.
This is my code:
package colorpicker;
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.GLUT;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLJPanel;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
public class ThreeDPanel extends JInternalFrame implements GLEventListener, MouseListener, MouseMotionListener {
public GLCanvas canvas;
public Animator animator;
public GLUT glut;
public ThreeDPanel() {
super("ThreeDPanel");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GLCapabilities capabilities = new GLCapabilities();
capabilities.setHardwareAccelerated(true);
capabilities.setDoubleBuffered(true);
capabilities.setStencilBits(8);
capabilities.setAlphaBits (8);
capabilities.setSampleBuffers(true);
capabilities.setNumSamples(4);
canvas = new GLCanvas(capabilities);
canvas.addGLEventListener(this);
canvas.addMouseListener(this);
canvas.addMouseMotionListener(this);
canvas.setVisible(true);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(canvas, BorderLayout.CENTER);
this.setSize(300, 300);
Dimension dim = this.getToolkit().getScreenSize();
this.setLocation((int)(dim.getWidth()-300)/2, (int)(dim.getHeight()-300)/2);
animator = new Animator(canvas);
this.setVisible(true);
animator.start();
}
private float view_rotx = 20.0f, view_roty = 30.0f, view_rotz = 0.0f;
private int logo;
private int prevMouseX, prevMouseY;
private boolean mouseRButtonDown = false;
public void init(GLAutoDrawable drawable) {
// Use debug pipeline
// drawable.setGL(new DebugGL(drawable.getGL()));
GL gl = drawable.getGL();
glut = new GLUT();
System.err.println("INIT GL IS: " + gl.getClass().getName());
System.err.println("Chosen GLCapabilities: " + drawable.getContext ());
gl.setSwapInterval(1);
float mat_specular[] =
{ 1.0f, 1.0f, 1.0f, 1.0f };
float mat_shininess[] =
{ 50.0f };
float pos[] = { 1.0f, 1.0f, 1.0f, 0.0f };
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glShadeModel(GL.GL_SMOOTH);
gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular, 0);
gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, mat_shininess, 0);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, pos, 0);
gl.glEnable(GL.GL_CULL_FACE);
gl.glEnable(GL.GL_LIGHTING);
gl.glEnable(GL.GL_LIGHT0);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glEnable(GL.GL_MULTISAMPLE);
gl.glDepthFunc(GL.GL_LESS);
logo = gl.glGenLists(1);
gl.glNewList(logo, GL.GL_COMPILE);
logo(gl);
gl.glEndList();
gl.glEnable(GL.GL_NORMALIZE);
drawable.addMouseListener(this);
drawable.addMouseMotionListener(this);
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL gl = drawable.getGL();
float h = (float)height / (float)width;
gl.glMatrixMode(GL.GL_PROJECTION);
System.err.println("GL_VENDOR: " + gl.glGetString(GL.GL_VENDOR));
System.err.println("GL_RENDERER: " + gl.glGetString(GL.GL_RENDERER));
System.err.println("GL_VERSION: " + gl.glGetString(GL.GL_VERSION));
gl.glLoadIdentity();
gl.glFrustum(-1.0f, 1.0f, -h, h, 5.0f, 60.0f);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -40.0f);
}
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glColor4d(1,0,0,0);
gl.glPushMatrix();
gl.glRotatef(view_rotx, 1.0f, 0.0f, 0.0f);
gl.glRotatef(view_roty, 0.0f, 1.0f, 0.0f);
gl.glRotatef(view_rotz, 0.0f, 0.0f, 1.0f);
gl.glPushMatrix();
gl.glTranslatef(-3.0f, -2.0f, 0.0f);
gl.glRotatef(0.0f, 0.0f, 0.0f, 1.0f);
gl.glCallList(logo);
gl.glPopMatrix();
gl.glPopMatrix();
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}
public static void logo (GL gl) {
gl.glBegin(GL.GL_POLYGON);
gl.glVertex3d(-2, 10, 1);
gl.glVertex3d(-2, 0, 1);
gl.glVertex3d( 0, 0, 1);
gl.glVertex3d( 0, 10, 1);
gl.glEnd();
gl.glBegin(GL.GL_POLYGON);
gl.glVertex3d( 0, 10, -1);
gl.glVertex3d( 0, 0, -1);
gl.glVertex3d( -2, 0, -1);
gl.glVertex3d( -2, 10, -1);
gl.glEnd();
gl.glBegin(GL.GL_POLYGON);
gl.glVertex3d( 0, 10, 1);
gl.glVertex3d( 0, 0, 1);
gl.glVertex3d( 0, 0, -1);
gl.glVertex3d( 0, 10, -1);
gl.glEnd();
gl.glBegin(GL.GL_POLYGON);
gl.glVertex3d( -2, 10, 1);
gl.glVertex3d( -2, 10, -1);
gl.glVertex3d( -2, 0, -1);
gl.glVertex3d( -2, 0, 1);
gl.glEnd();
gl.glBegin(GL.GL_POLYGON);
gl.glVertex3d( -2, 0, 1);
gl.glVertex3d( -2, 0, -1);
gl.glVertex3d( 0, 0, -1);
gl.glVertex3d( 0, 0, 1);
gl.glEnd();
gl.glBegin(GL.GL_POLYGON);
gl.glVertex3d( -2, 10, 1);
gl.glVertex3d( 0, 10, 1);
gl.glVertex3d( 0, 10, -1);
gl.glVertex3d( -2, 10, -1);
gl.glEnd();
}
// Methods required for the implementation of MouseListener
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
prevMouseX = e.getX();
prevMouseY = e.getY();
if ((e.getModifiers() & e.BUTTON3_MASK) != 0) {
mouseRButtonDown = true;
}
}
public void mouseReleased(MouseEvent e) {
if ((e.getModifiers() & e.BUTTON3_MASK) != 0) {
mouseRButtonDown = false;
}
}
public void mouseClicked(MouseEvent e) {}
// Methods required for the implementation of MouseMotionListener
public void mouseDragged(MouseEvent e) {
int x = e.getX();
int y = e.getY();
Dimension size = e.getComponent().getSize();
float thetaY = 360.0f * ( (float)(x-prevMouseX)/(float)size.width);
float thetaX = 360.0f * ( (float)(prevMouseY-y)/(float)size.height);
prevMouseX = x;
prevMouseY = y;
view_rotx += thetaX;
view_roty += thetaY;
}
public void mouseMoved(MouseEvent e) {}
}
Maybe it has something to do with my JInternalFrame?/?
Any help is welcome.
Casper