Author Topic: 3D cube + using sprite texture with MapTriangle (3D)  (Read 3902 times)

0 Members and 1 Guest are viewing this topic.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
3D cube + using sprite texture with MapTriangle (3D)
« on: December 04, 2020, 03:44:00 pm »
Hi. This is easy demo how use software sprite as hardware texture on surface. Image is output my uncomplete program, you can know it from Windows... it is created from ANI file:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. DIM Frame(15) AS LONG
  3. img = _LOADIMAGE("hg.gif", 32)
  4. _SETALPHA 100, &HFF000000 TO &HFF111111, img
  5. 'create sprite
  6. FOR s = 1 TO 15
  7.     Frame(s) = _NEWIMAGE(32, 32, 32)
  8.     _DEST Frame(s)
  9.     CLS , &H30909090
  10.     _PUTIMAGE , img, Frame(s), (32 * (s - 1), 0)-(32 * (s - 1) + 31, 31)
  11.  
  12. 'software animation to hardware animation
  13. FOR t = 1 TO 15
  14.     sw = _COPYIMAGE(Frame(t), 33)
  15.     _FREEIMAGE Frame(t)
  16.     Frame(t) = sw
  17.  
  18. 'create 3D cube and place animation as texture
  19. TYPE vertex3d
  20.     x AS SINGLE
  21.     y AS SINGLE
  22.     z AS SINGLE
  23.     a AS SINGLE
  24.     l AS SINGLE
  25.  
  26. DIM V(1 TO 8) AS vertex3d '8 3D vertexes
  27.  
  28. V(1).x = -1: V(1).y = 1: V(1).z = -3: V(2).x = 1: V(2).y = 1: V(2).z = -3: V(3).x = -1: V(3).y = -1: V(3).z = -3: V(4).x = 1: V(4).y = -1: V(4).z = -3 'openGL coordinate system
  29. V(5).x = -1: V(5).y = 1: V(5).z = -5: V(6).x = 1: V(6).y = 1: V(6).z = -5: V(7).x = -1: V(7).y = -1: V(7).z = -5: V(8).x = 1: V(8).y = -1: V(8).z = -5
  30.  
  31. camX = 0
  32. camZ = -5
  33.  
  34.  
  35. 'calculate points angles (.a) and line lenght from middle (.l) - middle of the cube is in 0, 0, 0, rotation center is on 0, 0, camZ
  36. FOR a = 1 TO 8
  37.     V(a).a = _ATAN2(V(a).z + 4, V(a).x) '  4 in Z axis is middle between coordinate 5 and 3
  38.     V(a).l = _HYPOT(V(a).z + 4, V(a).x)
  39.  
  40.  
  41. DIM NP(1 TO 8) AS vertex3d 'NP = new points
  42.  
  43. i = 1: Himg = Frame(i)
  44. ON TIMER(1 / 10) GOSUB ChangeFrame
  45.     roto = roto + .001
  46.  
  47.     'calculate new points position: - rotation X/Z
  48.     FOR c = 1 TO 8
  49.         NP(c).x = COS(roto + V(c).a) * V(c).l
  50.         NP(c).z = camZ + SIN(roto + V(c).a) * V(c).l
  51.         NP(c).y = V(c).y
  52.     NEXT c
  53.  
  54.     'map cube   SEE ATTACHED PICTURE!
  55.     index:
  56.     DATA 7,8,3,4: 'dno          bottom
  57.     DATA 5,6,1,2: 'vrch         top
  58.     DATA 1,2,3,4: 'celo         front
  59.     DATA 5,6,7,8: 'zadek        back
  60.     DATA 1,5,3,7: 'levy bok     left side
  61.     DATA 2,6,4,8: 'pravy bok    right side
  62.  
  63.     RESTORE index
  64.     FOR D = 1 TO 6
  65.         READ i1, i2, i3, i4
  66.         _MAPTRIANGLE (0, 0)-(32, 0)-(0, 32), Himg TO(NP(i1).x, NP(i1).y, NP(i1).z)-(NP(i2).x, NP(i2).y, NP(i2).z)-(NP(i3).x, NP(i3).y, NP(i3).z), 0
  67.         _MAPTRIANGLE (32, 0)-(0, 32)-(32, 32), Himg TO(NP(i2).x, NP(i2).y, NP(i2).z)-(NP(i3).x, NP(i3).y, NP(i3).z)-(NP(i4).x, NP(i4).y, NP(i4).z), 0
  68.     NEXT D
  69.     _DISPLAY
  70.  
  71. ChangeFrame:
  72. i = i + 1: IF i > 15 THEN i = 1
  73. Himg = Frame(i)
  74.  

sprite image need for run:
 
HG.GIF


This image show order in which is surface textured (source code row 60)
 
cube.png


FellippeHeitor

  • Guest
Re: 3D cube + using sprite texture with MapTriangle (3D)
« Reply #1 on: December 04, 2020, 03:57:11 pm »
OOOhh, fancy transparency! Very good looking, Petr.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: 3D cube + using sprite texture with MapTriangle (3D)
« Reply #2 on: December 04, 2020, 04:17:52 pm »
:) Thank you, Fellippe.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: 3D cube + using sprite texture with MapTriangle (3D)
« Reply #3 on: December 04, 2020, 04:34:26 pm »
Yes, very good example!   Thanks for sharing this.

