java - 如何在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, 0, 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, green, blue);
glBegin(GL_QUADS);
glVertex2f(x, y);
glVertex2f(x+50, 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, 0);
}
void rotate() {
glRotated(0.1, 0, 0, 1);
}
}
解决方案
glRotate
并glTranslate
操纵当前矩阵。您必须在绘制对象之前进行旋转和平移。使用 glPushMatrix
和glPopMatrix
(参见glPushMatrix
/glPopMatrix
)在更改它之前将当前矩阵保存在矩阵堆栈上,并在绘制对象后恢复它。
为旋转和平移添加变量并translate
在roatate
. 使用中的变量draw
:
private static class Square {
private double translateX, angleZ;
// [...]
void draw() {
glPushMatrix();
glTranslated(translateX, 0, 0);
glRotated(angleZ, 0, 0, 1);
glColor3f(red, green, blue);
glBegin(GL_QUADS);
glVertex2f(x, y);
glVertex2f(x+50, y);
glVertex2f(x+50, y+50);
glVertex2f(x, y+50);
glEnd();
glPopMatrix();
}
void transelate() {
translateX += 0.1;
}
void rotate() {
angleZ += 0.1;
}
}
推荐阅读
- javascript - 如何使用 input[type="Date"] 在 javascript 中验证出生日期?
- css - 结合变换时如何消除字体模糊和“锁定”效果:缩放与过渡:变换?
- sql - 将 plsql 异常错误写入 DMBS_OUTPUT 和 FND_FILE 作业日志
- android - 如何获取开启 GPS 定位的返回值?
- php - mySQL 连接在脚本中过早关闭
- rust - 如何将结构方法中的 &self 与另一个结构进行比较
- deprecated - Authorize.Net 中是否已弃用 XML API?
- node.js - 使用Node访问另一台服务器的FTP
- python - 如何仅提取数组名称和括号内的内容,并使它们成为键值对?
- javascript - 使用量角器在不同的路径中以角度截取屏幕截图