My best guess (without being able to compile and run your code), is to call positionLight() before drawAxes().
I tried not drawing the axes at all, but the effect is the same.
It's also possible that the normals on your floor are wrong (causing the light to look like it is facing that direction).
I have enabled GL_CULL_FACE, so my assumption is that if I can see the shape, then the normal is in the right direction. Is this a correct assumption?
Other then that, it's hard for us to guess without the rest of your code.
If it will help the cause, I will gladly post all my code so that you may run this.
// WINDOW class
package com.bacon.lwjgl.executable;
import com.bacon.lwjgl.Game;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
public class Window {
public static final String WINDOW_TITLE = "TEST-lwjgl";
public void start() {
Game game = new Game();
try {
Display.setDisplayMode(new DisplayMode((int) game.getWidth(), (int) game.getHeight()));
Display.setTitle(WINDOW_TITLE);
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
game.init();
while(!Display.isCloseRequested()) {
game.gameLoopBody();
Display.sync(60);
Display.update();
}
Display.destroy();
}
public static void main(String[] args) {
Window window = new Window();
window.start();
}
}
// GAME class
package com.bacon.lwjgl;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.gluLookAt;
import static org.lwjgl.util.glu.GLU.gluPerspective;
public class Game {
// defaults
public static final float DEFAULT_WIDTH = 800.0f;
public static final float DEFAULT_HEIGHT = 600.0f;
public static final double MOUSE_SENSITIVITY = 0.0005;
public static final float LOOK_FIELD = 70.0f;
// time related fields
private long lastTime;
// other
private Player player;
private float width;
private float height;
public Game(float width, float height) {
this.player = new Player();
this.width = width;
this.height = height;
}
public Game() {
this(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
private int getDelta() {
long time = getTime();
int delta = (int) (time - lastTime);
lastTime = time;
return delta;
}
private long getTime() {
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
public void init() {
initGL();
lastTime = getTime();
}
public void gameLoopBody() {
poll(getDelta());
draw();
}
public void initGL() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(LOOK_FIELD, width / height, 1.0f, Player.LOOK_DISTANCE);
glMatrixMode(GL_MODELVIEW);
Artist.initLighting();
//glColorMaterial (GL_FRONT, GL_EMISSION) ;
//glEnable (GL_COLOR_MATERIAL) ;
}
public void draw() {
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Artist.drawHUD(width, height);
gluLookAt(player.getX(), player.getY(), player.getZ(),
player.getTargetX(), player.getTargetY(), player.getTargetZ(),
0.0f, 1.0f, 0.0f);
Artist.drawAxes();
Artist.drawFloor();
//Artist.drawCircleApproximation(4.0f, 20);
Artist.drawMainShape();
Artist.positionLight();
}
/**
* Polls controller
* @param delta - millis since last frame
*/
public void poll(int delta) {
// only perform operations if Mouse is grabbed
if (Mouse.isGrabbed()) {
// move player
pollKeyboard(delta);
// move target
pollMouse(delta);
} else if (Mouse.isButtonDown(0)) {
Mouse.setGrabbed(true);
}
}
/**
* Poll for mouse movement
* @param delta - millis since last frame
*/
private void pollMouse(int delta) {
int mouseDX = Mouse.getDX();
int mouseDY = Mouse.getDY();
if (mouseDX != 0) {
player.rotateHorizontal(mouseDX * MOUSE_SENSITIVITY, delta);
}
if (mouseDY != 0) {
player.rotateVertical(mouseDY * MOUSE_SENSITIVITY, delta);
}
}
/**
* Poll keyboard for key presses
* @param delta - millis since last frame
*/
private void pollKeyboard(int delta) {
// hold keys
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
Mouse.setGrabbed(false);
return;
}
if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
player.moveForward(delta);
}
if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
player.moveBackward(delta);
}
if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
player.moveLeft(delta);
}
if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
player.moveRight(delta);
}
// press once keys
while(Keyboard.next()) {
if (Keyboard.getEventKey() == Keyboard.KEY_F11) {
if (Keyboard.getEventKeyState()) {
glPolygonMode(GL_FRONT, GL_LINE);
}
} else if (Keyboard.getEventKey() == Keyboard.KEY_F12) {
if (Keyboard.getEventKeyState()) {
glPolygonMode(GL_FRONT, GL_FILL);
}
}
}
}
}
// PLAYER class
package com.bacon.lwjgl;
public class Player {
// constants
public static final float LOOK_DISTANCE = 100.0f;
public static final float MOVE_SPEED = 0.02f;
// defaults
public static final float DEFAULT_STARTING_X = 5.0f;
public static final float DEFAULT_STARTING_Z = 1.0f;
public static final float DEFAULT_PLAYER_HEIGHT = 2.0f;
public static final double DEFAULT_STARTING_HOR_ANGLE = Math.PI;
public static final double DEFAULT_STARTING_VER_ANGLE = 0.0;
// position fields
private float x;
private float y;
private float z;
// lookingAt fields
private double theta;
private double sigma;
private float targetX;
private float targetY;
private float targetZ;
// player properties
private int hp;
public Player() {
x = DEFAULT_STARTING_X;
y = DEFAULT_PLAYER_HEIGHT;
z = DEFAULT_STARTING_Z;
theta = DEFAULT_STARTING_HOR_ANGLE;
sigma = DEFAULT_STARTING_VER_ANGLE;
updateTarget();
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public float getZ() {
return z;
}
public float getTargetX() {
return targetX;
}
public float getTargetY() {
return targetY;
}
public float getTargetZ() {
return targetZ;
}
// moving commands
public void rotateHorizontal(double theta, int delta) {
this.theta += theta * delta;
// keep theta in range 0 - 2Pi
double twoPI = 2.0 * Math.PI;
if (this.theta < 0 || this.theta >= twoPI) {
this.theta = this.theta % twoPI;
}
updateTarget();
}
public void rotateVertical(double sigma, int delta) {
this.sigma += sigma * delta;
if (this.sigma >= Math.PI/2.0) {
// surpassed maximum vertical rotation
this.sigma = Math.PI/2.0 - 0.001;
} else if (this.sigma <= -Math.PI/2.0) {
// surpassed minimum vertical rotation
this.sigma = -Math.PI/2.0 + 0.001;
}
updateTarget();
}
public void moveForward(int delta) {
x += Math.cos(theta) * MOVE_SPEED * delta;
z += Math.sin(theta) * MOVE_SPEED * delta;
updateTarget();
}
public void moveBackward(int delta) {
x -= Math.cos(theta) * MOVE_SPEED * delta;
z -= Math.sin(theta) * MOVE_SPEED * delta;
updateTarget();
}
public void moveLeft(int delta) {
double theta = this.theta + Math.PI/2; // move 90 degrees the left
x -= Math.cos(theta) * MOVE_SPEED * delta;
z -= Math.sin(theta) * MOVE_SPEED * delta;
updateTarget();
}
public void moveRight(int delta) {
double theta = this.theta + Math.PI/2; // move 90 degrees the right
x += Math.cos(theta) * MOVE_SPEED * delta;
z += Math.sin(theta) * MOVE_SPEED * delta;
updateTarget();
}
private void updateTarget() {
/*
x = r * cos(theta) * cos(sigma)
y = r * sin(theta) * cos(sigma)
z = r * sin(sigma)
*/
targetX = x + LOOK_DISTANCE * new Float(Math.cos(theta) * Math.cos(sigma));
targetY = LOOK_DISTANCE * new Float(Math.sin(sigma)) + DEFAULT_PLAYER_HEIGHT;
targetZ = z + LOOK_DISTANCE * new Float(Math.sin(theta) * Math.cos(sigma));
}
}
// ARTIST class (bad name)
package com.bacon.lwjgl;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import java.nio.FloatBuffer;
import static org.lwjgl.opengl.GL11.*;
public class Artist {
public static void drawHUD(float width, float height) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, width, height, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
float midX = width/2.0f;
float midY = height/2.0f;
float lineLength = height/50.0f;
glBegin(GL_LINES);
glColor3f(1.0f, 1.0f, 1.0f);
// x
glVertex2f(midX-lineLength/2-lineLength, midY);
glVertex2f(midX-lineLength/2, midY);
glVertex2f(midX+lineLength/2+lineLength, midY);
glVertex2f(midX+lineLength/2, midY);
// y
glVertex2f(midX, midY-lineLength/2-lineLength);
glVertex2f(midX, midY-lineLength/2);
glVertex2f(midX, midY+lineLength/2+lineLength);
glVertex2f(midX, midY+lineLength/2);
glEnd();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
public static void drawMainShape() {
glBegin(GL_TRIANGLES);
// face 1
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 10.0f, 5.0f);
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 10.0f, 10.0f);
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 5.0f, 10.0f);
// face 2
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(-2.5f, 10.0f, 5.0f);
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(-2.5f, 5.0f, 10.0f);
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(-2.5f, 10.0f, 10.0f);
// squ 1a
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 10.0f, 5.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 5.0f, 10.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-2.5f, 5.0f, 10.0f);
// are 1b
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-2.5f, 5.0f, 10.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-2.5f, 10.0f, 5.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 10.0f, 5.0f);
// squ 2a
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-2.5f, 5.0f, 10.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 5.0f, 10.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 10.0f, 10.0f);
// are 2b
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 10.0f, 10.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-2.5f, 10.0f, 10.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-2.5f, 5.0f, 10.0f);
glEnd();
}
public static void drawAxes() {
glBegin(GL_LINES);
// x-axis
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-10000.0f, 0.0f, 0.0f);
glVertex3f(10000.0f, 0.0f, 0.0f);
// y-axis
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, -10000.0f, 0.0f);
glVertex3f(0.0f, 10000.0f, 0.0f);
// z-axis
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, -10000.0f);
glVertex3f(0.0f, 0.0f, 10000.0f);
glEnd();
}
public static void drawFloor() {
float sizeOfEdge = 1.0f;
float red = 0.0f;
float green = 0.0f;
glBegin(GL_QUADS);
for (float x = 50.0f; x > -50.0f; x -= sizeOfEdge) {
for (float z = 50.0f; z > -50.0f; z -= sizeOfEdge) {
glColor3f(red, green, 1.0f);
glVertex3f(x, 0.0f, z);
glVertex3f(x, 0.0f, z-sizeOfEdge);
glVertex3f(x-sizeOfEdge, 0.0f, z-sizeOfEdge);
glVertex3f(x-sizeOfEdge, 0.0f, z);
//red = (red+0.1f) % 1.1f;
}
//red = 0.0f;
//green = (green+0.1f) % 1.1f;
}
glEnd();
}
public static void drawCircleApproximation(float radius, int numberOfSides) {
glBegin(GL_TRIANGLE_FAN);
glVertex2f(0.0f, 0.0f);
for(double theta = 0.0; theta < 2*Math.PI; theta += 2*Math.PI/numberOfSides) {
glVertex2f(new Float(Math.cos(theta))*radius, new Float(Math.sin(theta))*radius);
}
glVertex2f(new Float(Math.cos(0.0)) * radius, new Float(Math.sin(0.0)) * radius);
glEnd();
}
public static void initLighting() {
float[] ambientV = {0.5f, 0.5f, 0.5f, 1.0f};
float[] diffuseV = {1.0f, 0.0f, 0.0f, 1.0f};
//float[] blackV = {0.0f, 0.0f, 0.0f, 1.0f};
FloatBuffer ambient = BufferUtils.createFloatBuffer(4).put(ambientV);
FloatBuffer diffuse = BufferUtils.createFloatBuffer(4).put(diffuseV);
//FloatBuffer specular = BufferUtils.createFloatBuffer(4).put(whiteV);
glLight(GL_LIGHT1, GL_AMBIENT, (FloatBuffer)ambient.flip());
glLight(GL_LIGHT1, GL_DIFFUSE, (FloatBuffer)diffuse.flip());
//glLight(GL_LIGHT0, GL_SPECULAR, (FloatBuffer)specular.flip());
glEnable(GL_LIGHT1);
}
public static void positionLight() {
float[] position = {0.0f, 0.0f, 0.0f, 1.0f};
FloatBuffer lightPosition = BufferUtils.createFloatBuffer(4).put(position);
glLight(GL_LIGHT1, GL_POSITION, (FloatBuffer)lightPosition.flip());
}
}
Thanks!
hemoid