wireframe cube

I have this little function that draws 3d cube. I would like to use it also to draw the cube as wireframe that result I am getting is not correct. It keeps adding diagonal lines between corners of each side.



void myFunction(int myVar){

glBegin(GL_QUADS);

   
    glVertex3f(myVar,myVar,myVar);
    glVertex3f(-myVar,myVar,myVar);
    glVertex3f(-myVar,-myVar,myVar);
    glVertex3f(myVar,-myVar,myVar);
 
    glVertex3f(myVar,myVar,-myVar);
    glVertex3f(-myVar,myVar,-myVar);
    glVertex3f(-myVar,-myVar,-myVar);
    glVertex3f(myVar,-myVar,-myVar);
 
    glVertex3f(myVar,myVar,myVar);
    glVertex3f(myVar,-myVar,myVar);
    glVertex3f(myVar,-myVar,-myVar);
    glVertex3f(myVar,myVar,-myVar);
 
    glVertex3f(-myVar,myVar,myVar);
    glVertex3f(-myVar,-myVar,myVar);
    glVertex3f(-myVar,-myVar,-myVar);
    glVertex3f(-myVar,myVar,-myVar);
 
    glVertex3f(myVar,myVar,myVar);
    glVertex3f(-myVar,myVar,myVar);
    glVertex3f(-myVar,myVar,-myVar);
    glVertex3f(myVar,myVar,-myVar);
 
    glVertex3f(myVar,-myVar,myVar);
    glVertex3f(-myVar,-myVar,myVar);
    glVertex3f(-myVar,-myVar,-myVar);
    glVertex3f(myVar,-myVar,-myVar);
 
glEnd();

}


Can anyone suggest whats wrong or how to use this same function to draw cube in wireframe.

I tested your code, just add glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); before glBegin(). Then if you want to fill your polygon as before, call glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); after glEnd();