Author Topic: Static Arrays multidimensional  (Read 2389 times)

0 Members and 2 Guests are viewing this topic.

Arnold

  • Guest
Static Arrays multidimensional
« on: October 11, 2016, 10:34:53 AM »
Hi Charles,

this subject is in my mind since I worked with lesson 11 of the NeHe tutorials. I tried to get the members of a multidimensional array and applied the approach of \tests\constructs\MultiDimArray.o2bas. This led to:

float points[45*45*3]={0}    // The Array For The Points On The Grid Of Our "Wave"
'get array points
'macro gpoints(x1,x2,x3) points[x3*45*45 +x2*45 +x1]

But in the meantime I found that this would work too:
...
macro gpoints(x3,x2,x1) points[x3*45*3 +x2*3 +x1]

Is the approach below for accessing members of an array legal?

Roland

Code: OxygenBasic
  1. 'StaticArrays
  2. 'access static arrays
  3.  
  4. #include "$/inc/console.inc"
  5.  
  6. indexbase 0
  7.  
  8. ' One dimension
  9. dim as float line[4] => {1.0, 2.0, 3.0, 4.0}
  10. printl "Line: " & cr
  11. for i=0 to <4 : print line[i] & " " : next : printl
  12.  
  13. ' Two dimensions, row, column
  14. dim as float matrix [3*4] => {
  15. 11.0, 12.0, 13.0, 14.0,
  16. 15.0, 16.0, 17.0, 18.0,
  17. 19.0, 20.0, 21.0, 22.0 }
  18. 'Get elements
  19. 'y-> x
  20. macro gmatrix(y,x) matrix[y*4 +x]
  21.  
  22. printl "Matrix:"
  23. num=10
  24. for i=0 to <3 ' row
  25.   printl "row" i ":  "
  26.    for j=0 to <4 ' col
  27.      num+=1
  28.       print gmatrix(i,j) & "("  num "), "
  29.    next
  30. next
  31. printl
  32.  
  33.  
  34. ' Three dimensions, row, column, depth
  35. dim as int cube[2*3*4] => {
  36. 11,12,13,14,   15,16,17,18,  19,20,21,22,        'row(0)
  37.  23,24,25,26,  27,28,29,30,  31,32,33,34 }      'row(1)
  38. 'Get elements
  39. 'z-> y-> x
  40. macro gcube(z,y,x) cube[z*3*4 +y*4 +x]
  41. num=10
  42. printl "Cube: "
  43. for i=0 to <2                         'row
  44.   printl "row " i ":   "
  45.    for j=0 to <3                      'col
  46.      printl "col " j ":   "
  47.       for k= 0 to <4                  'depth
  48.         num+=1
  49.          print gcube(i,j,k) & "("  num "), "
  50.       next k
  51.    next j
  52. next i
  53. printl
  54.  
  55.  
  56. ' Four dimensions, row, column, depth, time
  57. dim as int sptime[5*4*3*2] => {0}
  58. 'Fetch elements
  59. 'z-> y-> x-> w
  60. macro  gsptime(z,y,x,w) sptime[z*4*3*2 +y*3*2 +x*2 +w]
  61. printl "Space-Time: "
  62. printl "Ask Einstein"
  63.  
  64. printl cr & "Enter ..."
  65. waitkey
  66.  

.

Charles Pegge

  • Guest
Re: Static Arrays multidimensional
« Reply #1 on: October 11, 2016, 11:51:49 AM »
Hi Roland,

It is up to the programmer whether to use major-index first or minor-index first in a multidimansional array.

C uses row-major order / major-index first. (But Opengl matrices are organised in column-major order, to confuse everyone)

https://en.wikipedia.org/wiki/Row-major_order

NB: Those macros require indexbase 0 to work correctly

PS: Thanks for all your NEHE Translations  :)
 I wonder if we can get them to work with jpeg or png images - for compactness.

Arnold

  • Guest
Re: Static Arrays multidimensional
« Reply #2 on: October 11, 2016, 11:22:59 PM »
Hi Charles,

thank you for the link. I now know what I have to look for. The terms row-major order / column-major order were news to me and I was not aware that different programming languages use different ways to access multidimensional arrays. Somehow it is curious now that both macros above will work for lesson 11 - maybe this happens incidentally.

According to soil.h the soil library should be able to load the format bmp, tga, dds, png, jpg and save in format bmp, tga and dds. But the soil library must still be checked and tested. Until now I used only one of the 14 functions.

My final goal will be to use Oxygen's own facilities for the NeHe lessons either using OpenglSceneFrame.inc or ConsoleG.inc. But as we have to replace our kitchen completely I will be prevented from working with Oxygen the next few days.

Roland