Results 1 to 5 of 5

Thread: Drawing a square visible surface problem

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2014
    Posts
    4

    Drawing a square visible surface problem

    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:

    wrongCube.jpg

    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:

    Code :
    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.

  2. #2
    Newbie Frequent Contributor
    Join Date
    Mar 2016
    Posts
    896
    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, 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.

  3. #3
    Senior Member Regular Contributor
    Join Date
    Sep 2013
    Posts
    186
    Or, alternatively, you could deactivate the culling of fron or back faces. This way your quads would be visible from either side.

  4. #4
    Junior Member Newbie
    Join Date
    Sep 2014
    Posts
    4
    Quote Originally Posted by Cornix View Post
    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.

  5. #5
    Junior Member Newbie
    Join Date
    Sep 2014
    Posts
    4
    Quote Originally Posted by Cornix View Post
    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.

Similar Threads

  1. Drawing a Square vs Loading a Square
    By krammark23 in forum OpenGL ES
    Replies: 3
    Last Post: 11-04-2008, 10:01 AM
  2. Drawing a square (problem with coordinates)
    By Tindor in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 10-31-2006, 02:42 PM
  3. Hermite surface drawing problem
    By imported_mini in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 10-28-2004, 09:37 PM
  4. Problem with drawing square sides, help!
    By blood.angel in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-14-2001, 06:21 AM
  5. visible surface detection w.r.t. ray tracing
    By drumminj in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 03-29-2000, 03:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean