【水】基于ege的简单3D模拟

我们模拟从被观察物体射出光线,在眼球焦点交汇,然后打到视网膜上成像

——足够了吧,剩下的难度应该是普及-

只是有一些常数可以自己调一下,看着顺眼就好

  1 #include <graphics.h>
  2 #include <cstdio>
  3     VECTOR3D a;
  4     VECTOR3D b;
  5     VECTOR3D c;
  6     VECTOR3D d;
  7     VECTOR3D a1;
  8     VECTOR3D b1;
  9     VECTOR3D c1;
 10     VECTOR3D d1;
 11 float k=0;
 12 #define fudu 0.1
 13 #define len 1
 14 #define lenz 20
 15 #define ox 300
 16 #define oy 300
 17 int one=500;
 18 float sdx(VECTOR3D a)
 19 {
 20     return (ox+one*a.x/(a.z+lenz)*len);
 21 }
 22 float sdy(VECTOR3D a)
 23 {
 24     return (oy+one*a.y/(a.z+lenz)*len);
 25 }
 26 void li(VECTOR3D a,VECTOR3D b)
 27 {
 28     line_f(sdx(a),sdy(a),sdx(b),sdy(b));
 29 }
 30 void mia(VECTOR3D a,VECTOR3D b,VECTOR3D c)
 31 {
 32     int mian[]=
 33     {
 34         sdx(a),sdy(a),
 35         sdx(b),sdy(b),
 36         sdx(c),sdy(c),
 37     };
 38     fillpoly(3,mian);
 39 }
 40 void print()
 41 {
 42     cleardevice();
 43     setcolor(0xFFFFFF);
 44     setfillcolor(0x9999FF);
 45     mia(a,b,c);
 46     mia(a,b,d);
 47     mia(a,c,d);
 48     mia(d,b,c);
 49     mia(a1,b1,c1);
 50     mia(a1,b1,d1);
 51     mia(a1,c1,d1);
 52     mia(d1,b1,c1);
 53     li(a,b);
 54     li(b,c);
 55     li(c,d);
 56     li(d,a);
 57     li(a,c);
 58     li(b,d);
 59     li(a1,b1);
 60     li(b1,c1);
 61     li(c1,d1);
 62     li(d1,a1);
 63     li(a1,c1);
 64     li(b1,d1);
 65     delay_ms(0);
 66 }
 67 void change()
 68 {
 69     char ch=getch();
 70     if(ch=='w')
 71     {
 72     a.Rotate(fudu,1,0,0);
 73     b.Rotate(fudu,1,0,0);
 74     c.Rotate(fudu,1,0,0);
 75     d.Rotate(fudu,1,0,0);
 76     a1.Rotate(fudu,1,0,0);
 77     b1.Rotate(fudu,1,0,0);
 78     c1.Rotate(fudu,1,0,0);
 79     d1.Rotate(fudu,1,0,0);
 80     }
 81     else    
 82     if(ch=='s')
 83     {
 84     a.Rotate(fudu,-1,0,0);
 85     b.Rotate(fudu,-1,0,0);
 86     c.Rotate(fudu,-1,0,0);
 87     d.Rotate(fudu,-1,0,0);
 88     a1.Rotate(fudu,-1,0,0);
 89     b1.Rotate(fudu,-1,0,0);
 90     c1.Rotate(fudu,-1,0,0);
 91     d1.Rotate(fudu,-1,0,0);
 92     }
 93     else
 94     if(ch=='a')
 95     {
 96     a.Rotate(fudu,0,1,0);
 97     b.Rotate(fudu,0,1,0);
 98     c.Rotate(fudu,0,1,0);
 99     d.Rotate(fudu,0,1,0);
100     a1.Rotate(fudu,0,1,0);
101     b1.Rotate(fudu,0,1,0);
102     c1.Rotate(fudu,0,1,0);
103     d1.Rotate(fudu,0,1,0);    
104     }
105     else
106     if(ch=='d')
107     {
108     a.Rotate(fudu,0,-1,0);
109     b.Rotate(fudu,0,-1,0);
110     c.Rotate(fudu,0,-1,0);
111     d.Rotate(fudu,0,-1,0);
112     a1.Rotate(fudu,0,-1,0);
113     b1.Rotate(fudu,0,-1,0);
114     c1.Rotate(fudu,0,-1,0);
115     d1.Rotate(fudu,0,-1,0);    
116     }
117     else
118     if(ch==' ')
119     {
120     a.Rotate(fudu,0,0,1);
121     b.Rotate(fudu,0,0,1);
122     c.Rotate(fudu,0,0,1);
123     d.Rotate(fudu,0,0,1);
124     a1.Rotate(fudu,0,0,1);
125     b1.Rotate(fudu,0,0,1);
126     c1.Rotate(fudu,0,0,1);
127     d1.Rotate(fudu,0,0,1);
128     }
129     else
130     if(ch=='[')
131         one-=10;
132     else
133     if(ch==']')
134         one+=10;
135 }
136 int main()
137 {
138     printf("wasd旋转[]缩放");
139     delay(1000);
140     initgraph(ox*2,oy*2);
141     setrendermode(RENDER_MANUAL);
142     a.x=0;
143     a.y=2;
144     a.z=-1.4142135623730950488016887242097/2;
145     b.x=0;
146     b.y=0;
147     b.z=1.4142135623730950488016887242097*1.5;
148     c.x=-1.7320508075688772935274463415059;
149     c.y=-1;
150     c.z=-1.4142135623730950488016887242097/2;
151     d.x=1.7320508075688772935274463415059;
152     d.y=-1;
153     d.z=-1.4142135623730950488016887242097/2;    
154     a1.x=1.7320508075688772935274463415059;
155     a1.y=1;
156     a1.z=1.4142135623730950488016887242097/2;
157     b1.x=-1.7320508075688772935274463415059;
158     b1.y=1;
159     b1.z=1.4142135623730950488016887242097/2;
160     c1.x=0;
161     c1.y=-2;
162     c1.z=1.4142135623730950488016887242097/2;
163     d1.x=0;
164     d1.y=0;
165     d1.z=-1.4142135623730950488016887242097*1.5;
166     print();
167     while(1)
168     {
169         change();
170         print();
171     }
172 }

画了一个梅塔特隆立方体玩玩,看着应该比较顺眼

原文地址:https://www.cnblogs.com/wanglichao/p/5651493.html