Jump to content
  • Advertisement
Sign in to follow this  
Migi0027

OpenGL LWJGL FPS style camera doesn't work

This topic is 2225 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys, I'm currently trying out the LWJGL for opengl in java.

I've tried to create a fps camera but failed, and now the only hope for me seems like you.

Please look at my code, its not so complicated...

OpenGLExample


package miguel.p.main;
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;
public class OpenGLExample {

int nCubes = 100;
Cube[] Cubes;

public void start() {
Cubes = new Cube[nCubes];

for (int i = 0; i<nCubes; i++)
{
Cubes = new Cube();
}

FPCameraController camera = new FPCameraController(0, 0, 0);

float dx = 0.0f;
float dy = 0.0f;
float dt = 0.0f; //length of frame
float lastTime = 0.0f; // when the last frame was
float time = 0.0f;

float mouseSensitivity = 0.05f;
float movementSpeed = 10.0f; //move 10 units per second

//hide the mouse
Mouse.setGrabbed(true);

Generate();

OpenGL opengl = new OpenGL();

try {
Display.setDisplayMode(new DisplayMode(800,600));
Display.create();

opengl.Init();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}

// init OpenGL here

while (!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {

time = Sys.getTime();
dt = (time - lastTime)/1000.0f;
lastTime = time;

//distance in mouse movement from the last getDX() call.
dx = Mouse.getDX();
//distance in mouse movement from the last getDY() call.
dy = Mouse.getDY();

//controll camera yaw from x movement fromt the mouse
camera.yaw(dx * mouseSensitivity);
//controll camera pitch from y movement fromt the mouse
camera.pitch(dy * mouseSensitivity);

//when passing in the distance to move
//we times the movementSpeed with dt this is a time scale
//so if its a slow frame u move more then a fast frame
//so on a slow computer you move just as fast as on a fast computer
if (Keyboard.isKeyDown(Keyboard.KEY_W))//move forward
{
camera.walkForward(movementSpeed*dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_S))//move backwards
{
camera.walkBackwards(movementSpeed*dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_A))//strafe left
{
camera.strafeLeft(movementSpeed*dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_D))//strafe right
{
camera.strafeRight(movementSpeed*dt);
}

//set the modelview matrix back to the identity
GL11.glLoadIdentity();
//look through the camera before you draw anything
camera.lookThrough();

Render();
Display.update();
}

Display.destroy();
}

public void Generate()
{
int ccount = 0;
for (float x = 0; x<10; x+=1)
{
for (float y = 0; y<10; y+=1)
{
Cubes[ccount].SetPos(x, -1.1f, y);
if (!(x == 0 && y == 0))
{
Cubes[ccount].SetColor(0.5f, 0.5f, 0.5f);
}

ccount+=1;
}
}
}

public static void main(String[] argv) {
OpenGLExample displayExample = new OpenGLExample();
displayExample.start();
}

public void Render()
{
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer

for (int i = 0; i<nCubes; i++)
{
Cubes.RenderCube();
}
}
}


FPCameraController


package miguel.p.main;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector3f;

//First Person Camera Controller
public class FPCameraController
{
//3d vector to store the camera's position in
private Vector3f position = null;
//the rotation around the Y axis of the camera
private float yaw = 0.0f;
//the rotation around the X axis of the camera
private float pitch = 0.0f;

//Constructor that takes the starting x, y, z location of the camera
public FPCameraController(float x, float y, float z)
{
//instantiate position Vector3f to the x y z params.
position = new Vector3f(x, y, z);
}

//increment the camera's current yaw rotation
public void yaw(float amount)
{
//increment the yaw by the amount param
yaw += amount;
}

//increment the camera's current yaw rotation
public void pitch(float amount)
{
//increment the pitch by the amount param
pitch += amount;
}

//moves the camera forward relative to its current rotation (yaw)
public void walkForward(float distance)
{
position.x -= distance * (float)Math.sin(Math.toRadians(yaw));
position.z += distance * (float)Math.cos(Math.toRadians(yaw));
}

//moves the camera backward relative to its current rotation (yaw)
public void walkBackwards(float distance)
{
position.x += distance * (float)Math.sin(Math.toRadians(yaw));
position.z -= distance * (float)Math.cos(Math.toRadians(yaw));
}

//strafes the camera left relitive to its current rotation (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));
}

//strafes the camera right relitive to its current rotation (yaw)
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));
}

