1

我遇到了 VBO 和键盘+鼠标输入的问题。

例如,如果我移动鼠标,立方体不会停止旋转,甚至会更快,与我在 3 台计算机上测试它的键盘相同 这是我的代码:Game.class package tk.mattze96.game;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.Date;
import org.lwjgl.BufferUtils;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.util.glu.GLU;

public class Game {

String version = "0.0.1";
Player player;
boolean[] keys = new boolean[256];

public Game(){
    player = new Player(this);
}


  public void start() {
// create our display window
try {
  Display.setTitle("Game "+version);
  Display.setDisplayMode(new DisplayMode(600,600));
  Display.create();
} catch (Exception e) {
  System.err.println(e.toString());

  System.exit(1);
}

// set up OpenGL
GL11.glClearColor(173/255f, 216/255f, 230/255f, 0.0f);//173,216,230

GL11.glShadeModel(GL11.GL_SMOOTH);

GL11.glEnable(GL11.GL_COLOR_MATERIAL);
GL11.glEnable(GL11.GL_DEPTH_TEST);

GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY);
GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);

// set up lighting
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_LIGHT0);

GL11.glMaterial(GL11.GL_FRONT, GL11.GL_SPECULAR, floatBuffer(1.0f, 1.0f, 1.0f, 1.0f));
GL11.glMaterialf(GL11.GL_FRONT, GL11.GL_SHININESS, 25.0f);

GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, floatBuffer(-5.0f, 5.0f, 15.0f, 0.0f));

GL11.glLight(GL11.GL_LIGHT0, GL11.GL_SPECULAR, floatBuffer(1.0f, 1.0f, 1.0f, 1.0f));
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, floatBuffer(1.0f, 1.0f, 1.0f, 1.0f));

GL11.glLightModel(GL11.GL_LIGHT_MODEL_AMBIENT, floatBuffer(0.1f, 0.1f, 0.1f, 1.0f));

// set up the camera
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(45.0f, Display.getHeight()/Display.getWidth(), 0.1f, 1000.0f);

GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();

GLU.gluLookAt(0.0f, 0.0f, 10.0f,
              0.0f, 0.0f, 0.0f,
              0.0f, 1.0f, 0.0f );

// create our vertex buffer objects
IntBuffer buffer = BufferUtils.createIntBuffer(1);
GL15.glGenBuffers(buffer);

int vertex_buffer_id = buffer.get(0);

