Solved

# Mouse - 2D to 3D coordinates

Posted on 2005-03-15
Medium Priority
4,885 Views
Hi,

Take the following code for an example:

From my own class...

void Line::OnLeftMouseButtonDown(int x, int y, WPARAM status)
{
m_StartPoint.x = -1.0 + ((2.0/1024.0f) * x);
m_StartPoint.y = 1.0 - ((2.0/768.0f) * y);

m_EndPoint.x = m_StartPoint.x;
m_EndPoint.y = m_StartPoint.y;
}

void Line::OnMouseMove(int x, int y, WPARAM status)
{
// Update the end point

m_EndPoint.x = -1.0 + ((2.0/1024.0f) * x);
m_EndPoint.y = 1.0 - ((2.0/768.0f) * y);
}

void Line::Render(void)
{
glBegin(GL_LINES);

glColor3f(1.0, 1.0, 1.0);

glVertex2f(m_StartPoint.x, m_StartPoint.y);
glVertex2f(m_EndPoint.x, m_EndPoint.y);

glEnd();
}

Ok, no problem, the line draws from m_StartPoint to m_EndPoint.

If I now add several lines to the Render method to change the perspective...

void Line::Render(void)
{
glMatrixMode(GL_PROJECTION);

gluPerspective(45.0f, 1024.0/768.0, 0.0f, 1.0f);

glTranslatef(0.0, 0.0, -2.0);

glBegin(GL_LINES);

glColor3f(1.0, 1.0, 1.0);

glVertex2f(m_StartPoint.x, m_StartPoint.y);
glVertex2f(m_EndPoint.x, m_EndPoint.y);

glEnd();
}

...the line starts and ends miles away from where I specified it using the mouse, so how can I draw the line in 3D like I did before?

Examples would be great as time is of the essense, hence the points.

0
Question by:Brent-Campbell
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 4

LVL 17

Accepted Solution

davebytes earned 1000 total points
ID: 13566601
You added four lines.  Why?  What were you doing originally, that was working, that you are now adding a perspective transformation and a translate into the mix?  Did you have a different GL_PROJECTION set, or none?  What are the lines supposed to do/be: 2D overlays, or actual 3D in a scene?

The easiest solution, if you don't ACTUALLY want the projection applied to the lines (i.e., rendering something else, then trying to render lines) is something quick like:

void Line::Render(void)
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();

gluPerspective(45.0f, 1024.0/768.0, 0.0f, 1.0f);
glTranslatef(0.0, 0.0, -2.0);

glPopMatrix();
glBegin(GL_LINES);

glColor3f(1.0, 1.0, 1.0);

glVertex2f(m_StartPoint.x, m_StartPoint.y);
glVertex2f(m_EndPoint.x, m_EndPoint.y);

glEnd();
}

But I need more details from you of what your desired result is... ;)

-d
0

Author Comment

ID: 13587662
Hi Dave,

Fair play mate, that was the desired result, spot on...I needed the push/pop matrix lines!

Many thanks!
0

Author Comment

ID: 13588611
Although, now I played with it somemore, it seems things aren't quite going exactly to plan, i'll investigate it a little more and get back to you with more info...
0

LVL 17

Expert Comment

ID: 13588950
definitely -- i'm monitoring the Q still.  -d
0

Author Comment

ID: 13672476
Hi,

Ok, I can sort of see what is going on now, but I'm not sure how to fix it, here is the un-working code but gives the idea of my desired result...

void Line3D::Initialisation(void)
{
m_Draw = FALSE;

m_StartPoint.x = 0.0f;
m_StartPoint.y = 0.0f;

m_EndPoint.x = 0.0f;
m_EndPoint.y = 0.0f;
}

void Line3D::OnLeftMouseButtonDown(int x, int y, WPARAM status) // My own method
{
m_StartPoint.x = -1.0 + ((2.0/1024.0f) * x);
m_StartPoint.y = 1.0 - ((2.0/768.0f) * y);

m_EndPoint.x = m_StartPoint.x;
m_EndPoint.y = m_StartPoint.y;

m_Draw = TRUE;
}

