I’m very new to JOGL, but I have been doing many tutorials.
I currently have a large box and some motion controls for movement and mouse looking.
My problem is that when I add a point light using:
private final float light_position[] = { 0.0f, 10.0f, 0.0f, 1.0f };
and
gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, light_position);
I expect a point light in the center of the cube 10 units up on the Y axis.
However the cube is not lit in a way I expected, instead one side of the cube is lit while the other is not.
I have attached a screenshot displaying the gradiation from the dark side to the lighter side.
Below is the relevant source code:
*Also any general suggestions not related to my problem would also be much appreciated.
Thank you,
Bryanv
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JFrame;
import net.java.games.jogl.GL;
import net.java.games.jogl.GLDrawable;
import net.java.games.jogl.GLEventListener;
import net.java.games.jogl.GLU;
public class JOGL extends JFrame implements GLEventListener, KeyListener, MouseMotionListener {
private static final long serialVersionUID = 1L;
private GL gl;
private GLU glu;
private static final float piover180 = 0.0174532925f;
private float[] camera_pos = { 0, 0, 0 };
private float[] camera_rot = { 0, 0, 0 };
private float walk_bounce_angle = 0f;
private final float light_ambient[] = { 0.3f, 0.3f, 0.3f, 1.0f };
private final float light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
private final float light_position[] = { 0.0f, 10.0f, 0.0f, 1.0f }; // what should give the correct result but doesn't
//private final float light_position[] = { 0.0f, 10.0f, 200.0f, 1.0f }; // gives what appears to be the correct result
private ArrayList<Geometry> objects;
private HashMap<String, Integer> textures;
private Point canvas_center;
private Robot robot;
public JOGL(JOGLMain main) {
this.camera_pos = new float[] { 0, 0, 0 };
this.objects = new ArrayList<Geometry>();
this.textures = new HashMap<String, Integer>();
try {
robot = new Robot();
} catch (Exception e) {
e.printStackTrace();
}
}
public void lighting() {
gl.glLightfv(GL.GL_LIGHT1, GL.GL_AMBIENT, light_ambient);
gl.glLightfv(GL.GL_LIGHT1, GL.GL_DIFFUSE, light_diffuse);
gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, light_position);
gl.glEnable(GL.GL_LIGHT1);
}
public void display(final GLDrawable drawable) {
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
glu.gluLookAt(0.0f, 5.0f, 0.0f, 0.0f, 5.0f, 1.0f, 0.0f, 1.0f, 0.0f);
gl.glRotatef(-this.camera_rot[0], 1.0f, 0.0f, 0.0f);
gl.glRotatef(this.camera_rot[1], 0.0f, 1.0f, 0.0f);
gl.glTranslatef(this.camera_pos[0], this.camera_pos[1], this.camera_pos[2]);
lighting();
gl.glFlush();
for (int i = 0; i < this.objects.size(); i++) {
(this.objects.get(i)).draw(gl);
}
gl.glFlush();
}
public void displayChanged(final GLDrawable drawable, final boolean modeChanged, final boolean deviceChanged) {
}
public void init(final GLDrawable drawable) {
gl = drawable.getGL();
glu = drawable.getGLU();
gl.glShadeModel(GL.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClearDepth(1.0f);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
gl.glEnable(GL.GL_LIGHTING);
loadTexture("bryan.png");
this.objects.add(new Box(100, 0, 0, 0, textures.get("bryan.png")));
this.objects.add(new Box(1, 0, 0, -90, textures.get("bryan.png")));
gl.glEnable(GL.GL_TEXTURE_2D);
drawable.addKeyListener(this);
drawable.addMouseMotionListener(this);
}
public void reshape(final GLDrawable drawable, final int x, final int y, final int width, final int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, ((float) width) / ((float) height), 1.0f, 5000.0f);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
canvas_center = new Point(width / 2, height / 2);
robot.mouseMove((int) canvas_center.x, (int) canvas_center.y);
}
public void loadTexture(String filename) {
int textureID = createTextureID();
gl.glBindTexture(GL.GL_TEXTURE_2D, textureID);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
TextureReader.Texture texture = null;
try {
texture = TextureReader.readTexture("textures/" + filename);
} catch (final Exception e) {
e.printStackTrace();
}
makeRGBTexture(texture, GL.GL_TEXTURE_2D, false);
textures.put(filename, textureID);
}
private int createTextureID() {
int[] id = new int[1];
gl.glGenTextures(1, id);
return id[0];
}
private void makeRGBTexture(final TextureReader.Texture img, final int target, final boolean mipmapped) {
if (mipmapped) {
glu.gluBuild2DMipmaps(target, GL.GL_RGB8, img.getWidth(), img.getHeight(), GL.GL_RGB, GL.GL_UNSIGNED_BYTE, img.getPixels());
} else {
gl.glTexImage2D(target, 0, GL.GL_RGB, img.getWidth(), img.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, img.getPixels());
}
}
public void keyPressed(final KeyEvent event) {
if (event.getKeyCode() == KeyEvent.VK_ESCAPE) {
JOGLMain.quit = true;
System.exit(0);
}
if (event.getKeyChar() == 'w') {
moveZ(1);
}
if (event.getKeyChar() == 's') {
moveZ(-1);
}
if (event.getKeyChar() == 'a') {
moveX(-1);
}
if (event.getKeyChar() == 'd') {
moveX(1);
}
if (event.getKeyCode() == KeyEvent.VK_UP) {
this.camera_pos[1] -= .3f;
}
if (event.getKeyCode() == KeyEvent.VK_DOWN) {
this.camera_pos[1] += .3f;
}
}
public void keyReleased(final KeyEvent event) {
}
public void keyTyped(final KeyEvent event) {
}
public void mouseDragged(MouseEvent event) {
}
void moveZ(float direction) {
camera_pos[0] += direction * Math.sin(camera_rot[1] * piover180) * 1;
camera_pos[2] -= direction * Math.cos(camera_rot[1] * piover180) * 1;
if (walk_bounce_angle <= 1.0f)
walk_bounce_angle = 359.0f;
else
walk_bounce_angle += direction * 100;
camera_pos[1] -= direction * Math.sin(walk_bounce_angle * piover180) * .07;
}
void moveX(float direction) {
camera_pos[0] += direction * Math.cos(camera_rot[1] * piover180) * 1;
camera_pos[2] += direction * Math.sin(camera_rot[1] * piover180) * 1;
}
public void mouseMoved(MouseEvent event) {
int delta_x = event.getPoint().x - canvas_center.x;
int delta_y = event.getPoint().y - canvas_center.y;
this.camera_rot[1] += delta_x * .1;
this.camera_rot[0] += delta_y * .1;
robot.mouseMove((int) canvas_center.x, (int) canvas_center.y);
}
}