Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4001708
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:56:16+00:00 2026-05-20T07:56:16+00:00

all, I am trying to multiply a matrix to a vector in OpenGL, but

  • 0

all, I am trying to multiply a matrix to a vector in OpenGL, but it turn out that the rendering result I got by calling my own multiplication function(OpenGLUtility::VectorMultiMatrix4d()) is different by calling the opengl function glMultMatrixf(). The two lines drawn on the screen is not same.

I put my code by calling two different ways as bellow.
The function OpenGLUtility::VectorMultiMatrix4d() is simple, just colume-major multiplication.

Can anyone give me some advices on this? Thanks a lot.


Code1:

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
double inverse[16];
OpenGLUtility::InverseMatrix(m_mat, inverse);


double tOrigin[4] = { 
    g_bottom_plane.m_Origin[0], 
    g_bottom_plane.m_Origin[1],
    g_bottom_plane.m_Origin[2],
    1.0 };

OpenGLUtility::VectorMultiMatrix4d(tOrigin,inversed);

double tNormal[4] = {
    g_bottom_plane.m_Normal[0],
    g_bottom_plane.m_Normal[1],
    g_bottom_plane.m_Normal[2],
    0.0 };


OpenGLUtility::VectorMultiMatrix4d(tNormal,inversed);

glBegin(GL_LINES);
glColor3f(0.0,1.0, 0.0);
glVertex4f(tOrigin[0], tOrigin[1], tOrigin[2], tOrigin[3]);
glVertex4f( tNormal[0]*tOrigin[3] + tOrigin[0], 
            tNormal[1]*tOrigin[3] + tOrigin[1], 
            tNormal[2]*tOrigin[3] + tOrigin[2], 
            tOrigin[3] );
glEnd();

//

void OpenGLUtility::VectorMultiMatrix4f(float* pVector, float* pMat)
{
    pVector[0] = pMat[0]*pVector[0] + pMat[4]*pVector[1] + pMat[ 8]*pVector[2] + pMat[12]*pVector[3] ;
    pVector[1] = pMat[1]*pVector[0] + pMat[5]*pVector[1] + pMat[ 9]*pVector[2] + pMat[13]*pVector[3] ;
    pVector[2] = pMat[2]*pVector[0] + pMat[6]*pVector[1] + pMat[10]*pVector[2] + pMat[14]*pVector[3] ;
    pVector[3] = pMat[3]*pVector[0] + pMat[7]*pVector[1] + pMat[11]*pVector[2] + pMat[15]*pVector[3] ;
}

Code 2

float inverse[16];

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glMultMatrixf(m_mat);

OpenGLUtility::InverseMatrix(m_mat, inverse);
glMultMatrixf(inverse);

double tOrigin[4] = { 
    g_bottom_plane.m_Origin[0], 
    g_bottom_plane.m_Origin[1], 
    g_bottom_plane.m_Origin[2], 
    1.0 };

double tNormal[4] = { 
    g_bottom_plane.m_Normal[0], 
    g_bottom_plane.m_Normal[1], 
    g_bottom_plane.m_Normal[2],
    0.0 };


glBegin(GL_LINES);
glColor3f(0.0,1.0, 0.0);
glVertex4f( tOrigin[0], tOrigin[1], tOrigin[2], tOrigin[3] );
glVertex4f( tNormal[0]*tOrigin[3] + tOrigin[0], 
            tNormal[1]*tOrigin[3] + tOrigin[1], 
            tNormal[2]*tOrigin[3] + tOrigin[2], 
            tOrigin[3] );
glEnd();

glMultMatrixf(m_mat);
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:56:17+00:00Added an answer on May 20, 2026 at 7:56 am

    The problem with your multiplication is that you modify your pVector before you are done with the multiplication. You need to store it in a temporary or different variable!

    Change your function to:

    void OpenGLUtility::VectorMultiMatrix4f(float* pVector, float* pMat)
    {
        float x = pMat[0]*pVector[0] + pMat[4]*pVector[1] + pMat[ 8]*pVector[2] + pMat[12]*pVector[3] ;
        float y = pMat[1]*pVector[0] + pMat[5]*pVector[1] + pMat[ 9]*pVector[2] + pMat[13]*pVector[3] ;
        float z = pMat[2]*pVector[0] + pMat[6]*pVector[1] + pMat[10]*pVector[2] + pMat[14]*pVector[3] ;
        float w = pMat[3]*pVector[0] + pMat[7]*pVector[1] + pMat[11]*pVector[2] + pMat[15]*pVector[3] ;
    
        pVector[0] = x;
        pVector[1] = y;
        pVector[2] = z;
        pVector[3] = w;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to rotate vectors using matrices, but got confused. I thought all
I'm trying to work out the total value of all the products in my
I am trying to figure out how to efficiently figure out all the numbers
I am trying to multiply two vectors together where each element of one vector
I was trying all of yesterday to try and integrate a SQL Database with
I've spent all morning trying to write what I thought was a simple bit
I have been trying all afternoon to get the jQuery Sifr Plugin ( http://jquery.thewikies.com/sifr/
I have been trying all day to get some data properly formatted in a
I've been trying all day with no luck to regex replace the new line
All I am trying to do is XmlSerializer serializer = new XmlSerializer(typeof(Stack<int>)); and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.