在空间中绘图:线。点、多边形
在空间中绘图:线。点、多边形
三维螺旋线
效果
代码
void RenderScene(void){
GLfloat x,y,z,angle; //使用当前的清除色清除窗口
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(xRot,1.0f,0.0f,0.0f);
glRotatef(yRot,0.0f,1.0f,0.0f); //对所有的点调用一次
glBegin(GL_POINTS);
z = -50.0f;
for(angle = 0.0f; angle <= (2.0f*GL_PI)*3.0f;angle += 0.1f)
{
x = 50.0f*sin(angle);
y = 50.0f*cos(angle);
glVertex3f(x,y,z);
z += 0.5f;
}
glEnd();
glPopMatrix();
glFlush();}
改变点大小
效果
代码
void RenderScene(void){
GLfloat x,y,z,angle;
GLfloat sizes[2];//保存支持点大小的范围
GLfloat step; //保存支持的点增长
GLfloat curSize;//保存当前点大小
//使用当前的清除色清除窗口
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(xRot,1.0f,0.0f,0.0f);
glRotatef(yRot,0.0f,1.0f,0.0f); //获取支持点的范围和增长
glGetFloatv(GL_POINT_SIZE_RANGE,sizes);
glGetFloatv(GL_POINT_SIZE_GRANULARITY,&step); //设置初始点大小
curSize = sizes[0]; //设置z坐标起始值
z = -50.0f; //对所有的点调用一次
for(angle = 0.0f; angle <= (2.0f*GL_PI)*3.0f;angle += 0.1f)
{
x = 50.0f*sin(angle);
y = 50.0f*cos(angle);
glPointSize(curSize);
glBegin(GL_POINTS);
glVertex3f(x,y,z);
glEnd();
z += 0.5f;
curSize += step;
}
glPopMatrix();
glFlush();
}
扇形
效果
代码
void RenderScene(void){
GLfloat x,y,z,angle; //使用当前的清除色清除窗口
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix(); //对所有的点调用一次
glBegin(GL_LINES);
z = 0.0f;
for(angle = 0.0f; angle <= GL_PI*3.0f;angle += 0.5f)
{
x = 50.0f*sin(angle);
y = 50.0f*cos(angle);
glVertex3f(x,y,z);
x = 50.0f*sin(angle+3.1415f);
y = 50.0f*cos(angle+3.1415f);
glVertex3f(x,y,z);
}
glEnd();
glPopMatrix();
glFlush();}
线段的跨接和循环
效果
代码
void RenderScene(void){
GLfloat x,y,z,angle; //使用当前的清除色清除窗口
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix(); //对所有的点调用一次
glBegin(GL_LINE_STRIP);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(50.0f,50.0f,0.0f);
glVertex3f(50.0f,100.0f,0.0f);
glEnd();
glPopMatrix();
glFlush();}
将glBegin改为glBegin(GL_LINE_LOOP);后的结果将把最后一个点和第一个点连接起来,形成一个封闭的图形:
线宽度
效果
代码
void RenderScene(void)
{
GLfloat y;
GLfloat fSizes[2];
GLfloat fCurrSize;
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
//get line size metrics and save the smallest value
glGetFloatv(GL_LINE_WIDTH_RANGE,fSizes); //
glGetFloatv(GL_POINT_SIZE_GRANULARITY,&step);
fCurrSize = fSizes[0];
for(y = -90.0f; y <= 90.0f;y += 20.0f)
{
//set the line width
glLineWidth(fCurrSize);
glBegin(GL_LINES);
glVertex2f(-80.0f,y);
glVertex2f(80.0f,y);
glEnd();
fCurrSize += 1.0f;
}