代码之家  ›  专栏  ›  技术社区  ›  juztcode

opengl——VAO是否也存储texrture单元调用

  •  0
  • juztcode  · 技术社区  · 5 年前

    创建VAO、VBO和应用纹理的非常基本的基础是这样的:

    unsigned int VBO, VAO, EBO;   //#1
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);
    
    glBindVertexArray(VAO);
    
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
    
    // position attribute
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);
    // color attribute
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
    glEnableVertexAttribArray(1);
    // texture coord attribute
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
    glEnableVertexAttribArray(2);    //#2
    

    对于创建纹理:

    unsigned int texture1, texture2;  
    // texture 1
    // ---------
    glGenTextures(1, &texture1); //#3
    glBindTexture(GL_TEXTURE_2D, texture1); //#4
     // set the texture wrapping parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);   // set texture wrapping to GL_REPEAT (default wrapping method)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    // set texture filtering parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    // load image, create texture and generate mipmaps
    int width, height, nrChannels;
    stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis.
    // The FileSystem::getPath(...) is part of the GitHub repository so we can find files on any IDE/platform; replace it with your own image path.
    unsigned char *data = stbi_load(FileSystem::getPath("resources/textures/container.jpg").c_str(), &width, &height, &nrChannels, 0);
    if (data)
    {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
        glGenerateMipmap(GL_TEXTURE_2D);
    }
    else
    {
        std::cout << "Failed to load texture" << std::endl;
    }
    stbi_image_free(data);  
    
    
    //some more code , inside the render loop
    glActiveTexture(GL_TEXTURE0);   #5
    glBindTexture(GL_TEXTURE0, texture1);   #6
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE0, texture1);
    
    glDrawElements(...);
    glfwSwapBuffers(...);
    //end of render loop
    

    根据我从中学到的 learnopengl ,来自第一线 #1 排队 #2 ,则通话存储在 VAO 这就是为什么我们不必一遍又一遍地写东西,我们只需切换 为了画画。
    但是,代码来自第行吗 #3 排队 #6 还存储在 ? 如果是,那我们为什么不直接在第五行后面写第五行呢 #3 ? 如果不是,我们如何将特定的纹理单元链接到特定的纹理单元 如果我们使用多个 VAOs ?

    编辑:

    在glBindTexture(GL_TEXTURE_2D,texture1)之后,继续进行的纹理调用会影响当前绑定的纹理,不是吗?那么这是否意味着glActiveTexture(…)还会影响当前绑定的纹理吗?为什么我们在使用glBindTexture(…)激活纹理后再次绑定它?

    1 回复  |  直到 5 年前
        1
  •  3
  •   Nicol Bolas    5 年前

    第1行和第2行之所以“存储在VAO中”,是因为这些函数就是这么做的。这些函数在当前绑定的顶点数组对象中设置状态。

    顾名思义,顶点数组对象是处理顶点数组的对象。纹理管理、纹理存储和使用纹理进行渲染都有必要 没有什么 与顶点数组有关。因此,这些函数都不会以任何方式影响VAO状态,这与修改 vector<int> 不会修改某些文件的内容 list<float> 对象

    如果我们使用多个VAO,我们如何将特定的纹理单元链接到特定的VAO?

    同样,VAO处理顶点数据。纹理不是顶点数据,所以VAO不关心它们,反之亦然。你不能把纹理和VAO联系起来。

    您可以使用VAO和纹理(以及其他内容)来执行 翻译 .vao和纹理在渲染过程中各自做不同的事情,因此彼此之间没有直接关系。