Question

I wanted to build a cricket ground with OpenGL. I made several polygons to indicate field, pitch and bowling lines. But the problem is when I am trying to use gluCylinder to make stumps I made depth glEnable(GL_DEPTH_TEST), but my polygons are then not working. I just want to know how Can I use gluCylinder to make stumps with those polygons I have made.

I have following code, but want to add stumps here but I cant,

#include <GL/gl.h>
#include <GL/glut.h>
static double deg=0.0;
void display(void)
{

glClear (GL_COLOR_BUFFER_BIT);

glRotatef(deg, 0.0, 0.0, 1.0); // Rotate by deg
// field
glColor3f (0.0, 0.5, 0.0);
glBegin(GL_POLYGON);
glVertex3f (0, 0, 0.0);
glVertex3f (1, 0, 0.0);
glVertex3f (1, 0.75, 0.0);
glVertex3f (0.8, 0.82, 0.0);
glVertex3f (0.6, 0.85, 0.0);
glVertex3f (0.4, 0.85, 0.0);
glVertex3f (0.2, 0.82, 0.0);
glVertex3f (0.0, 0.75, 0.0);
glEnd();

// pitch
glColor3f (0.25, 0.30, 0.0);
glBegin(GL_POLYGON);
glVertex3f(0.5,0.65,0.0);
glVertex3f(0.47,0.35,0.0);
glVertex3f(0.60,0.35,0.0);
glVertex3f(0.57,0.65,0.0);
glEnd();

//ump line
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.49,0.63,0.0);
glVertex3f(0.49,0.6315,0.0);
glVertex3f(0.58,0.6315,0.0);
glVertex3f(0.58,0.63,0.0);
glEnd();

//bat line
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.46,0.40,0.0);
glVertex3f(0.46,0.4025,0.0);
glVertex3f(0.61,0.4025,0.0);
glVertex3f(0.61,0.40,0.0);
glEnd();
glFlush ();
}
void init (void)
{

glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27: // "esc" on keyboard
exit(0);
break;
case 97: // "a" on keyboard
deg = deg+5.0;
glutPostRedisplay();
break;
case 100:
deg = deg-5.0;
glutPostRedisplay();
break;
}
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (600, 600);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0; 
}
Was it helpful?

Solution

glEnable(GL_DEPTH_TEST) requires a depth buffer. Make sure to allocate one via oring in GLUT_DEPTH in your glutInitDisplayMode() call.

Make sure to clear your new depth buffer via oring in GL_DEPTH_BUFFER_BIT in your glClear() call.

#include <GL/glut.h>

static double deg=0.0;

void display(void)
{
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    glRotatef(deg, 0.0, 0.0, 1.0); // Rotate by deg

    // field
    glColor3f (0.0, 0.5, 0.0);
    glBegin(GL_POLYGON);
    glVertex3f (0, 0, 0.0);
    glVertex3f (1, 0, 0.0);
    glVertex3f (1, 0.75, 0.0);
    glVertex3f (0.8, 0.82, 0.0);
    glVertex3f (0.6, 0.85, 0.0);
    glVertex3f (0.4, 0.85, 0.0);
    glVertex3f (0.2, 0.82, 0.0);
    glVertex3f (0.0, 0.75, 0.0);
    glEnd();

    // pitch
    glColor3f (0.25, 0.30, 0.0);
    glBegin(GL_POLYGON);
    glVertex3f(0.5,0.65,0.0);
    glVertex3f(0.47,0.35,0.0);
    glVertex3f(0.60,0.35,0.0);
    glVertex3f(0.57,0.65,0.0);
    glEnd();

    //ump line
    glColor3f (1.0, 1.0, 1.0);
    glBegin(GL_POLYGON);
    glVertex3f(0.49,0.63,0.0);
    glVertex3f(0.49,0.6315,0.0);
    glVertex3f(0.58,0.6315,0.0);
    glVertex3f(0.58,0.63,0.0);
    glEnd();

    //bat line
    glColor3f (1.0, 1.0, 1.0);
    glBegin(GL_POLYGON);
    glVertex3f(0.46,0.40,0.0);
    glVertex3f(0.46,0.4025,0.0);
    glVertex3f(0.61,0.4025,0.0);
    glVertex3f(0.61,0.40,0.0);
    glEnd();

    glPopMatrix();

    glFlush ();
}

void init (void)
{
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void keyboard(unsigned char key, int x, int y)
{
    switch (key) {
    case 27: // "esc" on keyboard
        exit(0);
        break;
    case 'a': // "a" on keyboard
        deg = deg+5.0;
        glutPostRedisplay();
        break;
    case 'z':
        deg = deg-5.0;
        glutPostRedisplay();
        break;
    }
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize (600, 600);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("hello");
    init ();
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 0; 
}

Be aware that GL_POLYGON only supports convex polygons.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top