Jump to content

  • Log In with Google      Sign In   
  • Create Account


How can I track the mouse cursor position and then draw a line ?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 DarkHorseKnight   Members   -  Reputation: 143

Like
0Likes
Like

Posted 22 May 2012 - 01:35 PM

How can I track the mouse cursor position and then draw a line at that exact location?

I tried to do this myself by using glUnProject(); and store the 3 last values and use them as position for the glVertex3f(); vertex positioning obviously that didn't work it would draw the line but not at the exact location of the cursor or close enough that it wasn't noticeable after running the program.

So I was trying to deciper a way to do it and I'm 100% honestly stumped....


So in other words if someone knows how to do this feel free to tell me how you did it smile.png

OpenGL 3.0
WIN32 API
C++


Thanks in Advance

Sponsor:

#2 slicer4ever   Crossbones+   -  Reputation: 3416

Like
0Likes
Like

Posted 22 May 2012 - 01:59 PM

Are you drawing a 2D line, or 3D line?

If it's 3D gluUnProject is defiantly what you need, mind posting your code?

If it's just 2D then you simply need to track your mouse(although it sounds like a 3D projection from your description.)

Also, could you possible provide a picture of a test scene so we can see exactly what your attempting to do?
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#3 DarkHorseKnight   Members   -  Reputation: 143

Like
0Likes
Like

Posted 22 May 2012 - 03:04 PM

Hello yes I'm trying to draw a 3D Line here is the code thats most important in this case

The Setup :


void init ()
{
glClearColor(1.0,0.0,0.0,1.0);
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(90,SCREEN_HEIGHT/SCREEN_WIDTH,0.0,1500.0);
		glMatrixMode(GL_MODELVIEW);
		glEnable(GL_DEPTH_TEST);		  
		glEnable(GL_LIGHTING);  //we enable lighting, to make the 3D object to 3D
		glEnable(GL_LIGHT0);
		float col[]={1.0,1.0,1.0,1.0};  //light color is white
		glLightfv(GL_LIGHT0,GL_DIFFUSE,col);

}
  

Drawing The Line :

	case WM_LBUTTONDOWN:
		
		  glBegin( GL_LINES );
		  glColor3f( 1.0f, 0.0f, 0.0f );
		  glVertex3f(posX, posY, 0.0f);
		  glVertex3f(posX, posY, posZ );
		  glEnd();
	  
		  cout << "Test" << endl;
	  

	break;

Displaying the graphics:

void display()
{

		glLoadIdentity();
		float pos[]={-1.0,1.0,-2.0,1.0};		//set the position
		glLightfv(GL_LIGHT0,GL_POSITION,pos);
		//glRotatef(angle,angle,0.0,2.0);
		cout << name << endl;
	 // glCallList(cube);
	  for(std::vector<GLuint>::iterator I = cube.begin(); I != cube.end(); ++I) {
	glCallList(*I);
	  }
	  
		  glGetIntegerv(GL_VIEWPORT, viewport);
		  glGetDoublev(GL_PROJECTION_MATRIX, projection);
		
		  GetCursorPos(&mouse);
		  ScreenToClient(hwnd, &mouse);
  
		  winX = (float)mouse.x;
		  winY = (float)mouse.y;
		  winY = (float)viewport[3] - winY;
		  glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
		  gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ );
		  cout << mouse.x << endl;
		  cout << mouse.y << endl;
	  
		  SwapBuffers(hDC);
		  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	  
	  
}


And theres not much to see accept a black background with a red line that shows on the frustum only if I left click many times Posted Image

Hope that helps

Edited by DarkHorseKnight, 22 May 2012 - 03:06 PM.


#4 szecs   Members   -  Reputation: 2102

Like
0Likes
Like

Posted 22 May 2012 - 11:49 PM

A screenshot would still be useful.
"red line that shows on the frustum only if I left click many times"
This is not clear. Is something drawn? Is it drawn in the proper place?

The depth buffer is not infinitely precise. *Especially not at the far end of the frustum. You mustn't even use zero value of the near plane, because that screws the calculations. Look into the parameters of gluPerspective and what they mean.

So my guess: the lines are calculated fine except for the wrong perspective definition. Even if you define it correctly (with a >0.0 near plane value), the calculated world coordinates will be imprecise due to floating point errors. That means lines you want to draw at the end of the frustum may be culled (or partially culled) by the frustum, I hope it's clear.

