資源簡(jiǎn)介
用visual studio環(huán)境可以運(yùn)行,實(shí)現(xiàn)Z_buffer掃描線(xiàn)消隱算法。源碼中有主要的步驟注釋說(shuō)明。用到了OpenGL的函數(shù)、庫(kù)文件、頭文件和glut庫(kù)

代碼片段和文件信息
//?Auth:?zousong@cad.zju.edu.cn
//?Date:?2008-12-25
//?定義是否每個(gè)面片使用隨機(jī)顏色值
#define?RANDOM_COLOR?1
#include?
#include?
#include?
#include?
#include?
#include?
#ifdef?RANDOM_COLOR
#include?
#include?
#endif
using?namespace?std;
#define?MAX_WIDTH?(1024*2)
#define?MAX_HEIGHT?(768*2)
//定義圖像
#define INIT_WIDTH?1024
#define INIT_HEIGHT?768
GLubyte?image[INIT_HEIGHT][INIT_WIDTH][3];
//?定義顏色結(jié)構(gòu)體
typedef?struct??
{
unsigned?char?r?b?g?a;
}?color_t;
//?定義點(diǎn)結(jié)構(gòu)
typedef?struct??
{
double?x?y?z;
}?point_t;
//?定義分類(lèi)邊表
typedef?struct?_line_t?
{
int?x; //?邊的上端點(diǎn)的x坐標(biāo)
double?dx; //?相鄰兩條掃描線(xiàn)交點(diǎn)的x坐標(biāo)差dx(-1/k)
int?dy; //?邊跨越的掃描線(xiàn)數(shù)目
int?id; //?邊所屬多邊形的編號(hào)
//_line_t?*?next; //?下個(gè)分類(lèi)邊表指針?由于本算法中分類(lèi)邊表存于STL?Vector中?故不需要
}?line_t;
//?定義活化邊表
typedef?struct?_active_line_t?
{
double?xl; //?左交點(diǎn)的x坐標(biāo)
double?dxl; //?(左交點(diǎn)邊上)兩相鄰掃描線(xiàn)交點(diǎn)的x坐標(biāo)之差
int?dyl; //?以和左交點(diǎn)所在邊相交的掃描線(xiàn)數(shù)為初值?以后向下每處理一條掃描線(xiàn)減1
double?zl; //?左交點(diǎn)處多邊形所在平面的深度值
double?dzx; //?沿掃描線(xiàn)向右走過(guò)一個(gè)像素時(shí)?多邊形所在平面的深度增量.?多于平面方程?dzx?=?-a/c?(c!=?0)
double?dzy; //?沿y方向向下移過(guò)一根掃描線(xiàn)時(shí)?多邊形所在平面的深度增量.?對(duì)于平面方程?dzy?=?b/c?(c!=?0)
#if?0??//?由于邊對(duì)分拆在兩個(gè)活化邊表結(jié)構(gòu)中,故右節(jié)點(diǎn)的元素可以省略
// double?xr; //?右交點(diǎn)的x坐標(biāo)
// double?dxr; //?(右交點(diǎn)邊上)兩相鄰掃描線(xiàn)交點(diǎn)的x坐標(biāo)之差
// int?dyr; //?以右交點(diǎn)所在邊相交的掃描線(xiàn)數(shù)為初值?以后向下每處理一條掃描線(xiàn)減1
#endif
// int id; //?交點(diǎn)所在多邊形的編號(hào)?由于將活化邊表存于各自所屬的活化多邊形中?故所屬I(mǎi)D不需要保存
//_active_line_t?*?next; //?下個(gè)活化邊表指針?由于本算法中活化邊表存于STL?Vector中?故不需要
}?active_line_t;
//?定義分類(lèi)多邊形表?||??活化多邊形表
typedef?struct??_poly_t
{
int?id; //?多邊形編號(hào)
double?abcd; //?多邊形所在的平面的方程系數(shù)?ax?+?by?+?cz?+?d?=?0
int?dy; //?多邊形跨越的掃描線(xiàn)數(shù)目?||?多邊形跨越的剩余掃描線(xiàn)數(shù)目
color_t?color; //?多邊形顏色
vector??ale;??//?活化邊表?放在活化多邊形中?對(duì)于分類(lèi)多邊形表無(wú)意義
//_poly_t?*?next; //?下個(gè)分類(lèi)多邊形指針?||?下個(gè)活化多邊形指針?由于本算法中多邊形表存于STL?Vector中?故不需要
}?poly_t;
inline?bool?is_zero(double?f)
{
if?(abs(f)?1e-8)
return?true;
return?false;
}
inline?int?double_to_int(double?f)
{
return?(int)(f+0.5);
}
//?對(duì)活化多邊形表排序的比較函數(shù)?
bool?cmp(const?active_line_t?&lhs?const?active_line_t?&rhs)
{
if?(double_to_int(lhs.xl)? return?true;
}?else?if?(?double_to_int(lhs.xl)?==?double_to_int(rhs.xl)??){
if?(double_to_int(lhs.dxl)? return?true;
}
return?false;
}
//?從obj文件中讀取的點(diǎn)
vector? point;
//?分類(lèi)多形型表
vector? poly[MAX_HEIGHT];
//?分類(lèi)邊表
vector? line[MAX_HEIGHT];
//?活化多邊形表?內(nèi)部包含各個(gè)活化多邊形表中的活化邊表對(duì)
vector??ape;
//?z值數(shù)組
double z[MAX_WIDTH];
//?顏色數(shù)組
color_t color[MAX_WIDTH];
//?處理字符串用的buffer
char buf[1024];
int?cal(const?char?*?file_name?int?max_width?int?max_height?double?scale)
{
//?判斷參數(shù)是否合法
if?(file_name?==?NULL?||?max_width?<=?0?||?max_width?>?MAX_WIDTH?||?max_height?<=?0?||?max_height?>?MAX_HEIGHT?||?scale?1.0)?{
printf(“cal?err!\n“);
return?-1;
}
#ifdef?
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????文件??????16535??2007-12-30?10:48??bin\cone.obj
?????文件????????839??2008-01-04?01:34??bin\cube.obj
?????文件??????27670??2008-09-16?14:54??bin\glut.h
?????文件?????237568??2008-09-16?14:54??bin\glut32.dll
?????文件??????28728??2008-09-16?14:54??bin\glut32.lib
?????文件????????188??2008-12-31?19:46??bin\run.bat
?????文件?????122880??2008-12-31?19:15??bin\SL_Z_buffer.exe
?????文件??????57515??2007-12-30?10:48??bin\sphere.obj
?????文件??????61417??2007-12-30?10:48??bin\teapot.obj
?????文件????????286??2008-01-01?20:42??bin\test.obj
?????文件??????33973??2007-12-30?10:48??bin\torus.obj
?????文件?????179844??2007-12-30?10:48??bin\torusknot.obj
?????文件????3080597??2008-01-03?23:58??bin\venusm.obj
?????文件?????122880??2008-12-31?19:15??SL_Z_buffer\debug\SL_Z_buffer.exe
?????文件??????16535??2007-12-30?10:48??SL_Z_buffer\SL_Z_buffer\cone.obj
?????文件????????839??2008-01-04?01:34??SL_Z_buffer\SL_Z_buffer\cube.obj
?????文件???????9346??2010-11-06?10:56??SL_Z_buffer\SL_Z_buffer\Debug\BuildLog.htm
?????文件??????????2??2011-01-14?11:18??SL_Z_buffer\SL_Z_buffer\Debug\cl.command.1.tlog
?????文件??????????2??2011-01-14?11:18??SL_Z_buffer\SL_Z_buffer\Debug\CL.read.1.tlog
?????文件??????????2??2011-01-14?11:18??SL_Z_buffer\SL_Z_buffer\Debug\CL.write.1.tlog
?????文件?????????65??2008-12-31?19:15??SL_Z_buffer\SL_Z_buffer\Debug\mt.dep
?????文件????????145??2008-12-31?19:15??SL_Z_buffer\SL_Z_buffer\Debug\SL_Z_buffer.exe.intermediate.manifest
?????文件????????114??2011-01-14?11:18??SL_Z_buffer\SL_Z_buffer\Debug\SL_Z_buffer.lastbuildstate
?????文件????????994??2011-01-14?11:18??SL_Z_buffer\SL_Z_buffer\Debug\SL_Z_buffer.log
?????文件??????????0??2011-01-14?11:18??SL_Z_buffer\SL_Z_buffer\Debug\SL_Z_buffer.unsuccessfulbuild
?????文件??????86016??2011-01-14?11:18??SL_Z_buffer\SL_Z_buffer\Debug\vc100.pdb
?????文件?????117760??2010-11-06?10:56??SL_Z_buffer\SL_Z_buffer\Debug\vc80.idb
?????文件?????179301??2008-12-31?19:15??SL_Z_buffer\SL_Z_buffer\Debug\z_buffer.obj
?????文件??????27670??2008-09-16?14:54??SL_Z_buffer\SL_Z_buffer\glut.h
?????文件?????237568??2008-09-16?14:54??SL_Z_buffer\SL_Z_buffer\glut32.dll
............此處省略35個(gè)文件信息
評(píng)論
共有 條評(píng)論