当我翻译形状时,它显示相反的坐标x和y

时间:2014-12-20 09:16:49

标签: java opengl jogl

当我在openGL中翻译形状时,它会将形状显示在错误的位置。例如,如果我将其翻译为(-150 -150)

GLUT glut=new GLUT();
gl.glPushMatrix();
gl.glTranslated(-150,-150,0);
glut.glutSolidCone(100f,100,10,10);
gl.glPopMatrix();

它显示(编辑:缺少链接到屏幕截图),但是对于

GLUT glut=new GLUT();
gl.glPushMatrix();
gl.glTranslated(150,150,0);
glut.glutSolidCone(100f,100,10,10);
gl.glPopMatrix();

显示screenshot

整个代码是

import com.sun.opengl.util.Animator;
import com.sun.opengl.util.gl2.*;    //GLUT contain predefined shapes
import javax.media.opengl.glu.GLU;
import javax.media.opengl.GL2;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLAutoDrawable;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Template3D extends JFrame implements GLEventListener,KeyListener,MouseMotionListener,MouseListener{

    GLCanvas canvas;
    Animator a;
    double r=0;;
    double t=0;
    boolean right=true;
    boolean left=false;

    public Template3D(){
        canvas=new GLCanvas();
        a=new Animator(canvas);
        a.start();
        add(canvas);
        canvas.addGLEventListener(this);
        canvas.addKeyListener(this);
    }
    public void init(GLAutoDrawable d){

        GL2 gl=d.getGL().getGL2();
        gl.glClearColor(1f,1f,1f,0);
        gl.glEnable(GL2.GL_DEPTH_TEST);
        gl.glOrtho(400,-400,300,-300,400,-400);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glMatrixMode(GL2.GL_MODELVIEW);


    }
    public void display(GLAutoDrawable d){

        GL2 gl=d.getGL().getGL2();
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT|GL2.GL_DEPTH_BUFFER_BIT);

        gl.glColor3f(1,0,1);

        //predefined shapes
        GLUT glut=new GLUT();

        gl.glPushMatrix();
        gl.glTranslated(150,150,0);
        glut.glutSolidCone(100f,100,10,10);
        gl.glPopMatrix();
    //  gl.glPushMatrix();
    //  gl.glTranslatef(150,150,0);
    //  glut.glutSolidTorus(100f,100,10,10);
    //  gl.glPopMatrix();

    }
    public void reshape(GLAutoDrawable d,int x,int y,int width,int high){}
    public void dispose(GLAutoDrawable d){}

    //KeyListener
    public void keyPressed(KeyEvent e){}
    public void keyReleased(KeyEvent e){}
    public void keyTyped(KeyEvent e){}

    //MouseMotionListener
    public void mouseMoved(MouseEvent e){}
    public void mouseDragged(MouseEvent e){}
    public void mouseDroped(MouseEvent e){}

    //MouseListener
    public void mouseClicked(MouseEvent e){}
    public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}




    public static void main(String []dfg){
        Template3D sample=new Template3D();
        sample.setSize(800,600);
        sample.setVisible(true);
        sample.setDefaultCloseOperation(EXIT_ON_CLOSE);
        sample.setLocationRelativeTo(null);
    }
}

1 个答案:

答案 0 :(得分:1)

您的症状是由此次电话引起的:

gl.glOrtho(400,-400,300,-300,400,-400);

glOrtho()的签名是:

void glOrtho(GLdouble left, GLdouble right,
             GLdouble bottom, GLdouble top,  
             GLdouble nearVal, GLdouble farVal); 

所以你的价值观是:

left:   400
right: -400
bottom: 300
top:   -300

左边大于右边,x坐标方向是从右到左。底部大于顶部,y坐标方向从上到下。

因此,如果使用正坐标值转换几何体,它将向左和向下移动。这正是你所看到的。

如果你想要一个更传统的坐标系,x从左到右,y从下到上,你的glOrtho()调用应该是这样的:

gl.glOrtho(-400.0, 400.0, -300.0, 300.0, -400.0, 400.0);