void Line3D::OnMouseMove(int x, int y, WPARAM status) // My own method
{
// Update the end point
m_EndPoint.x = -1.0 + ((2.0/1024.0f) * x);
m_EndPoint.y = 1.0 - ((2.0/768.0f) * y);
}

void Line3D::OnLeftMouseButtonUp(int x, int y, WPARAM status) // My own method
{
m_StartX = x;
m_StartY = y;

m_Draw = FALSE;
}

void Line3D::Render(void)
{
if (m_Draw)
{
glMatrixMode(GL_PROJECTION);
// glPushMatrix();

gluPerspective(45.0f, 1024.0/768.0, 0.0f, 1.0f);
glTranslatef(0.0, 0.0, -2.0);

// glPopMatrix();

glRotatef(-65, 1.0f, 0.0f, 0.0f);

Point a, b, c, d;

a.x = m_EndPoint.x - 0.05;
a.y = m_EndPoint.y;

b.x = m_EndPoint.x + 0.05;
b.y = m_EndPoint.y;

c.x = m_StartPoint.x + 0.05;
c.y = m_StartPoint.y;

d.x = m_StartPoint.x - 0.05;
d.y = m_StartPoint.y;

glColor3f(1.0, 1.0, 1.0);

glVertex2f(a.x, a.y);
glVertex2f(b.x, b.y);
glVertex2f(c.x, c.y);
glVertex2f(d.x, d.y);

glEnd();
}
}

Using the glPushMatrix() and glPopMatrix() commands (but without the glRotatef(-65, 1.0f, 0.0f, 0.0f) line) the line is drawn to the mouse coordinates as I'm trying to achieve but the line is not drawn into the distance, therefore I apply glPushMatrix(), glPopMatrix(), and glRotatef commands but then the mouse coordinates don't follow the mouse.

I'm offering 150 points for this as it's quite urgent and it's doing my head in!

0

LVL 17

Expert Comment

ID: 13676270
Can you please explain "drawn into the distance"?  You are using 2D screen coordinates, what is 'distance'?  Is the line supposed to be z-buffered, or is it supposed to be NON-2D, but in fact 3D, somehow interacting in the scene view?

-d
0

Author Comment

ID: 13676957
Ok,

The plan is....I choose a point on the screen to be the start point (e.g bottom of the screen and in the middle), and then drag the line to the center of the screen which is the end point and I want the line to appear that it is in perspective i.e the line thickness gets shorter the further up the screen the mouse Y point is.  The above code I supplied does this but does not follow the mouse pointer.  Using the glPushMatrix and glPopMatrix (without the glRotate line) the line follows the mouse perfectly but the line is not drawn in perspective, so I added the glRotate command.  This works, I have the perspective now (only if I remove the glPushMatrix and glPopMatrix commands) but then the line doesn't start and end anywhere near my selected mouse start and end points. Does that make any sense?

Cheers.
0

LVL 17

Expert Comment

ID: 13677475
Yes, because you are translating the points in space, no longer at their original positions.

I'd probably personally recommend that you 'by hand' do the effect, rather than trying to fudge something up via perspective transformation...

For instance, take your start point, and make two new points perpendicular to the current 'line' vector at a distance of, I dunno, two pixels each side?  Then do the same for the destination point, but scale from 0-2 pixels each side based upon something like the ratio of maximum corner-to-corner of the screen versus the current vector/line length.  Then, ignore the actual start/end line points, but instead draw a triangle fan using the four (two start, two end) points.  You can even then set the color of the start and end points in order to get a blend, and also adjust the color based on the vector length.

That's more likely easy to get on the mark, and easy to 'control' the 'effects' on the line as you drag it out.  That any help?

-d
0

Author Comment

ID: 13680400
You know more about this than me, and if my current method could have been fixed I'm sure you would have suggested a solution, so yeah the new method seems the way to go.

0

Author Comment

ID: 13680556
davebytes,

Please respond to http://www.experts-exchange.com/Programming/Game_Development/Game_Graphics/OpenGL/Q_21372799.html so that I can award you the points.

Cheers!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of â€¦
Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  Iâ€™ll start with the algorâ€¦
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized â€¦
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as formâ€¦
###### Suggested Courses
Course of the Month14 days, 1 hour left to enroll