简体   繁体   English

OpenGL对多种纹理感到困惑

[英]OpenGL confused about multiple textures

I'm developing a 3d program with OpenGL 3.3. 我正在使用OpenGL 3.3开发3d程序。 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() . 您将使用glUniform1i()glActiveTexture() Thus given your fragment shader has multiple uniform sampler2D : 因此,鉴于您的片段着色器具有多个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. 然后,将活动纹理设置为GL_TEXTURE0GL_TEXTURE1并绑定纹理。

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 . 像以前一样,您同样可以具有uniform float transition This would allow you to fade between textures, making it possible to fade between slides like in PowerPoint, so to speak. 这将让你的纹理之间褪色,从而能够幻灯片之间褪色像在PowerPoint中,可以这么说。

Try reading LearnOpenGL's Textures tutorial . 尝试阅读LearnOpenGL的Textures教程

Lastly there's a minimum of 80 texture units. 最后,至少有80个纹理单位。 You can check specifically how many you have available using GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS . 您可以使用GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS具体检查可用的GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS

You can use index buffers. 您可以使用索引缓冲区。 Define the vertices once, and then use one index buffer to draw the portion of the mesh with the first texture, then use the second index buffer to draw the portion that needs the second texture. 定义一次顶点,然后使用一个索引缓冲区绘制具有第一个纹理的网格部分,然后使用第二个索引缓冲区绘制需要第二个纹理的部分。

Here's the general formula: 这是一般公式:

  1. Setup the vertex buffer 设置顶点缓冲区
  2. Setup the shader 设置着色器
  3. Setup the first texture 设置第一个纹理
  4. Setup and draw the index buffer for the part of the mesh that should use the first texture 设置并绘制应使用第一个纹理的网格部分的索引缓冲区
  5. Setup the second texture 设置第二个纹理
  6. Setup and draw the index buffer for the part of the mesh that should use the second texture. 设置并绘制应使用第二个纹理的网格部分的索引缓冲区。

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

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