gluCylinder()如何工作OpenGL

时间:2012-07-18 16:36:49

标签: c opengl glu

我想用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; 
}

1 个答案:

答案 0 :(得分:0)

glEnable(GL_DEPTH_TEST)需要深度缓冲区。请务必在or来电中通过GLUT_DEPTH glutInitDisplayMode()分配一个。

务必通过or来电GL_DEPTH_BUFFER_BIT glClear()来清除新的深度缓冲区。

#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仅支持多边形。