2013-11-01 53 views
0

所以我想旋轉對象,我調用glPushMatrix()來保存矩陣glTranslatef,因爲我想讓對象擁有自己的軸,所以我將把對象移動到屏幕中間,然後我做了旋轉,調用glPopMatrix()並沒有任何反應:dObject not moving

package Tanc; 

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.*; 
public class Tanc{ 

public Tanc(){ 
    try{ 
     Display.setDisplayMode(new DisplayMode(640,480)); 
     Display.setTitle("Tancc"); 
     Display.create(); 
    }catch(LWJGLException e){ 
     e.printStackTrace(); 
    } 
    GL11.glMatrixMode(GL11.GL_MODELVIEW); 
    GL11.glLoadIdentity(); 
    GL11.glOrtho(1, 1, 1, 1, 1, -1); 
    GL11.glEnable(GL11.GL_TEXTURE_2D); 
    GL11.glMatrixMode(GL11.GL_PROJECTION); 

    float y_angle = 0; 

    while (!Display.isCloseRequested()) { 
     GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); 
     GL11.glPushMatrix(); 

/*  
     GL11.glRectd(0, 0, 0.2f, 0.3f); 
     // GL11.glRotated(50, 1, 0, 0); 
     // GL11.glRotated(50, 0, 1, 0); 
     GL11.glRotated(1, 0, 0, 1); 
    // GL11.glTranslatef(0, 0, 0); 
     GL11.glTranslatef(-0.1f, -0.15f, 0); 
    // GL11.glTranslatef(-1f, -0.15f, 0); 
     GL11.glPopMatrix(); 
*/ 
     GL11.glLoadIdentity(); 
     GL11.glPushMatrix(); 
     GL11.glBegin(GL11.GL_QUADS); 
     GL11.glVertex2f(0f, 0f); 
     GL11.glVertex2f(0.2f, 0f); 
     GL11.glVertex2f(0.2f, 0.3f); 
     GL11.glVertex2f(0f, 0.3f); 
     GL11.glEnd(); 
     GL11.glTranslatef(-0.1f, -0.15f, 0); 
     GL11.glRotatef(30, 1, 0, 1); 
     GL11.glPopMatrix(); 
     Display.update(); 
     Display.sync(60); 
    }  
    Display.destroy(); 
    System.exit(0); 
} 

public static void main(String[] args){ 
    new Tanc(); 
} 

}

回答

2

這是因爲你在做同樣的事情,每一幀。

你是:創建一個三角形,翻譯它,並將其從0旋轉到30度。下一幀,一切都重置,並且它完全一樣,從0度旋轉到30度。

您需要某種持久性數據。

GL11.glRotatef(xRot, 1, 0, 1); 
//... 
xRot = xRot > 360 ? 0 : xRot + 10; 
+0

什麼都沒有改變。 –

+0

沒有道理。你有沒有編碼背景?如果沒有,我會回到基礎知識,然後進入3D編程。 –

+0

另外,確保你在while循環之外聲明瞭xRot,否則你會遇到同樣的問題。 –

1

天鷹隊長的回答是正確的。

此外,在繪製之前進行轉換。

0

我終於修好了:)而且它只旋轉一個對象:D。

package Tanc; 

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.*; 
public class Tanc{ 
private void useless(){ 
    /*  
    GL11.glRectd(0, 0, 0.2f, 0.3f); 
    // GL11.glRotated(50, 1, 0, 0); 
    // GL11.glRotated(50, 0, 1, 0); 
    GL11.glRotated(1, 0, 0, 1); 
// GL11.glTranslatef(0, 0, 0); 
    GL11.glTranslatef(-0.1f, -0.15f, 0); 
// GL11.glTranslatef(-1f, -0.15f, 0); 
    GL11.glPopMatrix(); 
*/ 

     //   GL11.glLoadIdentity(); 
     } 
    public Tanc(){ 

    try{ 
     Display.setDisplayMode(new DisplayMode(640,480)); 
     Display.setTitle("Tancc"); 
     Display.create(); 
    }catch(LWJGLException e){ 
     e.printStackTrace(); 
    } 
    GL11.glMatrixMode(GL11.GL_MODELVIEW); 
    GL11.glLoadIdentity(); 
    GL11.glOrtho(1, 1, 1, 1, 1, -1); 
    GL11.glEnable(GL11.GL_TEXTURE_2D); 
    GL11.glMatrixMode(GL11.GL_PROJECTION); 

    float y_angle = 0; 
    float xrot= Mouse.getDY(); 
    float yrot =Mouse.getDX(); 
    float a=0,b=0,c=0; 
    while (!Display.isCloseRequested()) { 

     GL11.glPushMatrix(); 
     GL11.glRotatef(yrot, 0, 0, 1); 
     GL11.glRotatef(xrot, 0, 0, 1); 
     GL11.glTranslatef(a, b, 0); 
     GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); 


     GL11.glBegin(GL11.GL_QUADS); 
     GL11.glVertex2f(0f, 0f); 
     GL11.glVertex2f(0.2f, 0f); 
     GL11.glVertex2f(0.2f, 0.3f); 
     GL11.glVertex2f(0f, 0.3f); 
     GL11.glEnd(); 



     if(Keyboard.isKeyDown(Keyboard.KEY_A)){ 
     xrot = xrot > 360 ? 0 : xrot + 5; 
    // a += 0.01f; 
     // b += 0.01f; 
     } 
    // if(Keyboard.isKeyDown(Keyboard.KEY_D)){ 
      // if(xrot>360){ 
      // xrot = 0; 
      //}else { 
       xrot += Mouse.getDY()*1f; 
       yrot -= Mouse.getDX()*1f; 
      // } 
    // } 
     GL11.glPopMatrix(); 

     GL11.glBegin(GL11.GL_QUADS); 


     GL11.glVertex2f(0.2f, 0.2f); 
     GL11.glVertex2f(0.3f, 0.2f); 
     GL11.glVertex2f(0.3f, 0.3f); 
     GL11.glVertex2f(0.2f, 0.3f); 
     GL11.glEnd(); 

     Display.update(); 
     Display.sync(60); 
    }  
    Display.destroy(); 
    System.exit(0); 
} 

public static void main(String[] args){ 
    new Tanc(); 
} 

}