   SDL texture to OpenGL texture
  Hey. I have code that loads an SDL surface and then uses it to create an OpenGL texture (the latter was taken from here:Surface Surface_Cache::Load_SDL_Surface(const std::string& file_name, bool use_colour_key){std::string fn = Directory::abs_path(file_name);SDL_Surface* loaded = NULL;SDL_Surface* optimized = NULL;Uint32 map_colour_key = 0;loaded = IMG_Load(fn.c_str());if(loaded == NULL && !fn.empty()){throw LRE_Exception("Surface at: " + fn + " failed to load.\nSDL: " + SDL_GetError());}optimized = SDL_DisplayFormat(loaded);SDL_FreeSurface(loaded);if(optimized == NULL){throw LRE_Exception("Surface at: " + fn + " failed to optimise.\nSDL: " + SDL_GetError());}if(use_colour_key){// Create the colour key.map_colour_key = SDL_MapRGB(optimized->format, Settings::Get().Def_Colour_Key().r,Settings::Get().Def_Colour_Key().g, Settings::Get().Def_Colour_Key().b);// Sets the colour key to the Surface.SDL_SetColorKey(optimized, SDL_RLEACCEL | SDL_SRCCOLORKEY, map_colour_key);}    Surface surface = Create_Surface(optimized, file_name, use_colour_key);    cache.insert(std::make_pair(file_name, surface));LOG("Surface at " + file_name + " loaded.");return surface;}int power_of_two(int input){    int value = 1;    while(value < input)    {   value <<= 1;    }    return value;}Surface Surface_Cache::Create_Surface(SDL_Surface* sdl_surface,const std::string& file_name, bool is_colour_key){    // GL needs power of two sizes for textures.    int w = power_of_two(sdl_surface->w);    int h = power_of_two(sdl_surface->h);    //texcoord[0] = 0.0f; // min x    //texcoord[1] = 0.0f; // min y    //texcoord[2] = (GLfloat)sdl_surface->w / w; // max x    //texcoord[3] = (GLfloat)sdl_surface->h / h; // max y    SDL_Surface* image = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 24,#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */  0x000000FF,  0x0000FF00,  0x00FF0000,  0xFF000000#else  0xFF000000,  0x00FF0000,  0x0000FF00,  0x000000FF#endif    );    if(image == NULL)    {   throw LRE_Exception(std::string(__FUNCTION__) + "failed.\nSDL: " + SDL_GetError());    }    // Save the alpha blending attributes.    Uint32 saved_flags = sdl_surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);    Uint8 saved_alpha = sdl_surface->format->alpha;    if((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)    {   SDL_SetAlpha(sdl_surface, 0, 0);    }    /* Copy the surface into the GL texture image. */    SDL_Rect area = {0, 0, sdl_surface->w, sdl_surface->h};    SDL_BlitSurface(sdl_surface, &area, image, &area);    /* Restore the alpha blending attributes */    if((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)    {   SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);    }    /* Create an OpenGL texture for the image */    GLuint texture = 0;    glGenTextures(1, &texture);    glBindTexture(GL_TEXTURE_2D, texture);    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);    SDL_FreeSurface(image);    return Surface(sdl_surface, file_name, is_colour_key, texture, Point(w, h));}The SDL surface loads fine and execution in Create_Surface runs until glTexImage2D at which point I get an exception:Quote:Unhandled exception at 0x01d804c8 in Checkers_SDL_OpenGL.exe: 0xC0000005: Access violation reading location 0x065a1000.I'm curious why I'm getting this exception. The image (SDL_Surface*) has what seems like a valid address and a call to glGetError returns 0...Cheers.   
  I only skimmed the code, but it looks like you're requesting a 24-bit surface from SDL, but then submitting it to OpenGL as a 32-bit surface (GL_RGBA, GL_UNSIGNED_BYTE). I'm not sure if this is the problem, but it might be worth taking a look at.   ;  Hey jyk.I tried changing it to 32 bit surface:SDL_Surface* image = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */  0x000000FF,  0x0000FF00,  0x00FF0000,  0xFF000000#else  0xFF000000,  0x00FF0000,  0x0000FF00,  0x000000FF#endif    );glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);But it's just a white screen. Here is my drawing code:glBindTexture(GL_TEXTURE_2D, 1);    float w = 640, h = 480;glBegin(GL_QUADS);{glColor3d(1, 1, 1);   glTexCoord2f(0, 0);glVertex2f(0, 0);glTexCoord2f(w / 1024, 0);glVertex2f(w, 0);glTexCoord2f(0, h / 512);glVertex2f(w, h);glTexCoord2f(w / 1024, h / 512);glVertex2f(0, h);}glEnd();The divides are in there due to the whole power-of-two texture dimensions requirement of OpenGL.I need some form of colour keying though, so I need it to be RGBA. I am using SDL's colour keying because I don't think transparent PNGs work with SDL.Cheers.   ;Quote:Original post by MybowlcutBut it's just a white screen.White textures usually means the texture object is invalid. Could be due to invalid parameters, but also (SURPRISE!) because you tried to create it before the rendering context. Use glGetError to see if there are any error, and locate them if there are.Another thing that bothers me is that you bind texture ID 1 in the drawing function. You should bind the ID returned by glGenTextures. If the name generated is not 1, you will bind an unused texture ID, which becomes an invalid texture object at that point.Quote:Original post by MybowlcutThe divides are in there due to the whole power-of-two texture dimensions requirement of OpenGL.That requirement was completely removed in OpenGL 2.0, released almost 5 years ago in 2004.   ;Quote:Original post by Brother BobQuote:Original post by MybowlcutBut it's just a white screen.White textures usually means the texture object is invalid. Could be due to invalid parameters, but also (SURPRISE!) because you tried to create it before the rendering context. Use glGetError to see if there are any error, and locate them if there are.Another thing that bothers me is that you bind texture ID 1 in the drawing function. You should bind the ID returned by glGenTextures. If the name generated is not 1, you will bind an unused texture ID, which becomes an invalid texture object at that point.Quote:Original post by MybowlcutThe divides are in there due to the whole power-of-two texture dimensions requirement of OpenGL.That requirement was completely removed in OpenGL 2.0, released almost 5 years ago in 2004.I'm not creating it before the rendering context.I have changed my code to this:glBindTexture(GL_TEXTURE_2D, s.Get_GL_Tex_ID());   float w = window_w, h = window_h;glBegin(GL_QUADS);{ // Draw quad clockwise.glColor3d(1, 1, 1);glTexCoord2f(0, 1);glVertex2f(0, 0);glTexCoord2f(1, 1);glVertex2f(w, 0);glTexCoord2f(1, 0);glVertex2f(w, h);glTexCoord2f(0, 0);glVertex2f(0, h);  /*glTexCoord2f(0, 0);glVertex2f(0, 0);glTexCoord2f(640 / 1024, 0);glVertex2f(w, 0);glTexCoord2f(0, 480 / 512);glVertex2f(w, h);glTexCoord2f(640 / 1024, 480 / 512);glVertex2f(0, h);*/}glEnd();It now draws the texture but like this (the texture should be covering the whole screen as it is 640x480):I think the code that is commented out was drawing it in the wrong position and/or too larqe, because I debugged it and (although it's not a good way to do it) the texture id was 1.The code that I based mine off mentions that the texture will be upside down but can be remedied by using glOrtho... although I'm not really sure I understand that.As for the power of two requirement being removed, if I don't load the texture with the dimensions as power of two, it crashes. Drawing the texture using the divisions I guess is wrong since it was drawing it funny... but as it stands now it's not drawing it correctly so I'm unsure what to do.Cheers.   ;  Perhaps you could post the code where you set up the viewport and the projection transform.   ;  Maybe this will help you? It worked for me, when I tried it months ago.   ;  Thanks for that, Servant of the Lord. It has flipped the image the right way around. I also changed the drawing code and the image looks like a better size, but it's still too small. So, I'm guessing the divisions are the issue.Here is all the code, jyk:int main(int argc, char* args[]){try{if(SDL_Init(SDL_INIT_EVERYTHING) == -1){throw LRE_Exception(SDL_GetError());}   int window_w = 640, window_h = 480;   SDL_Surface* screen = SDL_SetVideoMode(window_w, window_h, 24, SDL_OPENGL);    if(screen == NULL)    {    return 0;    }glClearColor(0.0,0.0,0.0,1.0);// background colour    glClearDepth(1.0f);// depth buffer    glEnable(GL_DEPTH_TEST);// (z-buffer) depth testing    glDepthFunc(GL_LEQUAL);// type of depth testing    glShadeModel(GL_SMOOTH);// enables smooth shading    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);   glEnable(GL_TEXTURE_2D);   // enables textures   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);   /* This will make GL co-ords map to screen pixel co-ords. */   glViewport(0, 0, window_w, window_h);   glMatrixMode(GL_PROJECTION);   glLoadIdentity();   // Want origin at top-left.   glOrtho(0, window_w, window_h, 0, -1, 1);  glMatrixMode(GL_MODELVIEW);   glLoadIdentity();   // Clear the screen of old crap.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   // Reset the view   glLoadIdentity();   // Account for line/point thingy.   glTranslated(0.375, 0.375, 0);   // Fixes flipped & mirrored texture issue.   glMatrixMode(GL_TEXTURE);   glRotatef(180.0f,0.0f,0.0f,1.0f);   glScalef(-1.0f,1.0f,1.0f);   glMatrixMode(GL_MODELVIEW);  //SDL_WM_SetCaption("wwweeeee", NULL);glBegin(GL_LINES);   glVertex2d(0, 0);glVertex2d(.5, -.5);   glEnd();Settings::Initialise("settings.xml");Logger::Initialise(Settings::Get().Logger_Fn());// Initialise the AudioSDL_Audio_Player::Initialise();Surface_Cache::Initialise();Surface s = Surface_Cache::Get().Surface_At("Images\\i_menu.png", false);   glBindTexture(GL_TEXTURE_2D, s.Get_GL_Tex_ID());   float w = window_w, h = window_h;glBegin(GL_QUADS);{ // Draw quad clockwise./*glColor3d(1, 1, 1);glTexCoord2f(0, 1);glVertex2f(0, 0);glTexCoord2f(1, 1);glVertex2f(w, 0);glTexCoord2f(1, 0);glVertex2f(w, h);glTexCoord2f(0, 0);glVertex2f(0, h);*/    glTexCoord2f(0, h / 512);glVertex2f(0, 0);glTexCoord2f(w / 1024, h / 512);glVertex2f(w, 0);glTexCoord2f(w / 1024, 0);glVertex2f(w, h);glTexCoord2f(0, 0);glVertex2f(0, h);}glEnd();   SDL_GL_SwapBuffers();   // Clear the screen of old crap.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   // Reset the view   glLoadIdentity();SDL_Event event_;bool quit = false;   while(!quit){SDL_PollEvent(&event_);quit = event_.type == SDL_QUIT;}}catch(const std::exception& r){std::cout << "Exception caught: " << r.what() << std::endl;}    Surface_Cache::Clean_Up();    SDL_Quit();    return 0;}