Select and Pick lines in OpenGL
-
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.
-
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.
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 -
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 -
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 -
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