OpenGL进阶(四):用参数方程绘制椭球体

发表于2017-09-14
评论0 1.6k浏览

很多人知道球体的绘制,但要去绘制椭圆球体就不一定会了,为此本篇文章要教大家的是用参数方程绘制椭球体,一起来看看吧。


我们知道球体的参数方程是这样的:

x=r·sin(α)·cos(β)
y=r·sin(α)·sin(β)
z=r·cos(α)

椭圆的参数方程是:

x=rx·sin(α)·cos(β)
y=ry·sin(α)·sin(β)
z=rz·cos(α)

在这个基础上进行一些修改就可以实现椭圆的绘制了!

代码实现如下:

  1. /***************************************************************************** 
  2. Copyright: 2012, ustc All rights reserved. 
  3. contact:k283228391@126.com 
  4. File name: main.c 
  5. Description:using opengl in SDL. 
  6. Author:Silang Quan 
  7. Version: 1.0 
  8. Date: 2012.12.01 
  9. *****************************************************************************/  
  10. #include   
  11. #include   
  12. #include   
  13. #include   
  14. #include   
  15. #include   
  16. #define pi 3.1415926  
  17. SDL_Surface *screen;  
  18. typedef struct Point3f  
  19. {  
  20.  GLfloat x;  
  21.  GLfloat y;  
  22.  GLfloat z;  
  23. }point;  
  24.   
  25.   
  26. int getPoint2(GLfloat rx,GLfloat ry,GLfloat rz,GLfloat a,GLfloat b,point &p)  
  27. {  
  28.     p.x=rx*sin(a*pi/180.0)*cos(b*pi/180.0);  
  29.     p.y=ry*sin(a*pi/180.0)*sin(b*pi/180.0);  
  30.     p.z=rz*cos(a*pi/180.0);  
  31.     return 1;  
  32. }  
  33. point* getPointMatrix2(GLfloat rx,GLfloat ry,GLfloat rz,GLint slices)  
  34. {  
  35.  int i,j,w=2*slices,h=slices;  
  36.  float a=0.0,b=0.0;  
  37.  float hStep=180.0/(h-1);  
  38.  float wStep=360.0/w;  
  39.  int length=w*h;  
  40.  point *matrix;  
  41.  matrix=(point *)malloc(length*sizeof(point));  
  42.  if(!matrix)return NULL;  
  43.  for(a=0.0,i=0;i
  44.   for(b=0.0,j=0;j
  45.    getPoint2(rx,ry,rz,a,b,matrix[i*w j]);  
  46.  return matrix;  
  47. }  
  48.   
  49.   
  50. void drawSlice(point &p1,point &p2,point &p3,point &p4)  
  51. {  
  52.  glBegin(GL_LINE_LOOP);  
  53.  glColor3f(0,1,0);  
  54.  glVertex3f(p1.x,p1.y,p1.z);  
  55.  glVertex3f(p2.x,p2.y,p2.z);  
  56.  glVertex3f(p3.x,p3.y,p3.z);  
  57.  glVertex3f(p4.x,p4.y,p4.z);  
  58.  glEnd();  
  59. }  
  60.   
  61. int drawOval(GLfloat rx,GLfloat ry,GLfloat rz,GLint slices)  
  62. {  
  63.     int i=0,j=0,w=2*slices,h=slices;  
  64.     point *mx;  
  65.     mx=getPointMatrix2(rx,ry,rz,slices);  
  66.     if(!mx)return 0;  
  67.     for(;i
  68.  {  
  69.     for(j=0;j
  70.     {  
  71.         drawSlice(mx[i*w j],mx[i*w j 1],mx[(i 1)*w j 1],mx[(i 1)*w j]);  
  72.     }  
  73.     drawSlice(mx[i*w j],mx[i*w],mx[(i 1)*w],mx[(i 1)*w j]);  
  74.  }  
  75.  free(mx);  
  76.  return 1;  
  77. }  
  78. void quit( int code )  
  79. {  
  80.     SDL_Quit( );  
  81.     /* Exit program. */  
  82.     exit( code );  
  83. }  
  84. void handleKeyEvent( SDL_keysym* keysym )  
  85. {  
  86.     switch( keysym->sym )  
  87.     {  
  88.     case SDLK_ESCAPE:  
  89.         quit( 0 );  
  90.         break;  
  91.     case SDLK_SPACE:  
  92.         break;  
  93.     default:  
  94.         break;  
  95.     }  
  96. }  
  97. void resizeGL(int width,int height)  
  98. {  
  99.     if ( height == 0 )  
  100.     {  
  101.         height = 1;  
  102.     }  
  103.     //Reset View  
  104.     glViewport( 0, 0, (GLint)width, (GLint)height );  
  105.     //Choose the Matrix mode  
  106.     glMatrixMode( GL_PROJECTION );  
  107.     //reset projection  
  108.     glLoadIdentity();  
  109.     //set perspection  
  110.     gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0 );  
  111.     //choose Matrix mode  
  112.     glMatrixMode( GL_MODELVIEW );  
  113.     glLoadIdentity();  
  114. }  
  115. void handleEvents()  
  116. {  
  117.     // Our SDL event placeholder.  
  118.     SDL_Event event;  
  119.     //Grab all the events off the queue.  
  120.     while( SDL_PollEvent( &event ) ) {  
  121.         switch( event.type ) {  
  122.         case SDL_KEYDOWN:  
  123.             // Handle key Event  
  124.             handleKeyEvent( &event.key.keysym );  
  125.             break;  
  126.         case SDL_QUIT:  
  127.             // Handle quit requests (like Ctrl-c).  
  128.             quit( 0 );  
  129.             break;  
  130.         case SDL_VIDEORESIZE:  
  131.             //Handle resize event  
  132.             screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 16,  
  133.                                       SDL_OPENGL|SDL_RESIZABLE);  
  134.             if ( screen )  
  135.             {  
  136.                 resizeGL(screen->w, screen->h);  
  137.             }  
  138.             break;  
  139.         }  
  140.     }  
  141. }  
  142.   
  143. void initSDL(int width,int height,int bpp,int flags)  
  144. {  
  145.     // First, initialize SDL's video subsystem.  
  146.     if( SDL_Init( SDL_INIT_VIDEO ) < 0 )  
  147.     {  
  148.         fprintf( stderr, "Video initialization failed: %s\n",  
  149.                  SDL_GetError( ) );  
  150.         quit( 1 );  
  151.     }  
  152.     atexit(SDL_Quit);  
  153.     //Set some Attribute of OpenGL in SDL  
  154.     SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );  
  155.     SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );  
  156.     SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );  
  157.     SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );  
  158.     SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );  
  159.   
  160.     //Set the video mode  
  161.     screen= SDL_SetVideoMode( width, height, bpp,flags);  
  162.     if(!screen )  
  163.     {  
  164.         fprintf( stderr, "Video mode set failed: %s\n",SDL_GetError( ) );  
  165.         quit( 1 );  
  166.     }  
  167.     resizeGL(screen->w, screen->h);  
  168.     //Set caption  
  169.     SDL_WM_SetCaption( "OpenGL Test", NULL );  
  170. }  
  171.   
  172. void renderGL()  
  173. {  
  174.     // Clear the color and depth buffers.  
  175.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );  
  176.     // We don't want to modify the projection matrix. */  
  177.     glMatrixMode( GL_MODELVIEW );  
  178.     glLoadIdentity( );  
  179.     // Move down the z-axis.  
  180.     glTranslatef( 0.0, 0.0, -25.0 );  
  181.     glRotatef(50.0f,0,1,1);  
  182.     //Draw a square  
  183.     drawSphere(10,20);  
  184.     //drawOval(5,8,15,20);  
  185.     SDL_GL_SwapBuffers( );  
  186. }  
  187. void initGL( int width, int height )  
  188. {  
  189.     float ratio = (float) width / (float) height;  
  190.     // Our shading model--Gouraud (smooth).  
  191.     glShadeModel( GL_SMOOTH );  
  192.     // Set the clear color.  
  193.     glClearColor( 0, 0, 0, 0 );  
  194.     // Setup our viewport.  
  195.     glViewport( 0, 0, width, height );  
  196.     //Change to the projection matrix and set our viewing volume.  
  197.     glMatrixMode( GL_PROJECTION );  
  198.     glLoadIdentity();  
  199.     gluPerspective( 60.0, ratio, 1.0, 100.0 );  
  200. }  
  201. int main( int argc, char* argv[] )  
  202. {  
  203.   
  204.     // Dimensions of our window.  
  205.     int width = 640;  
  206.     int height = 480;  
  207.     // Color depth in bits of our window.  
  208.     int bpp = 32;  
  209.     int flags= SDL_OPENGL|SDL_RESIZABLE;  
  210.     //Set the SDL  
  211.     initSDL(width, height, bpp,flags);  
  212.     //Set the OpenGL  
  213.     initGL( width, height );  
  214.   
  215.     //main loop  
  216.     while(true)  
  217.     {  
  218.         /* Process incoming events. */  
  219.         handleEvents( );  
  220.         /* Draw the screen. */  
  221.         renderGL( );  
  222.     }  
  223.     return 0;  
  224. }  


http://blog.csdn.net/silangquan/article/details/8351111

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引