如何在java中使用glrotate只旋转一个对象

如何解决如何在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);
            
            
    }
}

解决方法

glRotateglTranslate 操作当前矩阵。您必须在绘制对象之前进行旋转和平移。使用 glPushMatrixglPopMatrix(参见 glPushMatrix / glPopMatrix)将当前矩阵在更改之前保存在矩阵堆栈上,并在绘制对象后恢复它。
为旋转和平移添加变量,并在 translateroatate 中更改它们。使用 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?