*Read this

#5 szecs   Members   -  Reputation: 2102

Like
2Likes
Like

Posted 23 May 2012 - 12:03 AM

BTW, your whole logic is wrong, sorry I just noticed it now.

Sorry, it's hard to explain, there are fundamental flaws in your program.

OpenGL is not like DOS and EGA/VGA etc, where you can paint over-and over the scene and it will remain there like a canvas and everything will instantly get drawn on it. In OpenGL, commands are buffered, so you have to insert glFlush/glFinnish to force openGL to actually draw the stuff on the screen. Or you have to swap the buffers if you have double-buffer. Which you have (at least you use swappbuffers).

BUT:

You shouldn't (or better: mustn't) mix rendering with logic. You have rendering in your mouse handler logic, and you have logic stuff (reading and converting mouse coordinates) in your rendering. This thing can entirely screw rendering and logic, maybe it only works by accident in your case (we should see the WindProc to decide).

Frankly, the rendering and the logic is scattered all over the place, it's very hard to follow the program flow.



So:
Store the transformed (world) coordinates in a vector where you clicked with the mouse, then in the renderer, render the scene with the lines (you read the coordinates from the vector).
The most important thing is to decouple the logic from the render.


Maybe it's not clear at all, but honestly there are so many flaws in your program that it would be easier just to write the program for you than to explain it.


A note for rendering the lines: you should disable writing to the depth buffer (glDepthMask(false) ) so that you don't read the depth values of an already drawn line (unless you want that of course, but I doubt it).


Other problems: with lines, it's pretty much useless to use lighting. Lighting doesn't make things 3D as the comment in your code suggests. Maybe you copied it from a sloppy tutorial. Lighting makes the scene look 3D, if there are polygons. Lighting requires normals. You don't specify any normals, and how would you do for lines anyway? This may cause that the lines you draw are black, since 0,0,0 is the default value of a normal (since the normals' coordinates are state variables, unlike vertex coordinates).



Maybe you are doing too much things at once. The thing you want to do is not the most beginner openGL topic IMHO, but you do seem like a total beginner (I'm sorry if I sound harsh).

Edited by szecs, 23 May 2012 - 12:31 AM.


#6 DarkHorseKnight   Members   -  Reputation: 143

Like
-2Likes
Like

Posted 23 May 2012 - 01:23 AM

Dude my code is messed up because I leave stuff that I can in my code so I can come back to it later and my program flow is fine I can render 3d models to the screen and rotate the camera around with np and see them. I even coloured the models with no tex cords for fun and got this really cool model texture effect. Posted Image

I was asking how to draw a line under the mouse cursor not a course of fixing my so called problems ??? that were needed enforcements for my ease of use. Some people prefer less stuff. I prefer more stuff Posted Image .

but good advice for someone who needs that :)

Edited by DarkHorseKnight, 23 May 2012 - 01:27 AM.


#7 szecs   Members   -  Reputation: 2102

Like
0Likes
Like

Posted 23 May 2012 - 01:29 AM

Dude my code is messed up because I leave stuff that I can in my code so I can come back to it later and my program flow is fine I can render 3d models to the screen and rotate the camera around with np and see them. I even coloured the models with no tex cords for fun and got this really cool model texture effect. Posted Image

I was asking how to draw a line under the mouse cursor not a course of fixing my so called problems ??? that were needed enforcements for my ease of use. Some people prefer less stuff. I prefer more stuff Posted Image .

but good advice for someone who needs that Posted Image


Well, I was trying to help since the broken program flow may be the cause of the problem.
But ignore me if you like, just like my first post that explicitly shows a bug that is related.

#8 DarkHorseKnight   Members   -  Reputation: 143

Like
0Likes
Like

Posted 23 May 2012 - 01:40 AM

Well thank you for letting me know about why my graphics were being deleted when they went to far :)

#9 dpadam450   Members   -  Reputation: 863

Like
0Likes
Like

Posted 23 May 2012 - 08:56 AM

gluUnproject gets you a point in 3D space, you are calling glBegin(LINES) with posX, posY, 0. What are those? Are they in 3D space or are they the mouse coordinates?

When you draw that line, you also do not show anything about the current matrix, is it going to be the identity? Are you able to rotate your camera? If so, you have to calculate the mouses start and end both in 3D space by calling gluUnproject 2 times, one with a depth from the depth buffer, and the other with a depth equal to your near plane.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS