#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;
}
OpenGL 画螺旋线
最新推荐文章于 2021-09-01 18:54:53 发布