|
|
以下是源码:
#include<windows.h>
#include<math.h>
#include<gl/gl.h>
#include<gl/glu.h>
#include<gl/glaux.h>
void init();
void CALLBACK reshape(GLsizei w, GLsizei h);
void CALLBACK display();
GLfloat s,h;
//回调函数,绘制窗口时调用
void CALLBACK display()
{
//清空窗口,设置背景色为白
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
//取景变换
glLoadIdentity();
glLookAt(5,5,h,s,0,0,0,1,0);
//glRotatef(30,1,1,0);
//设置前景色为黑色
glColor3f(0,0,0);
//绘图开始,两条水平平行线
GLfloat RAD=3.1415926/180;
GLfloat x,y,z,r;
int i,j;
for(i=0;i<180;i+=5)
{
glBegin(GL_LINE_LOOP);
r=2*sin(i*RAD);
z=2*cos(i*RAD);
for(j=0;j<360;j+=10)
{
x=r*cos(j*RAD);
y=r*sin(j*RAD);
glVertex3f(x,y,z);
}
glEnd();
}
for(j=0;j<360;j+=10)
{
glBegin(GL_LINE_LOOP);
for(i=0;i<=180;i+=10)
{
r=2*sin(i*RAD);
z=2*cos(i*RAD);
x=r*cos(j*RAD);
y=r*sin(j*RAD);
glVertex3f(x,y,z);
}
glEnd();
}
//清空桢缓存
glFlush();
}
//openGL初始化,设置颜色为单一着色模式
void init()
{
glShadeModel(GL_FLAT);
s=0;
h=5;
}
//回调函数,窗口初始化和大小改变时,调用此函数
void CALLBACK reshape(GLsizei w, GLsizei h)
{
//设置当前矩阵为投影变换矩阵
glMatrixMode(GL_PROJECTION);
//设置投影变换
glLoadIdentity();
gluPerspective(30,1,-3,3);
//设置当前矩阵为模式变换矩阵
glMatrixMode(GL_MODELVIEW);
//设置视区变换
glViewport(0,0,w,h);
}
void CALLBACK Left()
{
s+=0.1;
}
void CALLBACK Right()
{
s-=0.1;
}
void CALLBACK Up()
{
h-=0.1;
}
void CALLBACK Down()
{
h+=0.1;
}
//主函数
void main()
{
//设置openGL显示模式,单缓存,RGB模式
ausInitDisplayMode(AUX_SINGLE|AUX_RGB);
//设置窗口位置,大小,标题
auxInitPosition(0,0,300,300);
auxInitWindow(“LinHG”);
init()
//设置回调函数
auxKeyFunc(AUX_LEFT,Left);
auxKeyFunc(AUX_RIGHT,Right);
auxKeyFunc(AUX_UP,Up);
auxKeyFunc(AUX_DOWN,Down);
auxReshapeFunc(reshape);
auxMainLoop(display);
}
//over |
|