Sign in to follow this  
JackTheRapper

ModelViewProj Matrix Problem

Recommended Posts

JackTheRapper    150
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
AverageJoeSSU    564
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.

-Joe




Share this post


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


// Vert shader
cgGLEnableProfile(CG_PROFILE_ARBVP1);
cgGLBindProgram(vertProgram);

// Frag shader
cgGLEnableProfile(CG_PROFILE_ARBFP1);
cgGLBindProgram(fragProgram);

// Diffuse texture
glActiveTextureARB(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
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
cgUpdateProgramParameters(vertProgram);
cgUpdateProgramParameters(fragProgram);

pMesh->Draw();

// Disable texture stages
glActiveTextureARB(GL_TEXTURE0);
glDisable(GL_TEXTURE_2D);

// Disable shaders
cgGLDisableProfile(CG_PROFILE_ARBFP1);
cgGLDisableProfile(CG_PROFILE_ARBVP1);



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
AverageJoeSSU    564
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this