这个问题在这里有答案:https://stackoverflow.com/questions/42596516/generating-mipmaps-manually
这是一个愚蠢的错误。很抱歉给您带来不便。

OpenGL很难学习。
根据我对mipmap的理解,我可以手动设置每个mipmap级别,这样我就可以更改要在我的纹理时使用的图像看起来更大或更小。

我尝试了以下代码,但与三角形的大小无关,它始终采用第一个mipmap级别。
为什么?
如果三角形更接近任何其他mipmap级别,为什么不使用它呢?

我真的很讨厌在问题中添加一堆代码,因为我强迫其他人浪费时间分析代码,而不是放出最令人振奋的代码段代码,但我不知道问题出在哪里,我别无选择。抱歉:(

#include <iostream>

#define GLEW_STATIC
#include <GL/glew.h>

#include <GLFW/glfw3.h>

const GLchar* VERTEX_SHADER_SOURCE =
        "#version 330 core                           \n"
        "layout (location = 0) in vec2 position;     \n"
        "layout (location = 1) in vec2 myTexCoord;   \n"
        "                                            \n"
        "out vec2 TexCoord;                          \n"
        "                                            \n"
        "void main()                                 \n"
        "{                                           \n"
        "    gl_Position = vec4(position,0.0f, 1.0f);\n"
        "    TexCoord = myTexCoord;                  \n"
        "}                                           \n";

const GLchar* FRAGMENT_SHADER_SOURCE =
        "#version 330 core                         \n"
        "in vec2 TexCoord;                         \n"
        "                                          \n"
        "out vec4 color;                           \n"
        "                                          \n"
        "uniform sampler2D ourTexture;             \n"
        "                                          \n"
        "void main()                               \n"
        "{                                         \n"
        "    color = texture(ourTexture, TexCoord);\n"
        "}                                         \n";

int main()
{
    //INIT STUFFS
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    GLFWwindow* window = glfwCreateWindow(800, 600, "", nullptr, nullptr);
    glfwMakeContextCurrent(window);
    glewExperimental = GL_TRUE;
    glewInit();
    glViewport(0, 0, 800, 600);



    //THE MOST INTERESTING PART
    GLuint texture;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    //Our image for the mipmap with level 0 and white color
    //The dimensions are 200 x 200 and is multiplied by 3 because I defined R,G and B colors
    unsigned char imageArray[200*200*3];
    for(int i = 0; i < 200*200*3; i++){
        imageArray[i] = 255;
    }

    //Our image for the mipmap with level 1-8 and blue color
    unsigned char imageArray2[100*100*3];
    for(int i = 0; i < 100*100*3; i++){
        if((i+1)%3 == 0)
            imageArray2[i] = 255;
        else
            imageArray2[i] = 0;
    }

    //All mipmap levels
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 200, 200, 0, GL_RGB, GL_UNSIGNED_BYTE, imageArray);
    glTexImage2D(GL_TEXTURE_2D, 1, GL_RGB, 100, 100, 0, GL_RGB, GL_UNSIGNED_BYTE, imageArray2);
    glTexImage2D(GL_TEXTURE_2D, 2, GL_RGB, 50,  50,  0, GL_RGB, GL_UNSIGNED_BYTE, imageArray2);
    glTexImage2D(GL_TEXTURE_2D, 3, GL_RGB, 25,  25,  0, GL_RGB, GL_UNSIGNED_BYTE, imageArray2);
    glTexImage2D(GL_TEXTURE_2D, 4, GL_RGB, 13,  13,  0, GL_RGB, GL_UNSIGNED_BYTE, imageArray2);
    glTexImage2D(GL_TEXTURE_2D, 5, GL_RGB, 7,   7,   0, GL_RGB, GL_UNSIGNED_BYTE, imageArray2);
    glTexImage2D(GL_TEXTURE_2D, 6, GL_RGB, 4,   4,   0, GL_RGB, GL_UNSIGNED_BYTE, imageArray2);
    glTexImage2D(GL_TEXTURE_2D, 7, GL_RGB, 2,   2,   0, GL_RGB, GL_UNSIGNED_BYTE, imageArray2);
    glTexImage2D(GL_TEXTURE_2D, 8, GL_RGB, 1,   1,   0, GL_RGB, GL_UNSIGNED_BYTE, imageArray2);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    //If I comment the line above and uncomment the following, the program will show a black triangle. Why?
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST);

    GLfloat vertices[] =
    //(un)comment the first and second lines of vertex coords to swap between a bigger or smaller triangle
    //It doesn't matter, this program always take the first mipmap level
    //But... Why? the triangle should be closer to any other mipmap level :( why does not take it?
    {
//          -1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 1.0f,      //Bigger triangle
            -0.05f, -0.05f, 0.05f, -0.05f, 0.0f, 0.05f, //Smaller triangle
            -1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 1.0f       //Triangle's texture coordinates
    };



    //VAOs AND VBOs
    GLuint VAO, VBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)(sizeof(GLfloat)*3*2));

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);



    //SHADERS
    GLuint program;
    GLuint vertex, fragment;

    vertex = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex, 1, &VERTEX_SHADER_SOURCE, NULL);
    glCompileShader(vertex);

    fragment = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragment, 1, &FRAGMENT_SHADER_SOURCE, NULL);
    glCompileShader(fragment);

    program = glCreateProgram();
    glAttachShader(program, vertex);
    glAttachShader(program, fragment);
    glLinkProgram(program);

    glDeleteShader(vertex);
    glDeleteShader(fragment);
    glUseProgram(program);



    //DRAWING OUR TRIANGLE
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glDrawArrays(GL_TRIANGLES, 0,3);
    glfwSwapBuffers(window);
    while (!glfwWindowShouldClose(window))
        glfwPollEvents();



    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    glfwTerminate();
    return 0;
}




