Archived

This topic is now archived and is closed to further replies.

Question about the OpenGL Game Programming bok (chapter 8)

This topic is 5281 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Alright, I''ve had this book for almost a year and I''ve been reading it on and off since I got it (no offense but there are some things that aren''t explained very well in this book which is why I plan on buying the new one). Referring to my comment above, I have a question about the heightfield terrian example in chapter 8. I know that the example uses a grayscale image for the height of the terrain but how in the hell does the code find out the RGB value of each pixel in this?
VG-Force | Ekim Gram Productions

Share on other sites
Check the InitializeTerrain() routine on page 271.

The image is loaded to memory using the BMP load routine presented earlier in the book. There is a pointer called "imageData" that points to the start of the image data in memory. The image data is stored sequentially in memory and can be thought of as a 1x(ImageSize*ImageSize) array. What the routine does is to use the current x and z loop values to calculate the appropriate place in the image data. Heres a 3x3 terrain array and a 1x9 array of image data - just like in the book example except much smaller.

eg.

Terrain grid
  0 1 2  -----0| | | |  -----1| | |o|  -----2| | | |  -----

The imageData pointer points to cell zero of the image data

imageData  |  v  0 1 2 3 4 5 6 7 8  -----------------0| | | | | |o| | | |  -----------------

Two loops, like in the InitializeTerrain() routine, go through the grid and calculate the appropriate imageData memory cell by using the current values of the two loops:

for ( int z = 0; z < 3; z++ ) {  for ( int x = 0; x < 3; x++ ) {    X coordinate = x loop current value    Z coordinate = z loop current value    Y coordinate = (z * GridWidth + x ) * dataoffset                   (z * 3 + x ) * 1  in our example  }}

the formula above gives the following values for our example

z x       y0 0 (0*3+0)*1 = cell 00 1 (0*3+1)*1 = cell 10 2 (0*3+2)*1 = cell 21 0 (1*3+0)*1 = cell 31 1 (1*3+1)*1 = cell 41 2 (1*3+2)*1 = cell 52 0 (2*3+0)*1 = cell 62 1 (2*3+1)*1 = cell 72 2 (2*3+2)*1 = cell 8

Here we want to access each cell sequentially, so the dataoffset is one. The image data has other data in the image data array that we dont need, so we have to skip that data. Thats the purpose of the offset. The BMP image format has the RGB value every third cell, so you need to muliply by 3 instead of 1 to jump to the next value.

eg. R is our RGB value, a and b represent other data. We want only cells 0,3,6,9 etc

 0 1 2 3 4 5 6 7 8 9 -------------------|R|a|b|R|a|b|R|a|b|R| -------------------

so the formula for us now would be (z * GridWitdth + x ) * 3, which would give the following:

z x       y0 0 (0*3+0)*3 = cell 00 1 (0*3+1)*3 = cell 3 0 2 (0*3+2)*3 = cell 6 1 0 (1*3+0)*3 = cell 9  1 1 (1*3+1)*3 = cell 12and so on

Hope that helps.

F451

[edited by - Fahrenheit451 on June 5, 2004 12:06:58 AM]

1. 1
Rutin
67
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• Forum Statistics

• Total Topics
633420
• Total Posts
3011790
• Who's Online (See full list)

There are no registered users currently online

×