### Author Topic: MAPTRIANGLE in 3D by Petr  (Read 83 times)

#### Qwerkey ##### MAPTRIANGLE in 3D by Petr
« on: March 26, 2020, 05:52:12 AM »
MAPTRIANGLE in 3D - ROTATION

Author: @Petr
Source: qb64.org Forum
URL: https://www.qb64.org/forum/index.php?topic=300.msg101478#msg101478
Version: 3D Rotation
Tags: [3D], [Graphics]

Description:
A demo to show rotation in 3D using MAPTRIANGLE 3D, without direct OpenGL statements.

Librarian's Note:  The sample given here is just one of a number of _MAPTRIANGLE(3D) examples which appear in Petr's post.  That post is a good learning resource for anyone wanting to learn 3D graphics using _MAPTRIANGLE(3D).

Source Code:
Code: QB64: [Select]
1. 'Program details:
2.
3. 'ad 1:  SUB init - read text "Petr", which is writed to virtual screen and write every pixel position to array T.
4. '       array T contains pixels positions data, recalculated to 3D in the same way as OpenGL (X = minus values to left, plus values to right, zero is middle,
5. '       Y = minus values down, plus values up, zero in middle, Z values - visible from -1 to lower values)
6. '
7. ' I see such a small reproach to myself in the definition of the T array. Next time I would write also new function, that would return a number
8. ' indicating the real number used pixels, because is an unnecessary waste of memory and performance to write  DIM T (16*8) for one character, if this
9. ' character use just 20 pixels.
10.
11. 'ad 2: (row 73 in source code): Program read array T. If in T is some record ( T().x >0 ) then is called SUB kostka. SUB kostka create cube in
12. '      coordinates X, Y, Z and use OpenGL coordinate system.
13.
14.
15.
16.
17. _TITLE "Next MAPTRIANGLE 3D demo"
18.
19. k = _PI(2) / 4 'k is angle 360 / 4 in radians, for 4 cube corners (cube is double quad)
20.
21.
22. '------------
23. DIM SHARED texture AS LONG, rot
24.
25.
26. DIM SHARED T(14 * 110) AS T
27.
28. init '                          SEE ad 1
29. '---------------------
30.
31.
32.
33. SCREEN _NEWIMAGE(800, 600, 32)
34. CLS , _RGB32(127, 127, 188)
35.
36.
37. DIM B(3) AS b
38.
39.
40. B(0).x = -1: B(0).y = -1
41. B(1).x = 1: B(1).y = -1
42. B(2).x = -1: B(2).y = -1
43. B(3).x = 1: B(3).y = -1 '            B() are starting coordinates for cube in middle screen
44.
45. w = _WIDTH(i)
46. h = _HEIGHT(i)
47.
48.     IF i THEN _FREEIMAGE i '         if exists hardware image (which contains clock), then free it from memory
49.     j = _NEWIMAGE(150, 100, 32) '    create software image (handle j)
50.     CLS , _RGBA32(0, 50, 127, 100)
51.     clock 0, 0 '                    draw new clock image, which contains current system time to software image (handle j)
52.     '                               copy software image j as hardware image i and then delete software image j
53.     i = _COPYIMAGE(j, 33)
54.
55.     clock 0, 0 '                    as you see, here you are in DEST 0. Clocks is now paint to screen to left corner (coordinates 0,0)
56.
57.     texture& = i '                  set i handle as texture& handle
58.     '--------------------
59.
60.     FOR Ys = 0 TO 17 '                            SEE ad 2
61.         zz2 = zz2 + .01
62.         FOR Xs = 0 TO 50
63.             IF T(i4).x > 0 THEN
64.                 X = T(i4).x + 70
65.                 Y = T(i4).y
66.                 z = T(i4).z - 70
67.                 kostka X - zz2, Y, z - 15 + zz2
68.             i4 = i4 + 1
69.     NEXT Xs, Ys
70.     i4 = 0
71.     IF zz2 > 140 THEN zz2 = -50
72.     '-----------------------------
73.
74.     Sx = 0
75.     Sz = -2.5
76.     Sy = -1
77.
78.
79.     Rx = 1
80.     Ry = -1
81.     Rz = 1
82.     ' info for rotation. To rotate the bodies, you must have all the points that are rotating in the same center. For this demo, it is easy to specify the radius of rotation,
83.     ' because all the points are just as far from the center as the cube is a symmetrical body. But if you want to write a 3D game, then if you want to write with MAPTRIANGLE,
84.     ' you need to map the floor using 4 triangles and calculate the radius for the points on the edges using the Pythagoras theorem:
85.     '
86.     '       A--------------------------B     x is your position, as you see, all points use different radius
87.     '       I\\\                   Y / I     Y is your triangle side 1, next is floor height - Y
88.     '       I   \\\\\\             Y/  I     X is your triangle side 2, next is floor width - X
89.     '       I          \\\\\\\\/// xXXXI     third sides calculate using Pythagoras.
90.     '       I       ////////////    \  I
91.     '       I //////                 \ I
92.     '       C--------------------------D
93.     '
94.     x1 = Sx + SIN(rot) * Rx: z1 = Sz + COS(rot) * Rz: y1R = Sy + SIN(rot) * Ry
95.     x2 = Sx + SIN(rot + k) * Rx: z2 = Sz + COS(rot + k) * Rz: y2R = Sy + SIN(rot + k) * Ry
96.
97.     x4 = Sx + SIN((2 * k) + rot) * Rx: z4 = Sz + COS((2 * k) + rot) * Rz: y3R = Sy + SIN((3 * k) + rot) * Ry
98.     x3 = Sx + SIN((3 * k) + rot) * Rx: z3 = Sz + COS((3 * k) + rot) * Rz: y4R = Sy + SIN((4 * k) + rot) * Ry
99.
100.     y1 = B(0).y
101.     y2 = B(1).y
102.     y4 = B(2).y
103.     y3 = B(3).y
104.
105.
106.
107.
108.     y5 = y1 + 1.5
109.     y6 = y2 + 1.5
110.     y7 = y3 + 1.5
111.     y8 = y4 + 1.5
112.
113.
114.     _MAPTRIANGLE (0, h)-(w, h)-(0, 0), i TO(x1, y1, z1)-(x2, y2, z2)-(x3, y3, z3) 'podlaha                 floor
115.     _MAPTRIANGLE (w, h)-(0, 0)-(w, 0), i TO(x2, y2, z2)-(x3, y3, z3)-(x4, y4, z4) 'podlaha
116.
117.
118.     _MAPTRIANGLE (0, h)-(w, h)-(0, 0), i TO(x1, y5, z1)-(x2, y6, z2)-(x3, y7, z3) 'strop                   roof
119.     _MAPTRIANGLE (w, h)-(0, 0)-(w, 0), i TO(x2, y6, z2)-(x3, y7, z3)-(x4, y8, z4) 'strop
120.
121.     _MAPTRIANGLE (0, h)-(w, h)-(0, 0), i TO(x1, y1, z1)-(x2, y2, z2)-(x1, y5, z1) 'prava stena             right wall
122.     _MAPTRIANGLE (w, h)-(0, 0)-(w, 0), i TO(x2, y2, z2)-(x1, y5, z1)-(x2, y6, z2) 'prava stena
123.
124.
125.     _MAPTRIANGLE (w, h)-(0, h)-(w, 0), i TO(x3, y3, z3)-(x4, y4, z4)-(x3, y7, z3) 'leva stena              left wall
126.     _MAPTRIANGLE (0, h)-(w, 0)-(0, 0), i TO(x4, y4, z4)-(x3, y7, z3)-(x4, y8, z4) 'leva stena
127.
128.
129.     _MAPTRIANGLE (w, h)-(0, h)-(w, 0), i TO(x1, y1, z1)-(x3, y3, z3)-(x1, y5, z1) 'zadni stena             back wall
130.     _MAPTRIANGLE (0, h)-(w, 0)-(0, 0), i TO(x3, y3, z3)-(x1, y5, z1)-(x3, y7, z3) 'zadni stena
131.
132.
133.     _MAPTRIANGLE (0, h)-(w, h)-(0, 0), i TO(x2, y2, z2)-(x4, y4, z4)-(x2, y6, z2) 'predni stena            front wall
134.     _MAPTRIANGLE (w, h)-(0, 0)-(w, 0), i TO(x4, y4, z4)-(x2, y6, z2)-(x4, y8, z4) 'zadni stena
135.
136.
137.     _LIMIT 50
138.
139.     rot = rot + .01
140.
141. SUB kostka (x, y, z) 'zadavas levy horni predni roh ,udela kostku v zadane x,y,z                         x y z are coordinates for left upper corner, do cube on this place
142.
143.
144.     W = _WIDTH(texture&)
145.     H = _HEIGHT(texture&)
146.
147.     '                g                       h                                e                  f
148.     MAPQUAD x + -1.5, y + 1.5, z + -1.5, x + .5, y + 1.5, z + -1.5, x + -1.5, y + -.5, z + -1.5, x + .5, y + -.5, z - 1.5, texture&
149.     MAPQUAD x + -1, y + 1, z + -1, x + 1, y + 1, z + -1, x + -1.5, y + 1.5, z + -1.5, x + .5, y + 1.5, z + -1.5, texture&
150.     MAPQUAD x + -1.5, y + -.5, z + -1.5, x + .5, y + -.5, z - 1.5, x + -1, y + -1, z + -1, x + 1, y - 1, z - 1, texture& '
151.     MAPQUAD x + -1, y + 1, z + -1, x + -1.5, y + 1.5, z + -1.5, x + -1, y + -1, z + -1, x + -1.5, y + -.5, z + -1.5, texture&
152.     MAPQUAD x + .5, y + 1.5, z + -1.5, x + .5, y + -.5, z - 1.5, x + 1, y - 1, z - 1, x + 1, y + 1, z + -1, texture&
153.     '                a                     b                    c                       d
154.     MAPQUAD x + -1, y + 1, z + -1, x + 1, y + 1, z + -1, x + -1, y + -1, z + -1, x + 1, y - 1, z - 1, texture&
155.
156.
157.
158.
159.
160. SUB MAPQUAD (x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, texture AS LONG)
161.     W = _WIDTH(texture&)
162.     H = _HEIGHT(texture&)
163.
164.     _MAPTRIANGLE (0, 0)-(W, 0)-(0, H), texture& TO(x1, y1, z1)-(x2, y2, z2)-(x3, y3, z3)
165.     _MAPTRIANGLE (W, 0)-(0, H)-(W, H), texture& TO(x2, y2, z2)-(x3, y3, z3)-(x4, y4, z4)
166.
167. SUB init
168.     text\$ = " Petr" 'width = 32 pixels (records)
169.
170.     virtual& = _NEWIMAGE(100, 100, 256)
171.     _DEST virtual&
172.     PRINT text\$
173.     _SOURCE virtual&
174.     i = 0
175.     FOR Y = 17 TO 0 STEP -1
176.         FOR X = 0 TO 50
177.             IF POINT(X, Y) <> 0 THEN
178.                 T(i).x = (-8.5 + X)
179.                 T(i).y = -18 + (16 - Y) * 2
180.                 T(i).z = -5 - X '                      this shift cubes in space consecutively. One pixel = One cube.
181.             i = i + 1
182.     NEXT X, Y
183.     _DEST 0: _SOURCE 0: _FREEIMAGE virtual&
184.     i = 0
185.
186.
187. SUB clock (x, y) '                                     This sub draw software image contains clock
188.     de = _DEST
189.     clocka& = _NEWIMAGE(100, 100, 32)
190.     _DEST clocka&
191.     vterina = VAL(RIGHT\$(TIME\$, 2))
192.     hodina = VAL(LEFT\$(TIME\$, 2))
193.     minuta = VAL(MID\$(TIME\$, 4, 2))
194.
195.     IF hodina > 12 THEN hodina = hodina - 12
196.     hodina = hodina + (1 / 59) * minuta
197.
198.
199.     vt = vterina + 45
200.     ho = hodina + 45
201.     mi = minuta + 45
202.
203.     pozicevterina = _PI(2) / 60 * vt
204.     poziceminuta = _PI(2) / 60 * ho * 5
205.     pozicehodina = _PI(2) / 60 * mi
206.
207.     xs = 50 + COS(pozicevterina) * 30
208.     ys = 50 + SIN(pozicevterina) * 30
209.
210.     xm = 50 + COS(poziceminuta) * 35
211.     ym = 50 + SIN(poziceminuta) * 35
212.
213.     xh = 50 + COS(pozicehodina) * 40
214.     yh = 50 + SIN(pozicehodina) * 40
215.
216.     FOR n = 1 TO 100
217.         LINE (n, 0)-(n, 99), _RGB32(127 - n, n, 27 + n), BF
218.     NEXT n
219.     LINE (0, 0)-(99, 99), _RGB32(255, 255, 255), B
220.
221.     COLOR _RGBA32(127, 127, 127, 150)
222.     _PRINTSTRING (35, 45), "QB64"
223.     COLOR _RGB32(255, 255, 255)
224.
225.
226.     LINE (50, 50)-(xh, yh), _RGB32(255, 255, 0)
227.     LINE (50, 50)-(xm, ym), _RGB32(255, 255, 0)
228.     LINE (50, 50)-(xs, ys), _RGB32(0, 255, 255)
229.     m = 0
230.     FOR kruh = 0 TO _PI(2) STEP _PI(2) / 60
231.         PSET (50 + COS(kruh) * 47, 50 + SIN(kruh) * 47)
232.         IF m MOD 5 = 0 THEN LINE (50 + COS(kruh) * 47, 50 + SIN(kruh) * 47)-(50 + COS(kruh) * 44, 50 + SIN(kruh) * 44), , BF
233.         m = m + 1
234.     NEXT kruh
235.     _DEST de
236.     _SETALPHA 100, , clocka&
237.     _PUTIMAGE (x, y), clocka&, de
238.     _FREEIMAGE clocka&
239.

« Last Edit: March 28, 2020, 07:10:20 AM by Qwerkey »