gpt4 book ai didi

c++ - GLSL - 两种纹理(未混合)

转载 作者:行者123 更新时间:2023-11-28 03:27:00 24 4
gpt4 key购买 nike

我正在使用 GLSL 处理纹理。如何使用 GLSL 处理两个纹理?一个人建议我在我的 GLSL 中做两个 samplers2D ...但是 GLSL 怎么知道应该使用哪个 samplers2D 呢? (我不是在谈论混合纹理...)

我听说我应该使用 glBindTexture。我怎样才能做到这一点?使用 glBindTexture?谁有这方面的例子?

OpenGL 3.3


编辑:

我有这个:

uniform Sampler2D texture1;
uniform Sampler2D texture2;

我需要绘制两个物体,使用纹理,那么GLSL如何根据我要绘制的物体知道他应该使用texture1还是texture2。这是我的问题。

最佳答案

您需要将每个纹理绑定(bind)到不同的纹理单元,然后使用多重纹理坐标。它看起来像这样(假设您已经有了纹理):

glActiveTexture (GL_TEXTURE0);  // First texture is going into texture unit 0
glBindTexture (GL_TEXTURE_2D, tex0);
glEnable (GL_TEXTURE_2D);

glActiveTexture (GL_TEXTURE1); // Second texture is going into texture unit 1
glBindTexture (GL_TEXTURE2D, tex1);
glEnable (GL_TEXTURE_2D);

glUseProgram (yourGLSLProgramID);
glUniform1i (sampler1Location, 0); // tell glsl that sampler 1 is sampling the texture in texture unit 0
glUniform1i (sampler2Location, 1); // tell glsl that sampler 2 is sampling the texture in texture unit 1
///... set the rest of your uniforms...

glBegin (GL_QUADS);
glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE1, 0.0, 0.0);
glVertexCoord2f(0.0, 0.0);

glMultiTexCoord2f(GL_TEXTURE0, 1.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE1, 1.0, 0.0);
glVertexCoord2f(width, 0.0);

glMultiTexCoord2f(GL_TEXTURE0, 1.0, 1.0);
glMultiTexCoord2f(GL_TEXTURE1, 1.0, 1.0);
glVertexCoord2f(width, height);

glMultiTexCoord2f(GL_TEXTURE0, 0.0, 1.0);
glMultiTexCoord2f(GL_TEXTURE1, 0.0, 1.0);
glVertexCoord2f(0.0, height);
glEnd();

关于c++ - GLSL - 两种纹理(未混合),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13660928/

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