2014-04-06 9 views
0

안녕하세요 lwjgl을 사용하여 카메라 좌표를 추가하는 데 문제가 있습니다. 나는 gluPerspective와 관련 있다고 생각합니다. 코드를 게시 할 것이지만, 마우스 지시를 움직이는 방법을 알려줄 수도 있습니다. 그리고 Minecraft와 같이 화면 중앙에 십자형을 긋습니다. 내 메인 코드에 MouseUpdates 코드를 구현하는 방법을 알려주십시오.LWJGL x, y, z를 사용하여 이동하는 방법

여기 내 코드입니다 :

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 도움을 드릴 수있는 충분한 정보를 제공해주기를 바랍니다.

답변

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