0

Say I want to draw a Ball in the scene and here are two different ways to do them.

float SUN_TRANSLATION_MATRIX[] = {
  1.0f, 0.0f, 0.0f, 0.0f,
  0.0f, 1.0f, 0.0f, 0.0f,
  0.0f, 0.0f, 1.0f, -15.0f,
  0.0f, 0.0f, 0.0f, 1.0f
};

void displaySolarSystem1(){
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslatef(0.0f, 0.0f, -15.f);
  glColor3f(1.0f, 0.8f, 0.5f);
  glutSolidSphere(2.0, 50, 40);
  glutSwapBuffers();
}

void displaySolarSystem(){
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();         
  glMultMatrixf(SUN_TRANSLATION_MATRIX);
  glColor3f(1.0f, 0.8f, 0.5f);
  glutSolidSphere(2.0, 50, 40); 
  glutSwapBuffers();
}

displaySolarSystem1 applies glTranslatef where displaySolarSystem uses Matrix operation the problem is that displaySolarSystem1 works as expected but the matrix failed.

What went wrong with displaySolarSystem()?

3
  • 3
    See stackoverflow.com/questions/4360918/…. Basically your matrix is transposed. Commented Jun 13, 2012 at 11:17
  • 1
    I'm not sure, but i gues that you should simply get the translation information from the matrix, and use glTranslatef to translate it just like in the displaySolarSystem1() Commented Jun 13, 2012 at 11:18
  • Not an answer but a debugging hint, use glGetDoublev(GL_MODELVIEW, modelMatrix); to inspect the matrix in the case where it does work then compare to the other one. Commented Jun 13, 2012 at 11:20

2 Answers 2

2

http://www.opengl.org/sdk/docs/man/xhtml/glMultMatrix.xml

Calling glMultMatrix with an argument of m = [...] replaces the current transformation with C × M × v

Which means that transformations are applied by multiplying matrix by vector, not vice versa. So, translation matrix would be something like this 1 0 0 X 0 1 0 Y 0 0 1 Z 0 0 0 1 But this is matrix, that is written in row-major order, and OpenGL takes column-major order matrices, so you need to transpose it. So, finally, you just need to use glMultTransposeMatrix which, if I remember right, is slightly slower, or transpose your matrix to look like this

float SUN_TRANSLATION_MATRIX[] = {
  1.0f, 0.0f, 0.0f, 0.0f,
  0.0f, 1.0f, 0.0f, 0.0f,
  0.0f, 0.0f, 1.0f, 0.0f,
  0.0f, 0.0f, -15.0f, 1.0f
};
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks Unwind. The problem was solved by changing glMultMatrixf(SUN_TRANSLATION_MATRIX); to glMultTransposeMatrixf(SUN_TRANSLATION_MATRIX); thanks for your hint of "this is a transposed matrix"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.