Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: gluProject issues

  1. #1

    gluProject issues

    Hello, I used the following procedure to get the screen coordinates of a point in the 3D matrix (with OpenGL)

    Code:
    // objx,objy,objz - 3D location of a point 
    // screenX, screenY - will hold screen coordinates of (objx,objy,objz) 
    procedure Matrix_To_Win(objx,objy,objz : integer;  var screenX : integer,  var screenY : integer); 
    var 
     model, proj : T16dArray;  //type defined in glu.pas 
     view : TViewportArray;    //also defined in glu.pas 
     winx,winy,winz : glDouble; 
    begin 
     glGetDoublev(GL_PROJECTION, @proj); 
     glGetDoublev(GL_MODELVIEW, @model); 
     glGetIntegerv(GL_VIEWPORT,@view); 
    
     gluProject(objx,objy,objz, proj, model, view, @winx, @winy, @winz); 
     screenX := Round(winx); 
     //since OpenGL treats lower left corner as a (0,0) winy must be 
     //transformed to normal window coordinates with (0,0) at upper left 
     //corner 
     screenY := ScreenHeight - Round(winy); 
    end;
    But for some reason, it bugs on the last two lines. I don't understand why, since all we're doing is changing the values of the variables! I thought it might have to do with the rounding or something in there, so I changed those two lines to
    Code:
    screenX:=0;
    screenY:=0;
    and it still bugged...
    Then I just commented them out and it worked fine (except that I don't get the correct output values, of course).
    I don't understand what's going on...

    Oh, and also, when objx,objy and objz are anything other than integers, i get the error
    Code:
    floating point overflow
    Isn't that weird, since coordinates should be reals or even GLdoubles ?

    Cheers

  2. #2

    gluProject issues

    I was just about to give you my tutorial on gluUnproject because of the title of the post!
    My site: DelphiTuts.com (coming soon)...

    Download Font Studio 4.21 here.

  3. #3

    gluProject issues

    Quote Originally Posted by Nitrogen
    I was just about to give you my tutorial on gluUnproject because of the title of the post!
    Oops! sorry about that! Can WILL or any other staff change that to gluProject? Cheers

    And Nitrogen, I have already read and used your gluUnproject tutorial :thumbup:

  4. #4

    gluProject issues

    I have looked in glu.pas and there is different ordering of parameters for gluProject. First should go modelview matrix (model) then projection matrix (proj). Probably this wrong ordering of matrices is causing errors.

  5. #5

    gluProject issues

    Quote Originally Posted by grudzio
    I have looked in glu.pas and there is different ordering of parameters for gluProject. First should go modelview matrix (model) then projection matrix (proj). Probably this wrong ordering of matrices is causing errors.
    Interesting! Because even swaping them around I get the same result!

    But still, my main problem is with assigning the variables at the end of the procedure. I don't understand why it bugs there. It all goes well when those two last lines are commented out, but freezes the game when they are not!

    Any ideas

  6. #6

    gluProject issues

    Can you post code showing how you use Matrix_To_Win function?

  7. #7

    gluProject issues

    Quote Originally Posted by grudzio
    Can you post code showing how you use Matrix_To_Win function?
    Ok, here is part of the procedure I call it in:

    Code:
    Procedure Where_Mouse;
    var i, a,b,c : integer;
        Winx,Winy, Winx2,Winy2 : integer;
        s,t : string;
    begin
     SetMatrix_Edge;     //Sets matrix at the top left corner of the board
                
     a:=0; b:=0; c:=0;
    
     for i:= 1 to 4 do  //to find the coordinates of a vertex around the mouse
     begin           
      Matrix_To_Win(a,0,b, WinX,WinY);   //the coordinates of the vertex we're at
    
      Matrix_To_Win(a+1,0,b, WinX2,WinY2);  //the coordinates of the 'reference' vertex (to tell direction)
      if AbsR(mpos.x-WinX)>AbsR(mpos.x-WinX2) then c:=1 //if mouse is closer to vertex a+1 than a,
       else c:=-1;
      if not (WinX=mpos.x) then    //if mouse is at that point, then don't do anything
       Repeat
        a += c ;     //else increase or decrease Xvertex depending on position of mouse relative to vertex
        Matrix_To_Win(a,0,b, WinX,WinY);
        Matrix_To_Win(a+1,0,b, WinX2,WinY2);
       Until &#40;&#40;mpos.x<WinX>=WinX2&#41;&#41; or &#40;&#40;mpos.x>=WinX&#41;and&#40;mpos.x <WinX2>100&#41; ;
    When grudzio asked about the procedure I call it in, I checked everything and added the last line
    Code:
    or &#40;Abs&#40;a&#41;>100&#41; ;
    It made sense to me that the way it was would not make it an infinite loop, but because now I can write text to the screen =)) I used it to display the value of WinX and WinY for the origin
    Code:
    Matrix_To_Win&#40;0,0,0, WinX,WinY&#41;;
    and I got WinX = 1786941367 and WinY = -10800336608 !

    So now I understand that the problem was not in the Matrix_To_Win procedure, but rather in this loop. However, it only does that because the procedure Matrix_to_Win returns such ridiculous values.
    Why does it return such values?

    EDIT: The code above didn't show up the way it should have...it looks like there was some bug here :scratch:

  8. #8

    gluProject issues

    OK, the enums passed to glGetDoublev in Matrix_To_win procedure are wrong. They should be GL_MODELVIEW_MATRIX and GL_PROJECTION_MATRIX.

  9. #9

    gluProject issues

    Quote Originally Posted by grudzio
    OK, the enums passed to glGetDoublev in Matrix_To_win procedure are wrong. They should be GL_MODELVIEW_MATRIX and GL_PROJECTION_MATRIX.
    Ok, I changed it. I get different values for WinX and WinY now, but it's still ridiculous ones...

    here is a copy of the source, if it helps you help me :
    http://www.uploadtemple.com/view.php/1172386230.zip

    (Both the procedure Matrix_To_Win and Where_Mouse are located in MainProc.pas; the procedure SetMatrix_Edge used in there is at the bottom of SmallProc.pas )

  10. #10

    gluProject issues

    Check variable declarations in Matrix_to_win. Winx, Winy and Winz should be declared as glDouble. Also procedure parameters objx, objy objz should be declared as glDoubles (just in case ).

Page 1 of 2 12 LastLast

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
  •