0

我正在尝试在屏幕上绘制一个四边形(正方形)。我将它插入到一个预先存在的绘制圆柱体的程序中。这是在正交模型视图矩阵中,我几乎肯定剪裁量是正确的(从原点的任何方向都是 200)。在我使用的显示函数中,我推了一个矩阵,向前平移 (0.0,0.0,-20.0),称为四边形,然后弹出矩阵。我不知道人们使用的任何常见的openGL设置是否可能使其不可见?有没有办法获取当前的剪辑量并在控制台中打印出来?

void quadrilateral() 
{
    glLoadIdentity();
    glColor3f(0.5f,0.0f,0.8f);
    glBegin(GL_QUADS);

    glVertex3f(-10.0f,-10.0f,0.0f);
    glVertex3f(-10.0f,10.0f,0.0f);
    glVertex3f(10.0f,10.0f,0.0f);
    glVertex3f(10.0f,-10.0f,0.0f);

    glEnd();
}


//This is called from a main function elsewhere
void draw(void)
{
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(500, 500); 
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Cylinder");
    glutReshapeFunc(CChangeSize);
    glutSpecialFunc(CSpecialKeys);
    glutKeyboardFunc(Ckeyboard);
    glutDisplayFunc(CRenderScene);
    CSetupRC();
    glPopMatrix();
}


void CSetupRC()
{
// Light values and coordinates
GLfloat  ambientLight[] = {0.4f, 0.4f, 0.4f, 1.0f };
GLfloat  diffuseLight[] = {0.7f, 0.7f, 0.7f, 1.0f };
GLfloat  specular[] = { 0.9f, 0.9f, 0.9f, 1.0f};
GLfloat  lightPos[] = { -50.0f, 200.0f, 200.0f, 1.0f };
GLfloat  specref[] =  { 0.6f, 0.6f, 0.6f, 1.0f };


glEnable(GL_DEPTH_TEST);    // Hidden surface removal
glEnable(GL_CULL_FACE);     // Do not calculate inside of solid object
glFrontFace(GL_CCW);

// Enable lighting
glEnable(GL_LIGHTING);

// Setup light 0
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular);

// Position and turn on the light
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glEnable(GL_LIGHT0);

// Enable color tracking
glEnable(GL_COLOR_MATERIAL);

// Set Material properties to follow glColor values
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

// All materials hereafter have full specular reflectivity
// with a moderate shine
glMaterialfv(GL_FRONT, GL_SPECULAR,specref);
glMateriali(GL_FRONT,GL_SHININESS,64);

// Black background
glClearColor(0.0f, 0.0f, 0.0f, 0.0f );


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_TEXTURE_2D);

}


// Called to draw scene
void CRenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Save the matrix state
glMatrixMode(GL_MODELVIEW);
glPushMatrix();

// Rotate about x and y axes
glRotatef(CxRot, 1.0f, 0.0f, 0.0f);
glRotatef(CyRot, 0.0f, 1.0f, 0.0f);

// Draw the cylinder
cylinder();

glPopMatrix();

// CylinderTwo
glMatrixMode(GL_MODELVIEW);
glPushMatrix();

// Rotate about x and y axes
glRotatef(CxRot, 1.0f, 0.0f, 0.0f);
glRotatef(CyRot, 0.0f, 1.0f, 0.0f);

// Draw the cylinder
glRotatef(120.0f, 1.0f, 1.0f, 1.0f);
cylinderTwo();

glPopMatrix();

// Random square
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(-30.0f,20.0f,10.0f);
quadrilateral();
glPopMatrix();

// Swap buffers
glutSwapBuffers();

}

4

2 回答 2

1

看起来您的缠绕顺序不正确:

glEnable(GL_CULL_FACE);

这条线意味着它将剔除背面。

glFrontFace(GL_CCW);

这会将正面设置为逆时针方向。

    glVertex3f(-10.0f,-10.0f,0.0f);
    glVertex3f(-10.0f,10.0f,0.0f);
    glVertex3f(10.0f,10.0f,0.0f);
    glVertex3f(10.0f,-10.0f,0.0f);

在这里,您的顶点按顺时针顺序排列,这意味着您正在背对相机进行绘制。要么禁用剔除:

    glDisable(GL_CULL_FACE);

顺时针制作正面:

    glFrontFace(GL_CW);

或将顶点的顺序更改为逆时针顺序。

于 2012-07-11T16:24:56.430 回答
0

在我看来,您的四边形向后缠绕。您已启用 GL_CULL_FACE,它会抛出任何顺时针缠绕的多边形,并且您的四边形具有顺时针缠绕。

禁用背面剔除,或将顶点的顺序切换为逆时针。

于 2012-07-11T16:23:25.367 回答