Advertisement

Mouse clicks and drawing

Started by June 25, 2004 07:36 PM
7 comments, last by Intamin AG 20 years, 5 months ago
Hi Ive been working through the NeHe tutorials for OpenGL, and was wondering how to do the following: When a use clicks a portion of the screen, for example, a box will be drawn at those coordinates. Ive gotten the WM_LBUTTONDOWN message to be handled and im able to get the x,y coordinates, convert them to the corresponding float values to draw them in OpenGL. However when i call the function to draw the Box, it never draws it. I call this function(s) when a mouseclick is intercepted

int DrawHexagon(float x, float y)
{
	Hexagon h;
	Point p(x,y);
	h.drawHexagon(p);

	return TRUE;
}

void Hexagon::drawHexagon(Point p)
{
	glTranslatef(p.x,p.y,0.00f);
				
	glBegin(GL_TRIANGLES);	

		//1
		glVertex3f(p.x+0.0f     ,p.y+0.0f     , p.z);					// Top
		glVertex3f(p.x+(-1.0f)  ,p.y+2.0f     , p.z);					// Top Left
		glVertex3f(p.x+ 1.0f    ,p.y+2.0f     , p.z);					// Top Right

		//2
		glVertex3f(p.x+1.0f     ,p.y+2.0f     , p.z);					// Top
		glVertex3f(p.x+0.0f     ,p.y+0.0f     , p.z);					// Bottom Left
		glVertex3f(p.x+2.0f     ,p.y+0.0f     , p.z);					// Bottom Right

		//3
		glVertex3f(p.x+1.0f     ,p.y+(-2.0f)  ,p.z);					// Top
		glVertex3f(p.x+0.0f     ,p.y+0.0f     ,p.z);					// Bottom Left
		glVertex3f(p.x+2.0f     ,p.y+0.0f     ,p.z);					// Bottom Right
		
		//4
		glVertex3f(p.x+ 0.0f    ,p.y+0.0f     ,p.z);					// Top
		glVertex3f(p.x+(-1.0f)  ,p.y+(-2.0f)  ,p.z);					// Bottom Left
		glVertex3f(p.x+ 1.0f    ,p.y+(-2.0f)  ,p.z);					// Bottom Right

		//5
		glVertex3f(p.x+(-1.0f)  ,p.y+(-2.0f)  ,p.z);					// Top
		glVertex3f(p.x+(-2.0f)  ,p.y+0.0f     ,p.z);					// Bottom Left
		glVertex3f(p.x+ 0.0f    ,p.y+0.0f     ,p.z);					// Bottom Right

		//6
		glVertex3f(p.x+(-1.0f)  ,p.y+2.0f     ,p.z);					// Top
		glVertex3f(p.x+(-2.0f)  ,p.y+0.0f     ,p.z);					// Bottom Left
		glVertex3f(p.x+0.0f     ,p.y+0.0f     ,p.z);					// Bottom Right

	glEnd();	
}
Now is this the correct way to draw to the screen? If i call the original function DrawGLScene(x,y); with those parameters the window seems to slow down when its created, leading me to believe that this isnt the way to do it?? Any ideas? If you need to see more code i can post it up. Thanks in advance.
My guess is it IS drawing, but you are drawing it at the wrong coordinates.

You are using glTranslatef to translate to the position of the mouse but you are using the absolute coordinates when drawing the triangles.

The solution: take out that glTranslatef OR erase all those p.x and p.y in the vertex fields.
Advertisement
I took out the glTranslatef to see if worked, and i also tried removing the p.x's etc and still no luck :(
Maybe the camera is in such a position it cannot see the object?
It may be too close or too far. That's all I can think of besides the glTranslatef issue (which you should remove).
Well i tried calling the DrawHexagon function on initialisation of the window, and it drew at the cordinates correctly, so i removed this and placed the function call when the mouse is clicked (i also used hardcoded coordinates to make sure my coordinates were correct).

Still no joy.

Sigh, do i have to call the following each time i wish to draw tho?

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	glLoadIdentity();


Im only calling this once during initialisation. does this need to be called each time i draw something?
Well i tried calling the DrawHexagon function on initialisation of the window, and it drew at the cordinates correctly, so i removed this and placed the function call when the mouse is clicked (i also used hardcoded coordinates to make sure my coordinates were correct).

Still no joy.

Sigh, do i have to call the following each time i wish to draw tho?


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();



Im only calling this once during initialisation. does this need to be called each time i draw something?


Mmm, if you are calling the drawHexagon only when the mouse is clicked you'll most probably not see the hexagon because once you stop clicking it won't be called, and it won't draw it. Since a computer iteration takes so little time, it may look to you like it hasn't been drawn. So you need to create some variable or method that says: "if the mouse is clicked, create a new hexagon at x,y position, and keep drawing it forever". First try out your function with some constant values, and ALWAYS call it. Then try your thing.

Oh and yeah you should call them on every single draw, at least if you take the glLoadIdentity() line, you'll get some freaky results sometimes (if you happen to do matrix transformations).
Advertisement
Great got it !

As you suggested i added a BOOLEAN which checks to see if it was clicked, and in the DrawGLScene checked if it was set to true, and it worked !!!

Thanks for all the help!
Good to know! Anytime!
In case you need added functionality rid the bool, add a counter so it only can draw a new one one like every 1/4 second
and use endless while-loops so you can have multiple ones :)
The world isn't unpredictable. It's CHAOTIC.

This topic is closed to new replies.

Advertisement