用参数方程绘制椭球体
原地址
首先参考这篇文章绘制一个球体:OpenGL 用参数方程绘制球
我们知道球体的参数方程是这样的:
x=r·sin(α)·cos(β)
y=r·sin(α)·sin(β)
z=r·cos(α)
椭圆的参数方程是:
x=rx·sin(α)·cos(β)
y=ry·sin(α)·sin(β)
z=rz·cos(α)
在这个基础上进行一些修改就可以实现椭圆的绘制了!
代码实现如下:
- /*****************************************************************************
- Copyright: 2012, ustc All rights reserved.
- contact:k283228391@126.com
- File name: main.c
- Description:using opengl in SDL.
- Author:Silang Quan
- Version: 1.0
- Date: 2012.12.01
- *****************************************************************************/
- #include <SDL/SDL.h>
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define pi 3.1415926
- SDL_Surface *screen;
- typedef struct Point3f
- {
- GLfloat x;
- GLfloat y;
- GLfloat z;
- }point;
- int getPoint2(GLfloat rx,GLfloat ry,GLfloat rz,GLfloat a,GLfloat b,point &p)
- {
- p.x=rx*sin(a*pi/180.0)*cos(b*pi/180.0);
- p.y=ry*sin(a*pi/180.0)*sin(b*pi/180.0);
- p.z=rz*cos(a*pi/180.0);
- return 1;
- }
- point* getPointMatrix2(GLfloat rx,GLfloat ry,GLfloat rz,GLint slices)
- {
- int i,j,w=2*slices,h=slices;
- float a=0.0,b=0.0;
- float hStep=180.0/(h-1);
- float wStep=360.0/w;
- int length=w*h;
- point *matrix;
- matrix=(point *)malloc(length*sizeof(point));
- if(!matrix)return NULL;
- for(a=0.0,i=0;i<h;i++,a+=hStep)
- for(b=0.0,j=0;j<w;j++,b+=wStep)
- getPoint2(rx,ry,rz,a,b,matrix[i*w+j]);
- return matrix;
- }
- void drawSlice(point &p1,point &p2,point &p3,point &p4)
- {
- glBegin(GL_LINE_LOOP);
- glColor3f(0,1,0);
- glVertex3f(p1.x,p1.y,p1.z);
- glVertex3f(p2.x,p2.y,p2.z);
- glVertex3f(p3.x,p3.y,p3.z);
- glVertex3f(p4.x,p4.y,p4.z);
- glEnd();
- }
- int drawOval(GLfloat rx,GLfloat ry,GLfloat rz,GLint slices)
- {
- int i=0,j=0,w=2*slices,h=slices;
- point *mx;
- mx=getPointMatrix2(rx,ry,rz,slices);
- if(!mx)return 0;
- for(;i<h-1;i++)
- {
- for(j=0;j<w-1;j++)
- {
- drawSlice(mx[i*w+j],mx[i*w+j+1],mx[(i+1)*w+j+1],mx[(i+1)*w+j]);
- }
- drawSlice(mx[i*w+j],mx[i*w],mx[(i+1)*w],mx[(i+1)*w+j]);
- }
- free(mx);
- return 1;
- }
- void quit( int code )
- {
- SDL_Quit( );
- /* Exit program. */
- exit( code );
- }
- void handleKeyEvent( SDL_keysym* keysym )
- {
- switch( keysym->sym )
- {
- case SDLK_ESCAPE:
- quit( 0 );
- break;
- case SDLK_SPACE:
- break;
- default:
- break;
- }
- }
- void resizeGL(int width,int height)
- {
- if ( height == 0 )
- {
- height = 1;
- }
- //Reset View
- glViewport( 0, 0, (GLint)width, (GLint)height );
- //Choose the Matrix mode
- glMatrixMode( GL_PROJECTION );
- //reset projection
- glLoadIdentity();
- //set perspection
- gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0 );
- //choose Matrix mode
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- }
- void handleEvents()
- {
- // Our SDL event placeholder.
- SDL_Event event;
- //Grab all the events off the queue.
- while( SDL_PollEvent( &event ) ) {
- switch( event.type ) {
- case SDL_KEYDOWN:
- // Handle key Event
- handleKeyEvent( &event.key.keysym );
- break;
- case SDL_QUIT:
- // Handle quit requests (like Ctrl-c).
- quit( 0 );
- break;
- case SDL_VIDEORESIZE:
- //Handle resize event
- screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 16,
- SDL_OPENGL|SDL_RESIZABLE);
- if ( screen )
- {
- resizeGL(screen->w, screen->h);
- }
- break;
- }
- }
- }
- void initSDL(int width,int height,int bpp,int flags)
- {
- // First, initialize SDL's video subsystem.
- if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
- {
- fprintf( stderr, "Video initialization failed: %s\n",
- SDL_GetError( ) );
- quit( 1 );
- }
- atexit(SDL_Quit);
- //Set some Attribute of OpenGL in SDL
- SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
- SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
- SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
- SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
- SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
- //Set the video mode
- screen= SDL_SetVideoMode( width, height, bpp,flags);
- if(!screen )
- {
- fprintf( stderr, "Video mode set failed: %s\n",SDL_GetError( ) );
- quit( 1 );
- }
- resizeGL(screen->w, screen->h);
- //Set caption
- SDL_WM_SetCaption( "OpenGL Test", NULL );
- }
- void renderGL()
- {
- // Clear the color and depth buffers.
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- // We don't want to modify the projection matrix. */
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity( );
- // Move down the z-axis.
- glTranslatef( 0.0, 0.0, -25.0 );
- glRotatef(50.0f,0,1,1);
- //Draw a square
- drawSphere(10,20);
- //drawOval(5,8,15,20);
- SDL_GL_SwapBuffers( );
- }
- void initGL( int width, int height )
- {
- float ratio = (float) width / (float) height;
- // Our shading model--Gouraud (smooth).
- glShadeModel( GL_SMOOTH );
- // Set the clear color.
- glClearColor( 0, 0, 0, 0 );
- // Setup our viewport.
- glViewport( 0, 0, width, height );
- //Change to the projection matrix and set our viewing volume.
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 60.0, ratio, 1.0, 100.0 );
- }
- int main( int argc, char* argv[] )
- {
- // Dimensions of our window.
- int width = 640;
- int height = 480;
- // Color depth in bits of our window.
- int bpp = 32;
- int flags= SDL_OPENGL|SDL_RESIZABLE;
- //Set the SDL
- initSDL(width, height, bpp,flags);
- //Set the OpenGL
- initGL( width, height );
- //main loop
- while(true)
- {
- /* Process incoming events. */
- handleEvents( );
- /* Draw the screen. */
- renderGL( );
- }
- return 0;
- }
用参数方程绘制椭球体相关推荐
- OpenGL进阶(四)-用参数方程绘制椭球体
首先参考这篇文章绘制一个球体:OpenGL 用参数方程绘制球 我们知道球体的参数方程是这样的: x=r·sin(α)·cos(β) y=r·sin(α)·sin(β) z=r·cos(α) 椭圆的参数 ...
- Matlab 利用参数方程绘制空心球体
基本原理: 实质为利用球面参数方程,利用网格化数据绘制 x=R*sin(theta)*cos(phi) y=R*sin(theta)*sin(phi) z=R*cos(theta) 绘制函数: fun ...
- matlab绘制 椭球体,如何用绘图法绘制椭球体
好吧,比我想象的要容易.有一个alphahull选项,它要求自动计算相应的三角剖分.在from plotly.offline import iplot, init_notebook_mode from ...
- 好用的机电revit软件丨revit中怎么画球体,半球体,椭球体?
好用的机电revit软件丨revit中怎么画球体,半球体,椭球体? revit中怎么画球体,半球体,椭球体?如图-1所示,如何通过内建模型创建呢? 单击[内建模型]命令→[族类型和参数]→选择[常规模 ...
- mysql 大地坐标系_1980西安坐标系采用的椭球体为: ( )_学小易找答案
[填空题]酸性缓冲溶液是由_______________组成. [判断题]用字符指针变量指向一个字符串常量,通过字符指针变量引用字符串常量. [单选题]您对我们这个品牌熟悉吗? [单选题]图代码运行后 ...
- arcpy投影(三)——定义投影、地理变换关系自定义和投影变换Project_managemen(含基准面/椭球体转换参数使用方法,arcpro/arcmap)
arcpy投影这一个专题从文件位置.文件含义.空间参照获取.转换关系查询.投影定义.自定义转换关系.投影变换这几个角度上系统的进行了介绍,整理出了: arcpy投影(一)--prj.gtf文件定义.路 ...
- GIS的基本概念二:大地水准面、旋转椭球体(椭球体)、大地基准面
上一章粗略整理了一下坐标系的概念,基本理解如何用坐标来表示地理空间.面对现实的地球,还是有一个疑问.众所周知,我们的地球表面是一个凹凸不平的表面,对于地球测量而言,地表是一个无法用数学公式进行表达的曲 ...
- 计算机图形学 | 实验四:绘制一个球体
计算机图形学 | 实验四:绘制一个球体 计算机图形学 | 实验四:绘制一个球体 封装Shader 为什么要封装Shader 如何使用 绘制球模型 球面顶点遍历 构造三角形图元 开启线框模式 开启面剔除 ...
- Proj.NET-地球椭球体、大地基准面及地图投影
地球椭球体(Ellipsoid) 众所周知我们的地球表面是一个凸凹不平的表面,而对于地球测量而言,地表是一个无法用数学公式表达的曲面,这样的曲面不能作为测量和制图的基准面.假想一个扁率极小的椭圆,绕大 ...
- matlab绘制三维球体,使用Matlab绘制三维圆柱体和球体
使用Cylinder功能函数绘制圆柱体侧面 在matlab中自带了绘制圆柱体的功能函数cylinder,其用法如下: 例1,绘制一个圆柱体的三维图像,要求圆柱体底面圆心在坐标原点,底面半径为3,高度为 ...
最新文章
- 干货 | 100+个NLP数据集大放送,再不愁数据!
- MFC中的MainFrame Dlg,App,Doc,View的关系
- 深入理解Objective-C:方法缓存
- 【转】React 16 中从 setState 返回 null 的妙用
- zookeeper安装包下载地址
- OpenCV学习笔记(四):XML,YAML(.txt,.doc)文件读写操作
- css 相同的css属性_CSS中的order属性
- java多个mapreduce_java – 在hadoop中运行多个MapReduce作业
- 内存中的栈空间与堆空间
- ARP协议,ARP诈骗图
- activiti流程消息事件触发messageEventReceived,MessageCatchingEvent 及消息启动流程
- 课堂对Complex类运算符重载的小练习
- java打印出日历_java控制台打印本月的日历
- tensorflow pb ckpt pbtxt
- 尔雅网络选课 大学计算机知识,2017-2018学年第1学期超星尔雅网络课程选课指南.PDF...
- 【基础】创建react脚手架
- Java知识点_类锁和对象锁的区别?
- c语言如何文件指针指向开头,fseek设置好文件指针 在C语言中fseek()的功能
- 《爬虫》爬取谷歌网页“人脸”图片
- 包装类-Wrapper
热门文章
- Alink漫谈(十一) :线性回归 之 L-BFGS优化
- ubuntu命令安装中文语言包_Ubuntu安装中文语言包
- 康托尔定理是怎样证明的?
- 【【运维】9个网络故障排除经典案例,你都得会吗?】
- w10系统服务器启动失败,三种方法教你解决Win10系统Apache启动失败问题
- 64位平台C/C++开发注意事项
- html页面出现504,web生产环境故障页面提示504错误
- 2021.11.20【读书笔记】|差异可变剪接事件及DTU分析
- 计算机二级答题技巧口诀,计算机二级考试复习技巧:考场答题经验分享
- 各种带有物理学特点的把妹法[转]