三角形不显示

时间:2015-08-06 10:42:31

标签: opengl

我正在尝试使用以下代码显示三角形。我使用的着色器非常基本,基本上只输出白色。我看不出错误在哪里。

void init( void ){

glClearColor( 0.0, 0.0, 0.0, 0.0 );

vec4 vertexPositions[] =
{
    0.75f, 0.75f, 0.0f, 1.0f,
    0.75f, -0.75f, 0.0f, 1.0f,
    -0.75f, -0.75f, 0.0f, 1.0f,
};

GLuint positionBufferObject;

glGenBuffers(1, &positionBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW);

GLuint program = InitShader( "vshader23.glsl", "fshader23.glsl" );
glUseProgram( program );

GLuint loc = glGetAttribLocation( program, "vPosition" );
glEnableVertexAttribArray(loc);

// tell openGL how to interpret the array of data stored in buffer
// this implicitly refers to the buffer currently bound to GL_ARRAY_BUFFER
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
}

void display( void )
{
// clears the window, sets color to glClearColor()
glClear( GL_COLOR_BUFFER_BIT );

// rendering function
glDrawArrays(GL_TRIANGLES, 0, 3);

glFlush();
}

1 个答案:

答案 0 :(得分:0)

在使用以下语句绘制三角形之前,也要清除深度缓冲区:

glClearColor(19.0f / 255.0f, 9.0f / 255.0f, 99.0f / 255.0f, 1.0f);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

我编译了代码,它现在正在运行。您可以找到我的代码Here