-1

我想用 OpenGL 建立一个板球场。我制作了几个多边形来表示场地、球场和保龄球线。但问题是当我试图用gluCylinder我做深度的树桩时glEnable(GL_DEPTH_TEST),我的多边形就不起作用了。我只想知道如何使用 gluCylinder 用我制作的多边形制作树桩。

我有以下代码,但想在这里添加树桩但我不能,

#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; 
}
4

1 回答 1

1

glEnable(GL_DEPTH_TEST)需要深度缓冲区。确保在您的通话中通过orin分配一个。GLUT_DEPTHglutInitDisplayMode()

确保通过调用中的 inor清除新的深度缓冲区。GL_DEPTH_BUFFER_BITglClear()

#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; 
}

请注意,GL_POLYGON仅支持多边形。

于 2012-07-18T17:55:10.387 回答