[SOLVED] LWGJL Camera not working with Display Lists

Started by tattyseal, December 29, 2013, 12:19:54

Previous topic - Next topic

tattyseal

EDIT: SOLVED THIS. I had to move the camera to after the glLoadIdentity() call in the render cube call.

So

LOAD IDENTITY

CAMERA LOOK THROUGH CALL

RENDER SCENE




Hello,

I made an application in the past that used no display lists and just used full out rendering on the go. It was bad, but the camera in it worked fine.

I lost that due to a hard drive failure.

Now, using Display Lists I have managed to make a much nicer version of it, it renders 2 cubes, but any camera tutorial I follow online does not work. I have tried loads of methods and I still cannot get it to work.

Here is my code.

[spoiler]
Main App Class (stats from Main.java which calls Cel.start(args[]);
package net.cel;

import net.cel.client.frames.FrameHandler;
import net.cel.client.renderer.CubeRenderer;
import net.cel.client.renderer.WorldRenderer;
import net.cel.cube.Cube;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;

public class Cel
{
	private static boolean running;
	
	public static WorldRenderer worldRenderer = new WorldRenderer(0);
	
	public static void start(String args[])
	{
		CubeRenderer.initCubeRenderingState();
		FrameHandler.getDelta();
		RenderUtils.registerDisplayList();
		
		Mouse.setGrabbed(true);
	}
	
	public void gameLoop()
	{
		running = true;
		
		while (running)
		{
			if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) && Mouse.isGrabbed())
			{
				Mouse.setGrabbed(false);
			}
			worldRenderer.renderTestingCubes();
			FrameHandler.update();
			Display.update();
			
			if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
			{
				running = false;
				Display.destroy();
				break;
			}
		}

	}
	
	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 WorldRenderer instance;
	
	public WorldRenderer(int dimId)
	{
		instance = this;
	}
	
	public void renderWorld()
	{
		/***
		 * Soon to be replaced with for loop world renderer
		 */
	}
	
	@Deprecated
	public void renderTestingCubes()
	{
		/***
		 * Do not use this method unless I instruct you to :P . It is not for modding.
		 */
		
		CubeRenderer.renderCube(new Vector3f(0.0f, -3.0f, -5.0f), new Vector3f(0.0f, -3.0f, -5.0f));
		CubeRenderer.renderCube(new Vector3f(-2f, 0f, 0f), new Vector3f(0.0f, 1.0f, 1.0f));
	}
	
	public 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
{
	public static void renderCube(Vector3f position, Vector3f color)
	{
		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();

		glColor3f(color.x, color.y, color.z);
		
		glTranslatef(position.x, position.y, position.z);

		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++;
	}
}

[/spoiler]

quew8

I cannot see anything there that I would call a Camera - you just seem to have a fixed view. So I'm guessing you got rid of the camera code because it wasn't working? People on this forum aren't going to write a camera for you. We are perfectly happy to help you debug code that isn't working but people aren't going to write it for you wholesale. It's a waste of our time, you don't learn anything and you would have to come back here whenever you needed something slightly different. Post the code of the camera which you felt you had the best understanding of (or just liked the best) and we'll go from there.

tattyseal

Quote from: quew8 on December 29, 2013, 13:14:50
I cannot see anything there that I would call a Camera - you just seem to have a fixed view. So I'm guessing you got rid of the camera code because it wasn't working? People on this forum aren't going to write a camera for you. We are perfectly happy to help you debug code that isn't working but people aren't going to write it for you wholesale. It's a waste of our time, you don't learn anything and you would have to come back here whenever you needed something slightly different. Post the code of the camera which you felt you had the best understanding of (or just liked the best) and we'll go from there.

Oops, forgot I cleaned it up last night I will post it when I get my camera code back in.

tattyseal

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

tattyseal

Quote from: quew8 on December 29, 2013, 13:14:50
I cannot see anything there that I would call a Camera - you just seem to have a fixed view. So I'm guessing you got rid of the camera code because it wasn't working? People on this forum aren't going to write a camera for you. We are perfectly happy to help you debug code that isn't working but people aren't going to write it for you wholesale. It's a waste of our time, you don't learn anything and you would have to come back here whenever you needed something slightly different. Post the code of the camera which you felt you had the best understanding of (or just liked the best) and we'll go from there.

Yes, it was not working, plus I done a big cleanup of code. I have about posted the code :)

tattyseal

SOLVED THIS. I had to move the camera to after the glLoadIdentity() call in the render cube call.

So

LOAD IDENTITY

CAMERA LOOK THROUGH CALL

RENDER SCENE