如何在 OpenGL 窗口中创建二维网格?
我正在尝试在整个窗口上打印二维网格。
代码:
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).
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
标准化设备坐标的范围为 [-1.0, 1.0]。将坐标从 [0.0,1.0] 映射到 [-1.0, 1.0]:
vert.push_back(glm::vec3(x, y, 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));