user2813286
user2813286

Reputation: 15

How to use glMultMatrix( )

I am new to opengl. I try to move the sphere around with my own matrix, but the result is not correct.

The sphere on the left side is what I expect, and I produce the result on the right side with glMultMatrixd(). What I did wrong?

void Display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glDisable(GL_LIGHTING);
    glDisable(GL_TEXTURE_2D);

    glPushMatrix();
    GLdouble translate[16] = {1,0,0,1,0,1,0,0,0,0,1,0,0,0,0,1};    
    glMultMatrixd(translate);
    DrawSphere();
    glPopMatrix();

    glPushMatrix();
    glTranslatef(1,0,0);
    DrawSphere();
    glPopMatrix();

    glutSwapBuffers();
}   
void Reshape(int width, int height) {
    tbReshape(width, height);

    glViewport(0, 0, width, height);
    glGetIntegerv(GL_VIEWPORT, viewport);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(60.0, (GLdouble)width/height, 0.01, 100);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.0, 0.0, -5, // eye
              0.0, 0.0, 0.0, // center
              0.0, 1.0, 0.0); // up
}

Upvotes: 0

Views: 8925

Answers (1)

afpro
afpro

Reputation: 1103

GLdouble translate[16] = {1,0,0,1,0,1,0,0,0,0,1,0,0,0,0,1};

is the same as

glTranslated(1, 0, 0)

means translate local coordinate system by (1, 0, 0)

result is on the right side.

nothing is wrong, you just didn't understand 'matrix algorithm'

suggest learn linear algebra before use matrix directly.

------------ edit--------------

sorry for my fault. GLdouble translate[16] = {1,0,0,0,0,1,0,0,0,0,1,0,1,0,0,1}; is the right matrix.

opengl's matrix is column-major, so: GLdouble m[16] layout as

m[0] m[4] m[8]  m[12]
m[1] m[5] m[9]  m[13]
m[2] m[6] m[10] m[14]
m[3] m[7] m[11] m[15]

Upvotes: 1

Related Questions