gpt4 book ai didi

c++ - 相同网格的多个图像,没有重复的三角形传输

转载 作者:行者123 更新时间:2023-11-30 01:09:55 24 4
gpt4 key购买 nike

我使用 OpenGL、GLEW 和 GLFW 为同一网格拍摄多张图像。网格(三角形)不会在每次拍摄中发生变化,只有 ModelViewMatrix 会发生变化。

这是我的主循环的重要代码:

for (int i = 0; i < number_of_images; i++) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* set GL_MODELVIEW matrix depending on i */
glBegin(GL_TRIANGLES);
for (Triangle &t : mesh) {
for (Point &p : t) {
glVertex3f(p.x, p.y, p.z);
}
}
glReadPixels(/*...*/) // get picture and store it somewhere
glfwSwapBuffers();
}

如您所见,我为要拍摄的每个镜头设置/传输三角形顶点。有没有只需要传输一次的解决方案?我的网格很大,所以这个转移需要相当长的时间。

最佳答案

在 2016 年,您不得使用glBegin/glEnd。决不。使用 Vertex Array Obejcts反而;并使用自定义 vertex和/或 geometry着色器重新定位和修改您的顶点数据。使用这些技术,您只需将数据上传到 GPU 一次,然后您就可以通过各种变换绘制相同的网格。

以下是您的代码的概要:

// 1. Initialization.
// Object handles:
GLuint vao;
GLuint verticesVbo;
// Generate and bind vertex array object.
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// Generate a buffer object.
glGenBuffers(1, &verticesVbo);
// Enable vertex attribute number 0, which
// corresponds to vertex coordinates in older OpenGL versions.
const GLuint ATTRIBINDEX_VERTEX = 0;
glEnableVertexAttribArray(ATTRIBINDEX_VERTEX);
// Bind buffer object.
glBindBuffer(GL_ARRAY_BUFFER, verticesVbo);
// Mesh geometry. In your actual code you probably will generate
// or load these data instead of hard-coding.
// This is an example of a single triangle.
GLfloat vertices[] = {
0.0f, 0.0f, -9.0f,
0.0f, 0.1f, -9.0f,
1.0f, 1.0f, -9.0f
};
// Determine vertex data format.
glVertexAttribPointer(ATTRIBINDEX_VERTEX, 3, GL_FLOAT, GL_FALSE, 0, 0);
// Pass actual data to the GPU.
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*3*3, vertices, GL_STATIC_DRAW);
// Initialization complete - unbinding objects.
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

// 2. Draw calls.
while(/* draw calls are needed */) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(vao);
// Set transformation matrix and/or other
// transformation parameters here using glUniform* calls.
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0); // Unbinding just as an example in case if some other code will bind something else later.
}

顶点着色器可能看起来像这样:

layout(location=0) in vec3 vertex_pos;
uniform mat4 viewProjectionMatrix; // Assuming you set this before glDrawArrays.

void main(void) {
gl_Position = viewProjectionMatrix * vec4(vertex_pos, 1.0f);
}

另请查看 this page一本优秀的现代加速图画书。

关于c++ - 相同网格的多个图像,没有重复的三角形传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39222455/

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