歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> OpenGL三維球體數據生成與繪制【附源碼】

OpenGL三維球體數據生成與繪制【附源碼】

日期:2017/3/1 9:58:13   编辑:Linux編程

OpenGL三維球體數據生成與繪制源碼:

#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://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2013年資料/4月/22日/OpenGL三維球體數據生成與繪制【附源碼】

Copyright © Linux教程網 All Rights Reserved