OpenGL: 显示列表绘制六面体

#include <math.h>
#include <stdlib.h>
#include <GL/glut.h>
#pragma comment(lib,"glut32.lib")

const double TWO_PI = 6.2831853;
//初始化窗口大小
GLsizei winWidth = 400, winHeight = 400;
GLuint regHex;
class screenPt
{
private:
    GLint x,y;//初始化坐标为(0,0)
public:
    screenPt()
	{
        x = y = 0;
    }
    void setCoords(GLint xCoord, GLint yCoord)
	{
        x = xCoord;
        y = yCoord;
    }
    GLint getx() const
	{
        return x;
    }
    GLint gety() const
	{
        return y;
    }
};
static void init (void)
{
    screenPt hexVertex, circCtr;
    GLdouble theta;
    GLint k;
  //设置圆心坐标
    circCtr.setCoords(winWidth / 2, winHeight / 2);

    glClearColor(1.0, 1.0, 1.0, 0.0);/* 显示窗口颜色为白色*/
    //Set up a display list for a red regular hexagon.
    /*Vertices for the hexagon are six equally spaced. 
        points aroud the circumference of a circle.*/

    regHex = glGenLists(1);//get an identifier for the display list
    glNewList(regHex,GL_COMPILE);
	{
		glColor3f(1.0,0.0,0.0);//set fill color for hexagon to red
        glBegin(GL_POLYGON);
		{
			for (k = 0; k < 6; k++)
            {
                theta = TWO_PI*k/6.0;
                hexVertex.setCoords(circCtr.getx()+150*cos(theta),
                                                        circCtr.gety()+150*sin(theta));
                glVertex2i(hexVertex.getx(),hexVertex.gety());
            }
		}
        glEnd();
	}
    glEndList();
}
void regHexagon()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glCallList(regHex);
    glFlush();
}

void winReshapeFcn(int newWidth,int newHeight)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, (GLdouble)newWidth, 0.0, (GLdouble)newHeight);
    glClear(GL_COLOR_BUFFER_BIT);
}

void main( int  argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(winWidth,winHeight);
    glutCreateWindow("Reshape-Function & Display-List Example");

    init();
    glutDisplayFunc(regHexagon);
    glutReshapeFunc(winReshapeFcn);

    glutMainLoop();
}

http://www.cnblogs.com/lizhi0320/archive/2009/07/25/1531009.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值