由于任何原因,这两行不能一起使用

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST);


EDIT :我认为上述两行无效,因为mipmaps仅适用于GL_TEXTURE_MIN_FILTER。但是,当我将

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
//If I comment the line above and uncomment the following, the program will show a black triangle. Why?
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST);

替换为


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);


它显示了一个黑色的三角形:(

我使用glGetError就像Dan Hulme在评论中指出的那样,但是对代码的最后更改我没有返回错误

编辑2:我解决了以2的幂设置大小的问题,但是,现在只需要第一个mipmap级别,这是新的源代码:

#include <iostream>

#define GLEW_STATIC
#include <GL/glew.h>

#include <GLFW/glfw3.h>

const GLchar* VERTEX_SHADER_SOURCE =
        "#version 330 core                           \n"
        "layout (location = 0) in vec2 position;     \n"
        "layout (location = 1) in vec2 myTexCoord;   \n"
        "                                            \n"
        "out vec2 TexCoord;                          \n"
        "                                            \n"
        "void main()                                 \n"
        "{                                           \n"
        "    gl_Position = vec4(position,0.0f, 1.0f);\n"
        "    TexCoord = myTexCoord;                  \n"
        "}                                           \n";

const GLchar* FRAGMENT_SHADER_SOURCE =
        "#version 330 core                         \n"
        "in vec2 TexCoord;                         \n"
        "                                          \n"
        "out vec4 color;                           \n"
        "                                          \n"
        "uniform sampler2D ourTexture;             \n"
        "                                          \n"
        "void main()                               \n"
        "{                                         \n"
        "    color = texture(ourTexture, TexCoord);\n"
        "}                                         \n";

int main()
{
    //Change the value of size by 1,2,4,8,16,32,64,128 or 256 and see how the color of the triangle doesn't change. Why does it happen?
    GLint size = 128;

    //INIT STUFFS
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    GLFWwindow* window = glfwCreateWindow(size,size, "", nullptr, nullptr);
    glfwMakeContextCurrent(window);
    glewExperimental = GL_TRUE;
    glewInit();
    glViewport(0, 0,size,size);



    GLuint texture;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    //Our image for the mipmap with odd level and green color
    //The dimensions are 256 x 256 and is multiplied by 3 because I defined R,G and B colors
    unsigned char imageArray[256*256*3];
    for(int i = 0; i < 256*256*3; i++){
        if((i+2)%3 == 0)
            imageArray[i] = 255;
        else
            imageArray[i] = 0;
    }

    //Our image for the mipmap with pair level and red color
    unsigned char imageArray2[256*256*3];
    for(int i = 0; i < 256*256*3; i++){
        if(i%3 == 0)
            imageArray2[i] = 255;
        else
            imageArray2[i] = 0;
    }

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 8);

    //All mipmap levels defined by hand
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, imageArray);
    glTexImage2D(GL_TEXTURE_2D, 1, GL_RGB, 128, 128, 0, GL_RGB, GL_UNSIGNED_BYTE, imageArray2);
    glTexImage2D(GL_TEXTURE_2D, 2, GL_RGB, 64,  64,  0, GL_RGB, GL_UNSIGNED_BYTE, imageArray);
    glTexImage2D(GL_TEXTURE_2D, 3, GL_RGB, 32,  32,  0, GL_RGB, GL_UNSIGNED_BYTE, imageArray2);
    glTexImage2D(GL_TEXTURE_2D, 4, GL_RGB, 16,  16,  0, GL_RGB, GL_UNSIGNED_BYTE, imageArray);
    glTexImage2D(GL_TEXTURE_2D, 5, GL_RGB, 8,   8,   0, GL_RGB, GL_UNSIGNED_BYTE, imageArray2);
    glTexImage2D(GL_TEXTURE_2D, 6, GL_RGB, 4,   4,   0, GL_RGB, GL_UNSIGNED_BYTE, imageArray);
    glTexImage2D(GL_TEXTURE_2D, 7, GL_RGB, 2,   2,   0, GL_RGB, GL_UNSIGNED_BYTE, imageArray2);
    glTexImage2D(GL_TEXTURE_2D, 8, GL_RGB, 1,   1,   0, GL_RGB, GL_UNSIGNED_BYTE, imageArray);

    GLfloat vertices[] = {
            -1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 1.0f,  //vertex coordinates
            0.0f, 0.0f                              //Triangle's texture coordinates
    };



    //VAOs AND VBOs
    GLuint VAO, VBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)(sizeof(GLfloat)*3*2));

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);



    //SHADERS
    GLuint program;
    GLuint vertex, fragment;

    vertex = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex, 1, &VERTEX_SHADER_SOURCE, NULL);
    glCompileShader(vertex);

    fragment = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragment, 1, &FRAGMENT_SHADER_SOURCE, NULL);
    glCompileShader(fragment);

    program = glCreateProgram();
    glAttachShader(program, vertex);
    glAttachShader(program, fragment);
    glLinkProgram(program);

    glDeleteShader(vertex);
    glDeleteShader(fragment);
    glUseProgram(program);



    //DRAWING OUR TRIANGLE
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glDrawArrays(GL_TRIANGLES, 0,3);
    glfwSwapBuffers(window);
    while (!glfwWindowShouldClose(window))
        glfwPollEvents();



    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    glfwTerminate();
    return 0;
}




评论

无关,但是您的imageArray2循环会更快(int i = 0; i <100 * 100 * 3; i + = 3){imageArray2 [i] = 0; imageArray2 [i + 1] = 0; imageArray [i + 2] = 255; },因为它避免了循环内的除法。我想这样也更清楚。

显然,mipmaps是按原样在程序中关闭的:您正在调试的东西就是为什么取消注释打开它们的行时会出现黑屏的原因。 GL调试的第一条规则是在任何地方都使用glGetError,看看是否有任何gl *函数调用失败。

@Dan Hulme如何打开mipmap?我正在关注的教程没有提及如何打开/关闭mipmap。

将GL_TEXTURE_MIN_FILTER设置为GL_NEAREST意味着将不使用mipmap,这就是为什么您在执行此操作时只能看到级别0的原因。

#1 楼

您需要四舍五入mipmap大小。
您的情况下,正确的顺序是200、100、50、25、12、6、3、1。