当前位置: 首页 > 知识库问答 >
问题:

OpenGL沿y轴透视扭曲场景比预期的要多得多

柳奇思
2023-03-14

嘿,我已经开始使用JOGL了,除了使用FloatBuffer矩阵时,我的创作在y轴上被扭曲了之外,一切都很顺利。

y轴畸变图片

它应该看起来像下面的图片,但目前我不能使它这样做,而仍然被允许移动。

它应该是什么样子的图片

我看到几年前,有人有这个问题(但关于z轴),但我无法使用他们给出的更正来解决我的问题。这是链接:Link

public static void main(String[]args)
{       
    Frame frame = new Frame("JOGL Events");
    Toolkit t=Toolkit.getDefaultToolkit();
    Image img=new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    Cursor pointer=t.createCustomCursor(img, new Point(0,0), "none");
    Driver m=new Driver();
    GLCanvas canvas = new GLCanvas();
    canvas.addGLEventListener(m);
    canvas.addKeyListener(m);
    canvas.addMouseListener(m);
    canvas.setFocusable(true);
    canvas.requestFocus();

    frame.add(canvas);
    frame.setUndecorated(true);
    frame.setSize(1024, 768);
    frame.setLocationRelativeTo(null);
    frame.setCursor(pointer);
    frame.setVisible(true);
    GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
    if(fullscreen){
        ge.getDefaultScreenDevice().setFullScreenWindow(frame);
    }
    final Animator animator = new Animator(canvas);
    animator.setRunAsFastAsPossible(true);
    animator.start();
    Rectangle r=frame.getBounds();
    center=new Point(r.x+r.width/2, r.y+r.height/2);
}

public void init(GLAutoDrawable drawable) {
    final GL2 gl = drawable.getGL().getGL2();
    gl.glShadeModel( GL2.GL_SMOOTH );
    gl.glClearColor( 0f, 0f, 0f, 0f );
    gl.glClearDepth( 1.0f );
    gl.glEnable( GL2.GL_DEPTH_TEST );
    gl.glDepthFunc( GL2.GL_LEQUAL );
    gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST );
    gh=new GameHandler(gl);
    gh.setMouseCenter(center);
}

public void dispose(GLAutoDrawable drawable) {

}

public void display(GLAutoDrawable drawable) {
    final GL2 gl = drawable.getGL().getGL2(); 
    gl.glShadeModel( GL2.GL_SMOOTH );
    gl.glClearColor( 0f, 0f, 0f, 0f );
    gl.glClearDepth( 1.0f );
    gl.glEnable( GL2.GL_DEPTH_TEST );
    gl.glDepthFunc( GL2.GL_LEQUAL );
    gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
    //gl.glColor3f(1f,0f,0f);
    gl.glClear (GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT );      
    // Clear The Screen And The Depth Buffer 
    gl.glLoadIdentity();

    //gl.glTranslatef( -0.0f, 0.0f, -6f );  // Move the triangle  
    //gl.glRotatef( rotZ+0f, rotX+.5f, rotY+1f, rotX+1.0f ); 


    //gl.glFrustumf(left, right, bottom, top, zNear, zFar);

    gh.run();
    gl.glFlush();
}

public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {

    final GL2 gl = drawable.getGL().getGL2(); 
    if(height <=0) 
    height =1; 
    final float h = ( float ) width / ( float ) height; 
    gl.glViewport( 0, 0, width, height ); 
    gl.glMatrixMode( GL2.GL_PROJECTION ); 
    gl.glLoadIdentity(); 
    glu.gluPerspective( 45.0f, h, 0.01f, 50.0f); 
    glu.gluLookAt(0, 0, 1, 0, 0, 0, 0, 1, 0);
    gl.glMatrixMode( GL2.GL_MODELVIEW ); 
    gl.glLoadIdentity();
        gl.glLoadIdentity();
    center=new Point(x+width/2, y+height/2);
    gh.setMouseCenter(center);
}
//in GameHandler
public void checkEvents()
{
    long now=System.nanoTime();
    float period=(float)((now-lastTime)*0.000005);
    lastTime=now;

    dx=MouseInfo.getPointerInfo().getLocation().x;
    dy=MouseInfo.getPointerInfo().getLocation().y;
    float head=(mouseCenter.x-dx)/3;
    float pit=(mouseCenter.y-dy)/3;

    if(head!=0) 
        player.setHeading(head*headSens);
    if(pit!=0) 
        player.setPitch(pit*pitchSens);
    if(ford) 
        player.setFord((float)period);
    if(back) 
        player.setBack((float)period);
    if(strafel) 
        player.setStrafel((float)period);
    if(strafer) 
        player.setStrafer((float)period);
    if(jump)
        player.jump("up");
    if(down)
        player.jump("down");
    player.set();

}

