未绘制OpenGL四边形

时间:2012-07-11 16:04:29

标签: opengl 3d drawing

我试图在屏幕上绘制四边形(正方形)。我把它插入到一个预先存在的程序中,该程序绘制了一个圆柱体。这是在正交模型视图矩阵中,我几乎是正面的裁剪音量是正确的(从原点到任何方向都是200)。在我正在使用的显示功能中,我推了一个矩阵,向前翻译(0.0,0.0,-20.0),称为quadriliteral,然后弹出矩阵。人们使用的任何常见的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();

}

2 个答案:

答案 0 :(得分: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);

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

答案 1 :(得分:0)

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

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