我正在尝试使用opengl 3.1获取3D三角形。它呈现为2D三角形,但是当我添加第三个坐标时,它并不像我想要的那样。我将0.0 z coodinate添加到所有三个三角形顶点。以下是代码:
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;
}
答案 0 :(得分:2)
你说它在Z上加零,但这就是你在顶点数组中实际指定的内容。在你的情况下,glVertexAttribPointer第二个参数应该是3。