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?
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_FILTER,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_FILTER,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*/
}
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];