纹理不显示

时间:2019-07-16 19:11:07

标签: c++ opengl textures

我试图在我的三角形上添加纹理,但是它只是显示为黑色三角形。这是我的启动函数,称为:

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(data)”语句导致文本开始写入日志,因此我知道图像已正确加载,但是我无法弄清楚三角形为什么是黑色。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

问题是通用顶点属性数据的设置数组。每个属性元组包含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的第二个参数是缓冲区的大小(以字节为单位)。您的缓冲区由GLfloat类型的15(5 * 3)个元素组成。因此大小为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 mapping,因此必须将参数更改为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);