Jump to content
  • Advertisement

Archived

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

Ekim_Gram

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 y
0 0 (0*3+0)*1 = cell 0
0 1 (0*3+1)*1 = cell 1
0 2 (0*3+2)*1 = cell 2
1 0 (1*3+0)*1 = cell 3
1 1 (1*3+1)*1 = cell 4
1 2 (1*3+2)*1 = cell 5
2 0 (2*3+0)*1 = cell 6
2 1 (2*3+1)*1 = cell 7
2 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 y
0 0 (0*3+0)*3 = cell 0
0 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 12
and so on


Hope that helps.

F451

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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!