gpt4 book ai didi

iphone - 如何在多纹理对象之后绘制具有漫反射纹理的对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:54:55 25 4
gpt4 key购买 nike

我正在使用多重纹理绘制一个对象。这很好,但是当我在那之后仅使用漫反射纹理绘制其他对象时,该漫反射纹理会与先前的纹理绑定(bind)。我知道我必须停用默认以外的纹理单元,并且我必须在 GL_TEXTURE0 处激活纹理单元,但它不起作用。

这是我的代码

glClientActiveTexture(GL_TEXTURE0); // first texture
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, stride, texCoordOffset);
glClientActiveTexture(GL_TEXTURE1); // second texture
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, stride, texCoordLightOffset);

// Enable 2D texturing for the first texture.
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);

// Enable 2D texturing for the second texture.
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);

// We are trying for the multi-textured lighting effect from the OpenGL
// ES 2.0 book, page 223, Figure 10-3. The relevant shader equation is
// basecolor * (lightcolor + 0.25), so we set the constant as a base colour
glColor4f(0.25f, 0.25f, 0.25f, 1.0f);


// Set the light map has the current texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,m_new_tex);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT,5.0f);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

// add the light map to the constant 0.25
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_ADD);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_PREVIOUS);

// Now set the background map (bricks) and modulate (multiply)
glActiveTexture(GL_TEXTURE1);
// we'll keep this code here as _texSelection will never be our lightmap
glBindTexture(GL_TEXTURE_2D, m_sky);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 5.0f);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

/* Set the texture environment mode for this texture to combine */
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);

/* Set the method we're going to combine the two textures by. */
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);

/* Use the previous combine texture as source 0*/
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PREVIOUS);

/* Use the current texture as source 1 */
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_TEXTURE);

/*
Set what we will operate on, in this case we are going to use
just the texture colours.
*/
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);


glBindBuffer(GL_ARRAY_BUFFER, drawable.VertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, drawable.IndexBuffer);

glDrawElements(GL_TRIANGLES, drawable.IndexCount, GL_UNSIGNED_SHORT, 0);

glDisable(GL_TEXTURE_2D);

对于简单的漫反射纹理,在绑定(bind)纹理之前我必须编写哪些命令?

任何其他示例代码都会有所帮助。

最佳答案

首先,您必须为每个未使用的纹理单元禁用纹理 (glDisable(GL_TEXTURE_2D)),您已经在这样做了。当然,您还必须为未使用的纹理单元禁用纹理坐标数组,因此当第二个纹理单元处于事件状态时调用 glDisableClientState(GL_TEXTURE_COORD_ARRAY)(在调用 glClientActiveTexture(GL_TEXTURE1) ).

当您完成多重纹理时,您当然必须调用 glActiveTexture(GL_TEXTURE0)glClientActiveTexture(GL_TEXTURE0)。否则,所有以下纹理操作都适用于纹理单元 1 而不是 0。

不过这些我在回答你另一个问题的时候已经说了。你读过吗?

编辑: 顺便说一下,glTexParameter 改变的状态是按纹理对象存储的,而不是按单元存储的。所以你不需要在每一帧调用这些,在创建纹理后调用一次就足够了。

关于iphone - 如何在多纹理对象之后绘制具有漫反射纹理的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8049180/

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