Mouse Co-ordinates Question
Hi All,
I am fairly new to this, so bare with me. I am trying to write a House Of The Dead style game, purely to teach myself OpenGL. I have read through NEHE''s tutorial # 23 which covers mouse input and have done the following (I know it could be optimised better, but wanted to get it working first) in my code:
I have a mouse routine that is called whenever the LMB is pressed that contains the following code:
GetCursorPos(&mpos);
mouseGLX = (float) mpos.x / 100.0f;
mouseGLY = (float) mpos.y / -100.0f;
Then in my draw routine I have:
glTranslatef(mouseGLX,mouseGLY,-10.0f);
glColor3f(1.0f,1.0f,1.0f);
glBegin(GL_TRIANGLES); // Drawing using triangles
glVertex3f( 0.0f, 0.0f, 0.0f); // Top
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd();
As you can see all I am trying to do at the moment is display a triangle where the user presses the mouse button. The triangle displays and moves around the screen, but just isnt quite right. Can anyone please help me? It would be greatly appreciated.
Thank you
Wiz74
Just another Corporate slave trying to release himself into the OpenGL void.
Ok sorry bout that.
Its a bit hard to explain but here goes.
It is like the position is limited to the right bottom quarter of the screen. The triangle moves left, right, up and down in acordance with the clicks, but remains in that quarter. If you click in the right place in that quarter you can actually get the mouse and the triangle to line up, but it has to be the exact right position on the screen.
It has to be something wrong with the way I am converting mpos.x and mpos.y to the GLfloat co-ordinates.
If anyone wants to have a look at the code I will gladly send it to you.
Thanx to all in advance for any assistance offered.
Its a bit hard to explain but here goes.
It is like the position is limited to the right bottom quarter of the screen. The triangle moves left, right, up and down in acordance with the clicks, but remains in that quarter. If you click in the right place in that quarter you can actually get the mouse and the triangle to line up, but it has to be the exact right position on the screen.
It has to be something wrong with the way I am converting mpos.x and mpos.y to the GLfloat co-ordinates.
If anyone wants to have a look at the code I will gladly send it to you.
Thanx to all in advance for any assistance offered.
Just another Corporate slave trying to release himself into the OpenGL void.
I have managed to get it a little better now. by adding the following code:
mouseGLX = 0.0f;
mouseGLY = 0.0f;
SetCursorPos(heightGL/2, widthGL/2);
But not perfect. It is no longer confined to the bottom right. It starts at the center but the further from the center you click, the further the triangle is from your mouse.
eg, If I click in the center it matches, if I click say 6 from the center the triangle might come up as 5, while if I click at 8, it may show up as 6 etc. NOTE these numbers relate to nothing, but am using only as an example to explain what I mean.
I am currently trying to find some way I can post the code on the net so you can have a look, but can not find anywhere to do it. And my browser (IE) is playing up something fierce which really doesnt help at all.
Basically I still need HELP!!
Thanx.
mouseGLX = 0.0f;
mouseGLY = 0.0f;
SetCursorPos(heightGL/2, widthGL/2);
But not perfect. It is no longer confined to the bottom right. It starts at the center but the further from the center you click, the further the triangle is from your mouse.
eg, If I click in the center it matches, if I click say 6 from the center the triangle might come up as 5, while if I click at 8, it may show up as 6 etc. NOTE these numbers relate to nothing, but am using only as an example to explain what I mean.
I am currently trying to find some way I can post the code on the net so you can have a look, but can not find anywhere to do it. And my browser (IE) is playing up something fierce which really doesnt help at all.
Basically I still need HELP!!
Thanx.
Just another Corporate slave trying to release himself into the OpenGL void.
My suggestion would be to look up the gluUnproject() function... then when you send it your mouse X, Y, and Z values it will tell you exactly what position that corresponds to in world space. Just use 1.0 for the Z value (or whatever the correct value is for your near clipping plane, since the mouse pointer is effectively ''on'' it). One thing to note: if you''re using Windows you''ll need to recalculate the mouse Y value... windows puts 0,0 in the upper left hand corner of the screen but OpenGL/Unix assumes 0,0 is in the lower left-hand corner (so make mpos.y = heightGL - mpos.y).
Thanx mate. I will look into it tonight and see how I go.
Once again thanx mate.
Once again thanx mate.
Just another Corporate slave trying to release himself into the OpenGL void.
I am a fool.
I dont know what I was thinking on the weekend, but of course it is going to appear like it isn''t right. The triangle is set 10 into the screen. I didn''t take depth into account for some stupid reason. I feel like a fool.
Chromebender,
I haven''t had the chance yet to llok at gluUnProject. Hopefully this week sometime.
Anyway better get back to work. They dont employ me as a C++ programmer unfortunetly, I am a Progress programmer and therefore cant put this down as work related in any sense.
Cya''s.
I dont know what I was thinking on the weekend, but of course it is going to appear like it isn''t right. The triangle is set 10 into the screen. I didn''t take depth into account for some stupid reason. I feel like a fool.
Chromebender,
I haven''t had the chance yet to llok at gluUnProject. Hopefully this week sometime.
Anyway better get back to work. They dont employ me as a C++ programmer unfortunetly, I am a Progress programmer and therefore cant put this down as work related in any sense.
Cya''s.
Just another Corporate slave trying to release himself into the OpenGL void.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement