我需要使用java扩展2d方块

时间:2015-02-25 15:27:23

标签: java opengl

我正在编写一个程序来转换,旋转和缩放2d方格。我有转换和旋转工作,但我需要帮助扩展。我似乎无法在互联网上找到任何帮助,因为我必须使用数学方程,我找不到所需的方程式。只是因为你知道我不想使用gl.glScaled()。我需要使用数学方程,但我无法弄明白。

package lab2;


public class Square {

public double [][] vertices = new double[4][2]; 


public Square(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
{
    vertices[0][0]=x1;
    vertices[0][1]=y1;
    vertices[1][0]=x2;
    vertices[1][1]=y2;
    vertices[2][0]=x3;
    vertices[2][1]=y3;
    vertices[3][0]=x4;
    vertices[3][1]=y4;
}

public double area()
{
    return (vertices[1][0]-vertices[0][0])*(vertices[1][0]-vertices[0][0])+(vertices[1][1]-vertices[0][1])*(vertices[1][1]-vertices[0][1]);
}

public void translate(double tx, double ty)
{
    for(int i=0;i<vertices.length;i++)
    {
        vertices[i][0]+=tx;
        vertices[i][1]+=ty;
    }
}
public void rotate(double theta)
{
    //double x =0;
    double x = (vertices[0][0]+vertices[2][0])/2;
    double y = (vertices[0][1]+vertices[2][1])/2;

    double oldX;

    int i;
    for(i = 0; i < 4; i++){
        oldX = vertices[i][0];
        vertices[i][0] = x + (vertices[i][0]-x)*Math.cos(theta*0.0174532925199)-(vertices[i][1]- y)*Math.sin(theta*0.0174532925199);
        vertices[i][1] = y + (oldX-x)*Math.sin(theta*0.0174532925199)+(vertices[i][1]-y)*Math.cos(theta*0.0174532925199);
    }
}

public void scale(double sx, double sy)
{

}
}

然后我也有了这个SquareControl.java,以便我可以控制我希望它如何工作,所以对于这个程序,我将使用关键事件

package lab2;

import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;

import com.sun.opengl.util.Animator;
import com.sun.opengl.util.FPSAnimator;

public class Square_Control implements GLEventListener, KeyListener {

Square square = new Square(100,100,200,100,200,200,100,200);
boolean rotating = false;
boolean scaling = false;
boolean enlarge = true;
double theta = 1;
double sx = 1.01, sy = 1.01;
GLProfile glp;
GLCapabilities caps;
GLCanvas canvas;

public Square_Control()
{
    glp = GLProfile.getDefault();
        caps = new GLCapabilities(glp);
        canvas = new GLCanvas(caps);

        Frame frame = new Frame("AWT Window Test");
        frame.setSize(300, 300);
        frame.add(canvas);
        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                System.exit(0);
                }
        });

        canvas.addGLEventListener(this);
        canvas.addKeyListener(this);
        canvas.requestFocus();
        Animator animator = new FPSAnimator(canvas,60);
        animator.add(canvas);
        animator.start();

}

public static void main(String[] args) {

    Square_Control sqc = new Square_Control();

}

public void update()
{
    if (rotating)
        square.rotate(theta);
    if (scaling)
    {
        square.scale(1, 1);

    }
}

@Override
public void display(GLAutoDrawable drawable) {
    update();
    GL2 gl = drawable.getGL().getGL2();
    gl.glClear(GL.GL_COLOR_BUFFER_BIT);
    gl.glColor3f(1, 0, 0);
    gl.glBegin(GL.GL_LINE_LOOP);
            gl.glVertex2d(square.vertices[0][0], square.vertices[0][1]);
            gl.glVertex2d(square.vertices[1][0], square.vertices[1][1]);
            gl.glVertex2d(square.vertices[2][0], square.vertices[2][1]);
            gl.glVertex2d(square.vertices[3][0], square.vertices[3][1]);
        gl.glEnd();



}

@Override
public void dispose(GLAutoDrawable drawable) {
    // TODO Auto-generated method stub

}

@Override
public void init(GLAutoDrawable drawable) {
    // TODO Auto-generated method stub
    GL2 gl = drawable.getGL().getGL2();
        gl.glMatrixMode(gl.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrtho(0, 300, 0, 300, -1, 1);
        gl.glViewport(0, 0, 300, 300);

}

@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
        int height) {
    // TODO Auto-generated method stub

}

@Override
public void keyPressed(KeyEvent arg0) {

    if (arg0.getKeyCode() == KeyEvent.VK_RIGHT)
        square.translate(4, 0);
    else if (arg0.getKeyCode() == KeyEvent.VK_LEFT)
        square.translate(-4, 0);
    else if (arg0.getKeyCode() == KeyEvent.VK_UP)
        square.translate(0, 4);
    else if (arg0.getKeyCode() == KeyEvent.VK_DOWN)
        square.translate(0, -4);

    //also add code to toggle rotation/scaling
    if(arg0.getKeyCode() == KeyEvent.VK_R)
    {
        rotating = !rotating;
    }

    if(arg0.getKeyCode() == KeyEvent.VK_S)
    {
        scaling = !scaling;
    }

}

@Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}   
}

1 个答案:

答案 0 :(得分:0)

要缩放对象,只需将每个顶点乘以缩放系数即可。缩放因子1.0将不起作用,而缩放因子2.0将使每个顶点的位置加倍,因此缩放它并可能翻译它。

如果您希望对象保持原位,您必须先将其翻译到对象的中心,缩放,然后转换回原来的位置。

这就是你想要做的工作软件。

您应该考虑使用Matrix函数。 glRotatef,glTranslatef和glScalef。