Results 1 to 3 of 3

Thread: Why is 2D view slightly off

  1. #1
    Senior Member Regular Contributor
    Join Date
    Jun 2006
    Location
    Edinburgh - Scotland
    Posts
    146

    Why is 2D view slightly off

    Stupid newbie problem. I have a 2D view (a map) which I set up with want positioned on the screen at bottomLeftX,bottomLeftY and Width,Height in size so I use
    Code :
    glViewport(blx,bly,wdt,hgt)

    I set up my projection matrix with
    Code :
     GLdouble originX = wdt/2 + blx;
     GLdouble originY = hgt/2 + bly;
     wdt *= zoom;
     hgt *= zoom;
     blx = originX-(wdt/2);
     bly = originY-(hgt/2);
     glOrtho(blx,wdt, bly,hgt, -1,1);
    With my simple settings of blx=0,bly=0, wdt=640,hgt=480 camera at -320,0,0 the object at 0,0,0 the object isn't centered in my 640x480 window (it very nearly is) and it moves slightly left as I zoom out.

    Everything works with 3D view (using gluPerspective), including zoom. What I don't get is why in 2D I need my camera at -320. Any pointers on where to look would be great.

    Thanks.

  2. #2
    Senior Member Frequent Contributor scratt's Avatar
    Join Date
    May 2008
    Location
    Thailand
    Posts
    555

    Re: Why is 2D view slightly off

    This is what I use for pixel perfect alignment in FBOs, when drawing to them in 2D for HUDs and procedural texture atlases.

    Code :
    	glViewport(0, 0, m_nTextureSize, m_nTextureSize);
    		glMatrixMode(GL_PROJECTION);
    		glPushMatrix();
    		glLoadIdentity();
    		glOrthof(0, m_nTextureSize, 0, m_nTextureSize, 0, 1);
    		glMatrixMode(GL_MODELVIEW);
    		glPushMatrix();
    		glLoadIdentity();
     
    		glTranslatef(0.5, 0.5, 0);

    You may or may not need the translate at the end, but that is to center the pixels on their respective coordinate.

    You will need to adjust the parameters in glViewport and glOrtho to reflect your offset into the screen, and the width / height of your square.

    Once you've done that you can draw your map using a QUAD with 2D coordinates and x,y coordinates which reflect it's width and height one to one.

  3. #3
    Senior Member Regular Contributor
    Join Date
    Jun 2006
    Location
    Edinburgh - Scotland
    Posts
    146

    Re: Why is 2D view slightly off

    The code you listed is the same as mine for the simple case when there is no zoom, but with zoom my blx,bly are wrong since I obviously don't understand glOrtho For the simple case (full screen, zoom in center) I need
    Code :
     GLdouble  wdt = screenWdt*zoom;
     Gldouble  hgt = screenHgt*zoom;
     blx = screenWdt - wdt;
     bly = screenHgt - hgt;
     glOrtho(blx,wdt, bly,hgt, -1,1);
    I'm now trying get it to work with a "sub-window" i.e. a non-full screen area at an arbitrary position...

Similar Threads

  1. Slightly more C++ oriented but...
    By Sythe Dragon in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 03-25-2010, 10:09 AM
  2. slightly off.. diagram suggestion
    By dronus in forum OpenGL: General
    Replies: 0
    Last Post: 11-09-2005, 12:28 PM
  3. Slightly OT: Where is Romka & GLF?
    By marcus256 in forum OpenGL: Advanced Coding
    Replies: 5
    Last Post: 08-20-2003, 08:23 AM
  4. Need suggestions... (might be slightly OT)
    By NitroGL in forum OpenGL: Advanced Coding
    Replies: 15
    Last Post: 06-25-2003, 08:55 AM
  5. Display-lists (getting slightly mad :-)
    By Schleppi in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 05-21-2001, 02:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean