Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Select and Pick lines in OpenGL

Select and Pick lines in OpenGL

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicsgame-devtutorialquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gopi Nath
    wrote on last edited by
    #1

    Hello Everybody, I am using VC++ and OpenGL for one application. I am new to OpenGL. I can able to load different objects and also able to create vertices. After that, I was able to select and pick those using glReadPixels and gluUnProject. Now, I need to select and pick lines (edges of the triangles). I searched in google and not able to find a proper result. Can anybody give me ideas how to implement this? Thanks in Advance. Regards, Gopi.

    L 1 Reply Last reply
    0
    • G Gopi Nath

      Hello Everybody, I am using VC++ and OpenGL for one application. I am new to OpenGL. I can able to load different objects and also able to create vertices. After that, I was able to select and pick those using glReadPixels and gluUnProject. Now, I need to select and pick lines (edges of the triangles). I searched in google and not able to find a proper result. Can anybody give me ideas how to implement this? Thanks in Advance. Regards, Gopi.

      L Offline
      L Offline
      leon de boer
      wrote on last edited by
      #2

      Find the line in your list that is closest the point or at least less than some tolerance value A number of Contributed implementations of the formula here plus an explaination of the mathematics Point, Line, Plane[^] Implementation would look something like this

      #define GLfloat float // No sure if you want floats or double you can change here
      /* Calculate euclidian distance between vertex points (x1,y1) and (x2,y2) */
      GLfloat GLDistance2D (GLfloat x1, GLfloat y1, // Vertex 1 (x1,y1)
      GLfloat x2, GLfloat y2) // Vertex 2 (x2,y2)
      {
      GLfloat dx, dy;
      dx = x2 - x1; // Diff x
      dy = y2 - y1; // Diff y
      if ( dx < -FLT_EPSILON || dx > FLT_EPSILON ||
      dy < -FLT_EPSILON || dy > FLT_EPSILON ) // Check dx, dy or both are non zero
      {
      return sqrtf(dx * dx + dy * dy); // Calculate the distance
      }
      return (GLfloat) 0.0; // Both dx and dy are zero
      }

      /* Given (px, py) and two points forming line segment(x1, y1), (x2, y2).
      Calculate the closest point(cpx, cpy) between the point and the line and
      return the distance.The linesegment flag is true the closest point must be
      on the line segment or false it does an extended infinite line test. */
      GLfloat GLDistanceToLine2D (GLfloat px, GLfloat py, // Test point (px,py)
      GLfloat x1, GLfloat y1, // Line vertex 1 (x1,y1)
      GLfloat x2, GLfloat y2, // Line vertex 2 (x2,y2)
      bool linesegment, // Flag true if line segment, false extended line
      GLfloat* cpx, GLfloat* cpy) // Closest point point result pointers
      {
      GLfloat u; // see Paul Bourke's original article(s)
      GLfloat dx = x2 - x1; // Diff x
      GLfloat dy = y2 - y1; // Diff y
      GLfloat SqLineMag = dx * dx + dy * dy; // Square of line magnitude
      if (SqLineMag > -FLT_EPSILON && SqLineMag < FLT_EPSILON) { // We have a single point there is no measurable distance between vertexes
      if (linesegment) return GLDistance2D(px, py, x1, y1); // Line segement test return distance between 2 pts
      else return (GLfloat)-1.0; // Extended line invalid return -1 as it's impossible
      }
      u = ((px - x1)* dx + (py - y1)* dy) / SqLineMag; // Calculate value u
      if ((linesegment) && ((u < FLT_EPSILON) || (u > (GLfloat)1.0))) // Line segment test and closest point is outside the segment

      G 3 Replies Last reply
      0
      • L leon de boer

        Find the line in your list that is closest the point or at least less than some tolerance value A number of Contributed implementations of the formula here plus an explaination of the mathematics Point, Line, Plane[^] Implementation would look something like this

        #define GLfloat float // No sure if you want floats or double you can change here
        /* Calculate euclidian distance between vertex points (x1,y1) and (x2,y2) */
        GLfloat GLDistance2D (GLfloat x1, GLfloat y1, // Vertex 1 (x1,y1)
        GLfloat x2, GLfloat y2) // Vertex 2 (x2,y2)
        {
        GLfloat dx, dy;
        dx = x2 - x1; // Diff x
        dy = y2 - y1; // Diff y
        if ( dx < -FLT_EPSILON || dx > FLT_EPSILON ||
        dy < -FLT_EPSILON || dy > FLT_EPSILON ) // Check dx, dy or both are non zero
        {
        return sqrtf(dx * dx + dy * dy); // Calculate the distance
        }
        return (GLfloat) 0.0; // Both dx and dy are zero
        }

        /* Given (px, py) and two points forming line segment(x1, y1), (x2, y2).
        Calculate the closest point(cpx, cpy) between the point and the line and
        return the distance.The linesegment flag is true the closest point must be
        on the line segment or false it does an extended infinite line test. */
        GLfloat GLDistanceToLine2D (GLfloat px, GLfloat py, // Test point (px,py)
        GLfloat x1, GLfloat y1, // Line vertex 1 (x1,y1)
        GLfloat x2, GLfloat y2, // Line vertex 2 (x2,y2)
        bool linesegment, // Flag true if line segment, false extended line
        GLfloat* cpx, GLfloat* cpy) // Closest point point result pointers
        {
        GLfloat u; // see Paul Bourke's original article(s)
        GLfloat dx = x2 - x1; // Diff x
        GLfloat dy = y2 - y1; // Diff y
        GLfloat SqLineMag = dx * dx + dy * dy; // Square of line magnitude
        if (SqLineMag > -FLT_EPSILON && SqLineMag < FLT_EPSILON) { // We have a single point there is no measurable distance between vertexes
        if (linesegment) return GLDistance2D(px, py, x1, y1); // Line segement test return distance between 2 pts
        else return (GLfloat)-1.0; // Extended line invalid return -1 as it's impossible
        }
        u = ((px - x1)* dx + (py - y1)* dy) / SqLineMag; // Calculate value u
        if ((linesegment) && ((u < FLT_EPSILON) || (u > (GLfloat)1.0))) // Line segment test and closest point is outside the segment

        G Offline
        G Offline
        Gopi Nath
        wrote on last edited by
        #3

        Thanks Leon for your quick reply. Will give it a try and update you. Regards, Gopi.

        1 Reply Last reply
        0
        • L leon de boer

          Find the line in your list that is closest the point or at least less than some tolerance value A number of Contributed implementations of the formula here plus an explaination of the mathematics Point, Line, Plane[^] Implementation would look something like this

          #define GLfloat float // No sure if you want floats or double you can change here
          /* Calculate euclidian distance between vertex points (x1,y1) and (x2,y2) */
          GLfloat GLDistance2D (GLfloat x1, GLfloat y1, // Vertex 1 (x1,y1)
          GLfloat x2, GLfloat y2) // Vertex 2 (x2,y2)
          {
          GLfloat dx, dy;
          dx = x2 - x1; // Diff x
          dy = y2 - y1; // Diff y
          if ( dx < -FLT_EPSILON || dx > FLT_EPSILON ||
          dy < -FLT_EPSILON || dy > FLT_EPSILON ) // Check dx, dy or both are non zero
          {
          return sqrtf(dx * dx + dy * dy); // Calculate the distance
          }
          return (GLfloat) 0.0; // Both dx and dy are zero
          }

          /* Given (px, py) and two points forming line segment(x1, y1), (x2, y2).
          Calculate the closest point(cpx, cpy) between the point and the line and
          return the distance.The linesegment flag is true the closest point must be
          on the line segment or false it does an extended infinite line test. */
          GLfloat GLDistanceToLine2D (GLfloat px, GLfloat py, // Test point (px,py)
          GLfloat x1, GLfloat y1, // Line vertex 1 (x1,y1)
          GLfloat x2, GLfloat y2, // Line vertex 2 (x2,y2)
          bool linesegment, // Flag true if line segment, false extended line
          GLfloat* cpx, GLfloat* cpy) // Closest point point result pointers
          {
          GLfloat u; // see Paul Bourke's original article(s)
          GLfloat dx = x2 - x1; // Diff x
          GLfloat dy = y2 - y1; // Diff y
          GLfloat SqLineMag = dx * dx + dy * dy; // Square of line magnitude
          if (SqLineMag > -FLT_EPSILON && SqLineMag < FLT_EPSILON) { // We have a single point there is no measurable distance between vertexes
          if (linesegment) return GLDistance2D(px, py, x1, y1); // Line segement test return distance between 2 pts
          else return (GLfloat)-1.0; // Extended line invalid return -1 as it's impossible
          }
          u = ((px - x1)* dx + (py - y1)* dy) / SqLineMag; // Calculate value u
          if ((linesegment) && ((u < FLT_EPSILON) || (u > (GLfloat)1.0))) // Line segment test and closest point is outside the segment

          G Offline
          G Offline
          Gopi Nath
          wrote on last edited by
          #4

          Hi Leon, Sorry for late reply. Got time to work on this now only. Awesome. This one works fine as I expected. Thanks again. Regards, Gopinath.

          1 Reply Last reply
          0
          • L leon de boer

            Find the line in your list that is closest the point or at least less than some tolerance value A number of Contributed implementations of the formula here plus an explaination of the mathematics Point, Line, Plane[^] Implementation would look something like this

            #define GLfloat float // No sure if you want floats or double you can change here
            /* Calculate euclidian distance between vertex points (x1,y1) and (x2,y2) */
            GLfloat GLDistance2D (GLfloat x1, GLfloat y1, // Vertex 1 (x1,y1)
            GLfloat x2, GLfloat y2) // Vertex 2 (x2,y2)
            {
            GLfloat dx, dy;
            dx = x2 - x1; // Diff x
            dy = y2 - y1; // Diff y
            if ( dx < -FLT_EPSILON || dx > FLT_EPSILON ||
            dy < -FLT_EPSILON || dy > FLT_EPSILON ) // Check dx, dy or both are non zero
            {
            return sqrtf(dx * dx + dy * dy); // Calculate the distance
            }
            return (GLfloat) 0.0; // Both dx and dy are zero
            }

            /* Given (px, py) and two points forming line segment(x1, y1), (x2, y2).
            Calculate the closest point(cpx, cpy) between the point and the line and
            return the distance.The linesegment flag is true the closest point must be
            on the line segment or false it does an extended infinite line test. */
            GLfloat GLDistanceToLine2D (GLfloat px, GLfloat py, // Test point (px,py)
            GLfloat x1, GLfloat y1, // Line vertex 1 (x1,y1)
            GLfloat x2, GLfloat y2, // Line vertex 2 (x2,y2)
            bool linesegment, // Flag true if line segment, false extended line
            GLfloat* cpx, GLfloat* cpy) // Closest point point result pointers
            {
            GLfloat u; // see Paul Bourke's original article(s)
            GLfloat dx = x2 - x1; // Diff x
            GLfloat dy = y2 - y1; // Diff y
            GLfloat SqLineMag = dx * dx + dy * dy; // Square of line magnitude
            if (SqLineMag > -FLT_EPSILON && SqLineMag < FLT_EPSILON) { // We have a single point there is no measurable distance between vertexes
            if (linesegment) return GLDistance2D(px, py, x1, y1); // Line segement test return distance between 2 pts
            else return (GLfloat)-1.0; // Extended line invalid return -1 as it's impossible
            }
            u = ((px - x1)* dx + (py - y1)* dy) / SqLineMag; // Calculate value u
            if ((linesegment) && ((u < FLT_EPSILON) || (u > (GLfloat)1.0))) // Line segment test and closest point is outside the segment

            G Offline
            G Offline
            Gopi Nath
            wrote on last edited by
            #5

            Hi Leon, This code is working fine for 2D points. Is there anyway to get the logic for 3D points? Thanks in advance. Regards, Gopinath.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups