---- 引言----
每次使用OpenGL或DirectX写三维程序的时候, 都有一种隔靴搔痒的感觉, 对于内部的三维算法的实现不甚了解. 其实想想, Win32中既然存在画线画点函数, 利用计算机图形学的知识, 我们用可以用纯C调用Win32实现三维绘图, 完全不用借助OpenGL和DirectX, 这有重复造轮子的嫌疑, 但是自己动手实现一遍, 毕竟也是有意义的.
[效果演示]
原始效果(100条浮动曲线)
加密以后的效果(200条浮动曲线)
[浮动水平线法绘图过程]
固定一个y值按步长变换给定一个x值, 从而可计算出平面截线一个点的z坐标值. 将改点投影到xoy平面上, 然后再变换到屏幕上. 如果是曲线端点要填充边界值. 接着检验此点的可见性,并用1表示上方可见, 0表示不可见, -1表示下方可见. 可见性检测就是用当前点的y值与上下浮动水平线数组中相应的元素值进行比较,y值大于上水平线数组中元素值或小于下水平线数组中元素值, 则当前点可见, 否则不可见. 往下再计算同一平面截线的另一点, 和上面点一样, 先投影到坐标平面上, 再变换到屏幕上. 先前的点叫紧前点, 当前的点为当前点. 紧前点和当前点的可见性主要有下面一些可能情形:
[编程实现要点]
曲面函数的定义
float SurfaceFun(float X, float Y)
{
float w1, w2, w3, FV;
w1=4*(X-2)*(X-2) + (Y-4)*(Y-4) - 1;
w2=(X-5)*(X-5)/9 + 4*(Y-2)*(Y-2) - 1;
w3=(X-5)*(X-5)/9 + 4*(Y-6)*(Y-6) - 1;
if(w1>85) w1=85;
if(w2>85) w2=85;
if(w3>85) w3=85;
FV=w1*w1*exp(-w1) + w2*w2*exp(-w2) + w3*w3*exp(-w3);
return(FV);
}
绘制曲面函数
void DrawSurface()
{
int Xe, Ye, Ln, Pt, XPre, YPre, XCur, YCur, Xi, Yi;
int *pi, LimY, VisCur, VisPre;
float X, Y, Z;
LimY=GetWindowHeight();
SetLineColor(BLUE);
for(Ln=0; Ln<=LNo; ++Ln)
{
Y=Y2-Ln*IncY;
X=X1;
Z=SurfaceFun(X,Y);
CalcuProject(X, Y, Z);
XPre = 0.5 + (XProj-F1)*EchX + C1;
YPre = 0.5 + (YProj-F3)*EchY + C3;
FillEdge(XPre, YPre, Xd, Yd);
VisPre = VisibilityTest(XPre, YPre);
for(Pt=0; Pt<=PNo; ++Pt)
{
X=X1+Pt*IncX;
Z=SurfaceFun(X,Y);
CalcuProject(X, Y, Z);
XCur = 0.5 + (XProj-F1)*EchX + C1;
YCur = 0.5 + (YProj-F3)*EchY + C3;
VisCur = VisibilityTest(XCur, YCur);
if( (HMax[XCur]==0) || (HMin[XCur]==LimY) ) VisCur = VisPre;
if(VisCur == VisPre)
{
if( (VisCur==1) || (VisCur==-1) )
{
if(0<=XCur)
PlotLine(XPre, LimY-60-YPre, Xi, LimY-60-YCur);
else if(0<=YCur)
PlotLine(XPre, LimY-60-YPre, XPre, LimY-60-YCur);
else
PlotLine(Xi, LimY-60-YPre, XPre, LimY-60-YPre);
HorizonInc(XPre, YPre, XCur, YCur);
}
}
else // VisCur!=VisPre
{
if(VisCur==0)
{
if(VisPre == 1)
{
pi = Inter(XPre, YPre, XCur, YCur, HMax);
Xi = *pi;
Yi = *(pi+1);
}
else
{
pi = Inter(XPre, YPre, XCur, YCur, HMin);
Xi = *pi;
Yi = *(pi+1);
}
if(0<=Xi)
PlotLine(XPre, LimY-60-YPre, Xi, LimY-60-Yi);
else if(0<=Yi)
PlotLine(XPre, LimY-60-Yi, XPre, LimY-60-Yi);
else
PlotLine(XPre, LimY-60-YPre, XPre, LimY-60-YPre);
HorizonInc(XPre, YPre, Xi, Yi);
}
else
{
if(VisCur == 1)
{
if(VisPre == 0)
{
pi = Inter(XPre, YPre, XCur, YCur, HMax);
Xi = *pi;
Yi = *(pi+1);
if(0<=Xi)
PlotLine(Xi, LimY-60-Yi, XCur, LimY-60-YCur);
else if(0<=Yi)
PlotLine(XCur, LimY-60-YCur, XCur, LimY-60-YCur);
else
PlotLine(XCur, LimY-60-YCur, XCur, LimY-60-YCur);
HorizonInc(Xi, Yi, XCur, YCur);
}
else
{
pi = Inter(XPre, YPre, XCur, YCur, HMin);
Xi = *pi;
Yi = *(pi+1);
if(0<=Xi)
PlotLine(XPre, LimY-60-YPre, Xi, LimY-60-Yi);
else if(0<=Yi)
PlotLine(XPre, LimY-60-YPre, XPre, LimY-60-Yi);
else
PlotLine(XPre, LimY-60-YPre, XPre, LimY-60-YPre);
HorizonInc(XPre, YPre, Xi, Yi);
pi = Inter(XPre, YPre, XCur, YCur, HMax);
Xi = *pi;
Yi = *(pi+1);
if(0<=Xi)
PlotLine(Xi, LimY-60-YCur, XCur, LimY-60-YCur);
else if(0<=Yi)
PlotLine(XCur, LimY-60-Yi, XCur, LimY-60-YCur);
else
PlotLine(XCur, LimY-60-YCur, XCur, LimY-60-YCur);
HorizonInc(Xi, Yi, XCur, YCur);
}
}
else // VisCur!=0, VisCur!=1
{
if(VisPre == 0)
{
pi = Inter(XPre, YPre, XCur, YCur, HMin);
Xi = *pi;
Yi = *(pi+1);
if(0<=Xi)
PlotLine(Xi, LimY-60-YCur, XCur, LimY-60-YCur);
else if(0<=Yi)
PlotLine(XCur, LimY-60-Yi, XCur, LimY-60-YCur);
else
PlotLine(XCur, LimY-60-YCur, XCur, LimY-60-YCur);
HorizonInc(Xi, Yi, XCur, YCur);
}
else // VisCur!=0, VisCur!=1, VisPre!=0
{
pi = Inter(XPre, YPre, XCur, YCur, HMax);
Xi = *pi;
Yi = *(pi+1);
if(0<=Xi)
PlotLine(XPre, LimY-60-YPre, Xi, LimY-60-Yi);
else if(0<=Yi)
PlotLine(XPre, LimY-60-YPre, XPre, LimY-60-Yi);
else
PlotLine(XPre, LimY-60-YPre, XPre, LimY-60-YPre);
HorizonInc(XPre, YPre, Xi, Yi);
pi = Inter(XPre, YPre, XCur, YCur, HMin);
Xi = *pi;
Yi = *(pi+1);
if(0<=Xi)
PlotLine(Xi, LimY-60-Yi, XCur, LimY-60-YCur);
else
PlotLine(XCur, LimY-60-YCur, XCur, LimY-60-YCur);
HorizonInc(Xi, Yi, XCur, YCur);
}
}
}
}
VisPre = VisCur;
XPre = XCur;
YPre = YCur;
}
FillEdge(XCur, YCur, Xg, Yg);
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。