C++ 纹理不显示

C++ 纹理不显示,c++,opengl,textures,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 = glf

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

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)”语句导致文本开始写入日志,因此我知道图像已正确加载,但我无法找出三角形为黑色的原因


任何帮助都将不胜感激

问题在于通用顶点属性数据的设置数组。每个属性元组由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)

glvertexattributepointer(0,3,GL_FLOAT,GL_FALSE,5*sizeof(FLOAT),(void*)0);
GlenableVertexAttributeArray(0);
glvertexattributepointer(1,2,GL_FLOAT,GL_FALSE,5*sizeof(FLOAT),(void*)(3*sizeof(FLOAT));
GlenableVertexAttributeArray(1);

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

glBufferData(GL_数组_BUFFER,15*sizeof(float),点,GL_STATIC_DRAW);

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

unsigned char*data=stbi_加载(“?.png”、&宽度、高度和NR分量,4);
的格式和内部格式必须为
GL\u RGBA

默认情况下,纹理缩小功能(
GL\u texture\u MIN\u FILTER
)是
GL\u NEAREST\u MIPMAP\u LINEAR
(请参阅)。由于不使用,必须将参数更改为
GL\u LINEAR

glTexImage2D(GL_纹理_2D,0,GL_RGBA,宽度,高度,0,GL_RGBA,GL_无符号字节,数据);
glTexParameteri(GL_纹理2D、GL_纹理最小过滤器、GL_线性);

我修复了这个问题,但屏幕仍然是黑色的:/n三角形仍然是黑色的,但至少现在整个三角形都显示出来了(以前只是部分三角形,但我只是忽略了它)。@steph阅读纹理时出现了另一个问题。(答案的第三部分)