如何在 OpenGL 窗口中创建二维网格?

发布于 2025-01-09 08:08:10 字数 1233 浏览 0 评论 0原文

我正在尝试在整个窗口上打印二维网格。

代码:

float slices(15);
std::vector<glm::vec3> vert;
std::vector<glm::uvec3> ind;

for (int j = 0; j <= slices; j++) {
    for (int i = 0; i <= slices; i++) {

        GLfloat x = (float)i / (float)slices;
        GLfloat y = (float)j / (float)slices;
        GLfloat z = 0;
        vert.push_back(glm::vec3(x, y, z));
        
    }
}
for (int j = 0; j < slices; j++) {
    for (int i = 0; i < slices; i++) {

        int row1 = j * (slices + 1);
        int row2 = (j + 1) * (slices + 1);

        ind.push_back(glm::uvec3(row1 + i, row1 + i + 1, row2 + i));
        ind.push_back(glm::uvec3(row1 + i, row2 + i + 1, row2 + i));

    }
}
{//generate vao and bind bufffer objects}
GLuint lenght = (GLuint)ind.size() * 3;

glBindVertexArray(vao);

glDrawElements(GL_LINES, lenght, GL_UNSIGNED_INT, NULL);

这只在我的窗口的四分之一(正x,y平面)上打印网格。

窗口输出

当我在 for 循环中的 i 或 j 前面放置一个负数时,如下所示:

(GLfloat x = (float)-i/(float)slices;  

我在窗口的其他象限之一中得到一个网格。我尝试通过擦除向量并用每个象限的坐标重新填充网格来多次渲染网格,但这似乎不起作用并且效率很低。

I am trying to print a 2d grid on my entire window.

code:

float slices(15);
std::vector<glm::vec3> vert;
std::vector<glm::uvec3> ind;

for (int j = 0; j <= slices; j++) {
    for (int i = 0; i <= slices; i++) {

        GLfloat x = (float)i / (float)slices;
        GLfloat y = (float)j / (float)slices;
        GLfloat z = 0;
        vert.push_back(glm::vec3(x, y, z));
        
    }
}
for (int j = 0; j < slices; j++) {
    for (int i = 0; i < slices; i++) {

        int row1 = j * (slices + 1);
        int row2 = (j + 1) * (slices + 1);

        ind.push_back(glm::uvec3(row1 + i, row1 + i + 1, row2 + i));
        ind.push_back(glm::uvec3(row1 + i, row2 + i + 1, row2 + i));

    }
}
{//generate vao and bind bufffer objects}
GLuint lenght = (GLuint)ind.size() * 3;

glBindVertexArray(vao);

glDrawElements(GL_LINES, lenght, GL_UNSIGNED_INT, NULL);

This only prints a grid on a fourth of my window (the postivive x,y plane).

window output

when I put a negative in front of the i or j in the for loop like so:

(GLfloat x = (float)-i/(float)slices;  

I get a grid in one of the other quadrants of my window. I've tried rendering the gird multiple times by erasing the vector and repopulating it with coordinates for each quadrant but this doesn't seem to work and seems highly inefficient.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖伴 2025-01-16 08:08:10

标准化设备坐标的范围为 [-1.0, 1.0]。将坐标从 [0.0,1.0] 映射到 [-1.0, 1.0]:

vert.push_back(glm::vec3(x, y, z));

vert.push_back(glm::vec3(x * 2.0f - 1.0f, y * 2.0f - 1.0f, z));

Normalized device coordinates are in range [-1.0, 1.0]. Map the coordinates from [0.0,1.0] to [-1.0, 1.0]:

vert.push_back(glm::vec3(x, y, z));

vert.push_back(glm::vec3(x * 2.0f - 1.0f, y * 2.0f - 1.0f, z));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文