GLFW窗口仅显示“清除颜色”,没有其他显示

时间:2019-06-01 09:10:04

标签: c++ opengl gcc glfw glad

我有一个问题,就是窗口显示的是清晰的颜色,即(橙色),我尝试了网络上的所有答案,但它们似乎没有帮助。我正在使用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 个答案:

答案 0 :(得分:3)

在着色器中,此表达式将尝试将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);