为什么这不是正方形?轻量级 [英] Why isn't this a square? LWJGL

查看:86
本文介绍了为什么这不是正方形?轻量级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的LWJGL窗口设置,我正在尝试使用glBegin(GL_QUADS)方法绘制一个正方形. Square square = new Square(25, 25, 25)是我调用Square类绘制正方形的方式...但是它是一个矩形.当我调用它时,我将所有25都作为参数传递.前两个是起始坐标,后25个是边长,如下所示.产生矩形我做错了什么?

I have a basic LWJGL window set up and I am trying to draw a square using the glBegin(GL_QUADS) method. Square square = new Square(25, 25, 25), is the way I am calling my Square class to draw the square... but it is a rectangle. When I call it I pass in all 25's as the parameters. the first two are the starting coordinates and the last 25 is the side length, as seen below. What am I doing wrong to produce a rectangle?

public Square(float x,float y,float sl) {
    GL11.glColor3f(0.5F, 0.0F, 0.7F);
    glBegin(GL11.GL_QUADS);
        glVertex2f(x, y);
        glVertex2f(x, y+sl);
        glVertex2f(x+sl, y+sl);
        glVertex2f(x+sl, y);
    glEnd();
}

我的视口代码

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity(); // Resets any previous projection matrices
    glOrtho(0, 640, 0, 480, 1, -1);
    glMatrixMode(GL_MODELVIEW);

推荐答案

使用glOrtho(0, 640, 0, 480, 1, -1);构造一个非正方形视口.这意味着,如果您的窗口与视口的大小不同(或至少具有相同的宽高比),则呈现的输出很可能会发生歪斜.

Using glOrtho(0, 640, 0, 480, 1, -1); constructs a non-square viewport. That means that the rendered output is more than likely going to be skewed if your window is not the same size as your viewport (or at least the same aspect ratio).

请考虑以下比较:

如果视口与窗口的大小相同,则应保持正方形.我正在使用 JOGL ,但是在我的调整大小功能中,我将视口重塑为窗口的新大小.

If your viewport is the same size as your window, then it should remain square. I'm using JOGL, but in my resize function, I reshape my viewport to be the new size of my window.

glcanvas.addGLEventListener(new GLEventListener() {
    @Override
    public void reshape(GLAutoDrawable glautodrawable, int x, int y, int width, int height) {
        GL2 gl = glautodrawable.getGL().getGL2();

        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity(); // Resets any previous projection matrices
        gl.glOrtho(0, width, 0, height, 1, -1);
        gl.glMatrixMode(GL2.GL_MODELVIEW);
    }

    ... Other methods

}

这篇关于为什么这不是正方形?轻量级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