OpenGL - 同一对象的不同表面上的不同纹理
我正在尝试为我使用 OpenGL 制作的游戏绘制一辆车。汽车对象存储在 .obj 文件中,并附带一个 .mtl 文件。
该汽车有 500 多个顶点、100 多个面,需要使用我自己的顶点和片段着色器对其应用 50 多种不同的纹理。我通过使用缓冲区对象来解决此问题,它会立即发送有关我的对象的所有信息。我的问题是尝试将不同的纹理应用到同一对象的不同表面。
我找到的这个问题的答案是将纹理绑定到特定的纹理编号(即 GL_TEXTURE0、GL_TEXTURE1 等),并将 Sampler2D 传递给引用它的片段着色器。但是,我可以用这种方式存储超过 50 个纹理吗?据我所知,它转到 GL_TEXTURE32。
另外,我如何告诉着色器仅将纹理应用于汽车对象的某个面?
I'm trying to draw a car for a game I'm making using OpenGL. The car object is stored in an .obj file with an accompanying .mtl file.
The car has 500+ vertices, 100+ faces and needs over 50 different textures applied to them, using my own vertex and fragment shader. I've gone about this by using buffer objects, which sends all information about my object at once. My problem is trying to apply different textures to different surfaces of the same object.
The answers I've found to this problem are to bind a texture to a certain texture number (i.e. GL_TEXTURE0, GL_TEXTURE1, etc...) and pass in a Sampler2D to the fragment shader referencing it. However, can I store over 50 textures in this way? As far as I can tell it goes to GL_TEXTURE32.
Also, how would I tell the shader to only apply a texture to a certain face of the car object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法以这种方式渲染模型,即一次绑定所有纹理。即使对于高端 GL 4.1 级硬件,片段着色器也只能访问 16 个纹理。也就是说,它只能有16个采样器。
纹理总数更高,因为其他着色器阶段也可以使用纹理。
处理该问题的正确方法是执行以下操作之一:
#3 是唯一不需要更改数据的解决方案。但请注意:如果您经常渲染这辆车(例如,每帧数千次),状态更改开销可能会成为性能问题。 #1 和 #2 的某种组合是通常首选的方法。
顺便说一句,如果您的模型只有几百个面,但有 50 个纹理,那么建模过程中可能出现了严重错误。
You cannot render the model this way, where you bind all the textures at once. A fragment shader, even for high-end GL 4.1-class hardware, can only access 16 textures. That is, it can only have 16 samplers.
The total texture count is higher because other shader stages can use textures as well.
The correct way to deal with it is to do one of the following:
#3 is the only solution that doesn't require changing data. But be warned: if you render this car often (say, thousands of times per frame) the state change overhead can be a performance issue. Some combination of #1 and #2 is the generally preferred method.
BTW, if your model only has a few hundred faces, but 50 textures, then it's likely something went horribly wrong in the modelling process.
不可能像这样使用任意数量的纹理,而不是以直接的方式。
基本上有 3 种解决方案:
调整纹理坐标并注意 mipmap 创建
(尽管一个像样的工具会自动为你做到这一点)。您可能还需要小心地为各向异性过滤留下足够的边界。
It's not possible to use an arbitrary number of textures just like that, not in a straightforward way.
There are basically 3 solutions:
to adjust texture coordinates and take care at mipmap creation
(though a decent tool will do that for you automatically). You may need to be careful to leave a sufficient border for anisotropic filtering too.
为单个对象切换 50 个纹理很快就会变成低性能的噩梦。使用纹理图集。
多重纹理旨在提供多个纹理层,如颜色层、凹凸层、光泽度等。但它们不能用于替换图集。
此外,如果您的模型由不同材料的零件组成,请将其拆分。每种材质都有自己的网格。
Switching 50 textures for a single object will quickly turn into a low-performance nightmare. Use a texture atlas.
Multitexturing is meant to provide multiple texture layers, like a colour layer, a bump layer, shinyness, etc. But they are not to be used for replacement of an atlas.
Also if your model consists of parts with different material, split it up. Each material in its own mesh.