今天玩了个小方块,3d效果呦
easyx吧
全部回复
仅看楼主
level 5
a1668659995 楼主
[酷]
400行,喜欢的小伙伴可以自己运行下
2016年03月25日 17点03分 1
level 5
a1668659995 楼主
void floodfill_3d(pt_3d * pt1,COLORREF color){
floodfill(cent_x+pt1->y*sqrt(3.0)/2-pt1->x*sqrt(3.0)/2,cent_y-pt1->z+pt1->y/2+pt1->x/2,color);
}
pt_3d operator+(pt_3d a,pt_3d b){
pt_3d c;
c.x=a.x+b.x;
c.y=a.y+b.y;
c.z=a.z+b.z;
return c;
}
pt_3d operator-(pt_3d a,pt_3d b){
pt_3d c;
c.x=a.x-b.x;
c.y=a.y-b.y;
c.z=a.z-b.z;
return c;
}
pt_3d operator/(pt_3d a,float b){
pt_3d c;
c.x=a.x/b;
c.y=a.y/b;
c.z=a.z/b;
return c;
}
pt_3d operator*(pt_3d a,float b){
pt_3d c;
c.x=a.x*b;
c.y=a.y*b;
c.z=a.z*b;
return c;
}
class cube{
public:
pt_3d **pt_8;
COLORREF face[6];//6面色彩
int index,index2;//不可见点
LINESTYLE *pstyle_line;
cube(pt_3d &pt1,pt_3d & pt2,pt_3d &pt3,pt_3d &pt4){//传输参数为一个面的3点和另一个面1个点
this->pt_8=new pt_3d *[8];
//定义3个向量求
pt_3d a=new pt_3d(0,0,0);
a=pt3-pt1;
pt_3d b=new pt_3d(0,0,0);
b=pt4-pt1;
this->pt_8[0]=new pt_3d(pt1);
this->pt_8[1]=new pt_3d(pt2);
this->pt_8[2]=new pt_3d(pt2);
*(this->pt_8[2])=*(this->pt_8[2])+a;
this->pt_8[3]=new pt_3d(pt3);
for (int i=0;i<4;i++)
{
this->pt_8[i+4]=new pt_3d(this->pt_8[i]);
*(this->pt_8[i+4])=*(this->pt_8[i+4])+b;
}
index=0;
face[0]=WHITE;face[1]=ORANGE;face[2]=BLUE;
face[3]=RED;face[4]=GREEN;face[5]=YELLOW;
this->pstyle_line=new LINESTYLE();
this->pstyle_line->style=PS_SOLID;
this->pstyle_line->thickness=3;
float all=0,tem;
for (int i=0;i<8;i++)
{
tem=pt_8[i]->x+pt_8[i]->y+pt_8[i]->z;
if (tem>all)
{
all=tem;
index2=i;
}
}
index=(index2+4)%8;
index=(index+2)%4+(int)(index/4)*4;
}
void draw_line(int ok){
setlinestyle(pstyle_line);
if (ok)
setlinecolor(LINE_COLOR);
else
setlinecolor(BACK_COLOR);
//setrop2(R2_XORPEN);
for (int i=0;i<4;i++)
{
if(i!=index&&(1+i)%4!=index)
line_3d(pt_8[0+i],pt_8[(1+i)%4]);
if(4+i!=index&&(1+i)%4+4!=index)
line_3d(pt_8[4+i],pt_8[(1+i)%4+4]);
if(i!=index&&4+i!=index)
line_3d(pt_8[0+i],pt_8[4+i]);
}
}
void draw_face(int ok){
int top_index=(index+4)%8;
top_index=(top_index+2)%4+(int)(top_index/4)*4;//求得距离视角最近的点
int p=0;//位记录需要涂的面
pt_3d tmp_pt;
switch(top_index)
{
case 0:
p|=0x13;break;
case 1:
p|=0x07;break;
case 2:
p|=0x0d;break;
case 3:
p|=0x19;break;
case 4:
p|=0x32;break;
case 5:
p|=0x26;break;
case 6:
p|=0x2c;break;
case 7:
p|=0x38;break;
default:return;
break;
}
/* 六个面对角
0--2 1--3
0--5 1--4
1--6 2--5
2--7 3--6
0--7 3--4
4--6 5--7
*/
if ((p&1)==1)
{
tmp_pt=*(this->pt_8[2])+*(this->pt_8[0]);
tmp_pt=tmp_pt/2;
if(ok)
setfillcolor(face[0]);
else
setfillcolor(BACK_COLOR);
floodfill_3d(&tmp_pt,LINE_COLOR);
}
if ((p&2)==2)
{
tmp_pt=*(this->pt_8[5])+*(this->pt_8[0]);
tmp_pt=tmp_pt/2;
if(ok)
setfillcolor(face[1]);
else
setfillcolor(BACK_COLOR);
floodfill_3d(&tmp_pt,LINE_COLOR);
}
if ((p&4)==4)
{
tmp_pt=*(this->pt_8[1])+*(this->pt_8[6]);
tmp_pt=tmp_pt/2;
if(ok)
setfillcolor(face[2]);
else
setfillcolor(BACK_COLOR);
floodfill_3d(&tmp_pt,LINE_COLOR);
}
if ((p&8)==8)
{
tmp_pt=*(this->pt_8[2])+*(this->pt_8[7]);
tmp_pt=tmp_pt/2;
if(ok)
setfillcolor(face[3]);
else
setfillcolor(BACK_COLOR);
floodfill_3d(&tmp_pt,LINE_COLOR);
}
if ((p&16)==16)
{
tmp_pt=*(this->pt_8[7])+*(this->pt_8[0]);
tmp_pt=tmp_pt/2;
if(ok)
setfillcolor(face[4]);
else
setfillcolor(BACK_COLOR);
floodfill_3d(&tmp_pt,LINE_COLOR);
}
if ((p&32)==32)
{
tmp_pt=*(this->pt_8[4])+*(this->pt_8[6]);
tmp_pt=tmp_pt/2;
if(ok)
setfillcolor(face[5]);
else
setfillcolor(BACK_COLOR);
floodfill_3d(&tmp_pt,LINE_COLOR);
}
}
void draw_circle(int radius,COLORREF color)
{
setfillcolor(color);
for(int i=0;i<8;i++)
{
solidcircle_3d(pt_8[i],radius,color);
}
}
void rotate(float rx,float ry,float rz){
for (int i=0;i<8;i++)
{
pt_8[i]->rotate(rx,ry,rz);
}
float all=0,tem;
for (int i=0;i<8;i++)
{
tem=pt_8[i]->x+pt_8[i]->y+pt_8[i]->z;
if (tem>all)
{
all=tem;
index2=i;
}
}
index=(index2+4)%8;
index=(index+2)%4+(int)(index/4)*4;
}
void size(float f)
{
for (int i=0;i<8;i++)
{
*(this->pt_8[i])=*(this->pt_8[i])*f;
}
}
};
void axes_init()
{
setlinecolor(LINE_COLOR);
cent_x=WIDTH/2;cent_y=HEIGHT/2;
//line(cent_x,cent_y,cent_x,0);
//line(cent_x,cent_y,cent_x-cent_y/2*sqrt(3.0),HEIGHT);
//line(cent_x,cent_y,cent_x+cent_y/2*sqrt(3.0),HEIGHT);
}
2016年03月25日 17点03分 3
level 5
a1668659995 楼主
void main()
{
MOUSEMSG m;
int r_button_down=0,l_button_down=0;
int last_x=0,last_y=0;
initgraph(WIDTH,HEIGHT);
setbkcolor(BACK_COLOR);
cleardevice();
axes_init();
pt_3d *te1=new pt_3d(BOX_WIDTH*-0.5,BOX_WIDTH*-0.5,BOX_WIDTH*-0.5),
*te2= new pt_3d(BOX_WIDTH*0.5,BOX_WIDTH*-0.5,BOX_WIDTH*-0.5),
*te3=new pt_3d(BOX_WIDTH*-0.5,BOX_WIDTH*0.5,BOX_WIDTH*-0.5),
*te4=new pt_3d(BOX_WIDTH*-0.5,BOX_WIDTH*-0.5,BOX_WIDTH*0.5);
cube *c_cube=new cube(*te1,*te2,*te3,*te4);
//c_cube->draw_circle(3,GREEN);
c_cube->draw_line(1);
c_cube->draw_face(1);
BeginBatchDraw();
//while(1)
{
for (int i=0;i<360;i++)
{
c_cube->draw_face(0);
c_cube->draw_line(0);
c_cube->rotate(1,0,0);
c_cube->draw_line(1);
c_cube->draw_face(1);
FlushBatchDraw();
Sleep(2);
}
for (int i=0;i<360;i++)
{
c_cube->draw_face(0);
c_cube->draw_line(0);
c_cube->rotate(0,1,0);
c_cube->draw_line(1);
c_cube->draw_face(1);
FlushBatchDraw();
Sleep(2);
}
for (int i=0;i<360;i++)
{
c_cube->draw_face(0);
c_cube->draw_line(0);
c_cube->rotate(0,0,1);
c_cube->draw_line(1);
c_cube->draw_face(1);
FlushBatchDraw();
Sleep(2);
}
}
while(1)
{
m=GetMouseMsg(); //get message form mouse
switch(m.uMsg)
{
case WM_MOUSEMOVE:
if(r_button_down)
{
c_cube->draw_face(0);
c_cube->draw_line(0);
c_cube->rotate((m.y-last_y)/-2,(m.y-last_y)/2,m.x-last_x);
c_cube->draw_line(1);
c_cube->draw_face(1);
FlushBatchDraw();
Sleep(2);
}
if(l_button_down)
{
c_cube->draw_face(0);
c_cube->draw_line(0);
cent_x+=(m.x-last_x);
cent_y+=(m.y-last_y);
c_cube->draw_line(1);
c_cube->draw_face(1);
FlushBatchDraw();
Sleep(2);
}
last_x=m.x;
last_y=m.y;
break;
case WM_RBUTTONDOWN:
r_button_down=1;
break;
case WM_RBUTTONUP:
r_button_down=0;
break;
case WM_LBUTTONDOWN:
l_button_down=1;
break;
case WM_LBUTTONUP:
l_button_down=0;
break;
case WM_MOUSEWHEEL:
c_cube->draw_face(0);
c_cube->draw_line(0);
if(m.wheel>0)
c_cube->size(1.01);
else
c_cube->size(0.99);
c_cube->draw_line(1);
c_cube->draw_face(1);
FlushBatchDraw();
break;
default:break;
}
}
closegraph();
}
2016年03月25日 17点03分 4
level 5
a1668659995 楼主
这是核心的旋转公式,
左键拖动,右键旋转,滑轮当然是缩放咯!!嘎嘎~~ 自己先占了5楼[惊哭]
2016年03月25日 17点03分 5
level 7
我觉得你可以写一个跟DirectX或者OpnGL一样的3D引擎出来
2016年03月27日 04点03分 6
至少我是看到这些矩阵变化就头疼[笑眼]
2016年03月27日 04点03分
@stophin 哈哈 我需要小伙伴,一个人头都大了
2016年03月28日 03点03分
来,我做你的小伙伴
2016年03月28日 11点03分
@stophin 哈哈 可以呀,可以交流交流
2016年03月29日 13点03分
level 11
厉害
2016年03月27日 05点03分 7
哈哈,就用了上面那个公式,可以参考平面图形旋转,就第一个矩阵的0和1都去掉变成二阶行列式,然后空间想象力要好点就o了
2016年03月28日 03点03分
level 6
是不是需要高深的数学知识?
2016年03月27日 10点03分 8
回复错人了,你看楼上吧
2016年03月28日 06点03分
level 8
效果很棒,高手啊。
2016年03月27日 12点03分 9
level 5
a1668659995 楼主
进军二阶魔方旋转动画
以前做的无动画的模拟图
2016年03月28日 06点03分 10
level 1
三维坐标怎么转平面坐标
2016年04月02日 09点04分 11
上面那有公式,看看呗
2016年04月02日 12点04分
level 1
我发现我在跟你做一样的东西
2016年05月04日 05点05分 12
[勉强]介意然我看看效果吗
2016年05月05日 10点05分
level 5
这是tc运行的吗?
2016年05月04日 15点05分 13
easyx
2016年05月05日 10点05分
level 6
请教一下楼主,就简单的正方体而言,你是如何规划它侧面线条的长度和角度的,
2016年05月12日 13点05分 14
可以看看正等轴测图的知识[吐舌]
2016年05月13日 04点05分
level 1
请教一下楼主,
pt_3d(pt_3d *pt)
{
this->x=pt->x;
this->y=pt->y;
this->z=pt->z;
}
作为新手,看不懂是怎样回事
还有cent x,cent y 的含义
2018年12月11日 08点12分 16
构造函数
2018年12月13日 09点12分
1