Hi guys ima bit confused ???
I’ve been trying to make just a basic 3d game to build up some knowledge in LWJGL but my lighting seems to be going really wierd.
The lighting changes depending on where the character is standing should this be happening or not?
Heres my code:
package src;
import de.matthiasmann.twl.utils.PNGDecoder;
import de.matthiasmann.twl.utils.PNGDecoder.Format;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.gluPerspective;
import org.lwjgl.util.vector.Vector3f;
public class test2 {
public float fov = 75;
public float maxLookDown = -85;
public float maxLookUp = 85;
public float mouseSpeed = 1;
public Vector3f rotation = new Vector3f(0,0,0);
public Vector3f lightpos = new Vector3f(10,10,10);
private static long lastFrame;
public int walkingSpeed = 10;
public boolean jumpingUp = false;
public boolean jumpingDown = false;
public float floorHeight = -5;
public float charheight = (float) 4;
public Vector3f position = new Vector3f(0,charheight,2);
public float jumpmax = (float) (charheight - 0.75);
private static long getTime() {
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
private static int getDelta() {
long currentTime = getTime();
int delta = (int) (currentTime - lastFrame);
lastFrame = getTime();
return delta;
}
public void glEnable2D(){
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
}
public void glDisable2D()
{
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
public test2(){
try {
Display.setDisplayMode(new DisplayMode(1000, 600));
Display.setResizable(true);
Display.setTitle("test");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective((float) fov, (float)Display.getWidth() / (float)Display.getHeight(), 0.001f, 100);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glEnable(GL_ALPHA_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_FOG);
glEnable(GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1.0f);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
float lightAmbient[] = { 1,1,1, 1.0f }; // Ambient Light Values
float lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; // Diffuse Light Values
float lightPosition[] = { position.x,position.y,position.z,1 }; // Light Position
ByteBuffer temp = ByteBuffer.allocateDirect(16);
temp.order(ByteOrder.nativeOrder());
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(lightAmbient).flip()); // Setup The Ambient Light
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, (FloatBuffer)temp.asFloatBuffer().put(lightDiffuse).flip()); // Setup The Diffuse Light
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION,(FloatBuffer)temp.asFloatBuffer().put(lightPosition).flip()); // Position The Light
GL11.glEnable(GL11.GL_LIGHT1);
int wall = glGenLists(1);
glNewList(wall,GL_COMPILE);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex3f(-5,-5, 0);
glTexCoord2f(0,6);
glVertex3f(-5,-2,0);
glTexCoord2f(20,6);
glVertex3f(5,-2,0);
glTexCoord2f(20,0);
glVertex3f(5,-5,0);
glTexCoord2f(0,0);
glVertex3f(-5,-5,-10);
glTexCoord2f(0,6);
glVertex3f(-5,-2,-10);
glTexCoord2f(20,6);
glVertex3f(-5,-2,0);
glTexCoord2f(20,0);
glVertex3f(-5,-5,0);
glTexCoord2f(0,0);
glVertex3f(-5,-5,-10);
glTexCoord2f(0,6);
glVertex3f(-5,-2,-10);
glTexCoord2f(20,6);
glVertex3f(5,-2,-10);
glTexCoord2f(20,0);
glVertex3f(5,-5,-10);
glTexCoord2f(0,0);
glVertex3f(5,-5,-10);
glTexCoord2f(0,6);
glVertex3f(5,-2,-10);
glTexCoord2f(20,6);
glVertex3f(5,-2,0);
glTexCoord2f(20,0);
glVertex3f(5,-5,0);
glTexCoord2f(0,0);
glVertex3f(5,-2,0);
glTexCoord2f(0,20);
glVertex3f(-5,-2,0);
glTexCoord2f(20,20);
glVertex3f(-5,-2,-10);
glTexCoord2f(20,0);
glVertex3f(5,-2,-10);
glEnd();
glEndList();
int block = glGenLists(1);
glNewList(block,GL_COMPILE);
{
}
glEndList();
int floor = glGenLists(1);
glNewList(floor,GL_COMPILE);
{
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex3f(5,floorHeight,0);
glTexCoord2f(0,20);
glVertex3f(-5,floorHeight,0);
glTexCoord2f(20,20);
glVertex3f(-5,floorHeight,-10);
glTexCoord2f(20,0);
glVertex3f(5,floorHeight,-10);
glEnd();
}
glEndList();
int triangle = glGenLists(1);
glNewList(triangle, GL_COMPILE);
{
double topPoint = 0.75;
glBegin(GL_TRIANGLES);
glColor4f(1,0,0,1f);
glVertex3d(0, topPoint, -5);
glColor4f(0,1,0,1f);;
glVertex3d(-1, -0.75, -4);
glColor4f(0,1,0,1f);
glVertex3d(1, -.75, -4);
glColor4f(1,0,0,1f);
glVertex3d(0, topPoint, -5);
glColor4f(0,1,0,1f);
glVertex3d(1, -0.75, -4);
glColor4f(0,1,0,1f);
glVertex3d(1, -0.75, -6);
glColor4f(1,0,0,1f);
glVertex3d(0, topPoint, -5);
glColor4f(0,1,0,1f);
glVertex3d(1, -0.75, -6);
glColor4f(0,1,0,1f);
glVertex3d(-1, -.75, -6);
glColor4f(1,0,0,1f);
glVertex3d(0, topPoint, -5);
glColor4f(0,1,0,1f);
glVertex3d(-1, -0.75, -6);
glColor4f(0,1,0,1f);
glVertex3d(-1, -.75, -4);
glEnd();
glBegin(GL_QUADS);
glColor4f(0,1,0,1f);
glVertex3d(-1, -.75, -4);
glVertex3d(-1, -.75, -6);
glVertex3d(1, -0.75, -6);
glVertex3d(1, -.75, -4);
glColor4f(1,1,1,1f);
glEnd();
}
glEndList();
Mouse.setGrabbed(true);
int floorTexture = glGenTextures();
{
InputStream in = null;
try {
in = new FileInputStream("res/floor.png");
PNGDecoder decoder = new PNGDecoder(in);
ByteBuffer buffer = BufferUtils.createByteBuffer(4 * decoder.getWidth() * decoder.getHeight());
decoder.decode(buffer, decoder.getWidth() * 4, Format.RGBA);
buffer.flip();
in.close();
glBindTexture(GL_TEXTURE_2D, floorTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
glBindTexture(GL_TEXTURE_2D, 0);
} catch (FileNotFoundException ex) {
System.err.println("Failed to find the texture files.");
Display.destroy();
System.exit(1);
} catch (IOException ex) {
System.err.println("Failed to load the texture files.");
Display.destroy();
System.exit(1);
}
}
int wallTexture = glGenTextures();
{
InputStream in = null;
try {
in = new FileInputStream("res/wood.png");
PNGDecoder decoder = new PNGDecoder(in);
ByteBuffer buffer = BufferUtils.createByteBuffer(4 * decoder.getWidth() * decoder.getHeight());
decoder.decode(buffer, decoder.getWidth() * 4, Format.RGBA);
buffer.flip();
in.close();
glBindTexture(GL_TEXTURE_2D, wallTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
glBindTexture(GL_TEXTURE_2D, 0);
} catch (FileNotFoundException ex) {
System.err.println("Failed to find the texture files.");
Display.destroy();
System.exit(1);
} catch (IOException ex) {
System.err.println("Failed to load the texture files.");
Display.destroy();
System.exit(1);
}
}
while(!Display.isCloseRequested()){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, floorTexture);
glCallList(floor);
glBindTexture(GL_TEXTURE_2D, wallTexture);
glCallList(wall);
glBindTexture(GL_TEXTURE_2D, 0);
glCallList(triangle);
glPushMatrix();
glLoadIdentity();
glEnable2D();
glLineWidth(1);
glBegin(GL_LINES);
glColor4f(0,0,0,1);
glVertex2f((Display.getWidth()/2)+15,Display.getHeight() / 2);
glVertex2f((Display.getWidth()/2)-15,Display.getHeight() / 2);
glVertex2f(Display.getWidth()/2,Display.getHeight()/2+15);
glVertex2f(Display.getWidth()/2,Display.getHeight()/2-15);
glEnd();
glColor4f(1,1,1,1);
glDisable2D();
{
FloatBuffer fogColours = BufferUtils.createFloatBuffer(4);
fogColours.put(new float[]{0, 0, 0, 1});
glClearColor(0, 0, 0, 1);
fogColours.flip();
glFog(GL_FOG_COLOR, fogColours);
glFogi(GL_FOG_MODE, GL_LINEAR);
glHint(GL_FOG_HINT, GL_NICEST);
glFogf(GL_FOG_START, 8);
glFogf(GL_FOG_END, 13);
glFogf(GL_FOG_DENSITY, 0.01f);
}
float mouseDX = Mouse.getDX() * mouseSpeed * 0.16f;
float mouseDY = Mouse.getDY() * mouseSpeed * 0.16f;
float delta = getDelta();
glLoadIdentity();
glRotatef(rotation.x, 1, 0, 0);
glRotatef(rotation.y, 0, 1, 0);
glRotatef(rotation.z, 0, 0, 1);
glTranslatef(position.x,position.y,position.z);
if(Mouse.isGrabbed()){
if (rotation.y + mouseDX >= 360) {
rotation.y = rotation.y + mouseDX - 360;
} else if (rotation.y + mouseDX < 0) {
rotation.y = 360 - rotation.y + mouseDX;
} else {
rotation.y += mouseDX;
}
if (rotation.x - mouseDY >= maxLookDown && rotation.x - mouseDY <= maxLookUp) {
rotation.x += -mouseDY;
}
else if(rotation.x - mouseDY >= 85){
rotation.x = 85;
rotation.x += -mouseDY;
}
else if(rotation.x - mouseDY <= -85){
rotation.x = -85;
rotation.x += -mouseDY;
}
}
boolean keyUp = Keyboard.isKeyDown(Keyboard.KEY_W) || Keyboard.isKeyDown(Keyboard.KEY_UP);
boolean keyLeft = Keyboard.isKeyDown(Keyboard.KEY_A) || Keyboard.isKeyDown(Keyboard.KEY_LEFT);
boolean keyRight = Keyboard.isKeyDown(Keyboard.KEY_D) || Keyboard.isKeyDown(Keyboard.KEY_RIGHT);
boolean keyDown = Keyboard.isKeyDown(Keyboard.KEY_S) || Keyboard.isKeyDown(Keyboard.KEY_DOWN);
boolean keySpace = Keyboard.isKeyDown(Keyboard.KEY_SPACE);
boolean keyCtrl = Keyboard.isKeyDown(Keyboard.KEY_LCONTROL);
if (keyUp && !keyLeft && !keyRight && !keyDown) {
float angle = rotation.y;
Vector3f newPosition = new Vector3f(position);
float v = (walkingSpeed * 0.0002f) * delta;
float deltaz = v * (float) Math.cos(Math.toRadians(angle));
float deltax = (float) (Math.sin(Math.toRadians(angle)) * v);
newPosition.z += deltaz;
newPosition.x -= deltax;
position.z = newPosition.z;
position.x = newPosition.x;
}
if(keyDown){
float angle = rotation.y - 180;
Vector3f newPosition = new Vector3f(position);
float v = (walkingSpeed * 0.0002f) * delta;
float deltaz = v * (float) Math.cos(Math.toRadians(angle));
float deltax = (float) (Math.sin(Math.toRadians(angle)) * v);
newPosition.z += deltaz;
newPosition.x -= deltax;
position.z = newPosition.z;
position.x = newPosition.x;
}
if(keyRight){
float angle = rotation.y + 90;
Vector3f newPosition = new Vector3f(position);
float v = (walkingSpeed * 0.0002f) * delta;
float deltaz = v * (float) Math.cos(Math.toRadians(angle));
float deltax = (float) (Math.sin(Math.toRadians(angle)) * v);
newPosition.z += deltaz;
newPosition.x -= deltax;
position.z = newPosition.z;
position.x = newPosition.x;
}
if(keyLeft){
float angle = rotation.y -90;
Vector3f newPosition = new Vector3f(position);
float v = (walkingSpeed * 0.0002f) * delta;
float deltaz = v * (float) Math.cos(Math.toRadians(angle));
float deltax = (float) (Math.sin(Math.toRadians(angle)) * v);
newPosition.z += deltaz;
newPosition.x -= deltax;
position.z = newPosition.z;
position.x = newPosition.x;
}
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
Display.destroy();
System.exit(0);
}
if(keySpace){
if(!jumpingDown==true)
jumpingUp=true;
}
float vel = (float) 0.05;
if(jumpingUp){
vel-=0.005;
position.y-=vel;
if(position.y<=jumpmax){
jumpingDown=true;
jumpingUp=false;
}
}
if(jumpingDown){
vel+=0.002;
position.y+=vel;
if(position.y>=charheight){
position.y=(float) charheight;
jumpingDown = false;
}
}
if(position.y-0.5<floorHeight){
jumpingDown = true;
}
while (Mouse.next()){
if (Mouse.isButtonDown(1)){
Mouse.setGrabbed(false);
}
if (Mouse.isButtonDown(0)){
Mouse.setGrabbed(true);
}
}
if (Display.wasResized()) {
glViewport(0, 0, Display.getWidth(), Display.getHeight());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, (float) Display.getWidth() / (float) Display.getHeight(), 0.001f, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Display.sync(60);
Display.update();
}
Display.destroy();
System.exit(0);
}
public static void main(String args[]){
test2 t = new test2();
}
}