Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

Thread: 2D rendering in OpenGL

  1. #11

    2D rendering in OpenGL

    Actually, opengl implementations are free to render quads as they like, that includes the most often case: splitting it into 2 triangles, so in most cases, like 99.9% of opengl implentations, your quads will end up as two triangles.
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

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

    2D rendering in OpenGL

    One thing that I've noticed OpenGL has issues with is drawing line specific to the pixel.

    This function should draw a line exactly from X1,Y1 to X2,Y2 this would include the pixels at the two points I specified. Unfortunately this does not happen. I think it may, in part have to do with the last position sent to OpenGL and when it changes states for the next drawing.

    [pascal]procedure DrawLine(X1, Y1, X2, Y2: Integer; ColorR, ColorG, ColorB: GLfloat);
    begin
    glDisable(GL_TEXTURE_2D); //Disable Textures, as we're drawing lines
    glColor3f(ColorR, ColorG, ColorB); //Set the new color!

    BeginOrtho; //Switch to 2D mode
    glPushMatrix; //Save Current Matrix
    glTranslatef(X1, Y1, 0);
    glBegin(GL_LINES); //Draw Using Lines
    glVertex2f(0, 0);
    glVertex2f(X2 - X1, Y2 - Y1);
    glEnd;
    glPopMatrix; //Reload Old Matrix
    EndOrtho;
    end;[/pascal]

    Yes, it uses the same methods as Kas posted above. [size=9px](same codebase after all )[/size]
    Jason McMillen
    Pascal Game Development
    Co-Founder





  3. #13

    2D rendering in OpenGL

    btw, I don't think you want to be doing BeginOrtho and EndOrtho all over the shop, if your whole game is in 2D. Just do it at start up and leave it be. If I'm wrong I'm sure some OpenGL gurus will correct me.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  4. #14

    2D rendering in OpenGL

    Quote Originally Posted by savage
    btw, I don't think you want to be doing BeginOrtho and EndOrtho all over the shop, if your whole game is in 2D. Just do it at start up and leave it be. If I'm wrong I'm sure some OpenGL gurus will correct me.
    Yep, that's the idea, I tried to keep the demo simple to understand, so I set it up like that.

    Though BeginOrtho really isn't that expensive, I call these functions multiple times a frame, for GUIs, HUDs,Billboards, Particles, etc etc in between 3D rendering. So either way it should be fine .

  5. #15

    2D rendering in OpenGL

    I dont think its a really bad thing to do, as fps games have for instance a 3d scene with a 2d hud. But, if its allround 2d, it's probably better to call it once per loop.

    edit: thatt'll teach me to leave new topic windows open and wait another 20 mins for an answer :-)

  6. #16

    2D rendering in OpenGL

    Now if someone only knew how to use SDL surfaces on OpenGL surfaces . At least every time I've tried it didn't work .

  7. #17

    2D rendering in OpenGL

    Quote Originally Posted by jdarling
    Now if someone only knew how to use SDL surfaces on OpenGL surfaces . At least every time I've tried it didn't work .
    Lol, I don't even think that's possible, is it? and would it be worth it?

  8. #18

    2D rendering in OpenGL

    Quote Originally Posted by WILL
    One thing that I've noticed OpenGL has issues with is drawing line specific to the pixel.

    This function should draw a line exactly from X1,Y1 to X2,Y2 this would include the pixels at the two points I specified. Unfortunately this does not happen. I think it may, in part have to do with the last position sent to OpenGL and when it changes states for the next drawing.
    all 2d switching code above will when sent coordinates set them pixel perfect, the only real problem with 2D mode is, when somebody has set forced anti-aliasing in the gfx driver and you are drawing lines without texture. What you get is NOT what you want in that case, you will for example see serious off by one coordinates on ATI driver platforms...
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  9. #19

    2D rendering in OpenGL

    Quote Originally Posted by jdarling
    Now if someone only knew how to use SDL surfaces on OpenGL surfaces . At least every time I've tried it didn't work .
    This is how I would use SDL surfaces with OpenGL.

    I. reading from buffer to SDL_Surface
    - create sdl surface 24 or 32 bpp (or convert existing one) depending if you want alpha or not
    - call glReadPixels
    example:
    [pascal]
    surface := SDL_CreateRGBSurface(SDL_SWSURFACE,w,h,32,rmask,gm ask,bmask,amask);
    glReadPixels(x,y,w,h,GL_RGBA,GL_UNSIGNED_BYTE,surf ace^.pixels);
    [/pascal]

    II. SDL_Surface to OpenGL texture
    - Again, load/create surface and convert to 24 or 32 bpp
    - create openGL texture from surface's data for example like this
    [pascal]
    function MakeTexture(surf : PSDL_Surface) : gluint;
    var
    texID,tex_fmt : gluint;
    begin
    glGenTextures( 1, @texID );
    glBindTexture( GL_TEXTURE_2D, TexID );
    //setup some texture parameters
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );

    if surf^.format.bitPerPixel = 32 then
    tex_fmt := GL_RGBA
    else
    tex_fmt := GL_RGB;

    glTexImage2D( GL_TEXTURE_2D,
    0,
    tex_fmt,
    surf^.w, surf^.h,
    0,
    tex_fmt,
    GL_UNSIGNED_BYTE,
    surf^.pixels );
    Result := texID;
    end;
    [/pascal]

    III. Read OpenGL texture to SDL_Surface
    - As always create/convert to proper pixel format
    - call glGetTexImage

    IV. Copy SDL surface directly to OpenGL color buffer (very slow)
    - Prepare surface
    - call glDrawPixels

    I hope it helps.

  10. #20

    2D rendering in OpenGL

    I can't remember if it was mentioned on this thread or another JEDI-SDL thread, but the SDL_ttf 3D demo uses a PSDL_Surface to render some text over a spinning 3d cube. Have a look at it.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

Page 2 of 2 FirstFirst 12

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
  •