Opengl不会显示任何内容

Opengl will not display anything

本文关键字:任何内 显示 Opengl      更新时间:2023-10-16

我正在尝试显示一个小三角形。

这是我的初始化代码:

void PlayerInit(Player P1)
{
    glewExperimental = GL_TRUE; 
    glewInit();
    //initialize buffers needed to draw the player
    GLuint vao;
    GLuint buffer;
    glGenVertexArrays(1, &vao);
    glGenBuffers(1, &buffer);
    //bind the buffer and vertex objects
    glBindVertexArray(vao);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    //Set up a buffer to hold 6 floats for position, and 9 floats for color
    glBufferData(GL_ARRAY_BUFFER, sizeof(float)*18, NULL, GL_STATIC_DRAW);
    //push the vertices of the player into the buffer
    glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*9, CalcPlayerPoints(P1.GetPosition()));
    //push the color of each player vertex into the buffer
    glBufferSubData(GL_ARRAY_BUFFER, sizeof(float)*9, sizeof(float)*9, CalcPlayerColor(1)); 
    //create and compile the vertex/fragment shader objects
    GLuint vs = create_shader("vshader.glsl" ,GL_VERTEX_SHADER);
    GLuint fs = create_shader("fshader.glsl" ,GL_FRAGMENT_SHADER);
    //create a program object and link the shaders
    GLuint program = glCreateProgram();
    glAttachShader(program, vs);
    glAttachShader(program, fs);
    glLinkProgram(program);
    //error checking for linking
    GLint linked;
    glGetProgramiv(program, GL_LINK_STATUS, &linked);
    if(!linked)
    {
        std::cerr<< "Shader program failed to link" <<std::endl;
        GLint logSize;
        glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logSize);
        char* logMsg = new char[logSize];
        glGetProgramInfoLog(program, logSize, NULL, logMsg);
        std::cerr<< logMsg << std::endl;
        delete [] logMsg;
        exit(EXIT_FAILURE);
    }
    glUseProgram(program);
    //create attributes for color and position to pass to shaders
    //enable each attribute
    GLuint Pos = glGetAttribLocation( program, "vPosition");
    glEnableVertexAttribArray(Pos);
    GLuint Col = glGetAttribLocation( program, "vColor");
    glEnableVertexAttribArray(Col);
    //set a pointer at the proper offset into the buffer for each attribute
    glVertexAttribPointer(Pos, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) (sizeof(float)*0));
    glVertexAttribPointer(Col, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) (sizeof(float)*9));
}

我没有太多编写着色器链接器的经验,所以我认为这可能是问题所在。我在着色器加载程序中进行了一些错误检查,但没有任何结果。所以我认为这很好。

接下来我有我的显示器和主要功能:

//display function for the game
void GameDisplay( void )
{
    //set the background color
    glClearColor(1.0, 0.0, 1.0, 1.0);
    //clear the screen
    glClear(GL_COLOR_BUFFER_BIT);
    //Draw the Player
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glutSwapBuffers();
}
//main function
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowSize(500, 500);
    glutCreateWindow("Asteroids");
    Player P1 = Player(0.0, 0.0);
    PlayerInit(P1);
    glutDisplayFunc(GameDisplay);
    glutMainLoop();
    return 0;
}

顶点着色器:

attribute vec3 vPosition;
attribute vec3 vColor;
varying vec4 color;
void main()
{   
    color = vec4(vColor, 1.0);
    gl_Position = vec4(vPosition, 1.0);
} 

片段着色器

varying vec4 color;
void main()
{
    gl_FragColor = color;
}

这就是所有相关的代码。CalcPlayerPoints只返回一个大小为9的浮点数组来保存三角形坐标。CalcPlayerColor也有类似的功能。

最后一个可能有助于诊断问题的问题是,每当我试图通过关闭应用程序的窗口来退出程序时,我都会在glutmain循环中得到一个断点,但如果我关闭控制台窗口,它会很好地退出。

编辑:我添加了着色器以供参考。编辑:我使用的是opengl 3.1版

如果没有着色器,我们无法判断错误代码是否不是GLSL(错误的顶点转换等)您是否尝试过检查glGetError,看看问题是否不是来自初始化代码?

也许可以尝试将片段着色器的输出设置为vec4(1.0, 0.0, 0.0, 1.0),以检查其正常输出是否格式错误。

您的最后一个问题似乎揭示了一个未定义的行为,如错误的内存分配/释放,这可能发生在Player类中(顺便考虑一下,在初始化代码中将对象作为引用传递,因为它可能会触发一个浅拷贝,然后触发一个双自由指针)。

原来我从用于计算顶点的函数返回顶点数组的方式有问题。修复后,其余代码运行良好。