Code with camera:
Main Game File, started from Main.java with start(args);
package net.cel;
import net.cel.client.frames.FrameHandler;
import net.cel.client.renderer.CubeRenderer;
import net.cel.client.renderer.WorldRenderer;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import static org.lwjgl.opengl.GL11.*;
public class Cel
{
public static WorldRenderer worldRenderer = new WorldRenderer(0);
public static CameraController cameraController = new CameraController();
public static CubeRenderer cubeRenderer = new CubeRenderer();
public static void start(String args[])
{
cameraController.setUpCamera();
CubeRenderer.initCubeRenderingState();
FrameHandler.getDelta();
FrameHandler.last_fps = FrameHandler.getTime();
RenderUtils.registerDisplayList();
Mouse.setGrabbed(true);
gameLoop();
}
public static void gameLoop()
{
while (!Display.isCloseRequested())
{
try
{
FrameHandler.update();
WorldRenderer.getInstance().renderTestingCubes();
cameraController.runCamera();
Display.update();
}
catch(Exception e)
{
exit(ErrorCodes.RENDER, e);
}
}
}
public static void exit(int error, Exception e)
{
System.out.println("Exited with error code " + error + " :");
e.printStackTrace();
System.exit(error);
}
}
WorldRenderer.java
package net.cel.client.renderer;
import org.lwjgl.util.vector.Vector3f;
public class WorldRenderer
{
private static WorldRenderer instance;
public WorldRenderer(int dimId)
{
instance = this;
}
public void renderWorld()
{
/***
* Soon to be replaced with for loop world renderer
*/
}
public void renderTestingCubes()
{
CubeRenderer.getInstance().renderCube(0.0f, -3.0f, -5.0f, 0.0f, -3.0f, -5.0f);
//CubeRenderer.getInstance().renderCube(-2f, 0f, 0f, 0.0f, 1.0f, 1.0f);
}
public static WorldRenderer getInstance()
{
return instance;
}
}
CubeRenderer.java
package net.cel.client.renderer;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_CULL_FACE;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
import static org.lwjgl.opengl.GL11.GL_LEQUAL;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_NICEST;
import static org.lwjgl.opengl.GL11.GL_PERSPECTIVE_CORRECTION_HINT;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_SMOOTH;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.glCallList;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glClearColor;
import static org.lwjgl.opengl.GL11.glClearDepth;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glDepthFunc;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glHint;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glShadeModel;
import static org.lwjgl.opengl.GL11.glTranslatef;
import static org.lwjgl.opengl.GL11.glViewport;
import net.cel.RenderUtils;
import org.lwjgl.opengl.Display;
import org.lwjgl.util.glu.GLU;
import org.lwjgl.util.vector.Vector3f;
public class CubeRenderer
{
private static CubeRenderer instance;
public CubeRenderer()
{
instance = this;
}
public static CubeRenderer getInstance()
{
return instance;
}
public void renderCube(float x, float y, float z, float colorX, float colorY, float colorZ)
{
glEnable(GL_TEXTURE_2D);
glEnable(GL_CULL_FACE);
//glViewport(0, 0, Display.getWidth(), Display.getHeight());
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(x,y,z);
glColor3f(1.0f, 1.0f, 1.0f);
glCallList(RenderUtils.cubeList);
}
public static void initCubeRenderingState()
{
glViewport(0, 0, Display.getWidth(), Display.getHeight());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLU.gluPerspective(45.0F, Display.getWidth() / Display.getHeight(),
1.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0f, 0f, 0f, 0.5f);
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
}
FrameHandler.java
package net.cel.client.frames;
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
public class FrameHandler
{
public static long last_frame;
public static int fps;
public static long last_fps;
public static void initializeFrameHandlerState()
{
getDelta();
last_frame = getTime();
}
public static void update()
{
updateFPS();
}
public static int getDelta()
{
long time = getTime();
int delta = (int) (time - last_frame);
last_frame = time;
return delta;
}
public static long getTime()
{
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
public static void updateFPS()
{
if (getTime() - last_fps > 1000)
{
Display.setTitle("Cel FPS: " + fps);
fps = 0;
last_fps += 1000;
}
fps++;
}
}
CameraController.java
package net.cel;
import net.cel.client.frames.FrameHandler;
import net.cel.client.gui.IngameCamera;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
public class CameraController
{
private IngameCamera camera;
private float dx, dy, dt, mouseSensitivity, movementSpeed, time, lastTime;
public static CameraController instance;
public CameraController()
{
instance = this;
}
public static CameraController getInstance()
{
return instance;
}
public void setUpCamera()
{
camera = new IngameCamera(0, 0, 0);
dx = 0.0f;
dy = 0.0f;
dt = 0.0f;
time = 0.0f;
lastTime = 0.0f;
mouseSensitivity = 0.05f;
movementSpeed = 10.0f;
}
public void runCamera()
{
time = Sys.getTime();
dt = (time - lastTime)/1000.0f;
lastTime = time;
dx = Mouse.getDX();
dy = Mouse.getDY();
camera.yaw(dx * mouseSensitivity);
camera.pitch(dy * mouseSensitivity);
if (Keyboard.isKeyDown(Keyboard.KEY_W))
{
camera.walkForward(movementSpeed*dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_S))
{
camera.walkBackwards(movementSpeed*dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_A))
{
camera.strafeLeft(movementSpeed*dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_D))
{
camera.strafeRight(movementSpeed*dt);
}
GL11.glLoadIdentity();
camera.lookThrough();
}
}
IngameCamera.java
package net.cel.client.gui;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector3f;
public class IngameCamera
{
private Vector3f position = null;
private float yaw = 0.0f;
private float pitch = 0.0f;
public IngameCamera(int x, int y, int z)
{
position = new Vector3f(x, y, z);
}
public void yaw(float amount)
{
yaw += amount;
}
public void pitch(float amount)
{
pitch += amount;
}
public void walkForward(float distance)
{
position.x -= distance * (float)Math.sin(Math.toRadians(yaw));
position.z += distance * (float)Math.cos(Math.toRadians(yaw));
}
public void walkBackwards(float distance)
{
position.x += distance * (float)Math.sin(Math.toRadians(yaw));
position.z -= distance * (float)Math.cos(Math.toRadians(yaw));
}
public void strafeLeft(float distance)
{
position.x -= distance * (float)Math.sin(Math.toRadians(yaw-90));
position.z += distance * (float)Math.cos(Math.toRadians(yaw-90));
}
public void strafeRight(float distance)
{
position.x -= distance * (float)Math.sin(Math.toRadians(yaw+90));
position.z += distance * (float)Math.cos(Math.toRadians(yaw+90));
}
public void lookThrough()
{
GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);
GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f);
GL11.glTranslatef(position.x, position.y, position.z);
}
}
There you go. When I load my game it is just showing the screen with 1 cube at the bottom, but I cannot move the camera or move with WASD