【问题标题】:How to use a regular coordinate system while drawing vertices in OpenGL using the GLM library使用 GLM 库在 OpenGL 中绘制顶点时如何使用常规坐标系
【发布时间】:2018-12-02 21:50:39
【问题描述】:

我目前正在通过尝试制作 2D 游戏来练习 OpenGL 和 C++。只是一些非常简单的事情。现在我已经设法使用 GLM 库来设置一个“相机”(有透视,这不是我想要的),但是在尝试显示一块像素艺术时遇到了一些问题我制作。

首先,有坐标系。它显然是为 3D 和在“世界空间”中绘制事物而设计的,而不是用于在屏幕上绘制像素艺术。我只想能够使用常规坐标系设置我正在绘制的每个顶点的大小和位置。这就是我选择 Stack Exchange 的原因。

据我所知,我应该使用正交视图,但将我的投影矩阵设置为 glm::ortho() 似乎并不能解决问题。事实上,什么也没有出现。我得到一个空白屏幕。起初,我认为这是由于我的顶点仍然具有 1 像素大小,但即使在更改它们的位置以形成适当大小的矩形(技术上是两个三角形,但仍然如此)之后,屏幕上什么也没有出现。

我可能没有正确使用glm::ortho(),或者是其他问题。


将投影矩阵设置为glm::ortho()

ProjectionMatrix = glm::ortho(0, 640, 480, 0);


设置顶点。
Vertex vertices[] = {
    // Position                     // Color                        // Texcoord
    glm::vec3(0.0f, 80.0f, 0.0f),   glm::vec3(1.0f, 0.0f, 0.0f),    glm::vec2(0.0f, 1.0f),  // bottom left
    glm::vec3(0.0f, 0.0f, 0.0f),    glm::vec3(0.0f, 1.0f, 0.0f),    glm::vec2(0.0f, 0.0f),  // top left
    glm::vec3(40.0f, 0.0f, 0.0f),   glm::vec3(0.0f, 0.0f, 1.0f),    glm::vec2(1.0f, 0.0f),  // top right
    glm::vec3(40.0f, 80.0f, 0.0f),  glm::vec3(1.0f, 1.0f, 0.0f),    glm::vec2(1.0f, 1.0f)   // bottom right
};

如果需要任何进一步的代码/信息,请随时告诉我。我有点不确定要展示什么,所以我决定坚持显而易见。

【问题讨论】:

    标签: c++ opengl glm-math


    【解决方案1】:

    glm library 中的函数是模板函数。参数的类型影响操作的类型。如果将整数常量传递给函数,则内部运算是整数运算,除法的结果也是整数。

    查看glm::ortho的实现:

    template<typename T>
    GLM_FUNC_QUALIFIER mat<4, 4, T, defaultp> ortho(T left, T right, T bottom, T top)
    {
        mat<4, 4, T, defaultp> Result(static_cast<T>(1));
        Result[0][0] = static_cast<T>(2) / (right - left);
        Result[1][1] = static_cast<T>(2) / (top - bottom);
        Result[2][2] = - static_cast<T>(1);
        Result[3][0] = - (right + left) / (right - left);
        Result[3][1] = - (top + bottom) / (top - bottom);
        return Result;
    }
    

    将浮点常量传递给函数,以解决您的问题:

    ProjectionMatrix = glm::ortho(0.0f, 640.0f, 480.0f, 0.0f);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多