Jump to content
• Advertisement

OpenGL GL_MODELVIEW matrix confusion

This topic is 3755 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have some experience with Direct3D, but I'm new to OpenGL. I'm having problems drawing a cube at an arbitrary location in the world. I managed to get things working using gluLookAt, but I'd prefer to create the view matrix myself then multiply it by the current objects world matrix e.g. glLoadMatrixd( camera.viewMatrix() * object.worldMatrix() ); So, the current setup is the object is located at the origin, with: right vector = <1, 0, 0> up vector = <0, 1, 0> look vector = <0, 0, -1> The camera is located at <0, 0, 10>. Its right, up, and look vectors are identical to the object (I'm not doing any rotations at the moment). The view matrix created by Camera::viewMatrix is:
"[         1,          0,          0,          0]
[         0,          1,          0,          0]
[         0,          0,         -1,        -10]
[         0,          0,          0,          1]"


const GLdouble* Camera_Class::View_Matrix(void)
{
if (m_View_Matrix_Dirty == false)
{
return m_View_Matrix;
}

// Row 1
m_View_Matrix[0] = m_Right_Vector[0];
m_View_Matrix[1] = m_Right_Vector[1];
m_View_Matrix[2] = m_Right_Vector[2];
m_View_Matrix[3] = -1.0 * (m_Position_Vector * m_Right_Vector);
// Row 2
m_View_Matrix[4] = m_Up_Vector[0];
m_View_Matrix[5] = m_Up_Vector[1];
m_View_Matrix[6] = m_Up_Vector[2];
m_View_Matrix[7] = -1.0 * (m_Position_Vector * m_Up_Vector);
// Row 3
m_View_Matrix[8] = m_Look_Vector[0];
m_View_Matrix[9] = m_Look_Vector[1];
m_View_Matrix[10] = m_Look_Vector[2];
m_View_Matrix[11] = -1.0 * (m_Position_Vector * m_Look_Vector);
// Row 4
m_View_Matrix[12] = 0.0;
m_View_Matrix[13] = 0.0;
m_View_Matrix[14] = 0.0;
m_View_Matrix[15] = 1.0;

m_View_Matrix_Dirty = false;

return m_View_Matrix;
}


Here is the paint function:
void OpenGL_Widget_Class::paintGL(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glLoadMatrixd(m_Camera.View_Matrix());

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_INDEX_ARRAY);

glVertexPointer(3, GL_DOUBLE, 0, m_Vertices);
glColorPointer(4, GL_DOUBLE, 0, m_Colors);
glIndexPointer(GL_INT, 0, m_Indices);

// Row major
// world_matrix = yaw * pitch * roll * world_matrix

// Column major
// world_matrix = ((world_matrix * roll) * pitch) * yaw

for(QList<Object_Class*>::iterator iter = m_Cube_Objects.begin(); iter != m_Cube_Objects.end(); ++iter)
{
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, m_Indices);
}

glFlush();
}


Other info: the near and far planes are set to 1 and 1000. Many thanks,

Share this post

Share on other sites
Advertisement
Ok some notes/tips:

GL stores things in column order your matrix should be:

[1, 0, 0, 0,
0, 1, 0, 0,
0, 0, -1, 0,
0, 0, 10, 1 ]

then try:

glMatrixMode(GL_MODELVIEW);
glLoadMatrixd(object); //object/camera may need to be switched, not sure cuz I
glMultMatrixd(camera); //never use LoadMatrix

You should let GL do things, because it's an API that does things uniformly and you don't have to worry about things like column order.

Make sure to use glMatrixMode(GL_MODELVIEW)

Share this post

Share on other sites
Hi,

Thanks for the tips.

Re your comments about the view matrix, according to my math book, if you are using column vectors, e.g. v' = m*v, then the view matrix should look like:

/*		right_x,	right_y,	right_z,	-(position dot right)		up_x,		up_y,		look_z,		-(position dot up)			look_x,		up_y,		look_z,		-(position dot look)				0,			0,			0,			1		*/

I also understand that i (row index), j (column index) refers to the jth row and ith column in an OpenGL matrix. Keeping both these things in mind, I rewrote my View_Matrix function:

