(OpenGL)绘制线但不是形状

时间:2016-10-29 09:30:21

标签: opengl

我目前正在学习OpenGL,根据我的理解,我必须致电

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

绘制线条之前。然后,绘制我需要调用的形状

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

绘制GL_TRIANGLES和GL_QUADS等形状。 我编写这段代码的目的是在顶部和3个形状绘制一条线,但只绘制了线条。 这是我的代码。

void drawScene() {
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective



glBegin(GL_LINES);

glColor3f(100,200,100);
glLineWidth(10.0f);
glVertex2f(-1.0f,0.8f);
glVertex2f(1.0f,0.8f);

glEnd();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glBegin(GL_QUADS); //Begin quadrilateral coordinates

//Trapezoid
glVertex3f(-0.7f, -1.5f, -5.0f);
glVertex3f(0.7f, -1.5f, -5.0f);
glVertex3f(0.4f, -0.5f, -5.0f);
glVertex3f(-0.4f, -0.5f, -5.0f);

glEnd(); //End quadrilateral coordinates


glBegin(GL_TRIANGLES); //Begin triangle coordinates

//Pentagon
glVertex3f(0.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);

glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);

glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);
glVertex3f(1.0f, 1.5f, -5.0f);

//Triangle
glVertex3f(-0.5f, 0.5f, -5.0f);
glVertex3f(-1.0f, 1.5f, -5.0f);
glVertex3f(-1.5f, 0.5f, -5.0f);

glEnd(); //End triangle coordinates

glutSwapBuffers(); //Send the 3D scene to the screen
}

有人可以向我解释如何在GL_PROJECTION和GL_MODELVIEW之间切换以及它们如何运作?

1 个答案:

答案 0 :(得分:2)

多边形的Z值是-5.0,它超出了设备坐标的默认[-1,1]范围,因此它们会被丢弃。

将所有glVertex3f(x, y, z)次来电替换为glVertex2f(x,y)