Results 1 to 5 of 5

Thread: Screen -> World: Works only with orthographic

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2008
    Posts
    12

    Screen -> World: Works only with orthographic

    I'm writing a routine to intersect the line from eye-point to the point on the screen with a plane (a plane is given in the world's coordinate system).

    Here's the pseudocode:
    Code :
    Vector3d
    intersect(windowX, windowY, plane) {
        Line line = new Line(
                        project(modelview, windowX, windowY, 3),
                        project(modelview, windowX, windowY, 10)
                    );
        return line.intersect(plane);
    }

    The code that implements this pseudocode works correctly with orthographic projection, but returns incorrect results with perspective projection.

    Here's the pseudocode of project routine:
    Code :
    Vector3d
    project(modelview, winX, winY, winZ) {
        double[] pos = new double[3];   
        gluUnProject(winX, viewport[3] - winY, winZ, modelview,
            projection, viewport, pos);
        return pos;
    }

    Please, can anyone explain what I am doing wrong?

  2. #2
    Senior Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    Re: Screen -> World: Works only with orthographic


    The second parameter for gluUnProject should be viewport[3] - winY - 1, but this is just a one pixel distance error.

    I notice that you pass only the modelview matrix for your project function. I think you don't need to pass it because you can call getDoublev(GL_MODELVIEW_MATRIX,...) inside your function.

    In my unProject function I do something like this
    Code :
      GLdouble proj[16];
      GLdouble model[16]; 
      GLint view[4];
     
      glGetDoublev(GL_MODELVIEW_MATRIX,model);
      glGetDoublev(GL_PROJECTION_MATRIX,proj);
      glGetIntegerv(GL_VIEWPORT,view);
      y = height - 1 - y;
      gluUnProject((GLdouble)x,(GLdouble)y,z,model,proj,view,
    	       &objx,&objy,&objz);

  3. #3
    Junior Member Newbie
    Join Date
    Aug 2008
    Posts
    12

    Re: Screen -> World: Works only with orthographic

    1) Are you sure about this -1 thing? I changed it, but it didn't fix the problem
    2) The current modelview matrix is simply not the one I need (there were some pushs and pops during rendering)
    3) Everything works fine without perspective, it gets worse only when I turn on the perspective..

  4. #4
    Senior Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    Re: Screen -> World: Works only with orthographic

    1) You need to subtract -1 because if the height of your viewport is 480, the range of y is [0 479].

    2) I think you need the most recent modelview and projection matrix when you unproject a screen coord to object coord.

    3) My guess is when you are in orthographic projection, if you get the modelview matrix and then you translate forward or backward, the result is correct because all line are parallel. But this is not the case in perspective view, so the first point of the line is very important.

    Edit:

    project(modelview, windowX, windowY, 3),
    project(modelview, windowX, windowY, 10)

    vs

    project(modelview, windowX, windowY, 0),
    project(modelview, windowX, windowY, 10)

    will have probably the same result in orthographic view,
    but not the same in perspective view.

  5. #5
    Junior Member Newbie
    Join Date
    Aug 2008
    Posts
    12

    Re: Screen -> World: Works only with orthographic

    trinitrotoluene

    1) Yes, you are right Thanks
    2) No, you need to send the modelview matrix with which the object was drawn
    3)

    Yes, the problem is actually that
    Line (
    project(modelview, windowX, windowY, 0),
    project(modelview, windowX, windowY, 10)
    )
    and
    Line (
    project(modelview, windowX, windowY, 3),
    project(modelview, windowX, windowY, 10)
    )
    are different

    Does anyone by any chance has an idea on how to solve this problem?

    project(modelview, windowX, windowY, 3),
    project(modelview, windowX, windowY, 10)

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2015, 05:43 PM
  2. Replies: 2
    Last Post: 06-02-2012, 08:10 AM
  3. Rotating around a World Space Axis - How Works?
    By subkey in forum OpenGL: General
    Replies: 2
    Last Post: 04-22-2012, 01:11 PM
  4. Screen to World
    By BobK in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 10-11-2002, 01:07 PM
  5. world-to-screen
    By playmesumch00ns in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 01-31-2002, 10:47 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