Mzq
Mzq

Reputation: 1854

How do I multiply a vector with the "ModelViewProjectionMatrix" in openGL?

I have a line in a vertex shader

gl_Position  = gl_ModelViewProjectionMatrix * vertex;

I need to do the same computation without a shader, like:

float vertex[4];
float modelviewProjection[16];
glGetFloatv(GL_MODELVIEW_MATRIX, modelviewProjection);
glMatrixMode(GL_PROJECTION_MATRIX);
glMultMatrixf(modelviewProjection);


for ( counter = 0; counter < numPoints; counter++ )
{

    vertex[0] = *vertexPointer + randomAdvance(timeAlive) + sin(ParticleTime); 
    vertex[1] = *( vertexPointer + 1 ) + randomAdvance(timeAlive) + timeAlive * 0.6f;
    vertex[2] = *( vertexPointer + 2 );
    glPushMatrix();
    glMultMatrixf(vertex);

    *vertexPointer = vertex[0];
    *( vertexPointer + 1 ) = vertex[1];
    *( vertexPointer + 2 ) = vertex[2];
    vertexPointer += 3;
    glPopMatrix();

}

Upvotes: 1

Views: 3513

Answers (1)

Damon
Damon

Reputation: 70136

If you have no suitable vector/matrix library, look into GLM (it can do that kind of thing without any fuss).

If you want to do it manually, the components of the transformed vector are the dot products of the respective rows in the matrix and the untransformed vector. That is because a vector can be seen as a matrix with one column (then just apply the rules of matrix multiplication).

Thus, assuming OpenGL memory layout, that would be:
x = x*m[0] + y*m[4] + z*m[8] + w*m[12], y = x*m[1] + y*m[5] + z*m[9] + w*m[13], etc.

Upvotes: 1

Related Questions