LWJGL如何使用x,y,z移动

时间:2014-04-06 09:00:57

标签: java 3d lwjgl voxel jinput

您好我使用lwjgl添加到相机坐标时遇到问题。我认为它与gluPerspective有关我会发布我的代码,但你也可以告诉我如何实现移动鼠标的指导。然后像Minecraft一样在屏幕中间放一个十字准线。请告诉我如何将MouseUpdates代码实现到我的主代码中。

这是我的代码:

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
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 BasicRenderer {
private boolean isWireMesh = true;
private boolean moveUp = false;
private boolean moveDown = false;
private boolean moveRight = false;
private boolean moveLeft = false;
private boolean moveSpace = false;
private boolean moveShift = false;
private boolean movementFlag = false;
private int VBOVertexHandle;
private int VBOColorHandle;
private float PX = 0;
private float PY = 0;
private float PZ = 0;
Block Block = new Block();
Chunk Chunk = new Chunk();
mouseUpdates mouse = new mouseUpdates();

public static void main(String[] args) throws LWJGLException
 {
  BasicRenderer r = new BasicRenderer();
         r.Start();
 }

 public void Start(){
     try{
         createWindow();
    InitGL();
    Run();
     }catch(Exception e) {
         e.printStackTrace();
     }
 }

   DisplayMode displayMode;

    private void createWindow() throws Exception {
         Display.setFullscreen(false);
         DisplayMode d[] = Display.getAvailableDisplayModes();
         for (int i = 0; i < d.length; i++) {
             if (d[i].getWidth() == 640
                 && d[i].getHeight() == 480
                 && d[i].getBitsPerPixel() == 32) {
                 displayMode = d[i];
                 break;
             }
         }
         Display.setDisplayMode(displayMode);
         Display.setTitle("BluePrint Maker");
         Display.create();
     }

    private void InitGL() {
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glShadeModel(GL11.GL_SMOOTH);
        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        GL11.glClearDepth(1.0);
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glDepthFunc(GL11.GL_LEQUAL);
        GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);

        GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();

        GLU.gluPerspective(45.0f, (float) displayMode.getWidth()
                / (float) displayMode.getHeight(), 0.1f, 100.0f);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
     }

    private void Run(){
        float rotateYaw = 1;

        while(!Display.isCloseRequested()){
            try{
                Render();
                Display.update();
                Display.sync(60);

                ProcessInput();
                GL11.glClear(GL11.GL_COLOR_BUFFER_BIT
                        | GL11.GL_DEPTH_BUFFER_BIT);
                GL11.glLoadIdentity();

                GL11.glTranslatef(-30f + PX, -40f + PY, -160f+PZ); 

                GL11.glRotatef(45f, 0.4f, 1.0f, 0.1f);
                GL11.glRotatef(45f, 0f, 1.0f, 0f);

                rotateYaw += 1;

                GL11.glRotatef(rotateYaw, 1f, 1.0f, 1f);
                DrawVBO();
                CreateVBO();
            }catch(Exception e){

            }
        }
        Display.destroy();
    }

    private void ProcessInput(){
        Keyboard.enableRepeatEvents(true);
          while(Keyboard.next()){

                //If key escape is down we shut the application down
                if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
                    System.exit(0);
                }

                //If key up was pressed move up
                else if(Keyboard.getEventKey() == Keyboard.KEY_UP) {
                    if(Keyboard.getEventKeyState()){
                        if(Keyboard.isRepeatEvent())
                        {
                        System.out.println("KEY DOWN!");
                        moveUp = true;
                        movementFlag = true;
                        }
                    }
                    else{
                        System.out.println("KEY RELEASED!");
                        moveUp = false;
                        movementFlag = false;
                    }
                }
                //If key down was pressed move down
                else if(Keyboard.getEventKey() == Keyboard.KEY_DOWN) {
                    if(Keyboard.getEventKeyState()){
                        if(Keyboard.isRepeatEvent())
                        {
                        System.out.println("KEY DOWN!");
                        moveDown = true;
                        movementFlag = true;
                        }
                    }
                    else{
                        System.out.println("KEY RELEASED!");
                        moveDown = false;
                        movementFlag = false;
                    }
                }
                else if(Keyboard.getEventKey() == Keyboard.KEY_RIGHT){
                if(Keyboard.getEventKeyState()){
                    if(Keyboard.isRepeatEvent())
                    {
                    System.out.println("KEY DOWN!");
                    moveRight = true;
                    movementFlag = true;
                    }
                }
                else{
                    System.out.println("KEY RELEASED!");
                    moveRight = false;
                    movementFlag = false;
                }
            }
                else if(Keyboard.getEventKey() == Keyboard.KEY_LEFT)
                if(Keyboard.getEventKeyState()){
                    if(Keyboard.isRepeatEvent())
                    {
                    System.out.println("KEY DOWN!");
                    moveLeft = true;
                    movementFlag = true;
                    }
                }
                else{
                    System.out.println("KEY RELEASED!");
                    moveLeft = false;
                    movementFlag = false;
                }
            }
            if(Keyboard.getEventKey() == Keyboard.KEY_SPACE) {
            if(Keyboard.getEventKeyState()){
                if(Keyboard.isRepeatEvent())
                {
                System.out.println("KEY DOWN!");
                moveSpace = true;
                movementFlag = true;
                }
            }
            else{
                System.out.println("KEY RELEASED!");
                moveSpace = false;
                movementFlag = false;
            }
        }
            else if(Keyboard.getEventKey() == Keyboard.KEY_LSHIFT) {
                if(Keyboard.getEventKeyState()){
                    if(Keyboard.isRepeatEvent())
                    {
                    System.out.println("KEY DOWN!");
                    moveShift = true;
                    movementFlag = true;
                    }
                }
                else{
                    System.out.println("KEY RELEASED!");
                    moveShift = false;
                    movementFlag = false;
                }
            }

                if(movementFlag == true && moveUp == true){
                    PZ = PZ + 5;
                }
                if(movementFlag == true && moveDown == true){
                    PZ = PZ - 5;
                }
                if(movementFlag == true && moveRight == true){
                    PX = PX + 5;
                }
                if(movementFlag == true && moveLeft == true){
                    PX = PX - 5;
                }
                if(movementFlag == true && moveSpace == true){
                    PY = PY + 5;
                }
                if(movementFlag == true && moveShift == true){
                    PY = PY - 5;
                }
            }

    private void DrawVBO() {
        GL11.glPushMatrix();
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOVertexHandle);
        GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0L);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOColorHandle);
        GL11.glColorPointer(3, GL11.GL_FLOAT, 0, 0L);
        GL11.glDrawArrays(GL11.GL_QUADS, 0, 24);
        GL11.glPopMatrix();
    }

    private void CreateVBO() {
        VBOColorHandle = GL15.glGenBuffers();
        VBOVertexHandle = GL15.glGenBuffers();
        FloatBuffer VertexPositionData = BufferUtils.createFloatBuffer(24 * 3);
        VertexPositionData.put(new float[] {
                1.0f, 1.0f, -1.0f,
                -1.0f, 1.0f, -1.0f,
                -1.0f, 1.0f, 1.0f,
                1.0f, 1.0f, 1.0f,

                1.0f, -1.0f, 1.0f,
                -1.0f, -1.0f, 1.0f,
                -1.0f, -1.0f, -1.0f,
                1.0f, -1.0f, -1.0f,

                1.0f, 1.0f, 1.0f,
                -1.0f, 1.0f, 1.0f,
                -1.0f, -1.0f, 1.0f,
                1.0f, -1.0f, 1.0f,

                1.0f, -1.0f, -1.0f,
                -1.0f, -1.0f, -1.0f,
                -1.0f, 1.0f, -1.0f,
                1.0f, 1.0f, -1.0f,

                -1.0f, 1.0f, 1.0f,
                -1.0f, 1.0f, -1.0f,
                -1.0f, -1.0f, -1.0f,
                -1.0f, -1.0f, 1.0f,

                1.0f, 1.0f, -1.0f,
                1.0f, 1.0f, 1.0f,
                1.0f, -1.0f, 1.0f,
                1.0f, -1.0f, -1.0f
                });
        VertexPositionData.flip();
        FloatBuffer VertexColorData = BufferUtils.createFloatBuffer(24 * 3);
        VertexColorData.put(new float[] { 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1,1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1,1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1,1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1,1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1,1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, });
        VertexColorData.flip();

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOVertexHandle);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, VertexPositionData,
                GL15.GL_STATIC_DRAW);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOColorHandle);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, VertexColorData,
                GL15.GL_STATIC_DRAW);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    }

    private void Render(){
        CreateVBO();
        DrawVBO();
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glLoadIdentity();

         GL11.glTranslatef(-3f, 0.0f, -20f);
         GL11.glRotatef(45f, 0.4f, 1.0f, 0.1f);
         GL11.glColor3f(255.0f, 255.0f, 255.0f); 

         Block.blockType = "Default";
         for(int x = 0; x<3;x++){
             for(int y = 0; y<3;y++){
             for(int z = 0; z<3;z++){
             RenderCube();
             GL11.glTranslatef(0f, 0.0f, 2f);
             }
             GL11.glTranslatef(0f, 2f, -6f);
             }
             GL11.glTranslatef(2f, -6f, 0);
            }
    }

    private void SetWireMesh(){
         GL11.glBegin(GL11.GL_LINE_LOOP);
    }

    private void SetHole(){
        GL11.glBegin(GL11.GL_QUADS );
    }

    public void RenderCube() {
        if(isWireMesh == true){
            SetWireMesh();
        }
        else{
            SetHole();
        }
        if(Block.blockType == "Default"){
             GL11.glColor3f(255.0f, 255.0f, 255.0f);
             GL11.glVertex3f(1.0f, 1.0f, -1.0f);
             GL11.glVertex3f(-1.0f, 1.0f, -1.0f);
             GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
             GL11.glVertex3f(1.0f, 1.0f, 1.0f);
             GL11.glColor3f(255.0f, 255.5f, 255.0f);
             GL11.glVertex3f(1.0f, -1.0f, 1.0f);
             GL11.glVertex3f(-1.0f, -1.0f, 1.0f);
             GL11.glVertex3f(-1.0f, -1.0f, -1.0f);
             GL11.glVertex3f(1.0f, -1.0f, -1.0f);
             GL11.glColor3f(255.0f, 255.0f, 255.0f); 
             GL11.glVertex3f(1.0f, 1.0f, 1.0f); 
             GL11.glVertex3f(-1.0f, 1.0f, 1.0f); 
             GL11.glVertex3f(-1.0f, -1.0f, 1.0f); 
             GL11.glVertex3f(1.0f, -1.0f, 1.0f);
             GL11.glColor3f(255.0f, 255.0f, 255.0f);
             GL11.glVertex3f(1.0f, -1.0f, -1.0f); 
             GL11.glVertex3f(-1.0f, -1.0f, -1.0f);
             GL11.glVertex3f(-1.0f, 1.0f, -1.0f);
             GL11.glVertex3f(1.0f, 1.0f, -1.0f);
             GL11.glColor3f(255.0f, 255.0f, 255.0f);
             GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
             GL11.glVertex3f(-1.0f, 1.0f, -1.0f);
             GL11.glVertex3f(-1.0f, -1.0f, -1.0f);
             GL11.glVertex3f(-1.0f, -1.0f, 1.0f); 
             GL11.glColor3f(255.0f, 255.0f, 255.0f);
             GL11.glVertex3f(1.0f, 1.0f, -1.0f);
             GL11.glVertex3f(1.0f, 1.0f, 1.0f);
             GL11.glVertex3f(1.0f, -1.0f, 1.0f); 
             GL11.glVertex3f(1.0f, -1.0f, -1.0f); 
             GL11.glEnd();
        }
        if(Block.blockType == "UserPlaced"){
             GL11.glColor3f(-1.0f, 1.0f, 1.0f);
             GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
             GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
             GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
             GL11.glVertex3f(1.0f, 1.0f, 1.0f);
             GL11.glColor3f(1.0f, 0.5f, 0.0f);
             GL11.glVertex3f(1.0f, -1.0f, 1.0f);
             GL11.glVertex3f(-1.0f, -1.0f, 1.0f);
             GL11.glVertex3f(-1.0f, -1.0f, -1.0f);
             GL11.glVertex3f(1.0f, -1.0f, -1.0f);
             GL11.glColor3f(1.0f, 0.0f, 0.0f); 
             GL11.glVertex3f(1.0f, 1.0f, 1.0f); 
             GL11.glVertex3f(-1.0f, 1.0f, 1.0f); 
             GL11.glVertex3f(-1.0f, -1.0f, 1.0f); 
             GL11.glVertex3f(1.0f, -1.0f, 1.0f);
             GL11.glColor3f(1.0f, 1.0f, 0.0f);
             GL11.glVertex3f(1.0f, -1.0f, -1.0f); 
             GL11.glVertex3f(-1.0f, -1.0f, -1.0f);
             GL11.glVertex3f(-1.0f, 1.0f, -1.0f);
             GL11.glVertex3f(1.0f, 1.0f, -1.0f);
             GL11.glColor3f(0.0f, 0.0f, 1.0f);
             GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
             GL11.glVertex3f(-1.0f, 1.0f, -1.0f);
             GL11.glVertex3f(-1.0f, -1.0f, -1.0f);
             GL11.glVertex3f(-1.0f, -1.0f, 1.0f); 
             GL11.glColor3f(1.0f, 0.0f, 1.0f);
             GL11.glVertex3f(1.0f, 1.0f, -1.0f);
             GL11.glVertex3f(1.0f, 1.0f, 1.0f);
             GL11.glVertex3f(1.0f, -1.0f, 1.0f); 
             GL11.glVertex3f(1.0f, -1.0f, -1.0f); 
             GL11.glEnd();
        }
    }

}

