VS2012下基于Glut OpenGL显示一些立体图形示例程序:

Glut下提供了一些现成的绘制立体的API,如glutWireSphere绘制球,glutWireCone绘制椎体,glutWireCube绘制立体,glutWireTorus绘制甜圈,glutWireTeapot绘制茶壶,glutWireOctahedron绘制八面体。

Demo程序利用这些API绘制相应的立体,单击鼠标右键在弹出的菜单里可以选择需要绘制的立体以及是wire还是solid填充的,按键盘的UP、DOWN、LEFT、RIGHT按键可以旋转坐标系。由于Glut提供的多面体API默认半径是1.0f的,我使用glScalef对其进行了放大。示例效果如下:




源代码:

// GlutDemo.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include 
#include 
//圆周率宏
#define GL_PI 3.1415f
//获取屏幕的宽度
GLint SCREEN_WIDTH=0;
GLint SCREEN_HEIGHT=0;
//设置程序的窗口大小
GLint windowWidth=400;
GLint windowHeight=300;
//绕x轴旋转角度
GLfloat xRotAngle=0.0f;
//绕y轴旋转角度
GLfloat yRotAngle=0.0f;
//受支持的点大小范围
GLfloat sizes[2];
//受支持的点大小增量
GLfloat step;

enum MODELS{
	SPHERE=0,
	CONE,
	CUBE,
	TORUS,
	DODECAHEDRON,
	TEAPOT,
	OCTAHEDRON,
	TETRAHEDRON,
	ICOSAHEDRON
};
GLint iModel=SPHERE;
GLboolean bWire=GL_TRUE;
//菜单回调函数
void processMenu(int value){
	switch(value){
		case 1:
			iModel=SPHERE;
			break;
		case 2:
			iModel=CONE;
			break;
		case 3:
			iModel=CUBE;
			break;
		case 4:
			iModel=TORUS;
			break;
		case 5:
			iModel=DODECAHEDRON;
			break;
		case 6:
			iModel=TEAPOT;
			break;
		case 7:
			iModel=OCTAHEDRON;
			break;
		case 8:
			iModel=TETRAHEDRON;
			break;
		case 9:
			iModel=ICOSAHEDRON;
			break;
		case 10:
			bWire=GL_TRUE;
			break;
		case 11:
			bWire=GL_FALSE;
			break;
		default:
			break;
	}
	//重新绘制
	glutPostRedisplay();
}
//显示回调函数
void renderScreen(void){
	//将窗口颜色清理为黑色
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	//将模板缓冲区值全部清理为1
	glClearStencil(1);
	//使能模板缓冲区
	glEnable(GL_STENCIL_TEST);
    //把整个窗口清理为当前清理颜色:黑色。清除深度缓冲区、模板缓冲区
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
    //将当前Matrix状态入栈
    glPushMatrix();
    //坐标系绕x轴旋转xRotAngle
    glRotatef(xRotAngle,1.0f,0.0f,0.0f);
    //坐标系绕y轴旋转yRotAngle
    glRotatef(yRotAngle,0.0f,1.0f,0.0f);
	//进行平滑处理 
	glEnable(GL_POINT_SMOOTH);
	glHint(GL_POINT_SMOOTH,GL_NICEST);
	glEnable(GL_LINE_SMOOTH);
	glHint(GL_LINE_SMOOTH,GL_NICEST);
	glEnable(GL_POLYGON_SMOOTH);
	glHint(GL_POLYGON_SMOOTH,GL_NICEST);
	
	//白色绘制坐标系
	glColor3f(1.0f,1.0f,1.0f);
	glBegin(GL_LINES);
		glVertex3f(-9.0f,0.0f,0.0f);
		glVertex3f(9.0f,0.0f,0.0f);
		glVertex3f(0.0f,-9.0f,0.0f);
		glVertex3f(0.0f,9.0f,0.0f);
		glVertex3f(0.0f,0.0f,-9.0f);
		glVertex3f(0.0f,0.0f,9.0f);
	glEnd();

	glPushMatrix();
	glTranslatef(9.0f,0.0f,0.0f);
	glRotatef(90.0f,0.0f,1.0f,0.0f);
	glutSolidCone(0.3,0.6,10,10);
	glPopMatrix();

	glPushMatrix();
	glTranslatef(0.0f,9.0f,0.0f);
	glRotatef(-90.0f,1.0f,0.0f,0.0f);
	glutSolidCone(0.3,0.6,10,10);
	glPopMatrix();

	glPushMatrix();
	glTranslatef(0.0f,0.0f,9.0f);
	glRotatef(90.0f,0.0f,0.0f,1.0f);
	glutSolidCone(0.3,0.6,10,10);
	glPopMatrix();

	//设置绘画颜色为金色
    glColor3f(0.0f,1.0f,0.0f);
	switch(iModel){
		//球
		case SPHERE:
			if(bWire){
				glutWireSphere(8.0f,20,20);
			}
			else{
				glutSolidSphere(8.0f,20,20);
			}
			break;
		//锥体
		case CONE:
			if(bWire){
				glutWireCone(4.0f,8.0f,20,20);
			}
			else{
				glutSolidCone(4.0f,8.0f,20,20);
			}
			break;
		//立体
		case CUBE:
			if(bWire){
				glutWireCube(8.0f);
			}
			else{
				glutSolidCube(8.0f);
			}
			break;
		//甜圈
		case TORUS:
			if(bWire){
				glutWireTorus(3.0f,6.0f,20,20);
			}
			else{
				glutSolidTorus(3.0f,6.0f,20,20);
			}
			break;
		//十六面体,默认半径1.0
		case DODECAHEDRON:
			glScalef(6.0f,6.0f,6.0f);//x,y,z轴均放大6倍
			if(bWire){
				glutWireDodecahedron();
			}
			else{
				glutSolidDodecahedron();
			}
			break;
		//茶壶
		case TEAPOT:
			if(bWire){
				glutWireTeapot(8.0f);
			}
			else{
				glutSolidTeapot(8.0f);
			}
			break;
		//八面体,默认半径1.0
		case OCTAHEDRON:
			glScalef(6.0f,6.0f,6.0f);//x,y,z轴均放大6倍
			if(bWire){
				glutWireOctahedron();
			}
			else{
				glutSolidOctahedron();
			}
			break;
		//四面体,默认半径1.0
		case TETRAHEDRON:
			glScalef(6.0f,6.0f,6.0f);//x,y,z轴均放大6倍
			if(bWire){
				glutWireTetrahedron();
			}
			else{
				glutSolidTetrahedron();
			}
			break;
		//二十面体,默认半径1.0
		case ICOSAHEDRON:
			glScalef(6.0f,6.0f,6.0f);//x,y,z轴均放大6倍
			if(bWire){
				glutWireIcosahedron();
			}
			else{
				glutSolidIcosahedron();
			}
			break;
		default:
			break;
	}

    //恢复压入栈的Matrix
    glPopMatrix();
    //交换两个缓冲区的指针
    glutSwapBuffers();
}
//设置Redering State 
void setupRederingState(void){
    //设置清理颜色为黑色
    glClearColor(0.0f,0.0,0.0,1.0f);
    //设置绘画颜色为绿色
    glColor3f(1.0f,1.0f,0.0f);
	//使能深度测试
	glEnable(GL_DEPTH_TEST);
	//获取受支持的点大小范围
	glGetFloatv(GL_POINT_SIZE_RANGE,sizes);
	//获取受支持的点大小增量
	glGetFloatv(GL_POINT_SIZE_GRANULARITY,&step);
	printf("point size range:%f-%f\n",sizes[0],sizes[1]);
	printf("point step:%f\n",step);
}
//窗口大小变化回调函数
void changSize(GLint w,GLint h){
    //横宽比率
    GLfloat ratio;
    //设置坐标系为x(-100.0f,100.0f)、y(-100.0f,100.0f)、z(-100.0f,100.0f)
    GLfloat coordinatesize=10.0f;
    //窗口宽高为零直接返回
    if((w==0)||(h==0))
        return;
    //设置视口和窗口大小一致
    glViewport(0,0,w,h);
    //对投影矩阵应用随后的矩阵操作
    glMatrixMode(GL_PROJECTION);
    //重置当前指定的矩阵为单位矩阵 
    glLoadIdentity();
    ratio=(GLfloat)w/(GLfloat)h;
    //正交投影
    if(w



你可能感兴趣的:(VS2012下基于Glut OpenGL显示一些立体图形示例程序:)