# Generate texture for a heightmap

I've recently been trying to blend multiple textures based on the height at different points in a heightmap. However i've been getting poor results. I decided to backtrack and just attempt to recreate one single texture from an SDL_Surface (i'm using SDL) and just send that into opengl. I'll put my code for creating the texture and reading the colour values. It is a 24bit TGA i'm loading, and i've confirmed that the rest of my code works because i was able to send the surfaces pixels directly to my createTextureFromData function and it drew fine.

struct RGBColour
{
RGBColour() : r(0), g(0), b(0) {}
RGBColour(unsigned char red, unsigned char green, unsigned char blue) : r(red), g(green), b(blue) {}

unsigned char r;
unsigned char g;
unsigned char b;
};

// new texture

for (int y = 0; y < reader->m_surface->h; y++)
{
for (int x = 0; x < reader->m_surface->w; x += 3)
{
int index = (y * reader->m_surface->w) + x;

newTexture[index] = colour.r;
newTexture[index + 1] = colour.g;
newTexture[index + 2] = colour.b;
}
}

{
Uint32 pixel;
Uint8 red, green, blue;
RGBColour rgb;

pixel = getPixel(m_surface, x, y);

SDL_LockSurface(m_surface);

SDL_GetRGB(pixel, m_surface->format, &red, &green, &blue);

SDL_UnlockSurface(m_surface);

rgb.r = red;
rgb.b = blue;
rgb.g = green;

return rgb;
}

// this function taken from SDL documentation
// http://www.libsdl.org/cgi/docwiki.cgi/Introduction_to_SDL_Video#getpixel
Uint32 SDLSurfaceReader::getPixel(SDL_Surface* surface, int x, int y)
{
int bpp = m_surface->format->BytesPerPixel;

Uint8 *p = (Uint8*)m_surface->pixels + y * m_surface->pitch + x * bpp;

switch (bpp)
{
case 1:
return *p;
case 2:
return *(Uint16*)p;
case 3:
if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32*)p;
default:
return 0;
}
}


I've been stumped at this, and I need help badly! Thanks so much for any advice.

• What precisely isn't working here? – Laurent Couvidou Apr 20 '12 at 14:26
• The code compiles fine, but when i'm applying this texture, i'm getting weird results. This is a screenshot of what i'm getting i.imgur.com/N6NFi.png and this is what it should be i.imgur.com/V6x0p.jpg – James Apr 20 '12 at 14:32
• As you can see its drawing the whole texture in 3 spots and only in 1/3 of the terrain. I dont know where i'm going wrong in my loading. – James Apr 20 '12 at 14:34