所以我想旋转对象,我调用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();
}
}
答案 0 :(得分:3)
这是因为你每帧都在做同样的事情。
你是:创建一个三角形,翻译它,并将其从0度旋转到30度。下一帧,一切都重置,它完全相同,从0度旋转到30度。
您需要某种持久性数据。
GL11.glRotatef(xRot, 1, 0, 1);
//...
xRot = xRot > 360 ? 0 : xRot + 10;
答案 1 :(得分:2)
Skyhawk上尉的回答是正确的。
此外,在绘图之前进行转换。
答案 2 :(得分: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();
}
}