|
|
发表于 2007-1-11 02:45:00
|
显示全部楼层
Re:做个调查问卷
我只知道有个红皮书。
白皮和黄皮的给个超连接?
我正想从0做个软件的渲染的东西。所以想看看。白皮的。
恩。其实很简单啊,有些东西只要做起来就简单了。我实验了下,用生成一个环,然后自己矩阵转换,SETUP Z.然后自己RASTER.几百行就搞定了。不过raster还是黑的。考虑加上材质。矩阵变化只有project的矩阵。自己实现这些,这个是我的一个心愿。
void calc(float *vt1,float *vt2,int *l,int *r,int left,int buttom,int top,int right,int &maxy1,int &maxy2)
{
float *l1,*l2;
if( vt2[1] > vt1[1] )
{
l1=vt1;l2=vt2;
} else {
l1=vt2;l2=vt1;
}
int y1= l1[1];
int y2 = l2[1];
if(y1< buttom ) y1=buttom;
if(y2> top ) y2=top;
if( y1< maxy1) maxy1=y1;
if( y2 > maxy2) maxy2=y2;
float dx = l2[0]-l1[0];
float dy= l2[1]-l1[1];
float slope = dx/dy;
float cx=l1[0]+slope*(l1[1]-y1);
//if( cx < left ) cx = left;
for(int i=y1;i<y2;++i)
{
if(cx<l )
{
if(cx< left) l= left;
else
l= cx;
}
if( cx>r )
{
if( cx > right )
r=right;
else
r=cx;
}
cx+=slope;
}
}
void calp(float *vts,unsigned short *id,int num,int left,int buttom,int top,int right,CPaintDC *dc)
{
int hi= top-buttom;
int *lv= new int[hi];
int *lr= new int[hi];
int maxy1,maxy2;
maxy1=top;
maxy2=buttom;
for(unsigned i=0;i<hi;++i)
{
lv=right;
lr=left;
}
for(unsigned i=0;i<num-1;++i)
{
calc(&vts[ id*4],&vts[id[i+1]*4],&lv[hi/2],&lr[hi/2],left,buttom,top,right,maxy1,maxy2);
}
calc(&vts[id[num-1]*4],&vts[id[0]*4],&lv[hi/2],&lr[hi/2],left,buttom,top,right,maxy1,maxy2);
for(int i=maxy1;i<maxy2;++i)
{
for(int j= lv[i+hi/2];j<=lr[i+hi/2];++j)
{
dc->SetPixel(j+300,i+300,0);
}
}
delete [] lv;
delete [] lr;
}
void calquad(float *vts,unsigned short *id,int num,int left,int buttom,int top,int right,CPaintDC *dc)
{
for(unsigned i=0;i<num;++i)
{
calp(vts,&id[i*4],4,left,buttom,top,right,dc);
}
}
float dot( float *f1,float *f2)
{
float sum =0.0;
for(unsigned i=0;i<4;++i)
{
sum+=f1*f2;
}
return sum;
}
float * mul(float *f1,float *f2,float *mat)
{
for(unsigned i=0;i<4;++i)
{
f2=dot(f1,&mat[i*4]);
}
return f2;
}
#include<math.h>
void lookat(float near_plane,float far_plane,float fov_horiz,float fov_vert,float *mat)
{
float h, w, Q;
w = (float)1/tan(fov_horiz*0.5);
h = (float)1/tan(fov_vert*0.5);
Q = far_plane/(far_plane - near_plane);
memset(mat,0,16*sizeof(float));
mat[0] = w;
mat[4+1] = h;
mat[8+2] = Q;
mat[8+3] = -Q*near_plane;
mat[12+2] = 1;
}
void gen(float *ck,float r,float c,int s1,int s2,unsigned short *ids)
{
float a=0;
for(unsigned i=0;i<s1;++i)
{
float af1=2*3.14/s1*i;
for(unsigned j=0;j<s2;++j)
{
float af2=2*3.14/s2*j;
float nu= r+c*cos(af1);
ck[(i*s2+j)*4]=nu*sin(af2);
ck[(i*s2+j)*4+1]=nu*cos(af2);
ck[(i*s2+j)*4+2]=c*sin(af1)+50;
ck[(i*s2+j)*4+3]=0;
}
}
for(unsigned i=0;i<s1;++i)
{
for(unsigned j=0;j<s2;++j)
{
ids[(i*s2+j)*4]=i*s2+j;
ids[(i*s2+j)*4+1]=((i+1)%s1)*s2+j;
ids[(i*s2+j)*4+3]=i*s2+((j+1)%s2);
ids[(i*s2+j)*4+2]=((i+1)%s1)*s2+((j+1)%s2);
}
}
}
void appproj(float *verts,float *out,float *mat,int num)
{
for(unsigned i=0;i<num;++i)
{
mul(&verts[i*4],&out[i*4],mat);
char buf[1024];
sprintf(buf,"%f %f %f %f",out[i*4],out[i*4+1],verts[i*4+2],verts[i*4+3]);
int a=0;
}
}
void setupz(float *verts,float *out,int num)
{
for(unsigned i=0;i<num;++i)
{
for(unsigned j=0;j<3;++j)
{
out[i*4+j] = verts[i*4+j]/verts[i*4+3];
}
char buf[1024];
sprintf(buf,"%f %f %f %f",verts[i*4],verts[i*4+1],verts[i*4+2],verts[i*4+3]);
int a=0;
}
}
void viewp(float *verts,float *out,int num,int w,int h)
{
for(unsigned i=0;i<num;++i)
{
out[i*4]*=w;
out[i*4+1]*=h;
}
}
void CChildView::OnPaint()
{
CPaintDC dc(this); // 用于绘制的设备上下文
float vts[10*10*4] ;
float vout[400];
unsigned short id[400];
gen(vts,30,5,5,20,id);
//unsigned short id[]={0,1,2};
float mat[16];
lookat(10,10000,3.14/2,3.14/2,mat);
appproj(vts,vout,mat,100);
setupz(vout,vout,100);
viewp(vout,vout,100,300,300);
//calquad(vout,
calquad(vout,id,100,-300,-300,300,300,&dc);
// TODO: 在此处添加消息处理程序代码
// 不要为绘制消息而调用 CWnd::OnPaint()
} |
|