2 a1136519154 a1136519154 于 2014.10.11 13:24 提问

初学OpenGL,照着书上的代码画出来只有一个红色三角形

去掉画三角形的代码也看不见四边形和圆形
#include
#include
#include
#define NUM 200 //绘制圆时用的顶点数
GLfloat r=1.0f; //圆半径
void DrawSceneGL(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除颜色及深度缓存
glLoadIdentity();
glColor3f(1.0f,0.0f,0.0f); //设置画笔颜色
glBegin(GL_TRIANGLES); //画三角形
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,0.0f);
glVertex3f(1.0f,-1.0f,0.0f);
glEnd();
glTranslatef(-1.5f,1.0f,-7.0f); //移动坐标
glTranslatef(3.0f,0.0f,0.0f);
glColor3f(0.0f,0.0f,1.0f); //设置画笔为蓝色
glBegin(GL_QUADS); //画四边形
glVertex3f(-1.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,0.0f);
glVertex3f(1.0f,-1.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);
glEnd();
glColor3f(1.0f,1.0f,0.0f);
glTranslatef(-1.5f,-2.5f,0.0f);
glLineWidth(2.0f); //画黄色线
glBegin(GL_LINES);
glVertex2f(-1.0f,0.0f);
glVertex2f(-1.0f,0.0f);
glEnd();
glBegin(GL_LINE_LOOP); //画圆
for(int i=0;i<NUM;i++)
glVertex3f(r*float(sin(2*3.14*i/NUM)),r*float(cos(2*3.14*i/NUM)),0.0);
glEnd();
glFlush();
}

int main(int argc,char ** argv){
glutInit(&argc,argv);
//glutInitWindowPosition(200,200);
glutCreateWindow("few example");
glutDisplayFunc(DrawSceneGL);

glutMainLoop();
return(0);

}

Csdn user default icon
上传中...
上传图片
插入图片