使用C语言实现二维,三维绘图算法(2)-解析曲面的显示
---- 引言----
每次使用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=*(X-)*(X-) + (Y-)*(Y-) - ;
w2=(X-)*(X-)/ + *(Y-)*(Y-) - ;
w3=(X-)*(X-)/ + *(Y-)*(Y-) - ; if(w1>) w1=;
if(w2>) w2=;
if(w3>) w3=; 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=; 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=; 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]==) || (HMin[XCur]==LimY) ) VisCur = VisPre; if(VisCur == VisPre)
{
if( (VisCur==) || (VisCur==-) )
{
if(<=XCur)
PlotLine(XPre, LimY--YPre, Xi, LimY--YCur);
else if(<=YCur)
PlotLine(XPre, LimY--YPre, XPre, LimY--YCur);
else
PlotLine(Xi, LimY--YPre, XPre, LimY--YPre); HorizonInc(XPre, YPre, XCur, YCur);
}
}
else // VisCur!=VisPre
{
if(VisCur==)
{
if(VisPre == )
{
pi = Inter(XPre, YPre, XCur, YCur, HMax);
Xi = *pi;
Yi = *(pi+);
}
else
{
pi = Inter(XPre, YPre, XCur, YCur, HMin);
Xi = *pi;
Yi = *(pi+);
} if(<=Xi)
PlotLine(XPre, LimY--YPre, Xi, LimY--Yi);
else if(<=Yi)
PlotLine(XPre, LimY--Yi, XPre, LimY--Yi);
else
PlotLine(XPre, LimY--YPre, XPre, LimY--YPre); HorizonInc(XPre, YPre, Xi, Yi);
}
else
{
if(VisCur == )
{
if(VisPre == )
{
pi = Inter(XPre, YPre, XCur, YCur, HMax);
Xi = *pi;
Yi = *(pi+); if(<=Xi)
PlotLine(Xi, LimY--Yi, XCur, LimY--YCur);
else if(<=Yi)
PlotLine(XCur, LimY--YCur, XCur, LimY--YCur);
else
PlotLine(XCur, LimY--YCur, XCur, LimY--YCur); HorizonInc(Xi, Yi, XCur, YCur);
}
else
{
pi = Inter(XPre, YPre, XCur, YCur, HMin);
Xi = *pi;
Yi = *(pi+); if(<=Xi)
PlotLine(XPre, LimY--YPre, Xi, LimY--Yi);
else if(<=Yi)
PlotLine(XPre, LimY--YPre, XPre, LimY--Yi);
else
PlotLine(XPre, LimY--YPre, XPre, LimY--YPre); HorizonInc(XPre, YPre, Xi, Yi); pi = Inter(XPre, YPre, XCur, YCur, HMax);
Xi = *pi;
Yi = *(pi+); if(<=Xi)
PlotLine(Xi, LimY--YCur, XCur, LimY--YCur);
else if(<=Yi)
PlotLine(XCur, LimY--Yi, XCur, LimY--YCur);
else
PlotLine(XCur, LimY--YCur, XCur, LimY--YCur); HorizonInc(Xi, Yi, XCur, YCur);
}
}
else // VisCur!=0, VisCur!=1
{
if(VisPre == )
{
pi = Inter(XPre, YPre, XCur, YCur, HMin);
Xi = *pi;
Yi = *(pi+); if(<=Xi)
PlotLine(Xi, LimY--YCur, XCur, LimY--YCur);
else if(<=Yi)
PlotLine(XCur, LimY--Yi, XCur, LimY--YCur);
else
PlotLine(XCur, LimY--YCur, XCur, LimY--YCur); HorizonInc(Xi, Yi, XCur, YCur);
}
else // VisCur!=0, VisCur!=1, VisPre!=0
{
pi = Inter(XPre, YPre, XCur, YCur, HMax);
Xi = *pi;
Yi = *(pi+); if(<=Xi)
PlotLine(XPre, LimY--YPre, Xi, LimY--Yi);
else if(<=Yi)
PlotLine(XPre, LimY--YPre, XPre, LimY--Yi);
else
PlotLine(XPre, LimY--YPre, XPre, LimY--YPre); HorizonInc(XPre, YPre, Xi, Yi); pi = Inter(XPre, YPre, XCur, YCur, HMin);
Xi = *pi;
Yi = *(pi+); if(<=Xi)
PlotLine(Xi, LimY--Yi, XCur, LimY--YCur);
else
PlotLine(XCur, LimY--YCur, XCur, LimY--YCur); HorizonInc(Xi, Yi, XCur, YCur);
}
}
}
} VisPre = VisCur;
XPre = XCur;
YPre = YCur;
} FillEdge(XCur, YCur, Xg, Yg);
}
}