其实就是画球体的时候,把z值搞分化了。这个球就画凹了。
然后经过角度旋转,效果挺不错的。
看效果图
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #include <unistd.h>
- #include <GL/glut.h>
- #include <math.h>
-
- #define xpix 400
- #define ypix 400
-
- int winid;
- float Rtang=0.0,Ra=0.0,Rb=0.0;
- int ArrSum;
- float dot[140000][3];
- float clr[140000][3];
-
- void display(void)
- {
- int i;
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- glEnableClientState(GL_VERTEX_ARRAY);
- glEnableClientState(GL_COLOR_ARRAY);
- glVertexPointer(3,GL_FLOAT,0,dot);
- glColorPointer(3,GL_FLOAT,0,clr);
- glPushMatrix();
- glRotatef(Rtang,1.0,0.0,0.0);
- glRotatef(Ra,0.0,1.0,0.0);
- glRotatef(Rb,1.0,0.0,0.0);
- glDrawArrays(GL_POINTS,0,ArrSum);
- glPopMatrix();
- glutSwapBuffers();
- }
- void next(void) {
- if (Rtang<=250.0) {
- usleep(1000);
- Rtang++;
- glutPostRedisplay();
- } else {
- glutIdleFunc(NULL);
- }
- }
-
-
- void init(void) {
- float ang,angb,len,deep,cx,cy;
- float add;
- int i=0;
- glClearColor(0.0,0.0,0.0,0.0);
- glEnable(GL_BLEND);
- glEnable(GL_DEPTH_TEST);
- for (ang=0.0;ang<3.1415/2.0;ang+=0.01) {
- len=100.0-cos(ang)*100.0; //here ,100.0-cos... change to circle
- deep=sin(ang)*100.0;
- add=6.28/(2.0*3.14*len);
- for (angb=0.0;angb<6.28;angb+=add) {
- cx=cos(angb)*len;
- cy=sin(angb)*len;
- dot[i][0]=cx; clr[i][0]=cx/100.0;
- dot[i][1]=cy; clr[i][1]=cy/100.0;
- dot[i][2]=deep-10; clr[i][2]=(100+deep)/200.0;
- i++;
- dot[i][0]=cx; clr[i][0]=cx/100.0;
- dot[i][1]=cy; clr[i][1]=cy/100.0;
- dot[i][2]=-deep+10; clr[i][2]=(100-deep)/200.0;
- i++;
- }
- }
- ArrSum=i-1;
- printf("%d,\n",i);
- }
-
- void reshape(int winx,int winy) {
- float fa=500.0;
- glViewport(0,0,winx,winy);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(70.0,1.0,3.0,800.0);
- //glOrtho(0.0,xpix,0.0,ypix,0.0,600.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(0.0,0.0,fa, 0.0,0.0,0.0, 0.0,1.0,fa);
- }
-
- void hitkey(unsigned char key,int mousex,int mousey) {
- static int stop=0;
- switch (key) {
- case 'q':
- glutDestroyWindow(winid);
- exit(0);
- break;
- case 'p':
- stop=1-stop;
- if (stop==1) {glutIdleFunc(NULL);} else {glutIdleFunc(next);}
- }
- }
- void mouse(int mousex,int mousey) {
- static int tx;
- static int ty;
- if (mousex>tx) Ra+=1;
- if (mousex<tx) Ra-=1;
- if (mousey>ty) Rb+=1;
- if (mousey<ty) Rb-=1;
- tx=mousex;
- ty=mousey;
- glutPostRedisplay();
- }
-
- int main(int argc,char *argv[])
- {
- glutInit(&argc,argv);
- glutInitWindowSize(xpix,ypix);
- glutInitWindowPosition(0,0);
- glutInitDisplayMode(GLUT_DEPTH|GLUT_RGBA|GLUT_DOUBLE);
- winid=glutCreateWindow("PakTC");
- init();
- glutReshapeFunc(reshape);
- glutDisplayFunc(display);
- glutKeyboardFunc(hitkey);
- glutMotionFunc(mouse);
- glutIdleFunc(next);
- glutMainLoop();
- return 0;
- }
复制代码 |
|