PDA

View Full Version : Drawing a square visible surface problem



bremenpl
09-06-2014, 11:45 AM
Hello there,
I am a very begginer in the whole openGL matter.
I am writing a program in Qt Creator for windows to solve a Rubics Cube by an User. For the beggining I would like to draw a simple 6 coloder cube. Although I have meet an obstacle that I cannot seem to pass. So far I have draw 3 walls of the cube. Ive noticed I dont know how to set the visible surface side of the draw object. please cosider bellow picture:

1428

As one can see the blue square is visible from the wrong side. Its not visible from the other one. I thought the normal vector decides which side the surface is colored but it doesnt.

Those methods are called to init and draw the image:


void MyGLWidget::initializeGL()
{
qglClearColor(Qt::black);

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glShadeModel(GL_SMOOTH);
}

void MyGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -10.0);
glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
draw();
}

void MyGLWidget::draw()
{
qglColor(Qt::red);
glBegin(GL_QUADS);
glNormal3f(-2,0,0);
glVertex3f(-1,-1,1);
glVertex3f(-1,1,1);
glVertex3f(-1,1,-1);
glVertex3f(-1,-1,-1);
glEnd();

qglColor(Qt::yellow);
glBegin(GL_QUADS);
glNormal3f(0,0,2);
glVertex3f(-1,1,1);
glVertex3f(-1,-1,1);
glVertex3f(1,-1,1);
glVertex3f(1,1,1);
glEnd();

qglColor(Qt::blue);
glBegin(GL_QUADS);
glNormal3f(2,0,0);
glVertex3f(1,1,-1);
glVertex3f(1,-1,-1);
glVertex3f(1,-1,1);
glVertex3f(1,1,1);
glEnd();

}

I would aprichiate any help really.

carsten neumann
09-06-2014, 12:19 PM
The normal vectors are used in lighting calculations. In order to decide what is the front/back of a triangle the winding of the vertices is used. This is (at least conceptually) determined by projecting the three corners of the triangle to the screen and then looking at if they are ordered clock wise or counter clock wise. By default OpenGL considers the side with counter clock wise winding to be the front (can be changed with glFrontFace (http://www.opengl.org/sdk/docs/man/html/glFrontFace.xhtml), but it should normally just be left alone).
To fix your problem you need to specify the corners of the blue side in a different order.

Cornix
09-06-2014, 01:02 PM
Or, alternatively, you could deactivate the culling of fron or back faces. This way your quads would be visible from either side.

bremenpl
09-06-2014, 04:29 PM
Or, alternatively, you could deactivate the culling of fron or back faces. This way your quads would be visible from either side.

Thank you for answer. Changing the corners order helped indeed. I dont know what that culling is but ill have a look onto it.

bremenpl
09-07-2014, 07:17 AM
Or, alternatively, you could deactivate the culling of fron or back faces. This way your quads would be visible from either side.

Thanks again Cornix I have the culling sort out. Later when I thought of it i recalled that I even had to deal with it in the class at university :P.
Thanks again.