2

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

这是我的初始化代码:

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 做了类似的事情。

最后一个可能有助于诊断问题的问题是,每当我尝试通过关闭应用程序窗口来退出程序时,都会在 glutmainloop 中获得一个断点,但是如果我关闭控制台窗口,它就会正常退出。

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

4

2 回答 2

2

没有着色器,我们不能说错误代码是否不是 GLSL(坏顶点转换等)。您是否尝试检查glGetError问题是否来自您的初始化代码?

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

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

于 2013-07-10T15:40:39.607 回答
0

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

于 2013-07-21T03:56:44.830 回答