PDA

View Full Version : heightmap



grimoire
08-09-2004, 02:25 PM
I am trying to make a heightmap program, but I can't seem to acquire the pixel data from a bitmap with SDL, I keep getting a seg fault; I know it is in the SDL_GetRGB function, but I don't know why?
int tl(void){
int status=0;
SDL_Surface *surface;
Uint32 *ptr,color;
Uint8 *r=NULL,*b=NULL,*g=NULL;
if((surface=SDL_LoadBMP("/home/michael/temp/lesson-6/lesson06/data/true.
bmp"))){
status=1;
glGenTextures(1,&texture[0]);
glBindTexture(GL_TEXTURE_2D,texture[0]);
glTexImage2D(GL_TEXTURE_2D,0,1,surface->w,surface->h,0,GL_BGR,GL
_UNSIGNED_BYTE,surface->pixels);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL _REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_NEAREST);
}
if(surface)
SDL_FreeSurface(surface);
ptr=((Uint32 *)surface->pixels+(1%surface->h)*(surface->pitch/4)+(1%surf
ace->w));
color=*ptr;
SDL_GetRGB(color,surface->format,&r,&g,&b);
return status;
}

08-09-2004, 04:07 PM
great scott! why are you freeing the surface before you access it? ive never used SDL, but ill bet this is a nono ;)

grimoire
08-09-2004, 05:06 PM
I thought that would be it when you told me, but it isn't. I commented it out and recompiled. I also changed SDL_GetRGB here is the new(not working) code
int tl(void){
int status=0;
SDL_Surface *surface;
Uint32 *ptr,color;
Uint8 *r=NULL,*b=NULL,*g=NULL;
if((surface=SDL_LoadBMP("/home/michael/temp/lesson-6/lesson06/data/true.
bmp"))){
status=1;
glGenTextures(1,&texture[0]);
glBindTexture(GL_TEXTURE_2D,texture[0]);
glTexImage2D(GL_TEXTURE_2D,0,1,surface->w,surface->h,0,GL_BGR,GL
_UNSIGNED_BYTE,surface->pixels);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL _REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_NEAREST);
}
ptr=((Uint32 *)surface->pixels+(1%surface->h)*(surface->pitch/4)+(1%surf
ace->w));
color=*ptr;
SDL_GetRGB(color,surface->format,&r,&g,&b);
return status;
/*remember to free when you get it working*/
}

08-09-2004, 05:26 PM
well, you have a few potential problems.

first, you test the condition of surface load success but then use the resulting surface regardless. maybe SDL returns a surface even on failure - dont know.

second, does SDL return a rgb image packed? a uint32 datum is 4 bytes wide, not 3. casting the pixels to unit32 and offsetting could exceed the array bounds.

third, maybe you could clarify the actual offset your calculating. better notation, so its clear

byte* color;

color = (byte*)pixels + (y*width + x)*3 for rgb

or

color = (byte*)pixels + (y*width + x)*4 for rgba

byte r = color[0];
byte g = color[1];
byte b = color[2];

you get the idea.