简体   繁体   中英

LWJGL How to move using x, y, z

Hello I am having trouble using lwjgl to add into the camera coordinates. I think its to do with the gluPerspective I will post my code but could you also tell me how to implement moving in the directing of the mouse. And put a cross hair in the middle of the screen like Minecraft. Please tell me how to implement the MouseUpdates code into my main code.

Here is My Code:

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();
        }
    }

}

I have created a class for the mouse Updates. Do not worry about the block and chunk classes. They are for later. The mouse class is for the crosshair and moving in the direction of it when you press the W key or up arrow.

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{

}

NOTE: I have tried using AWT and javax.swing to make a cross hair. Neither have worked with lwjgl. The website I took my information from here https://sites.google.com/site/voxelenginelwjglport/home I hope I have provided sufficient information to help you.

After a bit of research, I managed to find that gluPerspective was the minimum and maximum view distance. In my first translate call in the render function I changed the set values to PX, PY and PZ.

The render function now:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM