i’m doing some lod optimizations, where eventually i’m going to need to find the screen distance between two points, but first i have to find a single point, which is what i posted about.
all of the doc i’ve read on the ogl matrix multiplication is ordered modelview -> projection -> perspective division -> viewport, or modelview*projection, then what i’m not sure about.
also, i know that the matrices i’ve generated are correct, because i don’t call gluPerspective or glFrustum. I just call a glLoadMatrix for the projection matrix and the modelview matrix, and everything renders correctly.
what i’m doing to check here is taking a point, rendering it in a perspective state, then loading a -1x1 ortho projection, and redrawing it.
i’d assume that for a -1x1 ortho projection that the actual dimensions of the viewport would be irrelevant, but this seems to be where my problem is…
[This message has been edited by Succinct (edited 12-26-2000).]
If you mean that “ViewMatrix” is the model view matrix, then I can reassure you that the order of muliplication you mentioned above is incorrect, because if it was like you posted, i.e:
Screen=ViewMatrixProjectionMatrixWorld
then that means that “World” is getting multiplied with “ProjectionMatrix” befor it is multiplied with “ViewMatrix”, in other words, it’s getting projected “then” transformed ,which makes no sense.
Anyway it depends on how you imagine the matrix itself, in either way, the vector “World” should be multiplied with the model view matrix “first” then multiplied with the projection matrix:
Vector3d p = mViewMatrixmProjectionMatrixv;
return Vector3d( p.x/p.w,p.y/p.w,p.z/p.w );
}
oh, i’m using screen coords to reduce geometrey, not for culling.
i don’t usually like just posting code and saying “hey, debug this for me, cuz i cant”, but this time you asked i just like to compare algorithms, in plain english, maybe pseudocode.
thx, though, softland… but i’m not sure its the code, and not cuz i’m suffering from the “i’m right” syndrome that plagues so many programmers, it’s just cuz the code works for me in other places.
i use the multiply matrix in composing my view matrix. first i generate a translation matrix for the -position of the camera, rotate to the camera’s orientation, then translate to the near plane. that stuff all works out
the only thing i can see is maybe my MultiplyMatrix( m1,m2 ) is actually performing m2*m1, but that’s easily fixed, and i can write around that if it wasn’t, but now thinking about it, the vector matrix function isn’t even being used in this program, but i just cut and pasted it from my old ray-tracing program, where it worked.
basically, what my question really is: what ogl does after it gets these coordinates, called clip coordinates, i believe. ogl says that all the axis coords should be in the range [-1,1], but i’m not sure what w should be. shouldn’t z be 0 here? well, -(1,1,1) <= (x,y,z) <= (1,1,1), z != 0, w is large and positive
i know that next is the transformation to Normalized device coords, and that’s performed by dividing by w, but right now w is around 500… it’s making x and y very close to 0, cuz [-1,1]/500 = [-1/500,1/500]
man, i hope it’s the code, cuz that’s easier to fix!
thx again, softland
[This message has been edited by Succinct (edited 12-28-2000).]
I’m sorry for not replying soon, but I have some exams and I was experiencing some weird problems with my modem that kept me unconnected for a while. Anyway, I don’t know what’s the real problem in your code, that’s if there are any. However I want to make sure of 3 things:
How can you write p = mViewMatrixmProjectionMatrixv?
did you override the * operator? If so, are you sure how C is determining their precedence?
Are you sure that
Vector3d Camera::TransformPoint( const Vector3d& v )
is correct and that it shouldn’t be
Vector3d Camera::TransformPoint( const Vector4d& v )
Are you sure that
Vector3d p = mViewMatrixmProjectionMatrixv
is correct and that it shouldn’t be
Vector4d p = mViewMatrixmProjectionMatrixv
I’m sorry, but I’m a Delphi programmer and my experience with C is not as good. Anyway, I don’t know if you made some mistakes while you were posting the code.
it evaluates from left to right, and that’s the same as
(v*mViewMatrix)*mProjectionMatrix
the parameters are supossed to be 3d, cuz w is just 1, but yeah, p is 'spossed to be 4d. i just wrote that off the top of my head, cuz i deleted it before i decided to put it in the post good eyes.
i’m just curious what ogl does after the projection, but before the viewport scaling.
i dunno, is the source for glu open? like gluUnproject? /that/ works…
p.s., thx again, softland, no one’s ever seemed this interested in my coding questions
something about matrix multiplication :
the right order to execute the multiplications is:
ModelViewMatrixv = v1
ProjectonMatrixv1 = v2
which may be written so:
v2 = ProjectionMatrix*(ModelViewMatrixv)
but the matrix mults are associative:
(AB)C = A(BC), which means that
you can also write v2 = (ProjectionMatrixModelViewMatrix)v
and the result is same (a fine optimisation is to compute TransfomMatrix = ProjectionMatrixModelVeiwMatrix when you get them and then to calc v2 = TransformMatrix*v; this will spare one matrix mult which is not so fast anywhere…
I’m not sure, but I think that your code doesn’t make the multiplications in this order. Please check it
You must also count one last tranformation: glViewport() which is applied over the vector v2 to get the (X,Y) window coords.
you must apply the same transform too… (see glViewport() info in MSDN for the correct expressions…)
One thing more:
the matrix mults are NOT comutative, which means that AB != BA (in the most cases …
Your code is:
Vector3d p = mViewMatrixmProjectionMatrixv;
so in any case this is not the right order…
try to write it so:
Vector3d p = mProjectionMatrixmViewMatrixv;
don’t forget also glViewport() transform,
and at last I don’t think it’s a good idea to return 3d vector as a result from multiplication with 4x4 matrix* 4x1 vector, even if this may be correct in this case and to gain some performance…(which I don’t think will change your fps ever… )
[This message has been edited by martin_marinov (edited 01-04-2001).]