float[] vertex_data_array = {
//   x      y      z      nx     ny     nz     r      g      b      a
// back quad
     1.0f,  1.0f,  1.0f,  0.0f,  0.0f,  1.0f,  1.0f,  0.0f,  0.0f,  1.0f,
    -1.0f,  1.0f,  1.0f,  0.0f,  0.0f,  1.0f,  1.0f,  0.0f,  0.0f,  1.0f,
    -1.0f, -1.0f,  1.0f,  0.0f,  0.0f,  1.0f,  1.0f,  0.0f,  0.0f,  1.0f,
     1.0f, -1.0f,  1.0f,  0.0f,  0.0f,  1.0f,  1.0f,  0.0f,  0.0f,  1.0f,

// front quad
     1.0f,  1.0f, -1.0f,  0.0f,  0.0f, -1.0f,  0.0f,  1.0f,  0.0f,  1.0f,
    -1.0f,  1.0f, -1.0f,  0.0f,  0.0f, -1.0f,  0.0f,  1.0f,  0.0f,  1.0f,
    -1.0f, -1.0f, -1.0f,  0.0f,  0.0f, -1.0f,  0.0f,  1.0f,  0.0f,  1.0f,
     1.0f, -1.0f, -1.0f,  0.0f,  0.0f, -1.0f,  0.0f,  1.0f,  0.0f,  1.0f,

// left quad
    -1.0f,  1.0f, -1.0f, -1.0f,  0.0f,  0.0f,  0.0f,  0.0f,  1.0f,  1.0f,
    -1.0f,  1.0f,  1.0f, -1.0f,  0.0f,  0.0f,  0.0f,  0.0f,  1.0f,  1.0f,
    -1.0f, -1.0f,  1.0f, -1.0f,  0.0f,  0.0f,  0.0f,  0.0f,  1.0f,  1.0f,
    -1.0f, -1.0f, -1.0f, -1.0f,  0.0f,  0.0f,  0.0f,  0.0f,  1.0f,  1.0f,

// right quad
     1.0f,  1.0f, -1.0f,  1.0f,  0.0f,  0.0f,  1.0f,  0.0f,  1.0f,  1.0f,
     1.0f,  1.0f,  1.0f,  1.0f,  0.0f,  0.0f,  1.0f,  0.0f,  1.0f,  1.0f,
     1.0f, -1.0f,  1.0f,  1.0f,  0.0f,  0.0f,  1.0f,  0.0f,  1.0f,  1.0f,
     1.0f, -1.0f, -1.0f,  1.0f,  0.0f,  0.0f,  1.0f,  0.0f,  1.0f,  1.0f,

// top quad
    -1.0f,  1.0f, -1.0f,  0.0f,  1.0f,  0.0f,  1.0f,  1.0f,  0.0f,  1.0f,
    -1.0f,  1.0f,  1.0f,  0.0f,  1.0f,  0.0f,  1.0f,  1.0f,  0.0f,  1.0f,
     1.0f,  1.0f,  1.0f,  0.0f,  1.0f,  0.0f,  1.0f,  1.0f,  0.0f,  1.0f,
     1.0f,  1.0f, -1.0f,  0.0f,  1.0f,  0.0f,  1.0f,  1.0f,  0.0f,  1.0f,

// bottom quad
    -1.0f, -1.0f, -1.0f,  0.0f, -1.0f,  0.0f,  0.0f,  1.0f,  1.0f,  1.0f,
    -1.0f, -1.0f,  1.0f,  0.0f, -1.0f,  0.0f,  0.0f,  1.0f,  1.0f,  1.0f,
     1.0f, -1.0f,  1.0f,  0.0f, -1.0f,  0.0f,  0.0f,  1.0f,  1.0f,  1.0f,
     1.0f, -1.0f, -1.0f,  0.0f, -1.0f,  0.0f,  0.0f,  1.0f,  1.0f,  1.0f
};

FloatBuffer vertex_buffer_data = BufferUtils.createFloatBuffer(vertex_data_array.length);
vertex_buffer_data.put(vertex_data_array);
vertex_buffer_data.rewind();

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertex_buffer_id);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertex_buffer_data, GL15.GL_STATIC_DRAW);

// set up frame rate counter stuff
int framerate_count = 0;
long framerate_timestamp = new Date().getTime();
double rotate_x, rotate_y, rotate_z;



while (!Display.isCloseRequested()) {
    mapKeys();
    player.update();

  // increment frame rate counter, and display current frame rate
  // if it is time to do so
  framerate_count++;

  Date d = new Date();
  long this_framerate_timestamp = d.getTime();

  if ((this_framerate_timestamp - framerate_timestamp) >= 1000) {
    System.err.println("Frame Rate: " + framerate_count);

    framerate_count = 0;
    framerate_timestamp = this_framerate_timestamp;
  }

  // clear the display
  GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

  // perform rotation transformations
  GL11.glPushMatrix();

  rotate_x = ((double)this_framerate_timestamp / 30.0) % 360.0;
  rotate_y = ((double)this_framerate_timestamp / 20.0) % 360.0;
  rotate_z = ((double)this_framerate_timestamp / 10.0) % 360.0;


  /*GL11.glRotated(
    rotate_x,
    1.0,
    0.0,
    0.0
  );

  GL11.glRotated(
    rotate_y,
    0.0,
    1.0,
    0.0
  );

  GL11.glRotated(
    rotate_z,
    0.0,
    0.0,
    1.0
  );*/

  // render the cube
  GL11.glVertexPointer(3, GL11.GL_FLOAT, 40, 0);
  GL11.glNormalPointer(GL11.GL_FLOAT, 40, 12);
  GL11.glColorPointer(4, GL11.GL_FLOAT, 40, 24);

  GL11.glDrawArrays(GL11.GL_QUADS, 0, vertex_data_array.length / 10);

  // restore the matrix to pre-transformation values
  GL11.glPopMatrix();

  // update the display
  Display.sync(60);
  Display.update();
}

