简体   繁体   English

如何将纹理应用于 OpenGL 中的子网格?

[英]How to apply texture to submeshes in OpenGL?

I have one single mesh, which I've divided into submeshes and draw individually by using the indices of the vertices.我有一个网格,我将其划分为子网格并使用顶点的索引单独绘制。 However, I was wondering what the most efficient way of applying a different image to a different to each submesh.但是,我想知道将不同图像应用于每个子网格的最有效方法是什么。 These submeshes are being drawn seperately.这些子网格是单独绘制的。 For example, this is one submesh.例如,这是一个子网格。

mat4 view = identity_mat4();
mat4 persp_proj = perspective(100.0f, (float)width / (float)height, 0.1f, 100.0f);
mat4 model = identity_mat4();
model = rotate_y_deg(model, turn);
model = translate(model, vec3(ahead_x, 0.0f, ahead_z));
view = translate(view, vec3(0.0, 0.0, -5.0f));

// update uniforms & draw
glUniformMatrix4fv(proj_mat_location, 1, GL_FALSE, persp_proj.m);
glUniformMatrix4fv(view_mat_location, 1, GL_FALSE, view.m);
glUniformMatrix4fv(matrix_location, 1, GL_FALSE, model.m);
//glDrawElements(GL_TRIANGLES, mesh_data.mPointCount, GL_UNSIGNED_INT, 0);
glDrawArrays(GL_TRIANGLES, wheels[0], wheels[1] - wheels[0]);

Is there a simple way of achieving this?有没有一种简单的方法可以实现这一目标? I was considering binding the appropriate image for every mesh that is being drawn, but seeing as all my VBO managing is done in a different function, I thought it might be overly complicated.我正在考虑为正在绘制的每个网格绑定适当的图像,但是看到我所有的 VBO 管理都是在不同的 function 中完成的,我认为它可能过于复杂。 Thanks.谢谢。

In general, all sub-/meshes will have different materials and image maps.通常,所有子/网格将具有不同的材质和图像贴图。 Often they will require also different shaders, because the surface needs different BRDF (bidirectional reflectance distribution function) to convey the object's appearance.通常它们还需要不同的着色器,因为表面需要不同的 BRDF(双向反射分布函数)来传达对象的外观。

So you shouldn't think that having separate textures is something inherently bad.所以你不应该认为拥有单独的纹理本质上是不好的。 If there's not much objects or submeshes, you might set texture with every draw, and leave it like that.如果没有太多的对象或子网格,您可以在每次绘制时设置纹理,然后保持原样。

As your draw count will approach some hundreds calls per frame, you might see that cpu load getting higher, and fps dropping.由于您的绘制计数将接近每帧数百次调用,您可能会看到 CPU 负载越来越高,并且 fps 下降。 This is when you need to rethink the approach, and there's no silver bullet.这是您需要重新考虑方法的时候,而且没有灵丹妙药。 The "most efficient" is every time will be something different, depending on the things you need to draw. “最有效”是每次都会有所不同,具体取决于您需要绘制的内容。

For simplest optimization, you'll need to reorder objects drawing, so the objects with the same shader/texture will be drawn first, and you don't have to change textures between drawing multiple submeshes.为了最简单的优化,您需要重新排序对象绘制,因此将首先绘制具有相同着色器/纹理的对象,并且您不必在绘制多个子网格之间更改纹理。

More complex optimizations will require you to merge multiple submeshes with same material into one bigger mesh, so it would be draw in a single draw call.更复杂的优化将需要您将具有相同材质的多个子网格合并到一个更大的网格中,因此它将在一次绘制调用中绘制。 And at this point you might even create bigger texture on the fly (it's called "texture atlas"), combine multiple images into one texture, and reposition submeshes' texture coordinates to point in the correct portion of this new bigger image.在这一点上,您甚至可以动态创建更大的纹理(称为“纹理图集”),将多个图像组合成一个纹理,并重新定位子网格的纹理坐标以指向这个新的更大图像的正确部分。 This works especially good when there's many objects with small textures, like sprites.当有许多具有小纹理的对象(如精灵)时,这尤其适用。

In short, start doing complex design when you know you'll have to draw hundreds of objects every frame.简而言之,当您知道每帧必须绘制数百个对象时,就开始进行复杂的设计。 With simpler scene it's ok to bind submeshes' textures with each draw call.对于更简单的场景,可以在每次绘制调用时绑定子网格的纹理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM