Sign in to follow this  
giugio

[c++]Cube and face colors

Recommended Posts

I try to draw a cube , with multi face color. the problem is that some face appear trasparent and if i rotate the poly i see the hiddens face! this is the initgl code: glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_DIFFUSE); //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globalAmbient); glViewport(0, 0, width, height); // reset the viewport to new dimensions glMatrixMode(GL_PROJECTION); // set projection matrix current matrix glLoadIdentity(); // reset projection matrix // calculate aspect ratio of window gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f); glMatrixMode(GL_MODELVIEW); // set modelview matrix glLoadIdentity(); // reset modelview matrix and this is the render loop ,the camera class is ok , i tested it while (!exiting) { glClear(GL_COLOR_BUFFER_BIT /*GL_DEPTH_BUFFER_BIT*/ | GL_STENCIL_BUFFER_BIT); GetInput(); glLoadIdentity(); gluLookAt(m_camera->xPos, m_camera->yPos, m_camera->zPos,m_camera->xView ,m_camera->yView, m_camera->zView,m_camera->xUp, m_camera->yUp, m_camera->zUp); glBegin(GL_QUADS); glColor3f(0.0f, 0.0f,0.5f); glVertex3f(0.3f, 0.0f, 0.0f); glColor3f(0.0f, 0.0f,0.5f);glVertex3f(-0.2f, 0.0f, 0.0f); glColor3f(0.0f, 0.0f,0.5f); glVertex3f(-0.2f, 0.5f, 0.0f); glColor3f(0.0f, 0.0f,0.5f);glVertex3f(0.3f, 0.5f, 0.0f); glEnd(); // Back face. glBegin(GL_QUADS); glColor3f(0.0f, 0.0f,1.0f); glVertex3f(0.3f, 0.0f, -0.5f); glColor3f(0.0f, 0.0f,1.0f);glVertex3f(-0.2f, 0.0f, -0.5f); glColor3f(0.0f, 0.0f,1.0f); glVertex3f(-0.2f, 0.5f, -0.5f); glColor3f(0.0f, 0.0f,1.0f); glVertex3f(0.3f, 0.5f, -0.5f); glEnd(); // Left face. glBegin(GL_QUADS); glColor3f(0.0f, 0.5f,0.0f); glVertex3f(-0.2f, 0.0f, -0.5f); glColor3f(0.0f, 0.5f,0.0f); glVertex3f(-0.2f, 0.5f, -0.5f); glColor3f(0.0f, 0.5f,0.0f); glVertex3f(-0.2f, 0.5f, 0.0f); glColor3f(0.0f, 0.5f,0.0f); glVertex3f(-0.2f, 0.0f, 0.0f); glEnd(); // Right face. glBegin(GL_QUADS); glColor3f(0.0f, 1.0f,0.0f); glVertex3f(0.3f, 0.0f, -0.5f); glColor3f(0.0f, 1.0f,0.0f); glVertex3f(0.3f, 0.5f, -0.5f); glColor3f(0.0f, 1.0f,0.0f);glVertex3f(0.3f, 0.5f, 0.0f); glColor3f(0.0f, 1.0f,0.0f); glVertex3f(0.3f, 0.0f, 0.0f); glEnd(); // Top face. glBegin(GL_QUADS); glColor3f(0.5f, 0.0f,0.0f); glVertex3f(-0.2f, 0.5f, 0.0f); glColor3f(0.5f, 0.0f,0.0f); glVertex3f(-0.2f, 0.5f, -0.5f); glColor3f(0.5f, 0.0f,0.0f); glVertex3f(0.3f, 0.5f, -0.5f); glColor3f(0.5f, 0.0f,0.0f); glVertex3f(0.3f, 0.5f, 0.0f); glEnd(); // Bottom face. glBegin(GL_QUADS); glColor3f(1.0f, 0.0f,0.0f); glVertex3f(-0.2f, 0.0f, 0.0f); glColor3f(1.0f, 0.0f,0.0f); glVertex3f(-0.2f, 0.0f, -0.5f); glColor3f(1.0f, 0.0f,0.0f); glVertex3f(0.3f, 0.0f, -0.5f); glColor3f(1.0f, 0.0f,0.0f); glVertex3f(0.3f, 0.0f, 0.0f); glEnd(); // if(m_poly) // m_poly->Draw(); SwapBuffers(hDC); } thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by giugio
I try to draw a cube , with multi face color.
the problem is that some face appear trasparent and if i rotate the poly i see the hiddens face!
glClear(GL_COLOR_BUFFER_BIT  /*GL_DEPTH_BUFFER_BIT*/ | GL_STENCIL_BUFFER_BIT);

Why is GL_DEPTH_BUFFER_BIT commented out? You'll want to clear it each frame. I believe GL_DEPTH_TEST is enabled by default, but it's worth throwing in if the above doesn't fix the problem: glEnable(GL_DEPTH_TEST);

[Edit: Also, when posting large blocks of code like this, it's preferable to use the [source] and [/source] tags to make it more readable and take up less real estate. More on this in the FAQ.]

Share this post


Link to post
Share on other sites
Hi!

First of all, use [ source ] [ /source ] tags (without the spaces) when posting code.

The problem is with the order of your vertices. In OpenGL the order of your vertices defines their facing: by default (IIRC), counterclockwise is front-facing, clockwise is backfacing.

To test if this is really the problem you can disable backface culling by calling glDisable(GL_CULL_FACE). This should fix the see-through problem, however we need backface culling to render at reasonable speed so you should fix the ordering of your vertices.

Share this post


Link to post
Share on other sites
Quote:
Original post by jouley
... I believe GL_DEPTH_TEST is enabled by default, but it's worth throwing in if the above doesn't fix the problem: glEnable(GL_DEPTH_TEST);


GL_DEPTH_TEST is disable by default. So, we must enable it explicitly.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this