<分区>
我正在尝试使用 opengl 3.1 获取 3D 三角形。它可以很好地呈现为 2D 三角形,但是当我添加第三个坐标时,它并没有像我想要的那样进行。我将 0.0 z 坐标添加到所有三个三角形顶点。以下是代码:
void init( void )
{
vec3 points[NumPoints];
/*
points[0] = vec2(-0.9, 0.9 );
points[1] = (vec2(-0.9, -0.9));
points[2] = (vec2(0.9, -0.9));
*/
points[0] = vec3(-0.9, 0.9, 0.0 );
points[1] = vec3(-0.9, -0.9, 0.0 );
points[2] = vec3(0.9, -0.9, 0.0 );
// Create a vertex array object
GLuint vao;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
// Create and initialize a buffer object
GLuint buffer;
glGenBuffers( 1, &buffer );
glBindBuffer( GL_ARRAY_BUFFER, buffer );
glBufferData( GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW );
// Load shaders and use the resulting shader program
GLuint program = InitShader( "vshader21.glsl", "fshader21.glsl" );
glUseProgram( program );
// Initialize the vertex position attribute from the vertex shader
GLuint loc = glGetAttribLocation( program, "vPosition" );
glEnableVertexAttribArray( loc );
glVertexAttribPointer( loc, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) );
glClearColor( 1.0, 1.0, 1.0, 1.0 ); // white background
glEnable(GL_DEPTH_TEST);
}
void display( void )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the window
glDrawArrays( GL_TRIANGLES, 0, NumPoints ); // draw the points
glFlush();
}
int main( int argc, char **argv )
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH );
glutInitWindowSize( 512, 512 );
glutCreateWindow( "Triangle" );
glewInit();
init();
glutDisplayFunc( display );
glutKeyboardFunc( keyboard );
glutMainLoop();
return 0;
}