0

所以我想旋转对象,我调用 glPushMatrix() 来保存矩阵,glTranslatef 因为我希望对象有自己的轴,所以我将在屏幕中间移动对象,然后我做一个旋转,调用 glPopMatrix() 并没有任何反应: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{

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

}

4

3 回答 3

3

这是因为你每帧都在做同样的事情。

你是:创建一个三角形,平移它,然后将它从 0 度旋转到 30 度。下一帧,一切都重置了,它做同样的事情,从 0 度旋转到 30 度。

您需要某种持久性数据。

GL11.glRotatef(xRot, 1, 0, 1);
//...
xRot = xRot > 360 ? 0 : xRot + 10;
于 2013-11-01T15:09:03.593 回答
2

天鹰船长的回答是正确的。

此外,请在绘图之前进行转换。

于 2013-11-02T02:31:30.430 回答
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();
}

}

于 2013-11-02T11:13:05.640 回答