- Dav

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: 3D cube + using sprite texture with MapTriangle (3D)
« Reply #4 on: December 05, 2020, 04:11:12 am »
Here is an example of how to look at the same body from two sides at once - the image on the right is rotated 90 degrees compared to the original, to make it easier to distinguish that it is really a rotation of the cube, I intentionally extend its X axis. Use the same attachment as in previous case.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. DIM Frame(15) AS LONG
  3. img = _LOADIMAGE("hg.gif", 32)
  4. _SETALPHA 100, &HFF000000 TO &HFF111111, img
  5. 'create sprite
  6.  
  7. FOR s = 1 TO 15
  8.     Frame(s) = _NEWIMAGE(32, 32, 32)
  9.     _DEST Frame(s)
  10.     CLS , &H30909090
  11.     _PUTIMAGE , img, Frame(s), (32 * (s - 1), 0)-(32 * (s - 1) + 31, 31)
  12.  
  13. 'software animation to hardware animation
  14. FOR t = 1 TO 15
  15.     sw = _COPYIMAGE(Frame(t), 33)
  16.     _FREEIMAGE Frame(t)
  17.     Frame(t) = sw
  18.  
  19. 'create 3D cube and place animation as texture
  20. TYPE vertex3d
  21.     x AS SINGLE
  22.     y AS SINGLE
  23.     z AS SINGLE
  24.     a AS SINGLE
  25.     l AS SINGLE
  26.  
  27. DIM V(1 TO 8) AS vertex3d '8 3D vertexes
  28.  
  29. V(1).x = -2: V(1).y = 1: V(1).z = -3: V(2).x = 2: V(2).y = 1: V(2).z = -3: V(3).x = -2: V(3).y = -1: V(3).z = -3: V(4).x = 2: V(4).y = -1: V(4).z = -3 'openGL coordinate system
  30. V(5).x = -2: V(5).y = 1: V(5).z = -5: V(6).x = 2: V(6).y = 1: V(6).z = -5: V(7).x = -2: V(7).y = -1: V(7).z = -5: V(8).x = 2: V(8).y = -1: V(8).z = -5
  31.  
  32. camX = -4
  33. camZ = -10
  34.  
  35. camX2 = 4
  36. camZ2 = -10
  37.  
  38.  
  39.  
  40. 'calculate points angles (.a) and line lenght from middle (.l) - middle of the cube is in 0, 0, 0, rotation center is on 0, 0, camZ
  41. FOR a = 1 TO 8
  42.     V(a).a = _ATAN2(V(a).z + 4, V(a).x) '  4 in Z axis is middle between coordinate 5 and 3
  43.     V(a).l = _HYPOT(V(a).z + 4, V(a).x)
  44.  
  45.  
  46. DIM NP(1 TO 8) AS vertex3d 'NP = new points
  47. DIM NP2(1 TO 8) AS vertex3d 'NP2 = new points - rotated 90 degrees from the NP
  48.  
  49.  
  50.  
  51. i = 1: Himg = Frame(i)
  52. ON TIMER(1 / 25) GOSUB ChangeFrame
  53.     roto = roto + .001
  54.  
  55.     'calculate new points position: - rotation X/Z
  56.     FOR c = 1 TO 8
  57.         NP(c).x = camX + COS(roto + V(c).a) * V(c).l
  58.         NP(c).z = camZ + SIN(roto + V(c).a) * V(c).l
  59.         NP(c).y = V(c).y
  60.  
  61.         NP2(c).x = camX2 + COS(roto - _PI / 2 + V(c).a) * V(c).l
  62.         NP2(c).z = camZ2 + SIN(roto - _PI / 2 + V(c).a) * V(c).l
  63.         NP2(c).y = V(c).y
  64.     NEXT c
  65.  
  66.     'map cube   SEE ATTACHED PICTURE!
  67.     index:
  68.     DATA 7,8,3,4: 'dno          bottom
  69.     DATA 5,6,1,2: 'vrch         top
  70.     DATA 1,2,3,4: 'celo         front
  71.     DATA 5,6,7,8: 'zadek        back
  72.     DATA 1,5,3,7: 'levy bok     left side
  73.     DATA 2,6,4,8: 'pravy bok    right side
  74.  
  75.     RESTORE index
  76.     FOR D = 1 TO 6
  77.         READ i1, i2, i3, i4
  78.         _MAPTRIANGLE (0, 0)-(32, 0)-(0, 32), Himg TO(NP(i1).x, NP(i1).y, NP(i1).z)-(NP(i2).x, NP(i2).y, NP(i2).z)-(NP(i3).x, NP(i3).y, NP(i3).z), 0
  79.         _MAPTRIANGLE (32, 0)-(0, 32)-(32, 32), Himg TO(NP(i2).x, NP(i2).y, NP(i2).z)-(NP(i3).x, NP(i3).y, NP(i3).z)-(NP(i4).x, NP(i4).y, NP(i4).z), 0
  80.  
  81.         _MAPTRIANGLE (0, 0)-(32, 0)-(0, 32), Himg TO(NP2(i1).x, NP2(i1).y, NP2(i1).z)-(NP2(i2).x, NP2(i2).y, NP2(i2).z)-(NP2(i3).x, NP2(i3).y, NP2(i3).z), 0
  82.         _MAPTRIANGLE (32, 0)-(0, 32)-(32, 32), Himg TO(NP2(i2).x, NP2(i2).y, NP2(i2).z)-(NP2(i3).x, NP2(i3).y, NP2(i3).z)-(NP2(i4).x, NP2(i4).y, NP2(i4).z), 0
  83.  
  84.  
  85.     NEXT D
  86.     _DISPLAY
  87.  
  88. ChangeFrame:
  89. i = i + 1: IF i > 15 THEN i = 1
  90. Himg = Frame(i)
  91.