我为鼠标更新创建了一个类。不要担心块和块类。他们是为了以后。当您按下W键或向上箭头时,鼠标类用于十字准线并沿其方向移动。

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.util.glu.GLU;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;

public class mouseUpdates{

}

注意: 我尝试过使用AWT和javax.swing制作十字线。两者都没有与lwjgl合作。我从这里获取了我的信息的网站https://sites.google.com/site/voxelenginelwjglport/home 我希望我已经提供了足够的信息来帮助你。

1 个答案:

答案 0 :(得分:0)

经过一番研究,我发现gluPerspective是最小和最大视距。在我在渲染功能中的第一次翻译调用中,我将设置值更改为PX,PY和PZ。

现在渲染功能:

private void Render(){
        CreateVBO();
        DrawVBO();
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glLoadIdentity();

         GL11.glTranslatef(PX, PY, PZ);
         GL11.glRotatef(45f, 0.4f, 1.0f, 0.1f);
         GL11.glColor3f(255.0f, 255.0f, 255.0f); 

         Block.blockType = "Default";
         for(int x = 0; x<3;x++){
             for(int y = 0; y<3;y++){
             for(int z = 0; z<3;z++){
             RenderCube();
             GL11.glTranslatef(0f, 0.0f, 2f);
             }
             GL11.glTranslatef(0f, 2f, -6f);
             }
             GL11.glTranslatef(2f, -6f, 0);
            }
    }