2
\$\begingroup\$

I am working in OpenGL, and I am trying to create terrains using height maps.

I am using my own functions to load a TGA image, and in order to pass data to heightmap vertex shader I need to retrieve RGB components from the TGA image.

I am not quite sure how to do that, I saw in one tutorial that SDL library can be used to retrieve RGB component from a TGA image, but I do not want to use any such libraries because I am already able to load a TGA.

So, how do I recover RGB information per pixel from a TGA that we loaded?

\$\endgroup\$
8
  • 2
    \$\begingroup\$ You appear to have at least three questions here. How to generate the data for terrain, how to access RGB from your custom loaded TGA files and other ways of generating terrain. Please focus on a single question per post and clarify exactly what you're having trouble with. \$\endgroup\$
    – House
    Commented Nov 13, 2013 at 17:26
  • 1
    \$\begingroup\$ Don't hesitate posing three questions, by the way. \$\endgroup\$
    – danijar
    Commented Nov 13, 2013 at 19:49
  • \$\begingroup\$ fair enough :) changed the question. its more precise now. \$\endgroup\$
    – 2am
    Commented Nov 14, 2013 at 6:57
  • 2
    \$\begingroup\$ Are you implementing your TGA loading logic now, or have you already written that? If you've already got TGA loading, you should have access to the RGB data (by definition) \$\endgroup\$
    – ThorinII
    Commented Nov 14, 2013 at 7:10
  • 2
    \$\begingroup\$ What do you get out of your loading function? A byte array? Or some pixmap/texture? What format is it in? \$\endgroup\$
    – ThorinII
    Commented Nov 14, 2013 at 8:40

1 Answer 1

2
\$\begingroup\$

This should be very straightforward, since you are already loading the TGA yourself, and assuming you only want to deal with true-color TGA files (the TGA file format allows for alternative color map types; true-color is the one where pixel colors are stored directly, without a palette or anything).

TGA files have an 18-byte header, after which is an optional image ID string (usually not present). Palette data follows the ID string, and may exists even in a true-color image. After the palette data is the RGB pixel data you are interested in.

All of this should be old-hat if you have actually written the loader, because you'd need to account for all of this to get to the pixel data that you're loading into your textures.

The rest of the file is either raw or RLE RGB pixel data. The TGA format stores the blue component in the least-significant byte of the pixel, followed by the green byte, the red byte and finally the alpha byte (if present).

Consequently, you can read the RGB color data by simply modifying or reusing the portion of your RGA loader code that finds the offset of the pixel data, and then reading the bytes linearly from the file (using the information in the header to know how many bytes are in the file total).

More information about the TGA file format is available here.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .