纹理未显示

Texture Not Showing Up

本文关键字:显示 纹理      更新时间:2023-10-16

我试图在我的三角形上添加纹理,但它只是显示为一个黑色三角形。这是我被调用的起始函数:

GLuint vao;
GLuint shader_programme;
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API createSimpleWindow()
{

    if (std::this_thread::get_id() == MAIN_THREAD_ID)
    {
        oldContext = glfwGetCurrentContext();
        newContext = glfwCreateWindow(640, 480, "window", NULL, oldContext);
        glfwMakeContextCurrent(newContext);
        // start GLEW extension handler
        glewExperimental = GL_TRUE;
        glewInit();
        glEnable(GL_DEPTH_TEST); // enable depth-testing
        glDepthFunc(GL_LESS); // depth-testing interprets a smaller value as "closer"
//triangle pts
        float points[] = {
            0.0f,  0.5f,  0.0f,     0.5f, 1.0f,
            0.5f, -0.5f,  0.0f,     1.0f, 0.0f,
            -0.5f, -0.5f,  0.0f,    0.0f, 0.0f
        };
        GLuint vbo = 0;
        glGenBuffers(1, &vbo);
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), points, GL_STATIC_DRAW);
         vao = 0;
        glGenVertexArrays(1, &vao);
        glBindVertexArray(vao);
        //glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
        glEnableVertexAttribArray(1);
        GLuint vs = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(vs, 1, &vertex_shader, NULL);
        glCompileShader(vs);
        GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
        glShaderSource(fs, 1, &fragment_shader, NULL);
        glCompileShader(fs);
        shader_programme = glCreateProgram();
        glAttachShader(shader_programme, fs);
        glAttachShader(shader_programme, vs);
        glLinkProgram(shader_programme);
         glUseProgram(shader_programme);
        glGenTextures(1, &texture);
        glBindTexture(GL_TEXTURE_2D, texture);
        int width, height, nrComponents;
        unsigned char *data = stbi_load("/Users/roma/Desktop/Escape Tech/BitBucketRepos/blankpluginGLFW/BlankPlugin/PluginSource/source/container2.png", &width, &height, &nrComponents, 0);
        writeToLog("before data");
        if (data) {
            writeToLog("data contained!!!!!!");
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
        }
        glUniform1i(glGetUniformLocation(shader_programme, "texture1"), 0);
        //glfwCreateWindow(640, 480, "My Title", NULL, NULL);
        glfwSetKeyCallback(newContext, key_callback);
    }
    else
    {
        writeToLog("not main thread");
    }
}

下面是在 while 循环中调用的函数:

extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API windowMainLoop()
{
    writeToLog("render loop");
    glfwMakeContextCurrent(newContext);
    glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
    // loop until the window closed
    if (!glfwWindowShouldClose(newContext)) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        if (std::this_thread::get_id() == MAIN_THREAD_ID)
        {
            // bind Texture
            glBindTexture(GL_TEXTURE_2D, texture);
            glUseProgram(shader_programme);
            glBindVertexArray(vao);
            // draw points 0-3 from the currently bound VAO with current in-use shader
            glDrawArrays(GL_TRIANGLES, 0, 3);
            glfwSwapBuffers(newContext);
            // poll the events
            glfwPollEvents();
        }
        //switch back to old context
        glfwMakeContextCurrent(oldContext);
        writeToLog("finished render loop");
    }
}

我的"if(数据("语句导致文本开始写入日志,因此我知道图像正在正确加载,但我无法弄清楚为什么三角形是黑色的。

任何帮助将不胜感激!

问题是通用顶点属性数据的设置数组。每个属性元组由 5 个组件组成(x、y、z、u、v(:

float points[] = {
    0.0f,  0.5f,  0.0f,    0.5f, 1.0f,
    0.5f, -0.5f,  0.0f,    1.0f, 0.0f,
   -0.5f, -0.5f,  0.0f,    0.0f, 0.0f
};

因此,步幅参数必须5 * siezof(GLfloat)而不是6 * siezof(GLfloat)

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);

glBufferData 的第二个参数是缓冲区的大小(以字节为单位(。缓冲区由 15 (5 * 3( 个类型 GLfloat 的元素组成。所以大小是15 * sizeof(float)而不是9 * sizeof(float)

glBufferData(GL_ARRAY_BUFFER, 15 * sizeof(float), points, GL_STATIC_DRAW);

由于文件的格式是.png stbi_load的最后一个参数应设置为4,以确保获得纹理的4通道:

unsigned char *data = stbi_load("?.png", &width, &height, &nrComponents, 4);

glTexImage2D的格式和内部格式必须GL_RGBA
默认情况下,纹理缩小功能(GL_TEXTURE_MIN_FILTER(是GL_NEAREST_MIPMAP_LINEAR(见glTexParameteri(。由于不使用 mip 映射,因此必须将参数更改为 GL_LINEAR

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);