public void run()
{
    checkEvents();
    if(robot!=null)
        robot.mouseMove(mouseCenter.x, mouseCenter.y);
    player.draw(gl);
    for(Cube c:getCubes())
        c.draw(gl);

}
//in cube
public void draw(GL2 gl)
{
    gl.glBegin(GL2.GL_QUADS);
     gl.glColor3f(1f,0f,0f);
     gl.glVertex3f(x+-.5f, y+.5f, z+-.5f);
     gl.glVertex3f(x+.5f, y+.5f, z+-.5f);
     gl.glVertex3f(x+.5f, y+-.5f, z+-.5f);
     gl.glVertex3f(x+-.5f, y+-.5f, z+-.5f);

     gl.glColor3f(0f,1f,0f);
     gl.glVertex3f(x+-.5f, y+.5f, z+.5f);
     gl.glVertex3f(x+.5f, y+.5f, z+.5f);
     gl.glVertex3f(x+.5f, y+-.5f, z+.5f);
     gl.glVertex3f(x+-.5f, y+-.5f, z+.5f);

     gl.glColor3f(1f,0f,0f);
     gl.glVertex3f(x+-.5f, y+-.5f, z+.5f);
     gl.glVertex3f(x+-.5f, y+-.5f, z+-.5f);
     gl.glVertex3f(x+-.5f, y+.5f, z+-.5f);
     gl.glVertex3f(x+-.5f,y+.5f, z+.5f);


     gl.glColor3f(0f,0f,1f);
     gl.glVertex3f(x+.5f, y+.5f, z+-.5f);
     gl.glVertex3f(x+.5f, y+-.5f, z+-.5f);
     gl.glVertex3f(x+.5f, y+-.5f, z+.5f);
     gl.glVertex3f(x+.5f, y+.5f, z+.5f);

     gl.glColor3f(1f,0f,1f);
     gl.glVertex3f(x+-.5f, y+.5f, z+-.5f);
     gl.glVertex3f(x+-.5f, y+.5f, z+.5f);
     gl.glVertex3f(x+.5f, y+.5f, z+.5f);
     gl.glVertex3f(x+.5f, y+.5f, z+-.5f);

     gl.glColor3f(1f,.5f,1f);
     gl.glVertex3f(x+-.5f, y+-.5f, z+-.5f);
     gl.glVertex3f(x+-.5f, y+-.5f, z+.5f);
     gl.glVertex3f(x+.5f, y+-.5f, z+.5f);
     gl.glVertex3f(x+.5f, y+-.5f, z+-.5f);

     gl.glEnd();



     gl.glBegin(GL2.GL_LINES);
     gl.glColor3f(1f, 1f, 1f);
     gl.glVertex3f(x+-.5f, y+.5f, z+-.5f);
     gl.glVertex3f(x+.5f, y+.5f, z+-.5f);
     gl.glEnd();

     gl.glBegin(GL2.GL_LINES);
     gl.glColor3f(1f, 1f, 1f);
     gl.glVertex3f(x+.5f, y+.5f, z+-.5f);
     gl.glVertex3f(x+.5f, y+-.5f, z+-.5f);
     gl.glEnd();

     gl.glBegin(GL2.GL_LINES);
     gl.glColor3f(1f, 1f, 1f);
     gl.glVertex3f(x+.5f, y+-.5f, z+-.5f);
     gl.glVertex3f(x+-.5f, y+-.5f, z+-.5f);
     gl.glEnd();

     gl.glBegin(GL2.GL_LINES);
     gl.glColor3f(1f, 1f, 1f);
     gl.glVertex3f(x+-.5f, y+.5f, z+-.5f);
     gl.glVertex3f(x+-.5f, y+-.5f, z+-.5f);
     gl.glEnd();

     gl.glBegin(GL2.GL_LINES);
     gl.glColor3f(1f, 1f, 1f);
     gl.glVertex3f(x+-.5f, y+.5f, z+.5f);
     gl.glVertex3f(x+.5f, y+.5f, z+.5f);
     gl.glEnd();

     gl.glBegin(GL2.GL_LINES);
     gl.glColor3f(1f, 1f, 1f);
     gl.glVertex3f(x+.5f, y+.5f, z+.5f);
     gl.glVertex3f(x+.5f, y+-.5f, z+.5f);
     gl.glEnd();

     gl.glBegin(GL2.GL_LINES);
     gl.glColor3f(1f, 1f, 1f);
     gl.glVertex3f(x+.5f, y+-.5f, z+.5f);
     gl.glVertex3f(x+-.5f, y+-.5f, z+.5f);
     gl.glEnd();

     gl.glBegin(GL2.GL_LINES);
     gl.glColor3f(1f, 1f, 1f);
     gl.glVertex3f(x+-.5f, y+.5f, z+.5f);
     gl.glVertex3f(x+-.5f, y+-.5f, z+.5f);
     gl.glEnd();

     gl.glBegin(GL2.GL_LINES);
     gl.glColor3f(1f, 1f, 1f);
     gl.glVertex3f(x+-.5f, y+-.5f, z+-.5f);
     gl.glVertex3f(x+-.5f, y+-.5f, z+.5f);
     gl.glEnd();

     gl.glBegin(GL2.GL_LINES);
     gl.glColor3f(1f, 1f, 1f);
     gl.glVertex3f(x+.5f, y+-.5f, z+-.5f);
     gl.glVertex3f(x+.5f, y+-.5f, z+.5f);
     gl.glEnd();

     gl.glBegin(GL2.GL_LINES);
     gl.glColor3f(1f, 1f, 1f);
     gl.glVertex3f(x+-.5f, y+.5f, z+-.5f);
     gl.glVertex3f(x+-.5f, y+.5f, z+.5f);
     gl.glEnd();

     gl.glBegin(GL2.GL_LINES);
     gl.glColor3f(1f, 1f, 1f);
     gl.glVertex3f(x+.5f, y+.5f, z+-.5f);
     gl.glVertex3f(x+.5f, y+.5f, z+.5f);
     gl.glEnd();
}
//player class
public class Player {

private static final float _90=(float)Math.toRadians(90);
private static final float _maxPitch=(float)Math.toRadians(90);
private float heading=0.0f;
private float pitch=0.0f;
private float cosa, cosb, cosz, sina, sinb, sinz;
private float cosc=1.0f;
private float sinc=0.0f;
private float x,y,z;
private float[] mat={ 1,0,0,0,
        0,1,0,0,
        0,0,1,0,
        0,0,0,1};
private FloatBuffer matrix;

public Player()
{
    matrix=Buffers.newDirectFloatBuffer(mat.length);
    matrix.put(mat);
    x=y=z=0;
}

public void setHeading(float amount){
    heading-=amount;
    cosb=(float)Math.toRadians(Math.cos(heading));
    sinb=(float)Math.toRadians(Math.sin(heading));
    cosz=(float)Math.toRadians(Math.cos(heading+_90));
    sinz=(float)Math.toRadians(Math.sin(heading+_90));
}

public void setPitch(float amount){
    pitch-=amount;
    if(pitch>_maxPitch)pitch=_maxPitch;
    if(pitch<-_maxPitch)pitch=-_maxPitch;
    cosa=(float)Math.cos(pitch);
    sina=(float)Math.sin(pitch);
}

public void setFord(float amount){
    x+=cosz*cosa*amount*2;
    z+=sinz*cosa*amount*2;
    y+=Math.toRadians(sina)*2;

}

public void setBack(float amount){
    x-=cosz*cosa*amount*2;
    z-=sinz*cosa*amount*2;
    y-=Math.toRadians(sina)*2;
}

public void setStrafel(float amount){
    x+=cosb*amount*2;
    z+=sinb*amount*2;
}

public void setStrafer(float amount){
    x-=cosb*amount*2;
    z-=sinb*amount*2;
}

public void set(){
    matrix.put(0, cosc*cosb-sinc*sina*sinb);
    matrix.put(1, sinc*cosb+cosc*sina*sinb);
    matrix.put(2, -cosa*sinb);
    matrix.put(4, -sinc*cosa);
    matrix.put(5, cosc*cosa);
    matrix.put(6, sina);
    matrix.put(8, cosc*sinb+sinc*sina*cosb);
    matrix.put(9, sinc*sinb-cosc*sina*cosb);
    matrix.put(10, cosa*cosb);
    matrix.put(12, matrix.get(0)*x+matrix.get(4)*y+matrix.get(8)*z);
    matrix.put(13, matrix.get(1)*x+matrix.get(5)*y+matrix.get(9)*z);
    matrix.put(14, matrix.get(2)*x+matrix.get(6)*y+matrix.get(10)*z);
}

public void jump(String dir)
{
    if(dir.equals("up"))
        y-=.03;
    else if(dir.equals("down"))
        y+=.03;
}

public void draw(GL2 gl){
    gl.glLoadIdentity();
    gl.glBegin(gl.GL_QUADS);
    gl.glColor3f(5.0f, 0.0f, 3.0f);
    gl.glVertex3f(1.0f, 1.0f, 0.0f);
    gl.glVertex3f(2.0f, 1.0f, 0.0f);
    gl.glVertex3f(2.0f, 1.0f, -5.0f);
    gl.glVertex3f(1.0f, 1.0f, -5.0f);
    gl.glEnd();
    matrix.rewind();
    gl.glLoadMatrixf(matrix);
}

谢谢你抽出时间

共有1个答案

蒙华翰
2023-03-14

对不是角度的值调用toradians:

cosb=(float)Math.toRadians(Math.cos(heading));
sinb=(float)Math.toRadians(Math.sin(heading));
cosz=(float)Math.toRadians(Math.cos(heading+_90));
sinz=(float)Math.toRadians(Math.sin(heading+_90));

那是胡说八道。因为你只是有时做,这可能会导致扭曲。

