Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

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

Thread: How Do I Convert Mouse Position Cordinates to OpenGL cordinates?

  1. #1
    Intern Contributor
    Join Date
    Oct 2011
    Posts
    67

    Post How Do I Convert Mouse Position Cordinates to OpenGL cordinates?

    Hello I'm trying really hard to do the maths here but I have no clue what to do at all when it comes to converting coordinates?


    Here whats happening :

    1.) I'm trying to select a area.

    2.) This area that is selected is created by left clicking on the window.

    3.) OpenGL puts a quad and then I put a texture with a white coloring in the center in perspective projection onto the window and my shaders colorkey out the center.

    4.) Whatever is under that quad is then selected.

    But the problem :

    I want too snap the quad to the cursor at all times when I left click I have no idea how to do that and this is my attempt to do just that using gluUnProject and glTranslatef();

    oh before I show the code I'm using

    WIN32 API
    OpenGL 3.0
    GLSL


    Heres the code :

    This function is what attempts to snap the quad to the mouse but fails dramatically.

    Code :
    void display(){
          glClearColor(0.0,0.0,0.0,1.0);
     
     
          glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);   
     
     
          for(std::vector<GLuint>::iterator I = cube.begin(); I != cube.end(); ++I) 
          {
     
              glCallList(*I);
     
     
               }
     
     
     
                if(DrawArea == true)
             {
     
            glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
     
     
        cerr << winZ << endl;
     
     
        glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
     
     
        glGetDoublev(GL_MODELVIEW_MATRIX, projection);
     
     
        glGetIntegerv(GL_VIEWPORT, viewport);
     
     
        gluUnProject(winX, winY, winZ , modelview, projection, viewport, &posX, &posY, & posZ);
     
     
            glBindTexture(GL_TEXTURE_2D, DrawAreaTexture);
     
     
            glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
     
     
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     
            glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);    
     
            glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, DrawAreaSurface->w, DrawAreaSurface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, DrawAreaSurface->pixels);
     
     
            glEnable(GL_TEXTURE_2D);
     
     
            glBindTexture(GL_TEXTURE_2D, DrawAreaTexture);
     
     
        glTranslatef(posX , posY, posZ);
     
     
             glBegin(GL_QUADS);
     
     
             glTexCoord2f (0.0, 0.0);
             glVertex3f(0.5, 0.5, 0);
     
     
             glTexCoord2f (1.0, 0.0);
             glVertex3f(0, 0.5, 0);
     
             glTexCoord2f (1.0, 1.0);
             glVertex3f(0, 0, 0);
     
     
             glTexCoord2f (0.0, 1.0);
             glVertex3f(0.5, 0, 0);
     
     
             glEnd();
     
     
     
     
     
                }    
     
              SwapBuffers(hDC);
     
     
     
     
              //FOR SPAMMING 1/0
             // cerr << DrawArea << endl;              
    }

    This function is being called everytime in the main loop here :


    Code :
     while(GetMessage(&Msg, NULL, 0, 0) > 0)
      { 
     
       TranslateMessage(&Msg);  
     
     
       DispatchMessage(&Msg);
     
           display();
     
        }
     
     
      return Msg.wParam;
    }
    Last edited by noobscratcher; 06-22-2012 at 02:26 AM.

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,194
    Quote Originally Posted by noobscratcher View Post
    Code :
    ...
    glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
    glGetDoublev(GL_MODELVIEW_MATRIX, projection);
    glGetIntegerv(GL_VIEWPORT, viewport);
    gluUnProject(winX, winY, winZ , modelview, projection, viewport, &posX, &posY, & posZ);
    Take a look at the 2nd line above. See any problems?
    Last edited by Dark Photon; 06-22-2012 at 07:49 AM.

  3. #3
    Intern Contributor
    Join Date
    Oct 2011
    Posts
    67
    Ahh ha yes I didn't see that thanks for pointing that out!


    But also how can I snap a quad to the mouse cursor? what am I required to do to succeed

  4. #4
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,194
    Quote Originally Posted by noobscratcher View Post
    But also how can I snap a quad to the mouse cursor?
    You're on the right track.

    - Install mouse event handler
    - Get the X,Y window-space position of the mouse on left click
    - Draw your quad around that position

  5. #5
    Intern Contributor
    Join Date
    Oct 2011
    Posts
    67
    but how do I draw the quad around that position? glTranslating with posX, PosY, posZ returns a weird flickering textured mapped quad that seems to be rendered again with the orginal quad and they flicker because of left clicking and even if I move the mouse cursor the state and position of the texture mapped quad stays the same.

  6. #6
    Intern Contributor
    Join Date
    Oct 2011
    Posts
    67
    I'm still stuck I know I have to do some conversion but that conversion makes no sense and isn't obvious to work so can someone help.

  7. #7
    Intern Contributor
    Join Date
    Oct 2011
    Posts
    67
    Can someone answer Anyone?

  8. #8
    Junior Member Regular Contributor
    Join Date
    Mar 2012
    Posts
    129
    I am not sure if I understand your problem correctly, but it seems like you just want to draw a 2D quad over the main scene as a selection box. Switch to ortho, disable depth testing, and render the quad at the screen coordinates of the mouse. You then un-project the mouse coordinates so you can select stuff in 3D.

  9. #9
    Intern Contributor
    Join Date
    Oct 2011
    Posts
    67
    I want the 2d quad to be center to the mouse cursor when I left click thats what I want to know how to do.

  10. #10
    Junior Member Newbie
    Join Date
    Jan 2012
    Posts
    15
    Okay, soo.. it seems that you are trying to fuss with glTranslate is what I've been gathering. Depending on the OpenGL state, you may not get the results you expect (this can be true for many functions).

    Anyways, I have two suggestions:
    1. Make sure glTranslate is being called in the correct matrix mode. The correct matrix mode for you should be set using
    Code :
    [I]glMatrixMode(GL_MODELVIEW)[/I]
    If you have the GL_PROJECTION matrix set, you will be translating the camera, which isn't probably what you are intending to do.


    2. Try deleting the glTranslate method alltogether. Instead, you should take the x, y, values from the mouse coordinate, and use them directly to change the location/dimension of the actual square.

    Here's an example of what I mean:
    Code :
     
    if(screen_selected)//If the mouse is pressed on the screen, then draw the square
    {
     /*This example is ignoring texture requirements, however you can just put those in as before.
     Also, this uses the variables "initalMouseX" and "initalMouseY" which represent the XY coordinate of the mouse when the use clicks the mouse.
    The variables "currentMouseX", and "currentMouseY" represent the current XY coordinates of the mouse while the use still has the mouse button down. The reason we see "screenHeight - initalMouseY" or "screenHeight - currentMouseY" is because most implements of 2D graphics measures the mouse coordinate from the top-left corner of the screen.  Therefore, we need to flip the Y coordinate so that it matches the implementation of OpenGL which is typically the bottom-left corner.*/
        glBegin(GL_QUADS);
        glVertex3d(initalMouseX, screenHeight - initialMouseY, 0);
        glVertex3d(initalMouseX, screenHeight - currentMouseY, 0);
        glVertex3d(currentMouseX, screenHeight - currentMouseY, 0);
        glVertex3d(currentMouseX, screenHeight - initialMouseY, 0);
        glEnd();
    }
    //There should now be a quad, parallel with the screen.

    Comparing this to a 2D implementation I've made before (for group-selecting file icons), the above example seems as if it would work as is, simply because OpenGL does things differently. However, in that file-selector program, I had to do some other mathematics to keep the selection box the correct size. For that I measured what size it should be, then I translated it accordingly to fit the region selected.

    Hope this helps! Let me know how you are doing with this.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •