PDA

View Full Version : about Matrix Multiplication



cl781121
06-24-2016, 07:29 PM
hi there,i dont want to use glm lib,so i change code like below

glm code


glm::mat4 lightProjection, lightView;
glm::mat4 lightSpaceMatrix;
GLfloat near_plane = 1.0f, far_plane = 7.5f;
lightProjection = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, near_plane, far_plane);
lightView = glm::lookAt(lightPos, glm::vec3(0.0f), glm::vec3(0.0, 1.0, 0.0));
lightSpaceMatrix = lightProjection * lightView;


i change it like this:


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat near_plane = 1.0f, far_plane = 7.5f;
GLdouble lightProjection[16],lightView[16];
GLfloat lightSpaceMatrix[16];
glOrtho(-10.0f, 10.0f, -10.0f, 10.0f, near_plane, far_plane);
glGetDoublev(GL_PROJECTION_MATRIX, lightProjection);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(lightPos[0],lightPos[1],lightPos[2],0,0,0,0,1,0);
glGetDoublev(GL_MODELVIEW_MATRIX, lightView);


lightSpaceMatrix[16] = lightProjection[16]*lightView[16];


but it is incorrect,where is my wrong thanks

and error:Run-Time Check Failure #2 - Stack around the variable 'lightSpaceMatrix' was corrupted.

GClements
06-24-2016, 08:39 PM
lightSpaceMatrix[16] = lightProjection[16]*lightView[16];


The above is nonsense. All of those arrays have size 16, meaning that only indices in the range 0 to 15 inclusive are valid. And assuming that was supposed to be a matrix multiplication: C and C++ don't have matrix multiplication. You need to either use GLM or a similar library or write your own matrix multiplication function (in which case, you may as well write your own equivalents of glOrtho and gluLookAt while you're at it, because calls to glGet* should be avoided wherever possible).

cl781121
06-24-2016, 08:57 PM
The above is nonsense. All of those arrays have size 16, meaning that only indices in the range 0 to 15 inclusive are valid. And assuming that was supposed to be a matrix multiplication: C and C++ don't have matrix multiplication. You need to either use GLM or a similar library or write your own matrix multiplication function (in which case, you may as well write your own equivalents of glOrtho and gluLookAt while you're at it, because calls to glGet* should be avoided wherever possible).

yes,i got it ,thank you , is that right way to get Matrix like glm lib?

i just want to have a correct matirx like glm even it should be avoided (just for test)

thanks again!