const GLdouble* Camera_Class::View_Matrix(void){	/*	Regular matrix	0, 1, 2, 3,	4, 5, 6, 7,	8, 9, 10, 11,	12, 13, 14, 15	OpenGL matrix	0, 4, 8, 12,	1, 5, 9, 13,	2, 6, 10, 14,	3, 7, 11, 15,	The first row of the matrix occupies elements 0, 4, 8, 12 - not 0, 1, 2, 3		*/	if (m_View_Matrix_Dirty == false) 	{		return m_View_Matrix;	}	// First row of OpenGL matrix	m_View_Matrix[0] = m_Right_Vector[0];	m_View_Matrix[4] = m_Right_Vector[1];	m_View_Matrix[8] = m_Right_Vector[2];	m_View_Matrix[12] = -1 * (m_Position_Vector * m_Right_Vector);	// Second row of OpenGL matrix	m_View_Matrix[1] = m_Up_Vector[0];	m_View_Matrix[5] = m_Up_Vector[1];	m_View_Matrix[9] = m_Up_Vector[2];	m_View_Matrix[13] = -1 * (m_Position_Vector * m_Up_Vector);	// Third row of OpenGL matrix	m_View_Matrix[2] = m_Look_Vector[0];	m_View_Matrix[6] = m_Look_Vector[1];	m_View_Matrix[10] = m_Look_Vector[2];	m_View_Matrix[14] = -1 * (m_Position_Vector * m_Look_Vector);	// Fourth row of OpenGL matrix	m_View_Matrix[3] = 0.0;	m_View_Matrix[7] = 0.0;	m_View_Matrix[11] = 0.0;	m_View_Matrix[15] = 1.0;	m_View_Matrix_Dirty = false;	return m_View_Matrix;}

If the camera is positioned at the origin, this produces a view matrix that is an identity matrix.

[         1,          0,          0,          0][         0,          1,          0,          0][         0,          0,          -1,          0][         0,          0,          0,          1]

My object is positioned 4 units in front of the camera. Its world matrix is:

[         1,          0,          0,          0][         0,          1,          0,          0][         0,          0,          1,          -4][         0,          0,          0,          1]

In the paint function, I multiply the view matrix by the world matrix, then paint the object (a 2x2 cube).

e.g. Result_Matrix = View_Matrix * World_Matrix

The result is a black screen.

Setting the objects position to 4 made the cube visible.

This is a bit confusing because I understood that in OpenGL the +z-axis extends "out of the screen", and the -z-axis into the screen. To move an object in the direction of the world z-axis I'd normally do something like (<0, 0, -1> * distance) + object_position, but that does not work in this case, or my view matrix is still wrong :(

Anyway,

Thanks again

Share this post

Share on other sites
Your identity matrix has a -1 for the z-axis, so its not the identity..... In any system regardless of the z+ direction, the identity is still all 1's......

So there forefore, you had to switch your translation to this "new system" where you flipped the z-axis.

Share this post

Share on other sites
Quote:
 Original post by dpadam450GL stores things in column order your matrix should be:
Thats actually not quite true...

Quote:
 Column-major versus row-major is purely a notational convention. Note that post-multiplying with column-major matrices produces the same result as pre-multiplying with row-major matrices. The OpenGL Specification and the OpenGL Reference Manual both use column-major notation. You can use any notation, as long as it's clearly stated.Sadly, the use of column-major format in the spec and blue book has resulted in endless confusion in the OpenGL programming community. Column-major notation suggests that matrices are not laid out in memory as a programmer would expect.

Share this post

Share on other sites
Yea I've never heard of that, but that doesn't make sense because you cant have both unless there is an option to say "hey I want them row major now". I see what its saying but anytime you print a GL matrix, its always column major, even in GLSL. So can you tell GL how you want it?

Share this post

Share on other sites
Quote:
 Original post by dpadam450Your identity matrix has a -1 for the z-axis, so its not the identity..... In any system regardless of the z+ direction, the identity is still all 1's......So there forefore, you had to switch your translation to this "new system" where you flipped the z-axis.

Hi,

OK - I set my camera's look vector to <0, 0, 1>, the camera's position to <0, 0, 1>, and the cubes position to <0, 0, -2>.

If +z coordinates are behind the origin, and -z coordinates are in front of the origin, I can visualise this in my head, but the camera's look vector is pointing away from the cube - not at it.

In D3D, if the camera's look vector is <0, 0, 1>, then (a) <0, 0, 10> is a point in front of the origin, and (b) <0, 0, -10> is a point behind the origin.

<0, 0, 0> + (camera_look_vector * 10) = <0, 0, 10> (a).

I would expect things to work the same in OpenGL i.e.

Look vector = <0, 0, -1>
a = <0, 0, -10> // in front of the origin
b = <0, 0, 10> // behind the origin

<0, 0, 0> + (camera_look_vector * 10) = <0, 0, -10> (a)

Do you see what I'm getting at? I'm just trying to get this sorted in my head.

Thanks

Share this post

Share on other sites
Ok your having coordinate problems un-related to openGL.

Image your origin is the middle of your screen. If your look vector is 0,0,1, then your looking down the positive z-axis, or outward from your pc screen. Your only going to see things that have a positive z-coordinate....

Again, you have this z- concept because your saying "OpenGL's z+ is DX's z-", which is true.......IFFFFF you want to describe GL in terms of DX.

So give this frame if your camera matrix is the identity matrix and remember the z- is the vector into the screen, then anything you see needs to be in the z- axis.

Got it? Again, in your case here, your camera is looking out towards your face, and your cube is behind your pc screen..... complete opposite directions.

OpenGL frame y | | |______ x / /z 
 0 
 Share this post Link to post Share on other sites 
 
 
 davidr    122 davidr Member 122 Posted February 22, 2008 Quote:Original post by dpadam450Ok your having coordinate problems un-related to openGL.Image your origin is the middle of your screen. If your look vector is 0,0,1, then your looking down the positive z-axis, or outward from your pc screen. Your only going to see things that have a positive z-coordinate....Again, you have this z- concept because your saying "OpenGL's z+ is DX's z-", which is true.......IFFFFF you want to describe GL in terms of DX.So give this frame if your camera matrix is the identity matrix and remember the z- is the vector into the screen, then anything you see needs to be in the z- axis. Got it? Again, in your case here, your camera is looking out towards your face, and your cube is behind your pc screen..... complete opposite directions.OpenGL frame y | | |______ x / /zThanks for the clarification.In view space, the camera's vectors are aligned with <1, 0, 0>, <0, 1, 0>, and <0, 0, 1>, and objects need to have a position such as <x, y, -z> in order to be visible because OpenGL uses a right-handed coordinate system.Anyway, things seem to be working now. 0 Share this post Link to post Share on other sites songuke    122 songuke Member 122 Posted March 13, 2008 /* transform matrix */ Vector3f n = view - eye; //look Vector3f u = n.Cross(up); //right //Vector3f v = n.Cross(u); //left-handed - ??? Vector3f v = u.Cross(n); //right-handed - currently I used this u = u.Normalize(); v = v.Normalize(); n = n.Normalize(); /* make n as -z (negative z) in OpenGL (we look to the negative OpenGL) */ n = -n; //now become left-handed //translation Vector3f t = Vector3f(-eye.Dot(u), -eye.Dot(v), -eye.Dot(n)); /* interpreting as column-major like this in OpenGL * ( u.x, u.y, u.z, t.x, * v.x, v.y, v.z, t.y, * n.x, n.y, n.z, t.z, * 0 , 0, 0, 1 ) */ GLfloat m[] = { //so the matrix must be like this: u.x, v.x, n.x, 0, u.y, v.y, n.y, 0, u.z, v.z, n.z, 0, t.x, t.y, t.z, 1 };I tested this transformation and gained the same result as gluLookAt method.But now I'm having a problem deriving this for the billboard. The billboard code is like this: Vector3f n = vecView; //vector from object of billboard to camera Vector3f u = vecUp.Cross(n); /* !!! Point to the left??? */ Vector3f v = n.Cross(u); u = u.Normalize(); v = v.Normalize(); n = n.Normalize(); //no translation yet //Vector3f t = Vector3f(-posObj.Dot(u), -posObj.Dot(v), -posObj.Dot(n)); /* interpreting as column-major like this * ( u.x, u.y, u.z, t.x, * v.x, v.y, v.z, t.y, * n.x, n.y, n.z, t.z, * 0 , 0, 0, 1 ) */ /* THIS ONE NOT WORK!!! GLfloat m[] = { u.x, v.x, n.x, 0, u.y, v.y, n.y, 0, u.z, v.z, n.z, 0, t.x, t.y, t.z, 1 };*/ /*BUT THIS ONE WORKS!!! -> This matrix's rotation part (u, v, n) is wrong of its position.*/ GLfloat billboardMat[] = { u.x, u.y, u.z, 0, v.x, v.y, v.z, 0, n.x, n.y, n.z, 0, t.x, t.y, t.z, 1 };The billboard matrix is like this. I thought the billboardMat's elements is wrong but it does produce the right result of billboard.In the opposite, if I used the camera transformation matrix without the line n = -n, I still get the billboard effect but the rotation of Y axis is wrong (the image is upside down).It looks confusing. Anyone could give me some hints? :)Thanks in advance. 0 Share this post Link to post Share on other sites 
 Sign in to follow this   Followers 0 
 Go To Topic Listing Graphics and GPU Programming Advertisement 
 Advertisement Popular Tags 2D 3D Advice Algorithm Animation C# C++ Concept Design DX11 DX12 GameMaker Gameplay General Java Learning Marketing Mobile Music OpenGL Open Source Optimization PC Unity Unreal Popular Contributors Week Month Year All Time 1 Fulcrum.013 42 2 Rutin 18 3 jbadams 16 4 lawnjelly 14 5 Hodgman 12 Show More Advertisement Popular Now 14 Market Confussion, Video Game Industry? By Allen BipsterStarted Yesterday at 02:36 PM 12 Gameplay How do I apply direction to a velocity vector and then to the character position? By HashbrownStarted Yesterday at 03:19 AM 9 Where value comes from in game By csbrown28Started Friday at 08:24 PM 12 Best way to optimise 3d models By davejonesStarted Wednesday at 07:46 PM 37 WHO recognising 'gaming disorder' By jbadamsStarted Wednesday at 11:44 AM Forum Statistics Total Topics 631428 Total Posts 3000027 GameDev.net GameDev.net Articles GameDev.net Event Coverage GameDev.net Forums GameDev.net Blogs GameDev.net Gallery GameDev.net News GameDev.net Projects GDNet Chat All Activity Search In Everywhere This Forum This Topic More options... Find results that contain... Any of my search term words All of my search term words Find results in... Content titles and body Content titles only Home Forums Programming Graphics and GPU Programming GL_MODELVIEW matrix confusion 
 
 
 × Existing user? Sign In Sign Up Browse Back Articles & Tutorials Back All Categories Audio Business Game Design Industry Programming Visual Arts Columns Back GameDev Unboxed Event Coverage Back All Events Game Developers Conference Power Up Digital Games Conference GameDev.Market Links News Podcasts Back All Podcasts Game Dev Loadout Archive Community Back Beginners Blogs Calendar Chat Forums Back All Forums Audio Business Game Design Programming Visual Arts Community Affiliates Topical Workshops Gallery Groups Projects Back All Projects Games Game Assets Game Mods Developer Tools Store Careers Back Contractors Hobby Projects Game Jobs Back Browse on GameDev.Jobs Post a Job Members Back Chat GDNet+ Membership Guidelines Leaderboard Online Users Awards Search Back All Activity My Activity Streams Back Latest Topics Featured Blogs Search var ipsDebug=false;var CKEDITOR_BASEPATH='//www.gamedev.net/applications/core/interface/ckeditor/ckeditor/';var ipsSettings={cookie_path:"/",cookie_prefix:"ips4_",cookie_ssl:true,upload_imgURL:"",message_imgURL:"",notification_imgURL:"",baseURL:"//www.gamedev.net/",jsURL:"//www.gamedev.net/applications/core/interface/js/js.php",csrfKey:"e1fb5c3067be360c99e4d983517e08d0",antiCache:"2a361a8a02",disableNotificationSounds:false,useCompiledFiles:true,links_external:true,memberID:0,analyticsProvider:"ga",viewProfiles:true,mapProvider:'google',mapApiKey:"AIzaSyAeT7tk3vnWWmbgVISkLpbhkQvekG19rHM",}; ips.setSetting('date_format',jQuery.parseJSON('"mm\/dd\/yy"'));ips.setSetting('date_first_day',jQuery.parseJSON('0'));ips.setSetting('remote_image_proxy',jQuery.parseJSON('1'));ips.setSetting('ipb_url_filter_option',jQuery.parseJSON('"none"'));ips.setSetting('url_filter_any_action',jQuery.parseJSON('"allow"'));ips.setSetting('bypass_profanity',jQuery.parseJSON('0'));ips.setSetting('emoji_style',jQuery.parseJSON('"native"'));ips.setSetting('emoji_shortcodes',jQuery.parseJSON('"1"'));ips.setSetting('emoji_ascii',jQuery.parseJSON('"1"'));ips.setSetting('emoji_cache',jQuery.parseJSON('"1"'));ips.setSetting('quickSearchDefault',jQuery.parseJSON('"all"'));ips.setSetting('quickSearchMinimum',jQuery.parseJSON('3'));ips.setSetting('quickSearchShowAdv',jQuery.parseJSON('true'));ips.setSetting('quickSearchIn',jQuery.parseJSON('"title"')); { "@context": "http://schema.org", "@type": "DiscussionForumPosting", "url": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/", "discussionUrl": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/", "name": "GL_MODELVIEW matrix confusion", "headline": "GL_MODELVIEW matrix confusion", "text": "Hi,\n\nI have some experience with Direct3D, but I\u0027m new to OpenGL.\n\nI\u0027m having problems drawing a cube at an arbitrary location in the world.\n\nI managed to get things working using gluLookAt, but I\u0027d prefer to create the view matrix myself then multiply it by the current objects world matrix e.g.\n\nglLoadMatrixd( camera.viewMatrix() * object.worldMatrix() );\n\nSo, the current setup is the object is located at the origin, with:\n\nright vector = \u0026lt;1, 0, 0\u0026gt;\nup vector = \u0026lt;0, 1, 0\u0026gt;\nlook vector = \u0026lt;0, 0, -1\u0026gt;\n\nThe camera is located at \u0026lt;0, 0, 10\u0026gt;. Its right, up, and look vectors are identical to the object (I\u0027m not doing any rotations at the moment).\n\nThe view matrix created by Camera::viewMatrix is:\n\n\n\n\"[ 1, 0, 0, 0]\n[ 0, 1, 0, 0]\n[ 0, 0, -1, -10]\n[ 0, 0, 0, 1]\" \n\n\n\n\nconst GLdouble* Camera_Class::View_Matrix(void)\n{\n\tif (m_View_Matrix_Dirty == false) \n\t{\n\t\treturn m_View_Matrix;\n\t}\n\n\t// Row 1\n\tm_View_Matrix[0] = m_Right_Vector[0];\n\tm_View_Matrix[1] = m_Right_Vector[1];\n\tm_View_Matrix[2] = m_Right_Vector[2];\n\tm_View_Matrix[3] = -1.0 * (m_Position_Vector * m_Right_Vector);\n\t// Row 2\n\tm_View_Matrix[4] = m_Up_Vector[0];\n\tm_View_Matrix[5] = m_Up_Vector[1];\n\tm_View_Matrix[6] = m_Up_Vector[2];\n\tm_View_Matrix[7] = -1.0 * (m_Position_Vector * m_Up_Vector);\n\t// Row 3\n\tm_View_Matrix[8] = m_Look_Vector[0];\n\tm_View_Matrix[9] = m_Look_Vector[1];\n\tm_View_Matrix[10] = m_Look_Vector[2];\n\tm_View_Matrix[11] = -1.0 * (m_Position_Vector * m_Look_Vector);\n\t// Row 4\n\tm_View_Matrix[12] = 0.0;\n\tm_View_Matrix[13] = 0.0;\n\tm_View_Matrix[14] = 0.0;\n\tm_View_Matrix[15] = 1.0;\n\n\tm_View_Matrix_Dirty = false;\n\n\treturn m_View_Matrix;\n}\n\n\n\nHere is the paint function:\n\n\nvoid OpenGL_Widget_Class::paintGL(void)\n{\n\tglClear(GL_COLOR_BUFFER_BIT);\n\t\n\tglLoadMatrixd(m_Camera.View_Matrix());\n\t\n\tglEnableClientState(GL_VERTEX_ARRAY);\n\tglEnableClientState(GL_COLOR_ARRAY);\n\tglEnableClientState(GL_INDEX_ARRAY);\n\n\tglVertexPointer(3, GL_DOUBLE, 0, m_Vertices);\n\tglColorPointer(4, GL_DOUBLE, 0, m_Colors);\n\tglIndexPointer(GL_INT, 0, m_Indices);\n\n\t// Row major\n\t// world_matrix = yaw * pitch * roll * world_matrix\n\n\t// Column major\n\t// world_matrix = ((world_matrix * roll) * pitch) * yaw\n\n\tfor(QList\u0026lt;Object_Class*\u0026gt;::iterator iter = m_Cube_Objects.begin(); iter != m_Cube_Objects.end(); ++iter)\n\t{\n\t\tglDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, m_Indices);\n\t}\n\t\n\tglFlush();\n}\n\n\n\nOther info: the near and far planes are set to 1 and 1000.\n\nMany thanks,", "dateCreated": "2008-02-21T04:00:17+0000", "datePublished": "2008-02-21T04:00:17+0000", "pageStart": 1, "pageEnd": 1, "image": "https://secure.gravatar.com/avatar/9611539349ce0fb7de1a353e50922f97?d=https://www.gamedev.net/uploads/monthly_2017_08/D.png.cd445515adcfba28bd381e4a0f58a933.png", "author": { "@type": "Person", "name": "davidr", "image": "https://secure.gravatar.com/avatar/9611539349ce0fb7de1a353e50922f97?d=https://www.gamedev.net/uploads/monthly_2017_08/D.png.cd445515adcfba28bd381e4a0f58a933.png", "url": "https://www.gamedev.net/profile/70272-davidr/" }, "interactionStatistic": [ { "@type": "InteractionCounter", "interactionType": "http://schema.org/ViewAction", "userInteractionCount": 5833 }, { "@type": "InteractionCounter", "interactionType": "http://schema.org/CommentAction", "userInteractionCount": 10 }, { "@type": "InteractionCounter", "interactionType": "http://schema.org/FollowAction", "userInteractionCount": 19 } ], "comment": [ { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/?do=findComment\u0026comment=483755", "author": { "@type": "Person", "name": "davidr", "image": "https://secure.gravatar.com/avatar/9611539349ce0fb7de1a353e50922f97?d=https://www.gamedev.net/uploads/monthly_2017_08/D.png.cd445515adcfba28bd381e4a0f58a933.png", "url": "https://www.gamedev.net/profile/70272-davidr/" }, "dateCreated": "2008-02-21T04:00:17+0000", "text": "Hi,\n\nI have some experience with Direct3D, but I\u0027m new to OpenGL.\n\nI\u0027m having problems drawing a cube at an arbitrary location in the world.\n\nI managed to get things working using gluLookAt, but I\u0027d prefer to create the view matrix myself then multiply it by the current objects world matrix e.g.\n\nglLoadMatrixd( camera.viewMatrix() * object.worldMatrix() );\n\nSo, the current setup is the object is located at the origin, with:\n\nright vector = \u0026lt;1, 0, 0\u0026gt;\nup vector = \u0026lt;0, 1, 0\u0026gt;\nlook vector = \u0026lt;0, 0, -1\u0026gt;\n\nThe camera is located at \u0026lt;0, 0, 10\u0026gt;. Its right, up, and look vectors are identical to the object (I\u0027m not doing any rotations at the moment).\n\nThe view matrix created by Camera::viewMatrix is:\n\n\n\n\"[ 1, 0, 0, 0]\n[ 0, 1, 0, 0]\n[ 0, 0, -1, -10]\n[ 0, 0, 0, 1]\" \n\n\n\n\nconst GLdouble* Camera_Class::View_Matrix(void)\n{\n\tif (m_View_Matrix_Dirty == false) \n\t{\n\t\treturn m_View_Matrix;\n\t}\n\n\t// Row 1\n\tm_View_Matrix[0] = m_Right_Vector[0];\n\tm_View_Matrix[1] = m_Right_Vector[1];\n\tm_View_Matrix[2] = m_Right_Vector[2];\n\tm_View_Matrix[3] = -1.0 * (m_Position_Vector * m_Right_Vector);\n\t// Row 2\n\tm_View_Matrix[4] = m_Up_Vector[0];\n\tm_View_Matrix[5] = m_Up_Vector[1];\n\tm_View_Matrix[6] = m_Up_Vector[2];\n\tm_View_Matrix[7] = -1.0 * (m_Position_Vector * m_Up_Vector);\n\t// Row 3\n\tm_View_Matrix[8] = m_Look_Vector[0];\n\tm_View_Matrix[9] = m_Look_Vector[1];\n\tm_View_Matrix[10] = m_Look_Vector[2];\n\tm_View_Matrix[11] = -1.0 * (m_Position_Vector * m_Look_Vector);\n\t// Row 4\n\tm_View_Matrix[12] = 0.0;\n\tm_View_Matrix[13] = 0.0;\n\tm_View_Matrix[14] = 0.0;\n\tm_View_Matrix[15] = 1.0;\n\n\tm_View_Matrix_Dirty = false;\n\n\treturn m_View_Matrix;\n}\n\n\n\nHere is the paint function:\n\n\nvoid OpenGL_Widget_Class::paintGL(void)\n{\n\tglClear(GL_COLOR_BUFFER_BIT);\n\t\n\tglLoadMatrixd(m_Camera.View_Matrix());\n\t\n\tglEnableClientState(GL_VERTEX_ARRAY);\n\tglEnableClientState(GL_COLOR_ARRAY);\n\tglEnableClientState(GL_INDEX_ARRAY);\n\n\tglVertexPointer(3, GL_DOUBLE, 0, m_Vertices);\n\tglColorPointer(4, GL_DOUBLE, 0, m_Colors);\n\tglIndexPointer(GL_INT, 0, m_Indices);\n\n\t// Row major\n\t// world_matrix = yaw * pitch * roll * world_matrix\n\n\t// Column major\n\t// world_matrix = ((world_matrix * roll) * pitch) * yaw\n\n\tfor(QList\u0026lt;Object_Class*\u0026gt;::iterator iter = m_Cube_Objects.begin(); iter != m_Cube_Objects.end(); ++iter)\n\t{\n\t\tglDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, m_Indices);\n\t}\n\t\n\tglFlush();\n}\n\n\n\nOther info: the near and far planes are set to 1 and 1000.\n\nMany thanks,", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/?do=findComment\u0026comment=4164843", "author": { "@type": "Person", "name": "dpadam450", "image": "https://www.gamedev.net/uploads/profile/photo-thumb-92781.jpg", "url": "https://www.gamedev.net/profile/92781-dpadam450/" }, "dateCreated": "2008-02-21T04:20:50+0000", "text": "Ok some notes/tips:GL stores things in column order your matrix should be:[1, 0, 0, 0, 0, 1, 0, 0,0, 0, -1, 0,0, 0, 10, 1 ]then try:glMatrixMode(GL_MODELVIEW);glLoadMatrixd(object); //object/camera may need to be switched, not sure cuz I glMultMatrixd(camera); //never use LoadMatrixYou should let GL do things, because it\u0027s an API that does things uniformly and you don\u0027t have to worry about things like column order.Make sure to use glMatrixMode(GL_MODELVIEW)", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/?do=findComment\u0026comment=4165431", "author": { "@type": "Person", "name": "davidr", "image": "https://secure.gravatar.com/avatar/9611539349ce0fb7de1a353e50922f97?d=https://www.gamedev.net/uploads/monthly_2017_08/D.png.cd445515adcfba28bd381e4a0f58a933.png", "url": "https://www.gamedev.net/profile/70272-davidr/" }, "dateCreated": "2008-02-21T21:19:07+0000", "text": "Hi,Thanks for the tips.Re your comments about the view matrix, according to my math book, if you are using column vectors, e.g. v\u0027 = m*v, then the view matrix should look like:\n/*\t\tright_x,\tright_y,\tright_z,\t-(position dot right)\t\tup_x,\t\tup_y,\t\tlook_z,\t\t-(position dot up)\t\t\tlook_x,\t\tup_y,\t\tlook_z,\t\t-(position dot look)\t\t\t\t0,\t\t\t0,\t\t\t0,\t\t\t1\t\t*/\nI also understand that i (row index), j (column index) refers to the jth row and ith column in an OpenGL matrix. Keeping both these things in mind, I rewrote my View_Matrix function:const GLdouble* Camera_Class::View_Matrix(void){/*\tRegular matrix\t0, 1, 2, 3,\t4, 5, 6, 7,\t8, 9, 10, 11,\t12, 13, 14, 15\tOpenGL matrix\t0, 4, 8, 12,\t1, 5, 9, 13,\t2, 6, 10, 14,\t3, 7, 11, 15,\tThe first row of the matrix occupies elements 0, 4, 8, 12 - not 0, 1, 2, 3\t*/if (m_View_Matrix_Dirty == false) \t{return m_View_Matrix;\t}// First row of OpenGL matrix\tm_View_Matrix[0] = m_Right_Vector[0];\tm_View_Matrix[4] = m_Right_Vector[1];\tm_View_Matrix[8] = m_Right_Vector[2];\tm_View_Matrix[12] = -1 * (m_Position_Vector * m_Right_Vector);// Second row of OpenGL matrix\tm_View_Matrix[1] = m_Up_Vector[0];\tm_View_Matrix[5] = m_Up_Vector[1];\tm_View_Matrix[9] = m_Up_Vector[2];\tm_View_Matrix[13] = -1 * (m_Position_Vector * m_Up_Vector);// Third row of OpenGL matrix\tm_View_Matrix[2] = m_Look_Vector[0];\tm_View_Matrix[6] = m_Look_Vector[1];\tm_View_Matrix[10] = m_Look_Vector[2];\tm_View_Matrix[14] = -1 * (m_Position_Vector * m_Look_Vector);// Fourth row of OpenGL matrix\tm_View_Matrix[3] = 0.0;\tm_View_Matrix[7] = 0.0;\tm_View_Matrix[11] = 0.0;\tm_View_Matrix[15] = 1.0;\tm_View_Matrix_Dirty = false;return m_View_Matrix;}\nIf the camera is positioned at the origin, this produces a view matrix that is an identity matrix.[ 1, 0, 0, 0][ 0, 1, 0, 0][ 0, 0, -1, 0][ 0, 0, 0, 1] \nMy object is positioned 4 units in front of the camera. Its world matrix is:[ 1, 0, 0, 0][ 0, 1, 0, 0][ 0, 0, 1, -4][ 0, 0, 0, 1]\nIn the paint function, I multiply the view matrix by the world matrix, then paint the object (a 2x2 cube). e.g. Result_Matrix = View_Matrix * World_MatrixThe result is a black screen.Setting the objects position to 4 made the cube visible.This is a bit confusing because I understood that in OpenGL the +z-axis extends \"out of the screen\", and the -z-axis into the screen. To move an object in the direction of the world z-axis I\u0027d normally do something like (\u0026lt;0, 0, -1\u0026gt; * distance) + object_position, but that does not work in this case, or my view matrix is still wrong :(Anyway,Thanks again", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/?do=findComment\u0026comment=4165444", "author": { "@type": "Person", "name": "dpadam450", "image": "https://www.gamedev.net/uploads/profile/photo-thumb-92781.jpg", "url": "https://www.gamedev.net/profile/92781-dpadam450/" }, "dateCreated": "2008-02-21T21:38:55+0000", "text": "Your identity matrix has a -1 for the z-axis, so its not the identity..... In any system regardless of the z+ direction, the identity is still all 1\u0027s......So there forefore, you had to switch your translation to this \"new system\" where you flipped the z-axis.", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/?do=findComment\u0026comment=4165451", "author": { "@type": "Person", "name": "Crypter", "image": "https://secure.gravatar.com/avatar/b63896dbe7236bfb0ed9d4083df85fa1?d=https://www.gamedev.net/uploads/monthly_2017_08/C.png.4401d001ebe56b433c698ba1aabda9c8.png", "url": "https://www.gamedev.net/profile/66590-crypter/" }, "dateCreated": "2008-02-21T21:58:47+0000", "text": "Thats actually not quite true...\n", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/?do=findComment\u0026comment=4165462", "author": { "@type": "Person", "name": "dpadam450", "image": "https://www.gamedev.net/uploads/profile/photo-thumb-92781.jpg", "url": "https://www.gamedev.net/profile/92781-dpadam450/" }, "dateCreated": "2008-02-21T22:26:05+0000", "text": "Yea I\u0027ve never heard of that, but that doesn\u0027t make sense because you cant have both unless there is an option to say \"hey I want them row major now\". I see what its saying but anytime you print a GL matrix, its always column major, even in GLSL. So can you tell GL how you want it?", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/?do=findComment\u0026comment=4165491", "author": { "@type": "Person", "name": "davidr", "image": "https://secure.gravatar.com/avatar/9611539349ce0fb7de1a353e50922f97?d=https://www.gamedev.net/uploads/monthly_2017_08/D.png.cd445515adcfba28bd381e4a0f58a933.png", "url": "https://www.gamedev.net/profile/70272-davidr/" }, "dateCreated": "2008-02-21T23:02:48+0000", "text": "Hi,OK - I set my camera\u0027s look vector to \u0026lt;0, 0, 1\u0026gt;, the camera\u0027s position to \u0026lt;0, 0, 1\u0026gt;, and the cubes position to \u0026lt;0, 0, -2\u0026gt;.If +z coordinates are behind the origin, and -z coordinates are in front of the origin, I can visualise this in my head, but the camera\u0027s look vector is pointing away from the cube - not at it.In D3D, if the camera\u0027s look vector is \u0026lt;0, 0, 1\u0026gt;, then (a) \u0026lt;0, 0, 10\u0026gt; is a point in front of the origin, and (b) \u0026lt;0, 0, -10\u0026gt; is a point behind the origin.\u0026lt;0, 0, 0\u0026gt; + (camera_look_vector * 10) = \u0026lt;0, 0, 10\u0026gt; (a).I would expect things to work the same in OpenGL i.e.Look vector = \u0026lt;0, 0, -1\u0026gt;a = \u0026lt;0, 0, -10\u0026gt; // in front of the originb = \u0026lt;0, 0, 10\u0026gt; // behind the origin\u0026lt;0, 0, 0\u0026gt; + (camera_look_vector * 10) = \u0026lt;0, 0, -10\u0026gt; (a)Do you see what I\u0027m getting at? I\u0027m just trying to get this sorted in my head.Thanks\n", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/?do=findComment\u0026comment=4165588", "author": { "@type": "Person", "name": "dpadam450", "image": "https://www.gamedev.net/uploads/profile/photo-thumb-92781.jpg", "url": "https://www.gamedev.net/profile/92781-dpadam450/" }, "dateCreated": "2008-02-22T02:47:09+0000", "text": "Ok your having coordinate problems un-related to openGL.Image your origin is the middle of your screen. If your look vector is 0,0,1, then your looking down the positive z-axis, or outward from your pc screen. Your only going to see things that have a positive z-coordinate....Again, you have this z- concept because your saying \"OpenGL\u0027s z+ is DX\u0027s z-\", which is true.......IFFFFF you want to describe GL in terms of DX.So give this frame if your camera matrix is the identity matrix and remember the z- is the vector into the screen, then anything you see needs to be in the z- axis. Got it? Again, in your case here, your camera is looking out towards your face, and your cube is behind your pc screen..... complete opposite directions.OpenGL frame y | | |______ x / /z", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/?do=findComment\u0026comment=4165631", "author": { "@type": "Person", "name": "davidr", "image": "https://secure.gravatar.com/avatar/9611539349ce0fb7de1a353e50922f97?d=https://www.gamedev.net/uploads/monthly_2017_08/D.png.cd445515adcfba28bd381e4a0f58a933.png", "url": "https://www.gamedev.net/profile/70272-davidr/" }, "dateCreated": "2008-02-22T04:04:38+0000", "text": "Thanks for the clarification.In view space, the camera\u0027s vectors are aligned with \u0026lt;1, 0, 0\u0026gt;, \u0026lt;0, 1, 0\u0026gt;, and \u0026lt;0, 0, 1\u0026gt;, and objects need to have a position such as \u0026lt;x, y, -z\u0026gt; in order to be visible because OpenGL uses a right-handed coordinate system.Anyway, things seem to be working now.", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/?do=findComment\u0026comment=4180324", "author": { "@type": "Person", "name": "songuke", "image": "https://secure.gravatar.com/avatar/14428e4b568e8c08610df994e92132e5?d=https://www.gamedev.net/uploads/monthly_2017_08/S.png.908a31f2deb50634591ddeb39424d1b8.png", "url": "https://www.gamedev.net/profile/131512-songuke/" }, "dateCreated": "2008-03-13T22:16:07+0000", "text": "/* transform matrix */\tVector3f n = view - eye; //look\tVector3f u = n.Cross(up); //right//Vector3f v = n.Cross(u); //left-handed - ???\tVector3f v = u.Cross(n); //right-handed - currently I used this\tu = u.Normalize();\tv = v.Normalize();\t\tn = n.Normalize();/* make n as -z (negative z) in OpenGL (we look to the negative OpenGL) */\tn = -n; //now become left-handed//translation\tVector3f t = Vector3f(-eye.Dot(u), -eye.Dot(v), -eye.Dot(n));/* interpreting as column-major like this in OpenGL\t * ( u.x, u.y, u.z, t.x,\t * v.x, v.y, v.z, t.y,\t * n.x, n.y, n.z, t.z,\t * 0 , 0, 0, 1 ) */\tGLfloat m[] = { //so the matrix must be like this:\t\t\tu.x, v.x, n.x, 0,\t\t\tu.y, v.y, n.y, 0,\t\t\tu.z, v.z, n.z, 0,\t\t\tt.x, t.y, t.z, 1\t};\nI tested this transformation and gained the same result as gluLookAt method.But now I\u0027m having a problem deriving this for the billboard. The billboard code is like this: Vector3f n = vecView; //vector from object of billboard to camera\tVector3f u = vecUp.Cross(n); /* !!! Point to the left??? */\tVector3f v = n.Cross(u); \tu = u.Normalize();\tv = v.Normalize();\t\tn = n.Normalize();//no translation yet//Vector3f t = Vector3f(-posObj.Dot(u), -posObj.Dot(v), -posObj.Dot(n));/* interpreting as column-major like this\t * ( u.x, u.y, u.z, t.x,\t * v.x, v.y, v.z, t.y,\t * n.x, n.y, n.z, t.z,\t * 0 , 0, 0, 1 ) *//* THIS ONE NOT WORK!!!\tGLfloat m[] = {\t\t\tu.x, v.x, n.x, 0,\t\t\tu.y, v.y, n.y, 0,\t\t\tu.z, v.z, n.z, 0,\t\t\tt.x, t.y, t.z, 1\t};*//*BUT THIS ONE WORKS!!! -\u0026gt; This matrix\u0027s rotation part (u, v, n) is wrong of its position.*/\tGLfloat billboardMat[] = {\t\t\t\tu.x, u.y, u.z, 0,\t\t\t\tv.x, v.y, v.z, 0,\t\t\t\tn.x, n.y, n.z, 0,\t\t\t\tt.x, t.y, t.z, 1\t};\nThe billboard matrix is like this. I thought the billboardMat\u0027s elements is wrong but it does produce the right result of billboard.In the opposite, if I used the camera transformation matrix without the line n = -n, I still get the billboard effect but the rotation of Y axis is wrong (the image is upside down).It looks confusing. Anyone could give me some hints? :)Thanks in advance.", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/483755-gl_modelview-matrix-confusion/" } ] } { "@context": "http://www.schema.org", "@type": "WebSite", "name": "GameDev.net", "url": "https://www.gamedev.net/", "potentialAction": { "type": "SearchAction", "query-input": "required name=query", "target": "https://www.gamedev.net/search/?q={query}" }, "inLanguage": [ { "@type": "Language", "name": "English (USA)", "alternateName": "en-US" } ] } { "@context": "http://www.schema.org", "@type": "Organization", "name": "GameDev.net", "url": "https://www.gamedev.net/", "logo": "https://www.gamedev.net/uploads/themes/monthly_2017_04/gamedev-logo-2017-368x76.png.e986e41d566ff6c90485a5304985ed5f.png", "address": { "@type": "PostalAddress", "streetAddress": "", "addressLocality": null, "addressRegion": null, "postalCode": null, "addressCountry": null } } { "@context": "http://schema.org", "@type": "BreadcrumbList", "itemListElement": [ { "@type": "ListItem", "position": 1, "item": { "@id": "https://www.gamedev.net/forums/", "name": "Forums" } }, { "@type": "ListItem", "position": 2, "item": { "@id": "https://www.gamedev.net/forums/forum/4-programming/", "name": "Programming" } }, { "@type": "ListItem", "position": 3, "item": { "@id": "https://www.gamedev.net/forums/forum/5-graphics-and-gpu-programming/", "name": "Graphics and GPU Programming" } } ] } { "@context": "http://schema.org", "@type": "ContactPage", "url": "https://www.gamedev.net/contact/" } Important Information By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.   I accept Participate in the game development conversation and more when you create an account on GameDev.net! Sign me up!