OpenGL 画螺旋线

#include <windows.h>
#include <gl/glut.h>
#include <math.h>
#define GL_PI 3.1415f


static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;


void RenderScene(void)
{
   GLfloat x,y,z,angle;
   glClear(GL_COLOR_BUFFER_BIT);
   glColor3f(1.0f,0.0f,0.0f);
 glPointSize(10); 
   glPushMatrix();  //因为按键按了的话,原矩阵就发生改变,所以要保存原矩阵,在下次运用时就又从原矩阵开始
       glRotatef(xRot,1.0f,0.0f,0.0f);
       glRotatef(yRot,1.0f,0.0f,1.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.0*cos(angle);
	   glVertex3f(x,y,z);
	   z += 0.5f;
   }
   glEnd();
   glPopMatrix();
   glFlush(); 
    
}


void ChangeSize(GLsizei w,GLsizei h)
{
	GLfloat aspectRatio;
	if(h==0) h=1; 
	glViewport(0,0,w,h); 
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity(); 

	aspectRatio=(GLfloat)w/(GLfloat)h;
    if(w<=h)
	  glOrtho(-100.0,100.0,-100/aspectRatio,100.0/aspectRatio,100.0,-100.0);
	else
	  glOrtho(-100.0*aspectRatio,100.0*aspectRatio,-100,100.0,100.0,-100.0);

	glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


void SetupRC(void)   
{
	glClearColor(0.0f,0.0f,1.0f,1.0f);
}


void SpecialKeys(int key ,int x, int y)
{
	if(key==GLUT_KEY_UP)
	xRot -= 5.0f;
	if(key==GLUT_KEY_DOWN)
	xRot +=5.0f;
    if(key == GLUT_KEY_LEFT)
	yRot -=5.0f;
	if(key == GLUT_KEY_RIGHT)
	yRot +=5.0f;


	xRot = (GLfloat)((const int)xRot %360);
	yRot = (GLfloat)((const int)yRot %360);


	glutPostRedisplay(); //刷新绘图
}
int main(int argc, char* argv[])
{
	glutInit(&argc,argv); 
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA); 
	glutCreateWindow("simple");  
	glutDisplayFunc(RenderScene); 
	SetupRC();
	glutReshapeFunc(ChangeSize);
	glutSpecialFunc(SpecialKeys);//设置上下左右键对图形旋转的控制
	glutMainLoop(); 
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值