Results 1 to 6 of 6

Thread: Drawing part of SDL surface to OpenGL texture?

  1. #1

    Drawing part of SDL surface to OpenGL texture?

    The standard SDL blitting has become too slow for the game I'm currently programming, so I've looked into using OpenGL for drawing to the screen. I've managed to make a texture out of an SDL surface (a .png sprite map) and I've also figured out how to draw this texture to the screen with alpha channel and everything. However, as the SDL surface is a sprite map, I don't want to draw the entire texture to the screen. How do I draw only the part of it that contains the relevant sprite?

    I'm using this to draw the texture to the screen:

    Code:
    glBegin( GL_QUADS );
        //Top-left vertex (corner)
    	glTexCoord2i( 0, 0 );
    	glVertex3f( 0, 0, 0.0 );
    
    	//Bottom-left vertex (corner)
    	glTexCoord2i( 1, 0 );
    	glVertex3f( 512, 0, 0 );
    
    	//Bottom-right vertex (corner)
    	glTexCoord2i( 1, 1 );
    	glVertex3f( 512, 512, 0 );
    
    	//Top-right vertex (corner)
    	glTexCoord2i( 0, 1 );
    	glVertex3f( 0, 512, 0 );
    glEnd();
    SDL_GL_SwapBuffers();
    If I change any of these values, it just magnifies or shrinks the texture, which makes sense. I was thinking that I can just load the SDL surface spritemap into multiple textures, each holding a separate sprite, but then the textures wouldn't be a power of 2 size. So, how do I go about using my spritmap?
    Laserbrain Studios - Currently developing Hidden Asset!
    Ascii Sector
    - Real-time roguelike in space!

  2. #2

    Re: Drawing part of SDL surface to OpenGL texture?

    Try using glTexCoord2f() instead of glTexCoord2i() and give them value's between 0.0 and 1.0. Like this:

    [pascal]
    glBegin( GL_QUADS );
    //Top-left vertex (corner)
    glTexCoord2f( 0, 0 );
    glVertex3f( 0, 0, 0.0 );

    //Bottom-left vertex (corner)
    glTexCoord2f( 0.25, 0 );
    glVertex3f( 512, 0, 0 );

    //Bottom-right vertex (corner)
    glTexCoord2f( 0.25, 0.25 );
    glVertex3f( 512, 512, 0 );

    //Top-right vertex (corner)
    glTexCoord2f( 0, 0.25 );
    glVertex3f( 0, 512, 0 );
    glEnd();
    SDL_GL_SwapBuffers();
    [/pascal]

    This should display 1/4th of the texture. Hope this helps.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  3. #3

    Re: Drawing part of SDL surface to OpenGL texture?

    That certainly makes it possible for spritemaps with only a few sprites and where the pixel coordinates of the sprites can be converted to simple decimal points. But I have spritemaps with lots of sprites of various sizes on them and it would be a mess to calculate the decimal values for their positions. Isn't it possible to use the exact pixel coordinates instead, like it is when blitting with SDL? It would definitely make things a lot easier...
    Laserbrain Studios - Currently developing Hidden Asset!
    Ascii Sector
    - Real-time roguelike in space!

  4. #4
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Re: Drawing part of SDL surface to OpenGL texture?

    I use JEDI-SDL with OpenGL for my graphics myself and what I do is make a small function to do these calculations for me. Then I make individual functions for grabbing my segment of texture using simple x,y co-ordinates.

    The trick is to make functions that will do all the labour intensive stuff for you.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  5. #5

    Re: Drawing part of SDL surface to OpenGL texture?

    It's just:

    [pascal]
    TTextureTile = record
    u1, v1, u2, v2: Single;
    end;

    TSpriteTile = record
    x, y, width, height: Integer;
    end;

    function SpriteTileToTextureTile(ST: TSpriteTile; fTexWidth, fTexHeight: Integer): TTextureTile;
    begin
    Result.u1 := ST.x / fTexWidth;
    Result.v1 := ST.y / fTexHeight;
    Result.u2 := (ST.x + ST.width) / fTexWidth;
    Result.v2 := (ST.y + ST.height) / fTexHeight;
    end;
    [/pascal]

    Then just pass the data in TTextureTile to openGL and you're done.

    [pascal]
    var TT: TTextureTile;

    ....

    glTexCoord2f( TT.u1, TT.v1 );
    glVertex3f( 0, 0, 0.0 );

    glTexCoord2f( TT.u2, TT.v1 );
    glVertex3f( 512, 0, 0 );

    glTexCoord2f( TT.u2, TT.v2 );
    glVertex3f( 512, 512, 0 );

    glTexCoord2f( TT.u1, TT.v2 );
    glVertex3f( 0, 512, 0 );
    [/pascal]

    Hope this helps.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  6. #6

    Re: Drawing part of SDL surface to OpenGL texture?

    It sure did. I'll try it out. Thanks to both of you!
    Laserbrain Studios - Currently developing Hidden Asset!
    Ascii Sector
    - Real-time roguelike in space!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •