Animate a box + mooving with camera

Started by johnnyh, September 03, 2011, 22:13:52

Previous topic - Next topic

johnnyh

hello, I have a written a code that allow me to move with keyboard and mouse. I am drawing a box in the window and I would like the box to move. The problem is that when I am trying to move the box, my camera is frozen and I cant move anymore. here is my code :

package testlwjgl4;

import org.lwjgl.LWJGLException;
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 org.lwjgl.util.glu.GLU;


public class TestLWJGL4 {
    private float vitesseDeplacement = 0.1f;
    private float vitesseRotation = 0.5f;
    private float dernierTemps;
    private float temps;
    FPCameraController camera = new FPCameraController(0,0,0);
    private int dx;
    private int dy;
    private float dernierTempsFPS;
    private int FPS;
    private float F=10000;//distance Fog
    private float pos=0;
    
    
    public void start(){
        try {
	    Display.setDisplayMode(new DisplayMode(800,600));
	    Display.create();
	} catch (LWJGLException e) {
	    e.printStackTrace();
	    System.exit(0);
	}
        
        init();
        
        while(!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
            refresh();
            Display.update();
            Display.sync(50);
        }
        
        Display.destroy();
        }
    ////////////////////////////////////////////////////////////////////////////
    
    
    
    
    
    
    public void init(){
        GL11.glMatrixMode(GL11.GL_PROJECTION);
            GL11.glLoadIdentity();
            GLU.gluPerspective(70, (float)800/600, 1, F);
            //GL11.glOrtho(0, 800, 600, 0,1,1000 );
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        Mouse.setGrabbed(true);
        dernierTemps=System.nanoTime()/1000000;
        dernierTempsFPS=dernierTemps;
        
    }
    
    public void refresh(){
        temps=System.nanoTime()/1000000;
        float delta = temps-dernierTemps;
        if (temps-dernierTempsFPS > 1000){
            dernierTempsFPS=temps;
            Display.setTitle("FPS : "+FPS);
            FPS=0;
        }FPS++;
        
        float distance = delta*vitesseDeplacement;
        dernierTemps=temps;
        
        dx=Mouse.getDX();
        dy=Mouse.getDY();

        camera.yaw(dx*vitesseRotation);
        camera.pitch(dy*vitesseRotation);

        if (Keyboard.isKeyDown(Keyboard.KEY_Z)){
            camera.avancer(vitesseDeplacement*delta);
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_S)){
            camera.reculer(vitesseDeplacement*delta);
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_Q)){
            camera.pasGauche(vitesseDeplacement*delta);
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_D)){
            camera.pasDroite(vitesseDeplacement*delta);
        }
        
        GL11.glPushMatrix();
        camera.lookThrough();
        
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); 
        
//        for(int i=0;i<10000;i=i+70){
//            for(int j=0;j<1000;j=j+70){
//                dessinCube(i,-100,j,60);
//            }
//        }
        
        GL11.glPopMatrix();
        GL11.glPushMatrix();
        GL11.glTranslatef(0, 0, -pos);
        dessinCube(0,-60,-30,60);
        pos=pos+0.1f;
        GL11.glPopMatrix();
        
        
        
        GL11.glPopMatrix();
        

    }
    
    public void dessinCube(float xc, float yc, float zc, float h){
        GL11.glBegin(GL11.GL_QUADS);

            //face 1
            GL11.glColor3f(1f,0f,0f);
            GL11.glVertex3f(xc-h/2,yc+h/2,zc+h/2);
            GL11.glVertex3f(xc+h/2,yc+h/2,zc+h/2);
            GL11.glVertex3f(xc+h/2,yc-h/2,zc+h/2);
            GL11.glVertex3f(xc-h/2,yc-h/2,zc+h/2);
            
            //face 2
            GL11.glColor3f(0f,1f,0f);
            GL11.glVertex3f(xc+h/2,yc+h/2,zc+h/2);
            GL11.glVertex3f(xc+h/2,yc+h/2,zc-h/2);
            GL11.glVertex3f(xc+h/2,yc-h/2,zc-h/2);
            GL11.glVertex3f(xc+h/2,yc-h/2,zc+h/2);
            
            //face 3
            GL11.glColor3f(1f,0f,0f);
            GL11.glVertex3f(xc+h/2,yc+h/2,zc-h/2);
            GL11.glVertex3f(xc-h/2,yc+h/2,zc-h/2);
            GL11.glVertex3f(xc-h/2,yc-h/2,zc-h/2);
            GL11.glVertex3f(xc+h/2,yc-h/2,zc-h/2);
            
            //face 4
            GL11.glColor3f(0f,1f,0f);
            GL11.glVertex3f(xc-h/2,yc+h/2,zc+h/2);
            GL11.glVertex3f(xc-h/2,yc+h/2,zc-h/2);
            GL11.glVertex3f(xc-h/2,yc-h/2,zc-h/2);
            GL11.glVertex3f(xc-h/2,yc-h/2,zc+h/2);
            
            //face 5
            GL11.glColor3f(0f,0f,1.0f);
            GL11.glVertex3f(xc-h/2,yc+h/2,zc+h/2);
            GL11.glVertex3f(xc+h/2,yc+h/2,zc+h/2);
            GL11.glVertex3f(xc+h/2,yc+h/2,zc-h/2);
            GL11.glVertex3f(xc-h/2,yc+h/2,zc-h/2);
            
            //face 6
            GL11.glColor3f(0f,0f,1.0f);
            GL11.glVertex3f(xc-h/2,yc-h/2,zc+h/2);
            GL11.glVertex3f(xc+h/2,yc-h/2,zc+h/2);
            GL11.glVertex3f(xc+h/2,yc-h/2,zc-h/2);
            GL11.glVertex3f(xc-h/2,yc-h/2,zc-h/2);
            
        GL11.glEnd();
    }
    

    public static void main(String[] args) {
        TestLWJGL4 test = new TestLWJGL4();
        test.start();
    }
}

johnnyh

Anyone know how to solve that ? I think this is simple stuff but as i am new in programation and java, I dont know how to do this two things together :
1: move the camera with mouse and keyboard like any usual 3D game
2: animate an object, independant of the camera

I know how to do those two things separatly but not together.

Sorry for bad english.

johnnyh

Ok I have found how to deal with it.

in fact I didnt have to push/pop the ModelView matrix after calling the lookThrough method because it was reseting the point i was looking at.