简体   繁体   English

在opengl的矩形窗口上绘制一个正方形

[英]Drawing a square on a rectangular window in opengl

I am drawing a 3d cube in LWJGL 3.It renders fine with the window size being 600 * 480.It renders a perfect cube.我正在 LWJGL 3 中绘制一个 3d 立方体。它渲染得很好,窗口大小为 600 * 480。它渲染了一个完美的立方体。

But if i increase the window's width to 900 while the height remains the same.但是如果我将窗口的宽度增加到 900 而高度保持不变。 The cube have a rectangular shape.立方体具有矩形形状。

In 640 * 480 resolution the front face of cube is :在 640 * 480 分辨率下,立方体的正面是:

In 900 * 480 resolution the front face of cube is :在 900 * 480 分辨率下,立方体的正面是:

I want to render a perfect square without it resembling a rectangle.我想渲染一个完美的正方形,而不像矩形。

if required the, The render loop is as follows :如果需要,渲染循环如下:

private void loop() {

GL.createCapabilities();

        // Set the clear color
        glClearColor(0f, 0.0f, 0.0f, 0.0f);

        float x = 0.5f;
        float y = 0.5f;
        float z = 0.5f;
        float[] vertices = new float[] {
                // VO
                -x, y, z,
                // V1
                -x, -y, z,
                // V2
                x, -y, z,
                // V3
                x, y, z,
                // V4
                -x, y, -z,
                // V5
                x, y, -z,
                // V6
                -x, -y, -z,
                // V7
                x, -y, -z, };

        float[] colours = new float[] {
               0.5f, 0.0f, 0.0f,
              0.0f, 0.5f, 0.0f, 
             0.0f, 0.0f, 0.5f, 
             0.0f, 0.5f, 0.5f,
              0.5f,0.0f, 0.0f, 
             0.0f, 0.5f, 0.0f,
              0.0f, 0.0f, 0.5f,
              0.0f, 0.5f, 0.5f, };

        int[] indices = new int[] {
                // Front face
                0, 1, 3, 3, 1, 2,
                // Top Face
                4, 0, 3, 5, 4, 3,
                // Right face
                3, 2, 7, 5, 3, 7,
                // Left face
                6, 1, 0, 6, 0, 4,
                // Bottom face
                2, 1, 6, 2, 6, 7,
                // Back face
                7, 6, 4, 7, 4, 5, };

        RawModel model = Loader.loadRawModel(vertices, indices, colours);
        ShaderProgram shader = null;

        Transformation transformation = new Transformation();

        try {

            shader = new ShaderProgram(Utils.loadTextFile("src/shaders/vertexShader.txt"),
                    Utils.loadTextFile("src/shaders/fragmentShader.txt"));
            shader.link();
            shader.createUniform("projectionMatrix");
            shader.createUniform("worldMatrix");
        } catch (Exception e) {
            e.printStackTrace();
        }

        float rotation = 0;


        while (!glfwWindowShouldClose(window)) {
            glEnable(GL11.GL_DEPTH_TEST);
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

            shader.bind();

            shader.setUniform("projectionMatrix", transformation.getProjectionMatrix((float) Math.toRadians(60),
                    (float) (width / height), 0.1f, 1000f));
            shader.setUniform("worldMatrix", transformation.getWorldMatrix(new Vector3f(0, 0, -3f),
                    new Vector3f(rotation, rotation, rotation), 1));
            model.render();

            shader.unBind();

            glfwSwapBuffers(window); 
            glfwPollEvents();
        }

        shader.dispose();
        Loader.dispose();
    }

Thanks to @Derhass the problem is solved now.The problem was that i calculated the aspect ration as :感谢@Derhass,问题现在解决了。问题是我计算的纵横比为:

float aspectRatio = (float)(900/480);

which equals to 1.0f .等于1.0f

But when i added f to both width and height :但是当我将 f 添加到 width 和 height 时:

float aspectRatio = (float)(900f/480f);

It gave the right aspectRatio of 1.875 .它给出了正确的aspectRatio1.875

now the cube appears as square.现在立方体显示为正方形。

现在的立方体图像:

This is the result of how NDCs (Normalized Device Coordinates) work.这是 NDC(标准化设备坐标)工作方式的结果。 They don't take proportions of the window into account.他们没有考虑窗口的比例。 I believe that the issue is that you don't update the values of width and height when the dimensions of the window change.我认为问题在于当窗口的尺寸发生变化时,您没有更新widthheight的值。 All you have to do is write a framebuffer resize callback and set it with glfwSetFramebufferSizeCallback() :您所要做的就是编写一个帧缓冲区调整大小回调并使用glfwSetFramebufferSizeCallback()设置它:

glfwSetFramebufferSizeCallback(window, (window, newWidth, newHeight) -> {
        width = newWidth;
        height = newHeight;
    });

This sould solve the issue.这能解决问题。 Just make sure to put it before the rendering loop.只要确保把它放在渲染循环之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM