Jump to content
  • Advertisement
Sign in to follow this  

ModelViewProj Matrix Problem

This topic is 3033 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'm building my modelview and projection matrices by hand and using state.matrix.mvp in my vert shader to transform the vertices. I'm calculating the projection matrix and using glLoadMatrixf to set the perspective and using glLoadMatrixf/glMultMatrixf along with my calculated ModelView. Everything fine so far. I've made many apps using my base code and I have had no problems. However, I tried to create a ModelViewProj matrix from my ModelView and Projection matrices and send it to my shader so I can use my hand-rolled ModelViewProj rather than the state.matrix.mvp matrix. I know my matrix code works fine as i've been calculating matrices and inverses for all sorts of effects in various spaces and have had no problem. I know my projection matrix is fine because I've made ortho and perspective matrices for GUI and 3D applications and have had no problems (as i say, I'm loading my proj matrix straight in with no probs). It's just that when I send it to the shader i get nothing :S I'm using CG and the psuedo-code is something like this:

// This is calulated elswhere and then sent to the shader:
mtxModelView = mtxView * mtxModel
mtxModelViewProj = mtxProjection * mtxModelView

// I then send said matrix to the vert shader:
paramModelViewProj = cgGetNamedParameter(vertProgram, "ModelViewProj");	
cgSetMatrixParameterfr(paramModelViewProj, mtxModelViewProj.matrix);

// Then, in my shader I'm using it like this:
uniform float4x4 ModelViewProj
oPosition =  mul(ModelViewProj, iPosition);

...yet I get nothing. As I say, if I load the modelview and projection matrices into OGl and use the state.matrix.mvp in the shader it works fine. Any ideas?

Share this post

Link to post
Share on other sites
Are your matrices column-major or row major? Does the CG function expect them in your layout of choice?

Share this post

Link to post
Share on other sites
I've had issues with this before. Ensure that your calls to cg are in order. "updating program parameters" at the right time. I also agree with the guy above make sure the matrices are of the correct order but i assume they are.

i use multmatrix as well, but not load matrix, i start with the identity and mult the view followed by my objects matrices. Then i use the state parameters like you said, they seem to make the most sense to me.

Thats the best answer i can give without more info.

good luck.


Share this post

Link to post
Share on other sites
Hi, thanks for the responses. Here's my rendering/CG code I'm using:

// Vert shader

// Frag shader

// Diffuse texture
glBindTexture(GL_TEXTURE_2D, texBase);

SMatrix4x4 mtxView = *sceneCam.ModelView();
SMatrix4x4 mtxModel = pMesh->mtxInvModelView;
SMatrix4x4 mtxModelView = mtxView * mtxModel;
SMatrix4x4 mtxProjection = *sceneCam.Projection();
SMatrix4x4 mtxMVP = mtxProjection * mtxModelView;

// Vert MVP matrix
paramModelViewProj = cgGetNamedParameter(vertProgram, "ModelViewProj");
cgSetMatrixParameterfr(paramModelViewProj, mtxMVP.matrix);

// Frag eye position (object space)
paramObjEyePos = cgGetNamedParameter(fragProgram, "objEyePos");
cgSetParameter3f(paramObjEyePos, v3dObjEyePos.x, v3dObjEyePos.y, v3dObjEyePos.z);

// I don't normally call this but I am doing so as I see it in the CG example code. I've tried commenting it out to no avail


// Disable texture stages

// Disable shaders

I've also tried getting the ModelView and Proj matrices using glGetFloatv but the result is the same. I also tried transposing the MVP matrix but again, no luck. Here's my simple shader code (vert):

void main(in float4 iPosition : ATTR0,
in float4 iTangent : ATTR1,
in float4 iBiNormal : ATTR2,
in float4 iNormal : ATTR3,
in float4 iColour : ATTR4,
in float2 iTexCoord0 : ATTR5,
in float2 iTexCoord1 : ATTR6,
in float4 iCustomData : ATTR7,

out float4 oPosition : POSITION, // Translated and projected vertex position
out float3 oVertex : TEXCOORD0, // Vertex (object space)
out float3 oNormal : TEXCOORD1, // Normal (object space)

uniform float4x4 ModelViewProj)// : state.matrix.mvp) // ModelViewProjection matrix
// Transform and project the vertex
oPosition = mul(ModelViewProj, iPosition);

oVertex = iPosition.xyz;
oNormal = iNormal.xyz;

I'm pretty stumped now so any info would be much appreciated.

Share this post

Link to post
Share on other sites
this is going to sound weird, but maybe try a different and more advanced profile, i know that when i tried different profiles i got different results.

Also, what happens with your code?

does it just show a black screen? or do you get triangle soup?
also, i dont have my code with me to post but if you look in the sdk there is a getCGError function that is called after a lot of the api calls, maybe that could give you some more clues.

EDIT::: It has been a while so you'll have to forgive me, but i use the state parameters as well. I used to use hand rolled and ended up moving. Just curious, if your matrix stack is correct you should be getting the exact same thing. maybe do a getfloatv and get the contents of the pipe and output them.

[Edited by - AverageJoeSSU on April 2, 2010 12:51:45 PM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!