多纹理问题

时间:2010-07-17 17:41:12

标签: opengl multitexturing

我想通过使用固定的多纹理特征来混合多达4个纹理 管道。我可以成功使用2个纹理单元但是当我使用两个以上时 纹理单位出现奇怪的神器(有些纹理缺失或 错误的纹理坐标)。我不知道我做错了什么。

代码非常简单:

glGetIntegerv(GL_MAX_TEXTURE_UNITS, &TextureUnits); 
if (TextureUnits > 4)
    TextureUnits = 4;

// set textures parameters: texCoords and multitexturing combination
for (int j = 0; j < TextureUnits; ++j)
{
    glActiveTexture(GL_TEXTURE0 + j);

    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_WRAP_S, GL_CLAMP);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

    if (j == 0)
    {
        glEnable(GL_TEXTURE_2D);
        //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    }
    else
    {
        // Interpolate : 
        //      Arg0 * Arg2 + Arg1 * (1 - Arg2)
        // Set arguments to obtain the fallowing blending equation: 
        //      SRCrgb * SRCalpha + DSTrgb * (1 - SRCalpha)  
        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
        glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);  
        glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE);
        glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PREVIOUS);
        glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE);
        glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
        glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
        glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA);

        glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_INTERPOLATE);   
        glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE);
        glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_PREVIOUS);
        glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_TEXTURE);
        glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
        glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
        glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA, GL_SRC_ALPHA);
    }

    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glClientActiveTexture(GL_TEXTURE0 + j);
    glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &Mesh->Vertices[0].TexCoords);
}

...

for (int i = 0; i < TextureUnits; ++i)
{
    glActiveTexture(GL_TEXTURE0 + i);
    glDisable(GL_TEXTURE_2D);
}

// use multitexturing and mulipasses when there are needed. 
count = ; // number of textures to blend It could be from 1 to 4.
do
{
    tex = 0;
    while (index < count && tex != TextureUnits)
    {           
        texture = textures[index];

        glActiveTexture(GL_TEXTURE0 + tex);         
        glEnable(GL_TEXTURE_2D);

        setTextureMatrix(texture);

        glBindTexture(GL_TEXTURE_2D, texture->getGLTexture());

        index++;
        tex++;
    }

    if (tex < TextureUnits)
    {
        glActiveTexture(GL_TEXTURE0 + tex);
        glDisable(GL_TEXTURE_2D);
    }

    glDrawElements(...);

} while (index < count);

1 个答案:

答案 0 :(得分:0)

之后:

glGetIntegerv(GL_MAX_TEXTURE_UNITS, &TextureUnits); 

TextureUnits的价值是什么?我认为它是2。