Results 1 to 3 of 3

Thread: How to make mouse controlled 2d box with opengl

  1. #1

    How to make mouse controlled 2d box with opengl

    Hi.
    i want to write a node editor with opengl and i am thinking with it dglopeng.
    i can show 2d object with opengl but how i can control it with mouse? can i select, Drag&Drop , change?

  2. #2
    I don't know if something like this is supported directly by OpenGL. I seriously doubt it is.

    This means that you will have to implement it on your own. And to do this you need to store position of these boxes in a way so you can manipulate them and that the rendering phase is reading their position every frame so they can be rendered at correct position.

    Once you figure out how to store position of these boxes you need to devise a picking routine which will tell you which box is under your mouse cursor.
    If these boxes are not rotated then this routine could simply iterate through all of the boxes and check if mouse position is within box boundary like so:

    Code:
    function IsOverBox(Box: TRect; MousePos: TPoint): Boolean;
    begin
      Result := False;
      if MousePos.X >= Box.Left then
      begin
        if MousePos.X <= Box.Right then
        begin
          if MousePos.Y >= Box.Top then
          begin
            if MousePos.Y <= Box.Bottom then
            begin
              Result := True;
            end;
          end;
        end;
      end;
    end;
    If your rectangles are rotated then it becomes a bit more complicated as you need to do necessary spatial transformation to get the correct mouse position over your rectangle.

    If you have overlapping rectangles then I guess you are also using some kind of a Z order mechanism to figure out which one is on top. So in such case I recommend that you loop through all of your rectangles according their Z order starting with topmost.

  3. #3
    PGD Staff / News Reporter phibermon's Avatar
    Join Date
    Sep 2009
    Location
    England
    Posts
    524
    Actually surprisingly enough - while you do have to map your mouse coordinates into device space (in the form of a ray) - OpenGL (legacy) does have picking functionality with the GL_SELECTION render mode (see https://www.opengl.org/archives/reso.../selection.htm) or with modern OpenGL 3+ you can use occlusion queries to produce equivalent functionality or you can create your own system by testing against a depth buffer etc

    If you require per-poly / per-pixel picking then in all but the most simple of scenes? it will be faster to optimize picking with the GPU.

    Bare in mind however that it's not a free operation and while it's often faster than a CPU bound solution - you may still want to do it on the CPU anyway. As always benchmarking weighed against the other things you're doing on the GPU is recommended.

    I guess it depends if your game is CPU bound or GPU bound.
    Last edited by phibermon; 09-05-2016 at 06:25 AM.
    When the moon hits your eye like a big pizza pie - that's an extinction level impact event.

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
  •  
Comodo SSL