// clean things up/255
Display.destroy();
  }

  private void mapKeys(){
    //Update keys
    for(int i=0;i<keys.length;i++){
        keys[i] = Keyboard.isKeyDown(i);
    }
}

  public FloatBuffer floatBuffer(float a, float b, float c, float d) {
float[] data = new float[]{a,b,c,d};
FloatBuffer fb = BufferUtils.createFloatBuffer(data.length);
fb.put(data);
fb.flip();
return fb;
  }

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

播放器类:

package tk.mattze96.game;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
import org.lwjgl.util.vector.Vector3f;

public class Player {

Vector3f vector = new Vector3f();
Vector3f rotation = new Vector3f();
Vector3f vectorPrevious = new Vector3f();
boolean moveForward = false, moveBackward = false;
boolean strafeLeft = false, strafeRight = false;
static final float speed = 0.003f;
Game game;

public Player(Game game) {
    this.game = game;
    Mouse.setGrabbed(true);
}

public void update(){

    updatePrevious();
    input();
    updateVector();
    translatePostion();

}

public void translatePostion(){
    //This is the code that changes 3D perspective to the camera's perspective.
    GL11.glRotatef(rotation.x, 1, 0, 0);
    GL11.glRotatef(rotation.y, 0, 1, 0);
    GL11.glRotatef(rotation.z, 0, 0, 1);
    //-vector.y-2.4f means that your y is your feet, and y+2.4 is your head.
    GL11.glTranslatef(-vector.x, -vector.y, -vector.z);
}

public void updateVector(){
    if(moveForward){
        vector.x -= (float) (Math.sin(-rotation.y * Math.PI / 180) * speed);
        vector.z -= (float) (Math.cos(-rotation.y * Math.PI / 180) * speed);
    }
    if(moveBackward){
        vector.x += (float) (Math.sin(-rotation.y * Math.PI / 180) * speed);
        vector.z += (float) (Math.cos(-rotation.y * Math.PI / 180) * speed);
    }
    if(strafeLeft){
        vector.x += (float) (Math.sin((-rotation.y - 90) * Math.PI / 180) * speed);
        vector.z += (float) (Math.cos((-rotation.y - 90) * Math.PI / 180) * speed);
    }
    if(strafeRight){
        vector.x += (float) (Math.sin((-rotation.y + 90) * Math.PI / 180) * speed);
        vector.z += (float) (Math.cos((-rotation.y + 90) * Math.PI / 180) * speed);
    }
}

public void updatePrevious(){
    //Update last position (for collisions (later))
    vectorPrevious.x = vector.x;
    vectorPrevious.y = vector.y;
    vectorPrevious.z = vector.z;
}

public void input(){
    //Keyboard Input for Movement
    moveForward = game.keys[Keyboard.KEY_W];
    moveBackward = game.keys[Keyboard.KEY_S];
    strafeLeft = game.keys[Keyboard.KEY_A];
    strafeRight = game.keys[Keyboard.KEY_D];

    //Mouse Input for looking around...
    if(Mouse.isGrabbed()){
        float mouseDX = Mouse.getDX() * 0.8f * 0.0016f;
        float mouseDY = Mouse.getDY() * 0.8f * 0.0016f;
        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 >= -89 && rotation.x - mouseDY <= 89) {
            rotation.x += -mouseDY;
        } else if (rotation.x - mouseDY < -89) {
            rotation.x = -89;
        } else if (rotation.x - mouseDY > 89) {
            rotation.x = 89;
        }
    }
}

}
4

1 回答 1

0

Mouse.getDX()并且Mouse.getDY()只对鼠标移动做出反应,不需要任何类似if-else声明的戳。例如:

if(Mouse.isGrabbed()){
    float mouseDX = Mouse.getDX() * 0.8f * 0.0016f;
    float mouseDY = Mouse.getDY() * 0.8f * 0.0016f;
    rotation.y = mouseDX; //rotates around Y-axis just fine
/*here you need to think about rotation around X and Z axis, because it makes sence 
that mouseDY can rotate your camera around both axises*/
    rotation.x = mouseDY; //something
}
于 2013-01-18T19:36:30.870 回答