gpt4 book ai didi

c++ - 如何在 OpenGL 中创建网格并用线条绘制

转载 作者:行者123 更新时间:2023-11-30 03:13:40 24 4
gpt4 key购买 nike

我需要像这样创建一个网格:

  ----------------
| | | | | |
----------------
| | | | | |
----------------
| | | | | |
----------------

并只用线条渲染它。这就是我创建顶点和索引的方式:

  std::vector<glm::vec3> vertices;
std::vector<glm::uvec3> indices;

for(int j=0; j<=slices; ++j) {
for(int i=0; i<=slices; ++i) {
float x = (float)i/(float)slices;
float y = 0;
float z = (float)j/(float)slices;
vertices.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);

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

}
}

glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );

GLuint vbo;
glGenBuffers( 1, &vbo );
glBindBuffer( GL_ARRAY_BUFFER, vbo );
glBufferData( GL_ARRAY_BUFFER, vertices.size()*sizeof(glm::vec3), glm::value_ptr(vertices[0]), GL_STATIC_DRAW );
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, nullptr );

GLuint ibo;
glGenBuffers( 1, &ibo );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ibo );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(glm::uvec3), glm::value_ptr(indices[0]), GL_STATIC_DRAW );

glBindVertexArray( 0 );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
glBindBuffer( GL_ARRAY_BUFFER, 0 );

lenght = (GLuint)indices.size()*3;

这里是我如何渲染它的:

  glBindVertexArray(vao);

glDrawElements(GL_LINES, lenght, GL_UNSIGNED_INT, NULL);

glBindVertexArray(0);

结果不是我想要的。我也尝试使用 GL_LINE_LOOP 等我认为问题在于顶点和索引的创建

更新:

我使 Rabbit76 建议的更改看起来很完美!!

enter image description here

这里是创建顶点和索引的最终代码:

  std::vector<glm::vec3> vertices;
std::vector<glm::uvec4> indices;

for(int j=0; j<=slices; ++j) {
for(int i=0; i<=slices; ++i) {
float x = (float)i/(float)slices;
float y = 0;
float z = (float)j/(float)slices;
vertices.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);

indices.push_back(glm::uvec4(row1+i, row1+i+1, row1+i+1, row2+i+1));
indices.push_back(glm::uvec4(row2+i+1, row2+i, row2+i, row1+i));

}
}

glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );

GLuint vbo;
glGenBuffers( 1, &vbo );
glBindBuffer( GL_ARRAY_BUFFER, vbo );
glBufferData( GL_ARRAY_BUFFER, vertices.size()*sizeof(glm::vec3), glm::value_ptr(vertices[0]), GL_STATIC_DRAW );
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, nullptr );

GLuint ibo;
glGenBuffers( 1, &ibo );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ibo );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(glm::uvec4), glm::value_ptr(indices[0]), GL_STATIC_DRAW);

glBindVertexArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

lenght = (GLuint)indices.size()*4;

这是渲染网格的代码:

  glEnable(GL_DEPTH_TEST);

glBindVertexArray(vao);

glDrawElements(GL_LINES, lenght, GL_UNSIGNED_INT, NULL);

glBindVertexArray(0);

glDisable(GL_DEPTH_TEST);

最佳答案

[...] I need to render a line raster [...]

有很多可能性。基于这段代码,一个几乎没有变化的选项是为每个四边形创建四个线段。

改变索引的类型:

std::vector<glm::uvec4> indices;

添加线段:

indices.push_back(glm::uvec4(row1+i, row1+i+1, row1+i+1, row2+i+1));
indices.push_back(glm::uvec4(row2+i+1, row2+i, row2+i, row1+i));

创建和初始化缓冲区的数据存储时注意更改的类型:

glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(glm::uvec4),
indices.data(), GL_STATIC_DRAW);

分别(更优雅)

glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(*indices.data()),
indices.data(), GL_STATIC_DRAW);

最后计算出合适的索引数

lenght = (GLuint)indices.size()*4;

绘制线段

glBindVertexArray(vao);
glDrawElements(GL_LINES, lenght, GL_UNSIGNED_INT, NULL);

另一种选择是在整个网格上呈现线段:

std::vector<glm::uvec2> indices;
int rowLen = slices+1;
int noVert = (int)vertices.size();
for(int j=0; j<slices; ++j) {
indices.push_back(glm::uvec2(j * rowLen, (j+1) * rowLen - 1));
indices.push_back(glm::uvec2(j, noVert - rowLen + j));
}
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(*indices.data()),
indices.data(), GL_STATIC_DRAW);
lenght = (GLuint)indices.size()*2;

关于c++ - 如何在 OpenGL 中创建网格并用线条绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58494179/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com