Results 1 to 6 of 6

Thread: OpenGl Quickie

  1. #1
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45

    OpenGl Quickie

    Really stuck here; part because I never took OpenGl seriously enough, and part because this isnt all my code, but in a 2d environment, how to you convert the glvertex3f co-ordinates into pixels on a screen?

    All my verticies are glvertex3f( x, y, 0) where 0 is a constant so it stays 3d...

    Please help, drawing in Prometheus is a lot faster now, just that it doesnt draw where you want it, how you want it (yipee) I'd come across this problem before but the solution then was to sdl_blit it. Yea, its cheating but at the time performance wasnt priority 1.

    cheers,
    code_glitch.
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  2. #2
    Doing 2D in OpenGL is pretty simple, and just using glVertex3f doesn't automatically mean it's 3D.

    - Switch to orthogonal projection and use your screen size as the parameters (or a set virtual screen size if you want it to look the same everywhere)
    - Call glVertex3f with (virtual) screen coordinates like you'd draw in 2D. Use the Z-Coordinate to get free sorting

    Example :

    Code:
    glOrtho(0, 1280, 720, 0, -16, 16);
    glBegin(GL_QUADS);
     glVertex3f(100, 100, 0);
     glVertex3f(100, 300, 0);
     glVertex3f(300, 300, 0);
     glVertex3f(300, 100, 0);
    glEnd;
    // Draw another quad ontop. OpenGL will automatically sort if depth test is enabled
    glBegin(GL_QUADS);
     glVertex3f(200, 200, 1);
     glVertex3f(200, 500, 1);
     glVertex3f(500, 500, 1);
     glVertex3f(500, 200, 1);
    glEnd;

  3. #3
    It was a long time since I used OpenGL but IIRC the trick is in the projection matrix. Can't remember how (actually I never mastered the projection matrix configuration) but I think the red or the blue book have an example.

    [edit] Beaten...
    No signature provided yet.

  4. #4
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    Hmm, I've managed to sort of fix the problem. But now I have a new problem... The image is, how to put it, not garbled but..... well. check the attachment Its better but not really practical.

    To compare result is what the program looks like and Picture.png is what it should look like... Any ideas as to what I'm doing wrong? Here's my render code:

    Code:
    procedure Image.Draw(X,Y: Int64);
    var
        VxR: array [1..2] of LongInt;
        VyR: array [1..2] of LongInt;
        
        PxR: array [1..2] of Int64;
        PyR: array [1..2] of Int64;
        
    begin
        VxR[1] := 0;
        VxR[2] := Surface.W;
        VyR[1] := 0;
        VyR[2] := Surface.H;
        
        PxR[1] := X;
        PxR[2] := X + Surface.W;
        PyR[1] := Y;
        PyR[2] := Y + Surface.H;
    
        glBindTexture( GL_TEXTURE_2D, texture );
    
        glBegin( GL_QUADS );
    
        glTexCoord2i( VxR[1], VyR[2] );
        glVertex3f( PxR[1], PyR[2], 0. );
    
        glTexCoord2i( VxR[2], VyR[2] );
        glVertex3f( PxR[2], PyR[2], 0. );
    
        glTexCoord2i( VxR[2], VyR[1] );
        glVertex3f( PxR[2], PyR[1], 0. );
    
         glTexCoord2i( VxR[1], VyR[1] );
        glVertex3f( PxR[1], PyR[1], 0. );
        glEnd();
    end;
    Note that these images were originally PNG before PGD converted them to jpg....

    Edit: Almost forgot to mention: I am expecting it to go off the edge of the screen... I have a different scale procedure.
    Attached Images Attached Images
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  5. #5
    Maybe this thread can help you a bit:

    http://www.pascalgamedevelopment.com/showthread.php?6168-I-need-some-help-with-2D-drawing-libraries


    The first part is about pixel-perfect drawing.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  6. #6
    glTexCoord2i doesn't make your texture coordinates scale from 0..Width. They are still ranged from 0..1. Use glTexCoord2f with that proper range and it might solve it. Otherwise the error might be in texture loading.

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
  •