简体   繁体   English

如何渲染多个纹理?

[英]How to render multiple textures?

I'm trying to understand how to render multiple textures for different objects in OpenGL. 我试图了解如何在OpenGL中为不同的对象渲染多个纹理。 I decided to simply try it, as far as I'm aware glActivate is used to select the right texture, but it doesn't work as I expected it. 我决定只是尝试一下,据我所知,glActivate用于选择合适的纹理,但它不能像我预期的那样工作。 In my code, two objects are shown (in turns) but they have the same texture. 在我的代码中,显示了两个对象(轮流),但它们具有相同的纹理。 This is the texture initialization part of the code: 这是代码的纹理初始化部分:

bool (some args, unsigned int textureUnit, bool wrap)
{
    // some code

    OpenGL->glActiveTexture (GL_TEXTURE0 + textureUnit);
    glGenTextures (1, &textureID);
    glBindTexture (GL_TEXTURE_2D, textureID);
    glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, targaImage);

    if (wrap) {
        glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    } 
    else {
        glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    }

    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    OpenGL->glGenerateMipmap (GL_TEXTURE_2D);
}

textureUnit is 0 for the first texture, and 1 for the second. textureUnit为第一个纹理为0,第二个为1。 Model rendering code: 模型渲染代码:

{
    OpenGL->glBindVertexArray (vertexArrayId);
    glDrawElements (GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
}

Putting glActive between that part, or before that part doesn't change a thing. 将glActive置于该部分之间,或者在该部分之前不会改变任何事物。 So my question is, how to render multiple textures? 所以我的问题是,如何渲染多个纹理? Should I put glActivate somewhere else or do I need to change something else. 我应该把glActivate放在其他地方还是我需要改变别的东西。

There is some misunderstanding here, the glActiveTexture calls are for managing multiple textures when drawing the same object. 这里有一些误解,glActiveTexture调用用于在绘制相同对象时管理多个纹理。 For different objects, you just bind a different texture. 对于不同的对象,您只需绑定不同的纹理。 Just remove your call to ActiveTexture when setting your textures (or make sure it is always on 0 when you are drawing). 只需在设置纹理时移除对ActiveTexture的调用(或确保在绘制时始终为0)。 Then, when drawing: 然后,在绘图时:

{
    OpenGL->glBindVertexArray (vertexArrayId);
    glBindTexture(GL_TEXTURE_2D, textureID);     /* this has to be set somehow */
    glDrawElements (GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
} 

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

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