//translates and rotate the matrix so that it looks through the camera
//this dose basic what gluLookAt() does
public void lookThrough()
{
//roatate the pitch around the X axis
GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);
//roatate the yaw around the Y axis
GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f);
//translate to the position vector's location
GL11.glTranslatef(position.x, position.y, position.z);
}

}


OpenGL


package miguel.p.main;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
public class OpenGL {
public void Init()
{
GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping
GL11.glShadeModel(GL11.GL_SMOOTH); // Enable Smooth Shading
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
GL11.glClearDepth(1.0); // Depth Buffer Setup
GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Testing To Do
GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
GL11.glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
GLU.gluPerspective(
45.0f,
(float) Display.getWidth() / (float) Display.getHeight(),
0.1f,
100.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
// Really Nice Perspective Calculations
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
}
}


Cube


package miguel.p.main;
import org.lwjgl.opengl.GL11;
public class Cube {

private float posx, posy, posz;

public void SetPos(float x, float y, float z)
{
posx = x; posy = y; posz = z;
}

private float colorr, colorg, colorb;

public void SetColor(float red, float green, float blue)
{
colorr = red; colorb = blue; colorg = green;
}

public void RenderCube()
{
GL11.glLoadIdentity(); // Reset The Current Modelview Matrix
GL11.glTranslatef(posx, posy, posz); // Move Right 1.5 Units And Into The Screen 6.0
//GL11.glRotatef(rquad,1.0f,1.0f,1.0f); // Rotate The Quad On The X axis ( NEW )
GL11.glColor3f(0.5f, 0.5f, 1.0f); // Set The Color To Blue One Time Only
GL11.glBegin(GL11.GL_QUADS); // Draw A Quad
GL11.glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
GL11.glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
GL11.glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
GL11.glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
GL11.glColor3f(colorr,colorg,colorb); // Set The Color To Orange
GL11.glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
GL11.glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
GL11.glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
GL11.glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
GL11.glColor3f(colorr,colorg,colorb); // Set The Color To Red
GL11.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
GL11.glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
GL11.glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
GL11.glColor3f(colorr,colorg,colorb); // Set The Color To Yellow
GL11.glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Back)
GL11.glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Back)
GL11.glVertex3f(-1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Back)
GL11.glVertex3f( 1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Back)
GL11.glColor3f(colorr,colorg,colorb); // Set The Color To Blue
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
GL11.glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
GL11.glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
GL11.glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
GL11.glColor3f(colorr,colorg,colorb); // Set The Color To Violet
GL11.glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
GL11.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
GL11.glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
GL11.glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
GL11.glEnd();
}
}


The whole point is that my camera isn't moving...


I really thank you if you do so, please don't feel like you have to if your buisy.

Thank You

Share this post


Link to post
Share on other sites
Advertisement
[s]I never used lwjgl, but i think you're missing a swap-operation in your render method.[/s]

No it's called automatically, and i guess you're already seeing your scene.

Greetings,
weeska

Share this post


Link to post
Share on other sites
Seems like you reset the modelview matrix to identity in RenderCube by calling glLoadIdentity. Edited by Sponji

Share this post


Link to post
Share on other sites
Avoid glLoadIdentity() use glPushMatrix() and glPopMatrix() instead, put them around the object your drawing operations as so...


GL11.glPushMatrix();
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex3f().....
etc...
GL11.glPopMatrix();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!