1
\$\begingroup\$

I am trying to draw a picket fence, but the center of the picket vertex looks bent in the middle,

why is the picket bending inwards to the left?

// draw fence pickets
float a;
for (a=-25; a<=25; a+=0.256) {

    glBegin(GL_POLYGON);
    glColor3f(  1,  1,  1 );
    glVertex3f( a,       25, 0 );
    glVertex3f( a+0.128, 25, 0 );
    glVertex3f( a,       25, 1 );
    glVertex3f( a+0.128, 25, 1 );
    glEnd();

}

enter image description here

\$\endgroup\$
  • 1
    \$\begingroup\$ Since you are drawing a quadrilateral (4 verts), use GL_QUADS instead of GL_POLYGON. \$\endgroup\$ – glampert May 4 '14 at 2:53
  • \$\begingroup\$ @glampert Thanks, that made the rendering a lot faster. \$\endgroup\$ – kyle k May 4 '14 at 6:00
  • \$\begingroup\$ Note that you are using the very old fixed-function pipeline of OpenGL. I would start reading this free book to get up-to date :) arcsynthesis.org/gltut \$\endgroup\$ – Roy T. May 4 '14 at 12:01
2
\$\begingroup\$

Your vertices are in the wrong order. Try this:

glVertex3f( a,       25, 0 );
glVertex3f( a+0.128, 25, 0 );
glVertex3f( a+0.128, 25, 1 );
glVertex3f( a,       25, 1 );

Remember that the vertices have to be in the order you would find by tracing around the outline of the polygon counterclockwise. Your original order was cutting through the middle of the polygon.

\$\endgroup\$
  • \$\begingroup\$ +1 It's a recurring problem with people starting OpenGL programming. I also recommend this visual guide to illustrate how to use geometric primitives. Nice to see you on Stackexchange, Nathan, I've sent you a tweet! \$\endgroup\$ – karlphillip May 4 '14 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.