Reputation: 19
I had faced the problem of using a line loop to draw out a 3D cube by using open gl.
void cube1()
{
glBegin(GL_LINE_LOOP);
//Cube1
//Face1
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-0.1, 0, 0);
glVertex3f(0.4, 0, 0);
glVertex3f(0.4, -0.3, 0);
glVertex3f(-0.1, -0.3, 0);
//Face2
glColor3f(1.0, 1.0, 0.0);
glVertex3f(-0.1, -0.3, 0);
glVertex3f(-0.1, -0.3, 1);
glVertex3f(-0.1, 0, 1);
glVertex3f(-0.1, 0, 0);
//Face3
glColor3f(1.0, 0.0, 1.0);
glVertex3f(-0.1, 0, 0);
glVertex3f(-0.1, 0, 1);
glVertex3f(0.4, 0, 1);
glVertex3f(0.4, 0, 0);
//Face4
glColor3f(0.0, 1.0, 1.0);
glVertex3f(0.4, 0, 0);
glVertex3f(0.4, 0, 1);
glVertex3f(0.4, -0.3, 1);
glVertex3f(0.4, -0.3, 0);
//Face5
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.4, -0.3, 0);
glVertex3f(-0.1, -0.3, 0);
glVertex3f(-0.1, -0.3, 1);
glVertex3f(0.4, -0.3, 1);
//Face6
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.4, -0.3, 1);
glVertex3f(-0.1, -0.3, 1);
glVertex3f(-0.1, 0, 1);
glVertex3f(0.4, 0, 1);
glEnd();
}
However, I still could not form the correct shape for my cube.Below are my output. Can anyone know the solution?
Upvotes: 1
Views: 588
Reputation: 210908
A line loop is an endless primitive. It behaves differently from GL_QUADS
or GL_TRIANGLES
(see OpenGL - Primitive). Therefore, you must draw 6 separate line loops (or at least 4). One for each side:
//Face1
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex3f(-0.1, 0, 0);
glVertex3f(0.4, 0, 0);
glVertex3f(0.4, -0.3, 0);
glVertex3f(-0.1, -0.3, 0);
glEnd();
//Face2
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex3f(-0.1, -0.3, 0);
glVertex3f(-0.1, -0.3, 1);
glVertex3f(-0.1, 0, 1);
glVertex3f(-0.1, 0, 0);
glEnd();
// [...]
Upvotes: 1