PDA

View Full Version : Vertex Arrays, interleaved arrays, mouseMove func



Nibirue
02-13-2012, 08:53 AM
Hello all, I am working with OpenGL in C++ as a beginner to opengl, but not C++ so I apologize if my question is simple.

I need to write a mouseMove function that registers the (x,y) coordinate of the mouse when left clicked forming the vertices of a polygon with each successive left click. When you right click, the last made vertice connects to the original forming your polygon. I need to do this multiple times in the same canvas. So far my program draws the shape with each new vertice, but does not terminate via right click, rather an arbitrary bound I set. On top of this, I am not able to do multiple polygons in a single canvas. I think using the glInterleavedArrays function will do this, but the implementation for me is confusing. I have:


void mouseMove(int click, int state, int x, int y)
{
clearFramebuffer();
static int i = 0;
static int numVert = 0;
drawit();
//glEnableClientState(GL_VERTEX_ARRAY);

if(click==GLUT_LEFT_BUTTON &amp;&amp; state == GLUT_DOWN &amp;&amp; numVert<4){
keep_going==true;
vertices[i].x=x;
vertices[i].y=y;
i++;
clicked++;
numVert++;

}
if(click==GLUT_RIGHT_BUTTON &amp;&amp; state == GLUT_DOWN || numVert>4){ // more than numVert vertices

//glVertexPointer(2, GL_INT, 0, vertices);
//glDrawArrays(GL_POLYGON, 0, i/2);
keep_going=false;
clicked=0;
glFlush();
printf("finished!!!\n");
}
//glDisableClientState(GL_VERTEX_ARRAY);
glutPostRedisplay();
}

and this is called in my display function:


void display(void){
if(keep_going==true){
drawit();
glInterleavedArrays(GL_V2F,0, vertices);
//glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_INT, 0, vertices);
glDrawArrays(GL_POLYGON, 0, clicked);
glDisableClientState(GL_VERTEX_ARRAY);
} else if(keep_going==false){
clearFramebuffer();
}
glFlush();
}

My tests with glInterleavedArrays have all broken my program. If you can help me understand how to use it, I could finish it within my code. Please direct your explanation to something I could accomplish for my particular program.

BionicBytes
02-13-2012, 11:17 AM
Use either glinterleavedArrays or glVertexPointer but not both.
Remove the glEnableClientState and glVertexPointer and see what happens.