原地址

首先参考这篇文章绘制一个球体:OpenGL 用参数方程绘制球

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

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

椭圆的参数方程是:

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

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

代码实现如下:

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

用参数方程绘制椭球体相关推荐

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

    首先参考这篇文章绘制一个球体:OpenGL 用参数方程绘制球 我们知道球体的参数方程是这样的: x=r·sin(α)·cos(β) y=r·sin(α)·sin(β) z=r·cos(α) 椭圆的参数 ...

  2. Matlab 利用参数方程绘制空心球体

    基本原理: 实质为利用球面参数方程,利用网格化数据绘制 x=R*sin(theta)*cos(phi) y=R*sin(theta)*sin(phi) z=R*cos(theta) 绘制函数: fun ...

  3. matlab绘制 椭球体,如何用绘图法绘制椭球体

    好吧,比我想象的要容易.有一个alphahull选项,它要求自动计算相应的三角剖分.在from plotly.offline import iplot, init_notebook_mode from ...

  4. 好用的机电revit软件丨revit中怎么画球体,半球体,椭球体?

    好用的机电revit软件丨revit中怎么画球体,半球体,椭球体? revit中怎么画球体,半球体,椭球体?如图-1所示,如何通过内建模型创建呢? 单击[内建模型]命令→[族类型和参数]→选择[常规模 ...

  5. mysql 大地坐标系_1980西安坐标系采用的椭球体为: ( )_学小易找答案

    [填空题]酸性缓冲溶液是由_______________组成. [判断题]用字符指针变量指向一个字符串常量,通过字符指针变量引用字符串常量. [单选题]您对我们这个品牌熟悉吗? [单选题]图代码运行后 ...

  6. arcpy投影(三)——定义投影、地理变换关系自定义和投影变换Project_managemen(含基准面/椭球体转换参数使用方法,arcpro/arcmap)

    arcpy投影这一个专题从文件位置.文件含义.空间参照获取.转换关系查询.投影定义.自定义转换关系.投影变换这几个角度上系统的进行了介绍,整理出了: arcpy投影(一)--prj.gtf文件定义.路 ...

  7. GIS的基本概念二:大地水准面、旋转椭球体(椭球体)、大地基准面

    上一章粗略整理了一下坐标系的概念,基本理解如何用坐标来表示地理空间.面对现实的地球,还是有一个疑问.众所周知,我们的地球表面是一个凹凸不平的表面,对于地球测量而言,地表是一个无法用数学公式进行表达的曲 ...

  8. 计算机图形学 | 实验四:绘制一个球体

    计算机图形学 | 实验四:绘制一个球体 计算机图形学 | 实验四:绘制一个球体 封装Shader 为什么要封装Shader 如何使用 绘制球模型 球面顶点遍历 构造三角形图元 开启线框模式 开启面剔除 ...

  9. Proj.NET-地球椭球体、大地基准面及地图投影

    地球椭球体(Ellipsoid) 众所周知我们的地球表面是一个凸凹不平的表面,而对于地球测量而言,地表是一个无法用数学公式表达的曲面,这样的曲面不能作为测量和制图的基准面.假想一个扁率极小的椭圆,绕大 ...

  10. matlab绘制三维球体,使用Matlab绘制三维圆柱体和球体

    使用Cylinder功能函数绘制圆柱体侧面 在matlab中自带了绘制圆柱体的功能函数cylinder,其用法如下: 例1,绘制一个圆柱体的三维图像,要求圆柱体底面圆心在坐标原点,底面半径为3,高度为 ...

最新文章

  1. 干货 | 100+个NLP数据集大放送,再不愁数据!
  2. MFC中的MainFrame Dlg,App,Doc,View的关系
  3. 深入理解Objective-C:方法缓存
  4. 【转】React 16 中从 setState 返回 null 的妙用
  5. zookeeper安装包下载地址
  6. OpenCV学习笔记(四):XML,YAML(.txt,.doc)文件读写操作
  7. css 相同的css属性_CSS中的order属性
  8. java多个mapreduce_java – 在hadoop中运行多个MapReduce作业
  9. 内存中的栈空间与堆空间
  10. ARP协议,ARP诈骗图
  11. activiti流程消息事件触发messageEventReceived,MessageCatchingEvent 及消息启动流程
  12. 课堂对Complex类运算符重载的小练习
  13. java打印出日历_java控制台打印本月的日历
  14. tensorflow pb ckpt pbtxt
  15. 尔雅网络选课 大学计算机知识,2017-2018学年第1学期超星尔雅网络课程选课指南.PDF...
  16. 【基础】创建react脚手架
  17. Java知识点_类锁和对象锁的区别?
  18. c语言如何文件指针指向开头,fseek设置好文件指针 在C语言中fseek()的功能
  19. 《爬虫》爬取谷歌网页“人脸”图片
  20. 包装类-Wrapper

热门文章

  1. Alink漫谈(十一) :线性回归 之 L-BFGS优化
  2. ubuntu命令安装中文语言包_Ubuntu安装中文语言包
  3. 康托尔定理是怎样证明的?
  4. 【【运维】9个网络故障排除经典案例,你都得会吗?】
  5. w10系统服务器启动失败,三种方法教你解决Win10系统Apache启动失败问题
  6. 64位平台C/C++开发注意事项
  7. html页面出现504,web生产环境故障页面提示504错误
  8. 2021.11.20【读书笔记】|差异可变剪接事件及DTU分析
  9. 计算机二级答题技巧口诀,计算机二级考试复习技巧:考场答题经验分享
  10. 各种带有物理学特点的把妹法[转]