Ok. I was correctly an idiot. I did not have normals for my cube points and was rotating the defaults, not the light. Now I am trying to figure out how to use normal array with glNormalPointer and my vertexarray. Not sure how many points I need to set in my normal array. 1 per polygon face, or 1 per vertex. I’ve tried it with and without the comment for loops. Both with bizarre behavior. This is my entire code incase someone wants to try it and tell me something. Otherwise, I hope to have another answer for myself shortly?
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.FloatBuffer;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLJPanel;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.BufferUtil;
class joglsimple implements ActionListener, GLEventListener
{
GLU glu;
GLCanvas canvas;
FloatBuffer cubePoints;
FloatBuffer cubeNormals;
Animator animator;
float rotatex = 0f, rotatez = 0f;
joglsimple()
{
cubePoints = buildCubeArray();
cubeNormals = buildCubeNormals();
}
public FloatBuffer buildCubeNormals()
{
FloatBuffer fb = BufferUtil.newFloatBuffer(72);
float [] bottom = { 0, 0, -1 };
float [] top = { 0, 0, 1 };
float [] front = { 0, 1, 0 };
float [] back = { 0, -1, 0 };
float [] left = { -1, 0, 0 };
float [] right = { 1, 0, 0 };
//for ( int i = 0; i < 4; i ++)
fb.put(top);
//for ( int i = 0; i < 4; i ++)
fb.put(bottom);
//for ( int i = 0; i < 4; i ++)
fb.put(right);
//for ( int i = 0; i < 4; i ++)
fb.put(left);
//for ( int i = 0; i < 4; i ++)
fb.put(front);
//for ( int i = 0; i < 4; i ++)
fb.put(back);
fb.rewind();
return fb;
}
public FloatBuffer buildCubeArray()
{
FloatBuffer fb = BufferUtil.newFloatBuffer(72);
float i = 0.2f;
float [] bottom = { -i, i, -i,
-i, -i, -i,
i, -i, -i,
i, i, -i};
float [] front = { -i, i, -i,
-i, i, i,
i, i, i,
i, i, -i
};
float [] back = { -i, -i, -i,
-i, -i, i,
i, -i, i,
i, -i, -i
};
float [] top = { -i, i, i,
-i, -i, i,
i, -i, i,
i, i, i
};
float [] left = { -i, i, -i,
-i, -i, -i,
-i, -i, i,
-i, i, i
};
float [] right = { i, i, -i,
i, -i, -i,
i, -i, i,
i, i, i
};
fb.put(top);
fb.put(bottom);
fb.put(right);
fb.put(left);
fb.put(front);
fb.put(back);
fb.rewind();
return fb;
}
public void actionPerformed(ActionEvent e) {
}
public Container createMainPanel()
{
JPanel panel = new JPanel();
canvas = new GLCanvas();
canvas.setPreferredSize(new Dimension(600,600));
canvas.addGLEventListener(this);
animator = new Animator(canvas);
animator.start();
panel.add(canvas);
return panel;
}
public static void displayApp()
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("swing app jframe JFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
joglsimple jsimp = new joglsimple();
frame.setContentPane(jsimp.createMainPanel());
frame.pack();
frame.setVisible(true);
}
public static void main(String [] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
displayApp();
/* create and show the GUI */
}
});
}
public void display(GLAutoDrawable gld)
{
float [] light_position = { 0f, 0f, 10f, 0f };
GL gl = gld.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
glu.gluLookAt(0, 0, 2f, 0f, 0f, 0f, 0f, 1f, 0f);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_position, 0);
gl.glPushMatrix();
gl.glColor3f(1.0f, 0f, 0f);
gl.glRotatef(rotatex, 1.0f, 0f, 0f);
gl.glRotatef(rotatez, 0f, 0f, 1.0f);
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL.GL_FLOAT, 0, cubePoints);
gl.glEnableClientState(GL.GL_NORMAL_ARRAY);
gl.glNormalPointer(GL.GL_FLOAT, 0, cubeNormals);
for (int i = 0; i < 6; i ++)
{
gl.glDrawArrays(GL.GL_POLYGON, i * 4, 4);
}
gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL.GL_NORMAL_ARRAY);
gl.glPopMatrix();
gl.glPushMatrix();
gl.glColor3f(0f, 1.0f, 0f);
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL.GL_FLOAT, 0, cubePoints);
gl.glEnableClientState(GL.GL_NORMAL_ARRAY);
gl.glNormalPointer(GL.GL_FLOAT, 0, cubeNormals);
gl.glTranslatef(0.5f, 0.5f, 0f);
gl.glScalef(0.5f, 0.5f, 0.5f);
gl.glRotatef(-(rotatex/2f), 1.0f, 0f, 0f);
gl.glRotatef(-(rotatez/2f), 0f, 0f, 1.0f);
for (int i = 0; i < 6; i ++)
{
gl.glDrawArrays(GL.GL_POLYGON, i * 4, 4);
}
gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL.GL_NORMAL_ARRAY);
gl.glPopMatrix();
rotatex += 0.5f;
rotatez += 0.8f;
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
{
GL gl = drawable.getGL();
height = (height == 0) ? 1 : height;
gl.glViewport(0, 0, width, height); // Reset The Current Viewport
gl.glMatrixMode(GL.GL_PROJECTION); // Select The Projection Matrix
gl.glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
// apply projection matrix for viewing volume
glu.gluPerspective(50.0f, (float) width / height, 0f, 50.0f);
gl.glMatrixMode(GL.GL_MODELVIEW); // Select The Modelview Matrix
gl.glLoadIdentity();
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
{
}
public void init(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
this.glu = new GLU();
//drawable.setGL(new DebugGL(drawable.getGL()));
gl.glClearColor(0, 0, 0, 0);
gl.glShadeModel(GL.GL_SMOOTH);
float [] mat_specular = { 1.0f, 1.0f, 1.0f, 1.0f };
float [] mat_shininess = { 50.0f };
float [] light_position = { 0f, 0f, 10f, 0f };
float [] white_light = { 1.0f, 1.0f, 1.0f, 1.0f };
float [] model_ambient = { 0.1f, 0.1f, 0.1f, 1.0f };
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, light_position, 0);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, white_light, 0);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, white_light, 0);
gl.glLightModelfv(GL.GL_LIGHT_MODEL_AMBIENT, model_ambient, 0);
gl.glEnable(GL.GL_LIGHTING);
gl.glEnable(GL.GL_LIGHT0);
gl.glEnable(GL.GL_DEPTH_TEST);
System.out.println("Init GL is " + gl.getClass().getName());
}
}