Drawing Polygon using mouse!

Started by
4 comments, last by 3pid 14 years, 3 months ago
Hey guys, I want to draw a Polygon using mouse clicks . I store vertices coordinate in an array whenever the mouse click a position in opengl window. my only problem is that when I run this program a vertic has been drawn! and also whenever I click on my opengl screen another vertic would apear on my screen and It changes its position on its own!!!! whats wrong with my code???:-P int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer glLoadIdentity(); glTranslatef(0.0f,0.0f,-6.0f); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glRotatef(xrot,1.0f,0.0f,0.0f); glRotatef(yrot,0.0f,1.0f,0.0f); glColorPointer(3, GL_FLOAT, 0, Colors); glVertexPointer(3, GL_FLOAT, 0,vertex); glColor3f(1.0f,1.0f,0.0f); glDrawArrays(GL_POLYGON, 0, 24); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY);
Advertisement
Of course only a "vertic" will be drawn:

glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);

Why are these there? What values xrot and yrot have? You turn your polygon away from the camera very nicely.
Well I want to rotate my polygon so I declare
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);

GLfloat xrot; // X Rotation
GLfloat yrot; // Y Rotation

Is this wrong?
And here is how I get a mouse click coordinate



void mouseSelect()
{
GLint viewport[4];

glGetIntegerv( GL_VIEWPORT, viewport );
GLdouble modelview[16];
GLdouble projection[16];
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
POINT mouse; // Stores The X And Y Coords For The Current Mouse Position
GetCursorPos(&mouse); // Gets The Current Cursor Coordinates (Mouse Coordinates)
ScreenToClient(hWnd, &mouse);

GLfloat winX, winY, winZ;



winX = (float)mouse.x;
winY = (float)mouse.y;
winY = (float)viewport[3] - winY;
glReadPixels( winX, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);


vertex[C++]=posX;
vertex[C++]=posY;
vertex[C++]=posZ;}




this method will be called whenever the user left click on the screen

case WM_LBUTTONDOWN: // Did We Receive A Left Mouse Click?
{

mouseSelect();
return 0;
}
hey, what's happening is that your vertex array is full of (0, 0, 0) so you're drawing a GL_POLYGON with vertices at (0, 0, 0)..

so you need to track how many clicks the user has made, and draw that number many vertices..


secondly, if the user has only clicked once or twice, you probably won't see much.. you should throw in something to draw the individual vertices as dots or something, and maybe something to also draw lines between the verts, so you can see everything more clearly. just a thought...

see how all that works out and let me know if you still need help.

also you might want to make sure to glDisable(GL_CULL_FACE) I believe it is, just in case the user decides to draw the polygon counter-clockwise or something (because then opengl won't draw it as backface culling is all about that).
hi,Thankss!!!It was really useful!:-) I`m gonna rewrite my code and let you know
if(!lp)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
}
if(keys['L'])
lp=TRUE;

if(!fp && lp)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

if (keys['F'])
{
fp=TRUE;
}
if(fp)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}

This topic is closed to new replies.

Advertisement