OpenGL 对多个纹理感到困惑 [英] OpenGL confused about multiple textures

查看:102
本文介绍了OpenGL 对多个纹理感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 OpenGL 3.3 开发 3d 程序.到目前为止,我设法渲染了许多立方体和一些球体.我需要使用通用纹理对所有立方体的所有面进行纹理处理,只有一个面应该具有不同的纹理.我尝试使用单个纹理,一切正常,但是当我尝试添加另一个纹理时,程序似乎表现得很随机.
我的问题:
是否有合适的方法将多个纹理传递给着色器?
我应该如何跟踪面部以渲染正确的纹理?
谷歌搜索我发现两次定义顶点可能很有用,但我真的不明白为什么.

解决方案

是否有合适的方法将多个纹理传递给着色器?

您将使用 glUniform1i() 以及 glActiveTexture().因此,鉴于您的片段着色器有多个 uniform sampler2D:

uniform sampler2D tex1;统一的 sampler2D tex2;

然后在设置着色器时,将采样器统一设置为您希望它们关联的纹理单元:

glUniform1i(glGetUniformLocation(program, "tex1"), 0)glUniform1i(glGetUniformLocation(program, "tex2"), 1)

然后将活动纹理设置为 GL_TEXTURE0GL_TEXTURE1 并绑定纹理.

glActiveTexture(GL_TEXTURE0)glBindTexture(GL_TEXTURE_2D,纹理1)glActiveTexture(GL_TEXTURE1)glBindTexture(GL_TEXTURE_2D,纹理2)

<块引用>

我应该如何跟踪面部以渲染正确的纹理?

这取决于你想要什么.

  • 您可以根据法线决定使用哪种纹理(通常使用三平面纹理映射完成).
  • 您还可以有另一个属性来决定两个纹理之间的淡入淡出程度.

    color = mix(texture(tex1, texCoord), texture(tex2, texCoord), 0.2)

  • 和以前一样,您同样可以有一个uniform float transition.这将允许您在纹理之间淡入淡出,从而可以像在 PowerPoint 中一样在 幻灯片 之间淡入淡出.

尝试阅读 LearnOpenGL 的纹理教程.

最后至少有 80 个纹理单元.您可以使用 GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS 具体检查您有多少可用.

I'm developing a 3d program with OpenGL 3.3. So far I managed to render many cubes and some spheres. I need to texture all the faces of all the cubes with a common texture except one face which should have a different texture. I tried with a single texture and everything worked fine but when I try to add another one the program seems to behave randomly.
My questions:
is there a suitable way of passing multiple textures to the shaders?
how am I supposed to keep track of faces in order to render the right texture?
Googling I found out that it could be useful to define vertices twice, but I don't really get why.

解决方案

Is there a suitable way of passing multiple textures to the shaders?

You'd use glUniform1i() along with glActiveTexture(). Thus given your fragment shader has multiple uniform sampler2D:

uniform sampler2D tex1;
uniform sampler2D tex2;

Then as you're setting up your shader, you set the sampler uniforms to the texture units you want them associated with:

glUniform1i(glGetUniformLocation(program, "tex1"), 0)
glUniform1i(glGetUniformLocation(program, "tex2"), 1)

You then set the active texture to either GL_TEXTURE0 or GL_TEXTURE1 and bind a texture.

glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, texture1)

glActiveTexture(GL_TEXTURE1)
glBindTexture(GL_TEXTURE_2D, texture2)

How am I supposed to keep track of faces in order to render the right texture?

It depends on what you want.

  • You could decide which texture to use based the normal (usually done with tri-planar texture mapping).
  • You could also have another attribute that decides how much to crossfade between the two textures.

    color = mix(texture(tex1, texCoord), texture(tex2, texCoord), 0.2)
    

  • Like before, you can equally have a uniform float transition. This would allow you to fade between textures, making it possible to fade between slides like in PowerPoint, so to speak.

Try reading LearnOpenGL's Textures tutorial.

Lastly there's a minimum of 80 texture units. You can check specifically how many you have available using GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS.

这篇关于OpenGL 对多个纹理感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