GLFW 窗口仅显示 Clear Color 而没有其他内容

Posted

技术标签:

【中文标题】GLFW 窗口仅显示 Clear Color 而没有其他内容【英文标题】:GLFW window only displaying Clear Color and nothing else 【发布时间】:2019-06-01 09:10:03 【问题描述】:

我的问题是窗口显示清晰的颜色,即(橙色)我尝试了网络上的所有答案,但它们似乎对我没有帮助。我正在使用 GLFW 3.3 预编译的二进制文件。我将 GLAD 用于 OpenGL 函数。

代码没有给出任何错误或警告,而且 glGetError() 在代码的所有点都返回 0。

我已经尝试更改顺序,但 gDEBugger 给了我错误(即 PRIV_INSTRUCTION)并且 VAO 和 VBO 选项卡为空。

这是我的代码


#define GLFW_DLL

#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>

using namespace std;

static const char* vshSrc = "#version 330 core \n layout (location = 0) in vec3 tPos; void main()gl_Position = tPos.x, tPos.y, tPos.z; \0";
static const char* fshSrc = "#version 330 core \n layout (location = 0) out vec4 FragOut; void main()FragOut = vec4(1.0f,0.4f,0.3f,1.0f); \0";


/** Processes the input.
    *Edit the fuction to add more features. */
void getInput( GLFWwindow *win )
    if(glfwGetKey(win,GLFW_KEY_ESCAPE) == GLFW_PRESS)
            glfwSetWindowShouldClose(win,true);
    



int main( int argc, char **argv )

        //Initialization
        glfwInit();
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
        glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);

        //Make compatible for MacOSX
        #ifdef __APPLE__
            glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
        #endif // __APPLE__


        //Window creation

        GLFWwindow* window = glfwCreateWindow(640,480,"Illuminati",NULL,NULL);
        if (window == NULL)
        
            cout << "Window creation failed" << endl;
            return -1; 
        


        //GLAD

        glfwMakeContextCurrent(window);

        if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
        
            cout << "GLAD load error." << endl;
            return -1;
        

        //glViewport( 0, 0, 640, 480);


//==============================================================//

        //Shader programs


        //Vertex Shader
        unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(vertexShader, 1, &vshSrc, NULL);
        glCompileShader(vertexShader);

        //Check for compile errors
        int compilationSuccesful;

        char InfoLog[512];
        glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &compilationSuccesful);
        if ( !compilationSuccesful )
                    glGetShaderInfoLog(vertexShader, 512, NULL, InfoLog);
                    cout << "Vertex Shader compilation Failed. ERROR. \n \n" << InfoLog << endl;
        

        //Fragment Shader
        unsigned int fragmentShader;
        fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
        glShaderSource(fragmentShader, 1, &fshSrc, NULL);
        glCompileShader(fragmentShader);

        //error checking for fragment shader
        glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &compilationSuccesful);
        if ( !compilationSuccesful )
                    glGetShaderInfoLog(fragmentShader, 512, NULL, InfoLog);
                    cout << "Fragment Shader compilation Failed. ERROR. \n \n" << InfoLog << endl;
        

        //Shader Program

        unsigned int shaderProgram;
        shaderProgram = glCreateProgram();

        //Shader attachments
        glAttachShader(shaderProgram, vertexShader);
        glAttachShader(shaderProgram, fragmentShader);
        glLinkProgram(shaderProgram);

        //Checking for link errors
        glGetProgramiv(shaderProgram, GL_LINK_STATUS, &compilationSuccesful);
        if ( !compilationSuccesful )
                    glGetProgramInfoLog(shaderProgram, 512, NULL, InfoLog);
                    cout << "The program refused to link. ERROR. \n \n" << InfoLog << endl;
        

        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);

//----------------------------------------------------------------------------------------------------------//      

        //Vertex data and elements

        float rect[ ] 

            /* 0 bottom left*/       -0.5f, -0.5f,  0.0f,

            /* 1  top left*/             -0.5,   0.5f,  0.0f,

            /* 2 bottom right*/      0.5f,   -0.5f, 0.0f,

            /* 3 top right*/             0.5f,   0.5f,  0.0f

            ;

        unsigned int indices[] 

            0,1,2,  //first triangle
            1,3,2  //second triangle

        ;


//==============================================================//  

        //Buffers and VAO

        //VAO
        unsigned int VAO;
        glGenVertexArrays(1, &VAO);
        glBindVertexArray(VAO);

        //VBO
        unsigned int VBO;
        glGenBuffers(1,&VBO);

        //EBO
        unsigned int EBO;
        glGenBuffers(1, &EBO);



        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(rect), rect, GL_STATIC_DRAW);


        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);


//================================================================//

        //Vertex Attributes

        //glBindVertexArray(0);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float), (void*)0);
        glEnableVertexAttribArray(0);

        //for safety
        //glBindBuffer(GL_ARRAY_BUFFER, 0);

        //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        //A useless variable
        //unsigned int ui;

//===============================================================//

        //Render Loop


        while ( !glfwWindowShouldClose(window) )
        

            //Clear the buffer
            glClearColor(0.9f,0.7f,0.2f,1.0f);
            glClear(GL_COLOR_BUFFER_BIT);

            ////////////////////////////////////////////////////////////////////////////////////////////////
            //////////////////////////////////////////DRAWING//////////////////////////////////////////


            glBindVertexArray(VAO);
            glUseProgram(shaderProgram);
            //glDrawArrays(GL_TRIANGLES, 0, 4);
            glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT, 0);
            glBindVertexArray(0);
            //////////////////////////////////////////////END/////////////////////////////////////////////

            getInput(window);
            glfwSwapBuffers(window);
            glfwPollEvents();
        

        //Exit and destructors

            glfwTerminate();
            return 0;
    

//End

/*
       __
   <( .   )____
    /_______/
~~~~~~~~~
~~~~~~~~~

   */


【问题讨论】:

【参考方案1】:

在您的着色器中,此表达式将尝试将 tPos.z 的值分配给 并且仅将 tPos.z 分配给 gl_Position

gl_Position = tPos.x, tPos.y, tPos.z;

原因在于Comma operator 的语义,其中以, 分隔的所有表达式都被求值,但只有最后一个的结果被用作整个表达式的R 值。

你可能只是想swizzle它,你也必须分配.w

gl_Position = vec4(tPos.xyz, 1);

【讨论】:

以上是关于GLFW 窗口仅显示 Clear Color 而没有其他内容的主要内容,如果未能解决你的问题,请参考以下文章

无法在 GLFW 窗口标题中显示“ä”

为啥在 glfw 窗口上没有使用 opengl 显示位图图像?在 C++ 中读取位图图像文件时出现问题

使用OpenGL和GLFW的简单三角形[重复]

C ++ glfw3:全屏模式下的一个(两个)窗口不是真正的全屏(Mac Os)

无法创建 GLFW 窗口

在 Pyqt 窗口中使用 glfw 窗口