如何解决如何在java中使用glrotate只旋转一个对象
我试图只旋转一个正方形,但它无法旋转所有正方形
在此代码中,当我单击 N 时,它会创建一个新方块并将其推入我想要的数组中,当我单击 R 时
旋转特定方块而不是所有方块
当我在谷歌上搜索时,我发现 glrotate 移动了调用后创建的所有对象,我做到了
知道是否正确 包分配;
import static org.lwjgl.opengl.GL11.*;
import java.util.ArrayList;
import java.util.Random;
import org.lwjgl.opengl.display;
import org.lwjgl.opengl.displayMode;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
public class input {
private static final ArrayList<Square> shapes = new ArrayList<Square>();
private static boolean thingselected=false;
private static boolean flag=true;
public static void main(String[] args) {
// Todo Auto-generated method stub
try {
display.setdisplayMode(new displayMode(640,480));
display.setTitle("Input Demo");
display.create();
} catch (LWJGLException e) {
e.printstacktrace();
display.destroy();
System.exit(1);
}
glMatrixMode(GL_PROJECTION);
glOrtho(0,640,480,1,-1);
glMatrixMode(GL_MODELVIEW);
shapes.add(new Square(20,20));
while (!display.isCloseRequested()) {
glClear(GL_COLOR_BUFFER_BIT);
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
display.destroy();
System.exit(0);
}
while(Keyboard.next()) {
if(Keyboard.getEventKey()==Keyboard.KEY_N && Keyboard.getEventKeyState()) {
shapes.add(new Square(15,15));
}
}
for (final Square square:shapes) {
System.out.println(square.rotated);
if(Mouse.isButtonDown(0) && square.ismoved(Mouse.getX(),480-`Mouse.getY())&&!thingselected) {`
square.selected=true;
thingselected=true;
}
if(square.selected) {
square.update(Mouse.getDX(),-Mouse.getDY());
}
if(Mouse.isButtonDown(1) ) {
square.selected=false;
thingselected=false;
}
if(Keyboard.isKeyDown(Keyboard.KEY_C)&&square.ismoved(Mouse.getX(),480-Mouse.getY())&&!thingselected ) {
square.changeColor();
}
if(Keyboard.getEventKey() == Keyboard.KEY_R && square.ismoved(Mouse.getX(),480-Mouse.getY())){
if(flag)
square.rotated=true;
if(square.rotated)
{
square.rotate();
}
flag = false;
}
if(Keyboard.getEventKey() == Keyboard.KEY_T) {
square.transelate();
}
square.draw();
}
display.update();
display.sync(60);
}
display.destroy();
}
private static class Square{
public int x,y;
private float red,green,blue;
public boolean selected=false;
public boolean rotated=false;
Square(int x,int y){
this.x=x;
this.y=y;
Random random=new Random();
red=random.nextFloat();
green=random.nextFloat();
blue=random.nextFloat();
}
void draw() {
glColor3f(red,blue);
glBegin(GL_QUADS);
glVertex2f(x,y);
glVertex2f(x+50,y+50);
glVertex2f(x,y+50);
glEnd();
}
boolean ismoved(int mousex,int mousey) {
return mousex > x && mousex < x+50 && mousey > y && mousey < y+50;
}
void update(double dx,double dy) {
x+=dx;
y+=dy;
}
void changeColor() {
Random random=new Random();
red=random.nextFloat();
green=random.nextFloat();
blue=random.nextFloat();
}
void transelate() {
glTranslated(0.1,0);
}
void rotate() {
glrotated(0.1,1);
}
}
解决方法
glRotate
和 glTranslate
操作当前矩阵。您必须在绘制对象之前进行旋转和平移。使用 glPushMatrix
和 glPopMatrix
(参见 glPushMatrix
/ glPopMatrix
)将当前矩阵在更改之前保存在矩阵堆栈上,并在绘制对象后恢复它。
为旋转和平移添加变量,并在 translate
和 roatate
中更改它们。使用 draw
中的变量:
private static class Square {
private double translateX,angleZ;
// [...]
void draw() {
glPushMatrix();
glTranslated(translateX,0);
glRotated(angleZ,1);
glColor3f(red,green,blue);
glBegin(GL_QUADS);
glVertex2f(x,y);
glVertex2f(x+50,y+50);
glVertex2f(x,y+50);
glEnd();
glPopMatrix();
}
void transelate() {
translateX += 0.1;
}
void rotate() {
angleZ += 0.1;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。