 类似资料:
  • 中间的正方形是用正投影画出的立方体(这里立方体的意思是“四边相等”)。从左下角到中心的形状也是一个立方体,但用透视投影绘制! 显然,这并不是看起来不像一个立方体。下面是我的代码(它有点aa墙,所以我把它拆开了): 这些都在另一个类中,每秒调用30次。 我已经使用了zNear和zFar,但是到目前为止,我所尝试的值都没有得到正确的投影。 具体的问题是:为什么第二个立方体出现如此可怕的扭曲,我可以做什

  • 使用我的代码,当我旋转ModelView时;它单独旋转每个对象。 我试图旋转整个视图平面。我怎么能那么做? 渲染器称为每个并图框

  • 我刚刚实现了基本的opengl渲染到我的pyplay应用程序中,认为硬件加速会使程序运行得更快。相反,它要慢得多。 看起来问题是绘图功能。 这是我的opengl绘图功能 我的分析器为draw函数提供了什么 我的分析器文本的其余部分:(1个月后到期) http://pastebin.com/ApfiCQzw 我的源代码 https://bitbucket.org/claysmithr/warbots

  • Highcharts 组合图 以下实例演示了多Y轴的组合。 我们在前面的章节已经了解了 Highcharts 基本配置语法。接下来让我们来看下其他的配置。 配置 yAxis 配置 在 yAxis 属性中添加多条Y轴的值 。 var yAxis = [{ // 第一条Y轴 }, { // 第二条Y轴 },{ // 第三题Y轴 } }] 实例 文件名:hig

  • 给定一个X值(或百分比),我需要沿着二次Bezier曲线找到一点的Y坐标 该曲线是在android中使用以下内容绘制的折线图的一部分: