OpenGL三维球体体数据生成与绘制

OpenGL三维球体体数据生成与绘制include iostream include fstream include vector include math h include windows h include gl gl h include gl glu h include lt gl gl windows h math h vector fstream iostream

大家好,我是讯享网,很高兴认识大家。
#include<iostream> #include<fstream> #include<vector> #include<math.h> #include <windows.h> #include <gl/gl.h> #include <gl/glu.h> #include <gl/glut.h> #include <gl/glaux.h> #include <math.h> using namespace std; #define STEP 0.02 //采样间隔 //体素结构体 typedef struct Voxel { float x; float y; float z; int value; }Voxel; vector<Voxel> VolumeData;//体素数组 GLfloat h;//视点z轴坐标 //求两点间距离 float Distance(float a[3], float b[3]) { return sqrt( (a[0]-b[0]) * (a[0]-b[0]) + (a[1]-b[1]) * (a[1]-b[1]) + (a[2]-b[2]) * (a[2]-b[2]) ); } //生成球的体数据并保存到文件,参数是半径和球心 void GenerateVolumeData(float Radi, float Cent[3]) { float Radius = Radi;//半径 float Center[3];//球心 float BoundBoxStart[3];//包围盒起点坐标,左下角顶点 float BoundBoxEnd[3];//包围盒终点坐标,右上角顶点 //初始化 for(int m=0; m<3; m++) { Center[m] = Cent[m];//球心 BoundBoxStart[m] = Center[m] - Radius;//包围盒起点 BoundBoxEnd[m] = Center[m] + Radius + 0.02;//包围盒终点 } ofstream fout_VolumeData; fout_VolumeData.open("VolumeData.txt");//保存体数据的文件 //遍历包围盒内的点 for(float i=BoundBoxStart[0]; i<=BoundBoxEnd[0]; i+=STEP) { for(float j=BoundBoxStart[1]; j<=BoundBoxEnd[1]; j+=STEP) { for(float k=BoundBoxStart[2]; k<=BoundBoxEnd[2]; k+=STEP*2) { float point[3]={i,j,k}; float dist = Distance(point,Center);//点point和球心之间的距离 fout_VolumeData.setf(ios::fixed);//不以科学技术法显示 fout_VolumeData.precision(5);//设置精度 fout_VolumeData <<i<<" "<<j<<" "<<k<<" ";//输出坐标 if( fabs(dist-Radius) < 0.01 )//点point在球上,输出0 fout_VolumeData << 0; else if( dist < Radius )//点point在球内,输出-1 fout_VolumeData << -1; else// if( dist > Radius )//点point在球外,输出1 fout_VolumeData << 1; fout_VolumeData <<endl; } } } fout_VolumeData.close(); } //窗口初始化和大小改变时,调用此函数 void CALLBACK reshape(GLsizei w,GLsizei h) { glMatrixMode(GL_PROJECTION);//设置当前矩阵为投影变换矩阵 glLoadIdentity();//初始化当前矩阵 gluPerspective(20,1,0,3);//设置透视投影矩阵 glMatrixMode(GL_MODELVIEW);//设置当前矩阵为模式变换矩阵 glViewport(0,0,w,h);//设置视区变换 } //显示函数 void CALLBACK display() { glClearColor(0,1,1,1);//设置窗口背景颜色 glClear(GL_COLOR_BUFFER_BIT);//清颜色缓冲区 glLoadIdentity();//重置变换矩阵 gluLookAt(5,5,h,0,0,0,0,1,0);//设置视点坐标 glColor3f(1,0,0);//设置前景色 glBegin(GL_POINTS); //遍历体素数组 vector<Voxel>::iterator iter = VolumeData.begin(); for(; iter != VolumeData.end(); iter++) { if( iter->value==0) glVertex3f(iter->x,iter->y,iter->z); } glEnd(); glFlush();//刷新缓冲区 } //上方向键 void CALLBACK Up() { h+=0.1; } //下方向键 void CALLBACK Down() { h-=0.1; } void main() { float Radius = 1;//半径 float Center[3]={0,0,0};//球心 GenerateVolumeData(Radius,Center);//生成体数据并保存到文件 ifstream fin; fin.open("VolumeData.txt"); if(!fin) cout<<"无法打开体数据文件"<<endl; //将体数据读入体素数组 while(!fin.eof()) { Voxel vox; fin >> vox.x; fin >> vox.y; fin >> vox.z; fin >> vox.value; VolumeData.push_back(vox);//插入数组 } auxInitDisplayMode(AUX_SINGLE | AUX_RGBA);//设置窗口显示模式 auxInitPosition(0,0,500,500);//设置窗口位置 auxInitWindow("三维球体");//窗口标题 glShadeModel(GL_FLAT);//设置着色模式为恒定着色 //上下方向键调整视点位置 auxKeyFunc(AUX_UP,Up); auxKeyFunc(AUX_DOWN,Down); auxReshapeFunc(reshape);//重定形状 auxMainLoop(display);//循环绘制 }

讯享网

效果图:


讯享网

 

 

源码下载:http://download.csdn.net/detail/masikkk/


from: http://blog.csdn.net/masibuaa/article/details/

小讯
上一篇 2025-03-24 08:56
下一篇 2025-03-16 09:32

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/127849.html