立即注册
 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广州大学城网业务调整

[OpenGL] [小进阶]画一个杯具 [复制链接] qrcode

查看: 6414 | 回复: 0

523066680
发表于: 2010-11-15 22:42:51 | 显示全部楼层

其实就是画球体的时候,把z值搞分化了。这个球就画凹了。
然后经过角度旋转,效果挺不错的。
看效果图


  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <unistd.h>
  5. #include <GL/glut.h>
  6. #include <math.h>
  7. #define xpix 400
  8. #define ypix 400
  9. int winid;
  10. float Rtang=0.0,Ra=0.0,Rb=0.0;
  11. int ArrSum;
  12. float dot[140000][3];
  13. float clr[140000][3];
  14. void display(void)
  15. {
  16.    int i;
  17.    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  18.    glEnableClientState(GL_VERTEX_ARRAY);
  19.    glEnableClientState(GL_COLOR_ARRAY);
  20.    glVertexPointer(3,GL_FLOAT,0,dot);
  21.    glColorPointer(3,GL_FLOAT,0,clr);
  22.    glPushMatrix();
  23.      glRotatef(Rtang,1.0,0.0,0.0);
  24.      glRotatef(Ra,0.0,1.0,0.0);
  25.      glRotatef(Rb,1.0,0.0,0.0);
  26.      glDrawArrays(GL_POINTS,0,ArrSum);
  27.    glPopMatrix();
  28.    glutSwapBuffers();
  29. }
  30. void next(void) {
  31.   if (Rtang<=250.0) {
  32.      usleep(1000);
  33.      Rtang++;
  34.      glutPostRedisplay();
  35.   } else {
  36.      glutIdleFunc(NULL);
  37.   }
  38. }
  39. void init(void) {
  40.     float ang,angb,len,deep,cx,cy;
  41.     float add;
  42.     int i=0;
  43.     glClearColor(0.0,0.0,0.0,0.0);
  44.     glEnable(GL_BLEND);
  45.     glEnable(GL_DEPTH_TEST);
  46.     for (ang=0.0;ang<3.1415/2.0;ang+=0.01) {
  47.         len=100.0-cos(ang)*100.0;  //here ,100.0-cos... change to circle
  48.         deep=sin(ang)*100.0;
  49.           add=6.28/(2.0*3.14*len);
  50.         for (angb=0.0;angb<6.28;angb+=add) {
  51.             cx=cos(angb)*len;
  52.             cy=sin(angb)*len;
  53.             dot[i][0]=cx;   clr[i][0]=cx/100.0;
  54.             dot[i][1]=cy;   clr[i][1]=cy/100.0;
  55.             dot[i][2]=deep-10;  clr[i][2]=(100+deep)/200.0;
  56.                i++;
  57.             dot[i][0]=cx;  clr[i][0]=cx/100.0;
  58.             dot[i][1]=cy;  clr[i][1]=cy/100.0;
  59.             dot[i][2]=-deep+10;  clr[i][2]=(100-deep)/200.0;
  60.                i++;
  61.         }
  62.     }
  63.      ArrSum=i-1;
  64.      printf("%d,\n",i);
  65. }
  66. void reshape(int winx,int winy) {
  67.     float fa=500.0;
  68.     glViewport(0,0,winx,winy);
  69.     glMatrixMode(GL_PROJECTION);
  70.       glLoadIdentity();
  71.       gluPerspective(70.0,1.0,3.0,800.0);
  72.       //glOrtho(0.0,xpix,0.0,ypix,0.0,600.0);
  73.     glMatrixMode(GL_MODELVIEW);
  74.       glLoadIdentity();
  75.       gluLookAt(0.0,0.0,fa, 0.0,0.0,0.0, 0.0,1.0,fa);
  76. }
  77. void hitkey(unsigned char key,int mousex,int mousey) {
  78.      static int stop=0;
  79.      switch (key) {
  80.        case 'q':
  81.            glutDestroyWindow(winid);
  82.            exit(0);
  83.            break;
  84.        case 'p':
  85.            stop=1-stop;
  86.            if (stop==1) {glutIdleFunc(NULL);} else {glutIdleFunc(next);}
  87.       }
  88. }
  89. void mouse(int mousex,int mousey) {
  90.    static int tx;
  91.    static int ty;
  92.    if (mousex>tx) Ra+=1;
  93.    if (mousex<tx) Ra-=1;
  94.    if (mousey>ty) Rb+=1;
  95.    if (mousey<ty) Rb-=1;
  96.    tx=mousex;
  97.    ty=mousey;
  98.    glutPostRedisplay();
  99. }
  100. int main(int argc,char *argv[])
  101. {
  102.      glutInit(&argc,argv);
  103.      glutInitWindowSize(xpix,ypix);
  104.      glutInitWindowPosition(0,0);
  105.      glutInitDisplayMode(GLUT_DEPTH|GLUT_RGBA|GLUT_DOUBLE);
  106.      winid=glutCreateWindow("PakTC");
  107.      init();
  108.      glutReshapeFunc(reshape);
  109.      glutDisplayFunc(display);
  110.      glutKeyboardFunc(hitkey);
  111.      glutMotionFunc(mouse);
  112.      glutIdleFunc(next);
  113.      glutMainLoop();
  114.      return 0;
  115. }
复制代码
跳转到指定楼层
快速回复 返回顶部 返回列表