多重纹理问题

发布于 2024-09-10 11:12:44 字数 3072 浏览 3 评论 0原文

我想通过使用固定的多重纹理功能来混合最多 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);

I would like to blend up to 4 textures by using multitexturing feature in fixed
pipe line. I can successful use 2 texture units but when I use more then two
texture units there strange artifact occurred (some texture are missing or
wrong texture coords). I don't know what I am doing wrong.

The code is very simple:

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小苏打饼 2024-09-17 11:12:44

之后:

glGetIntegerv(GL_MAX_TEXTURE_UNITS, &TextureUnits); 

TextureUnits 的值是多少?我认为是2。

after this:

glGetIntegerv(GL_MAX_TEXTURE_UNITS, &TextureUnits); 

what is the value of TextureUnits? I think it is 2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文