📄 sphereview.cpp
字号:
edge->next=q->next;
q->next=edge;
}//insertEdge
//////////////////////////////
int yNext(int k,int cnt,Fpoint *pts){ int j;
if((k+1)>(cnt-1)) j=0;
else j=k+1;
while(pts[k].y==pts[j].y)
if((j+1)>(cnt-1)) j=0;
else j++;
return (pts[j].y);
}//yNext
//////////////////////////////
void makeEdgeRec(Fpoint lower,Fpoint upper,int yComp,Edge *edge,Edge *edges[]){
edge->dxPerScan=(float)(upper.x-lower.x)/(upper.y-lower.y);
edge->xIntersect=float(lower.x);
edge->mR=(float)(upper.c.R-lower.c.R)/(upper.y-lower.y);
edge->mG=(float)(upper.c.G-lower.c.G)/(upper.y-lower.y);
edge->mB=(float)(upper.c.B-lower.c.B)/(upper.y-lower.y);
edge->c.R=lower.c.R;
edge->c.G=lower.c.G;
edge->c.B=lower.c.B;/**/
if(upper.y<yComp) edge->yUpper=upper.y-1;
else edge->yUpper=upper.y;
insertEdge(edges[lower.y],edge);
}//makeEdgeRec
//////////////////////////////
void buildEdgeList(int cnt,Fpoint *pts,Edge *edges[]){
Edge *edge;
Fpoint v1,v2;
int i,yPrev=pts[cnt-2].y;
v1.x=pts[cnt-1].x; v1.y=pts[cnt-1].y;
v1.c.R=pts[cnt-1].c.R; v1.c.G=pts[cnt-1].c.G; v1.c.B=pts[cnt-1].c.B;/**/
for(i=0;i<cnt;i++){
v2.x =pts[i].x; v2.y=pts[i].y;
v2.c.R =pts[i].c.R; v2.c.G =pts[i].c.G; v2.c.B =pts[i].c.B;/**/
if(v1.y!=v2.y){
edge=(Edge *)malloc(sizeof(Edge));
if(v1.y<v2.y)
makeEdgeRec(v1,v2,yNext(i,cnt,pts),edge,edges);
else
makeEdgeRec(v2,v1,yPrev,edge,edges);
}//if
yPrev=v1.y;
v1.x =v2.x; v1.y =v2.y;
v1.c.R =v2.c.R; v1.c.G =v2.c.G; v1.c.B =v2.c.B;/**/
}//for
}//buildEdgeList
//////////////////////////////
void buildActiveList (int scan,Edge *active,Edge *edges[]){
Edge *p,*q;
p=edges[scan]->next;
while(p){
q=p->next;
insertEdge(active,p);
p=q;
}//while
}//buildActiveList
/**/
//////////////////////////////
void CSphereView::fillScan(int scan,Edge *active){
Edge *p1,*p2; int i;
p1=active->next;
while(p1){ p2=p1->next;
pRGB tcolor={p1->c.R,p1->c.G,p1->c.B};
float mR=(float)(p2->c.R-p1->c.R)/(p2->xIntersect-p1->xIntersect );
float mG=(float)(p2->c.G-p1->c.G)/(p2->xIntersect-p1->xIntersect );
float mB=(float)(p2->c.B-p1->c.B)/(p2->xIntersect-p1->xIntersect );
for(i=int(p1->xIntersect);i<p2->xIntersect;i++)
{ tcolor.R=(tcolor.R+mR);
tcolor.G=(tcolor.G+mG);
tcolor.B=(tcolor.B+mB);/**/
if(m_pDC) m_pDC->SetPixel(i,scan,RGB(tcolor.R,tcolor.G,tcolor.B));
}//for
p1=p2->next;
}//while
}//fillScan
//////////////////////////////
void deleteAfter(Edge *q){
Edge *p=q->next;
q->next=p->next;
free(p);
}// deleteAfer
//////////////////////////////
void updateActiveList(int scan,Edge *active){
Edge *q=active,*p=active->next;
while(p)
if(scan>=p->yUpper){
p=p->next;
deleteAfter(q);
}//if
else{
p->xIntersect=p->xIntersect+p->dxPerScan;
p->c.R=(p->c.R+p->mR); p->c.G=(p->c.G+p->mG); p->c.B=(p->c.B+p->mB);/**/
q=p;
p=p->next;
}//else
}//updateActiveList
//////////////////////////////
void resortActiveList (Edge *active){
Edge *q,*p=active->next;
active->next=NULL;
while(p){
q=p->next;
insertEdge(active,p);
p=q;
}//while
}// resortActiveList
void CSphereView::scanFill(int cnt,Fpoint *pts){
Edge *edges[WINDOW_HEIGHT],*active;
int i,scan;
for(i=0;i<WINDOW_HEIGHT;i++)
{edges[i]=(Edge *)malloc(sizeof(Edge));
edges[i]->next=NULL;
}//for
buildEdgeList(cnt,pts,edges);
active=(Edge *)malloc(sizeof(Edge));
active->next=NULL;
for(scan=0;scan<WINDOW_HEIGHT;scan++){
buildActiveList(scan,active,edges);
if(active->next){
fillScan(scan,active);
updateActiveList(scan,active);
resortActiveList(active);
}//if
}//for..
}//scanFill
//__________________________________________________________________
/////////////////////////////////////////////////////////////////////////////
// CSphereView
IMPLEMENT_DYNCREATE(CSphereView, CView)
BEGIN_MESSAGE_MAP(CSphereView, CView)
//{{AFX_MSG_MAP(CSphereView)
ON_COMMAND(ID_BUTTON_WANGGE, OnButtonWangge)
ON_COMMAND(ID_BUTTON_JINGMIAN, OnButtonJingmian)
ON_COMMAND(ID_BUTTON_FANSHE, OnButtonFanshe)
ON_COMMAND(ID_BUTTON_GUANGZHAO, OnButtonGuangzhao)
ON_COMMAND(ID_BUTTON_NS, OnButtonNs)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSphereView construction/destruction
CSphereView::CSphereView()
{
m_ForeColor=RGB(255,0,0);
m_BackColor=RGB(0,0,250);
}
CSphereView::~CSphereView()
{
}
BOOL CSphereView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CSphereView drawing
void CSphereView::OnDraw(CDC* pDC)
{
CSphereDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
m_pDC=new CClientDC(this);
/* Fpoint face[4];
face[0].x=357;face[0].y=119;
face[1].x=340;face[1].y=109;
face[2].x=331;face[2].y=106;
face[3].x=349;face[3].y=116;
face[0].c.R=185.58; face[0].c.G=15.58; face[0].c.B=45.58;
face[1].c.R=185.58; face[1].c.G=15.58; face[1].c.B=45.58;
face[2].c.R=185.58; face[2].c.G=15.58; face[2].c.B=45.58;
face[3].c.R=185.58; face[3].c.G=15.58; face[3].c.B=45.58;
scanFill(4,face);/**/
}
/////////////////////////////////////////////////////////////////////////////
// CSphereView printing
BOOL CSphereView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CSphereView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CSphereView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CSphereView diagnostics
#ifdef _DEBUG
void CSphereView::AssertValid() const
{
CView::AssertValid();
}
void CSphereView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CSphereDoc* CSphereView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSphereDoc)));
return (CSphereDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CSphereView message handlers
void CSphereView::OnButtonWangge()
{ m_bWangge=true;
viewpoint.x=300.0;
viewpoint.y=400.0;
viewpoint.z=500.0;
dengfen=28;
m_ForeColor=RGB(100,230,60);
sphereview();
m_bWangge=false;
}
void CSphereView::OnButtonJingmian()
{
m_bJingmian=true;
viewpoint.x=300.0;
viewpoint.y=400.0;
viewpoint.z=500.0;
dengfen=28;
kd=0.3;
ForeColor.R=150;
ForeColor.G=160;
ForeColor.B=180;
sphereview();
m_bJingmian=false;
}
void CSphereView::OnButtonFanshe()
{
m_bFanshe=true;
viewpoint.x=300;
viewpoint.y=400;
viewpoint.z=500;
dengfen=28;
kd=0.5;
ForeColor.R=30;
ForeColor.G=60;
ForeColor.B=90;
sphereview();
m_bFanshe=false;
}
void CSphereView::OnButtonGuangzhao()
{
m_bFanshe=true;
m_bGuangzhao=true;
viewpoint.x=300;
viewpoint.y=400;
viewpoint.z=500;
lightpoint.x=100;
lightpoint.y=400;
lightpoint.z=00;
dengfen=28;
kd=0.5;
ks=0.5;
ns=10;
ForeColor.R=30;
ForeColor.G=60;
ForeColor.B=90;
sphereview();
m_bGuangzhao=false;
m_bFanshe=false;
}
void CSphereView::OnButtonNs()
{
m_bFanshe=true;
m_bGuangzhao=true;
kd=0.5;
ks=0.5;
ns+=4;
sphereview();
m_bGuangzhao=false;
m_bFanshe=false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -