Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Rotating a single object out of 3

  1. #1
    Junior Member Regular Contributor
    Join Date
    Aug 2009
    Posts
    107

    Smile Rotating a single object out of 3

    I have 3 objects on the screen, I want to rotate onll 2 objects the axis and X,Y,Z. Another line is there which I want not to rotate. But with this code below all are moving. Check below my DrawScene function.
    Code :
    void CopenGLCoordView::DrawScene(CDC *pDC)
    {
        wglMakeCurrent(pDC->m_hDC, m_hrc);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    
        glEnable(GL_DEPTH_TEST);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
     
     
        if (wd <= ht)
        glOrtho(left, right, top*ht/wd, bottom*ht/wd, znear, zfar);
        else
        glOrtho(left*wd/ht, right*wd/ht, top, bottom, znear, zfar);
     
     
        glPushMatrix();
        glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
        glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
        glRotatef(rot[2], 0.0f, 0.0f, 1.0f);
        glCallList(axes);
        glPrintX("X");
        glPrintY("Y");
        glPrintZ("Z");
        glPopMatrix();
     
        drawLine();  //This is also moving...i want this to not to move...
     
     
        glFlush();
        SwapBuffers(pDC->m_hDC);
     
        wglMakeCurrent(NULL, NULL);
    }

    Any help is highly appreciated. Thanks Sujan

  2. #2
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,400
    You forgot to use glGetError().

    You are using the projection matrix stack which tends to be not very deep. There is maybe a stack space of 2 or 4 and it is possible you are exceeding it.

    Do all your model manipulation on the modelview, which tends to be 16 and more stack deep.
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  3. #3
    Junior Member Regular Contributor
    Join Date
    Aug 2009
    Posts
    107
    I am trying so that my line from drawLine should not rotate. Others should rotate. But with the below code nothing is rotating. I knew that we can use glPushMatrix() and glPopMatrix(). But it's not working. Please help
    Code :
    void CopenGLCoordView::DrawScene(CDC *pDC)
    {
    	wglMakeCurrent(pDC->m_hDC, m_hrc);
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
    	glEnable(GL_DEPTH_TEST);
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
     
     
    	drawLine();
     
     
    	glPushMatrix();
    	glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
    	glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
    	glRotatef(rot[2], 0.0f, 0.0f, 1.0f);
    	drawAxes();
    	glPrintX("X");
        glPrintY("Y");
    	glPrintZ("Z");
    	glPopMatrix();
     
     
    	glFlush();
    	SwapBuffers(pDC->m_hDC);	
    	wglMakeCurrent(NULL, NULL);
    }

  4. #4
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,400
    Don't you think this make more sense :

    Code :
    void CopenGLCoordView::DrawScene(CDC *pDC)
    {
        wglMakeCurrent(pDC->m_hDC, m_hrc);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    
        glEnable(GL_DEPTH_TEST);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
     
     
        if (wd <= ht)
        glOrtho(left, right, top*ht/wd, bottom*ht/wd, znear, zfar);
        else
        glOrtho(left*wd/ht, right*wd/ht, top, bottom, znear, zfar);
     
        glMatrixMode(GL_MODELVIEW);
     
        glPushMatrix();
        glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
        glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
        glRotatef(rot[2], 0.0f, 0.0f, 1.0f);
        glCallList(axes);
        glPrintX("X");
        glPrintY("Y");
        glPrintZ("Z");
        glPopMatrix();
     
        drawLine();  //This is also moving...i want this to not to move...
     
     
        glFlush();
        SwapBuffers(pDC->m_hDC);
     
        wglMakeCurrent(NULL, NULL);
    }
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  5. #5
    Intern Contributor
    Join Date
    Apr 2012
    Posts
    70
    Try adding another glLoadIdentity() just after the glMatrixMode(GL_MODELVIEW) command.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •