Double array (Matrix) passing problem
Hey, I''m EXTREMELY new to 3d programming (not at all to 2D) and I''m trying to set up all the matrices I would need.
I have the following. I declare:
typedef float Matrix[4][4];
then I want many functions, lets say:
void Identity(Matrix *matrix)
{
//Set matrix to identity matrix
}
The problem there is that if I do *matrix[0][0] = 1, *matrix[0][1] = 0, etc.... only the 1 gets filled in, I''m assuming I''m doing something wrong in derefrencing a two-dim array.
I also want something like
Matrix CrossProduct(Matrix Mat1, Matrix Mat2)
//(or Matrix *CrossProduct(...))
Unfortunately I cannot pass back atweo-dim array nor a pointer to one.
So how is it done in all the 3d gfx engines??????
I''m so desperate I decided to make a matrix class that has a float matrix[4][4] variable, and all those functions within the class to manipulate the matrix, but I dont see that as being the best approach
Thanks
- Rob
------------------------------
fclose(fp)
------------------------------
ByteMe95::~ByteMe95()My S(h)ite
If it was me I would have put the float array into a structure, such as:
The above works in VC++ and is probably how I would do it first time round. I'm no expert as I'm sure someone else will agree
Edited by - TPH on July 11, 2000 3:32:43 PM
Edited by - TPH on July 11, 2000 3:34:43 PM
typedef struct tagMATRIX{ float e[4][4];} MATRIX; [/source]Then pass it round as a MATRIX * to other methods. If you want to wrap it up in a class, just do something like:[source]class Matrix : public MATRIX{ void Identity(); // or whatever}
The above works in VC++ and is probably how I would do it first time round. I'm no expert as I'm sure someone else will agree
Edited by - TPH on July 11, 2000 3:32:43 PM
Edited by - TPH on July 11, 2000 3:34:43 PM
Well that''s what I started doing, making classes out of everything
But I was wondering if there''s just a way to pass it without making a class and all that pain, just pass it as a simple variable.
------------------------------
fclose(fp)
------------------------------
But I was wondering if there''s just a way to pass it without making a class and all that pain, just pass it as a simple variable.
------------------------------
fclose(fp)
------------------------------
ByteMe95::~ByteMe95()My S(h)ite
Saying as when you pass an array you''re really passing a pointer to data, just move the return value to the argument list.
instead of:
matrix CrossProd (Matrix Matrix1, Matrix Matrix2)
do this:
void CrossProd (Matrix Matrix1, ... , Matrix Matrix3)
and treat Matrix3 as if it were a return value. Any operation performed on Matrix3 will perform on the original data because it is not being copied when it is passed to the function. Rather what is passed is a pointer to the original data. Hopefully that doesn''t sound too confusing.
BTW, you CAN return a pointer to an array. You were probably passing back a pointer to a temporary variable which doesn''t exist outside the scope of the function it was declared in. If you declared it as constant it would work.
-BacksideSnap-
instead of:
matrix CrossProd (Matrix Matrix1, Matrix Matrix2)
do this:
void CrossProd (Matrix Matrix1, ... , Matrix Matrix3)
and treat Matrix3 as if it were a return value. Any operation performed on Matrix3 will perform on the original data because it is not being copied when it is passed to the function. Rather what is passed is a pointer to the original data. Hopefully that doesn''t sound too confusing.
BTW, you CAN return a pointer to an array. You were probably passing back a pointer to a temporary variable which doesn''t exist outside the scope of the function it was declared in. If you declared it as constant it would work.
-BacksideSnap-
So you''re saying if I have
typedef float Matrix[4][4];
Matrix A;
Identity(Matrix matrix)
{
matrix[0][0] = 1; matrix[1][0] = 0; ...
...
...
}
main()
{
Identity(A);
}
Then A will really be filled with the values? I think I actually tried that and it didnt work, any idea why? I thought it was because i had to pass it by reference, but then I had trouble figuring out how :o)
I dont know if I mentioned I''m using Borland Turbo C++ for this since I find it easier than VC6 and faster.
------------------------------
fclose(fp)
------------------------------
typedef float Matrix[4][4];
Matrix A;
Identity(Matrix matrix)
{
matrix[0][0] = 1; matrix[1][0] = 0; ...
...
...
}
main()
{
Identity(A);
}
Then A will really be filled with the values? I think I actually tried that and it didnt work, any idea why? I thought it was because i had to pass it by reference, but then I had trouble figuring out how :o)
I dont know if I mentioned I''m using Borland Turbo C++ for this since I find it easier than VC6 and faster.
------------------------------
fclose(fp)
------------------------------
ByteMe95::~ByteMe95()My S(h)ite
July 11, 2000 04:58 PM
That is correct.
The example you just posted should work.
Since your typedef is an array, you don''t need to pass in Matrix pointers. Ex:
Do: void Identity(Matrix m) { m[0][0]=1;... }
Don''t: void Identity(Matrix* m){ *m[0][0]=1; ...}
*m[0][0] doesn''t work because the [] operators get more precedence than the * operator. So essentially you were doing *(m[0][0]) which is totally different than what you want which is (*m)[0][0]. Passing in pointers to arrays is a bad idea anyway.
The example you just posted should work.
Since your typedef is an array, you don''t need to pass in Matrix pointers. Ex:
Do: void Identity(Matrix m) { m[0][0]=1;... }
Don''t: void Identity(Matrix* m){ *m[0][0]=1; ...}
*m[0][0] doesn''t work because the [] operators get more precedence than the * operator. So essentially you were doing *(m[0][0]) which is totally different than what you want which is (*m)[0][0]. Passing in pointers to arrays is a bad idea anyway.
quote: Original post by ByteMe95
Then A will really be filled with the values? I think I actually tried that and it didnt work, any idea why? I thought it was because i had to pass it by reference, but then I had trouble figuring out how :o)
I dont know if I mentioned I''m using Borland Turbo C++ for this since I find it easier than VC6 and faster.
------------------------------
fclose(fp)
------------------------------
I''ll forget I even saw that last sentence. And it wouldn''t even matter... I highly doubt Turbo c++ and VC++ handle arrays any differently.
Why do people always doubt me... Here you go, compile this, jackass, and then tell me it doesn''t work.
#include <stdio.h>void Identity (float Matrix[4][4]){ Matrix[0][0] = 1; Matrix[0][1] = 0; Matrix[0][2] = 0; Matrix[0][3] = 0; Matrix[1][0] = 0; Matrix[1][1] = 1; Matrix[1][2] = 0; Matrix[1][3] = 0; Matrix[2][0] = 0; Matrix[2][1] = 0; Matrix[2][2] = 1; Matrix[2][3] = 0; Matrix[3][0] = 0; Matrix[3][1] = 0; Matrix[3][2] = 0; Matrix[3][3] = 1;}int main(void){ int i, j; float Mat[4][4]; Identity (Mat); for (i=0;i<4;i++) { for (j=0;j<4;j++) { printf ("value %d%d: %1.1f \n", i, j, Mat<i>[j]); } } return 0;}
At least try something before writing it off, otherwise why even ask for help.
-BacksideSnap-
Whoa, wtf? That printf line should have Mat[j] rather than the greater than/less than things. Something got jacked up in the transition. But you already knew that...
-BacksideSnap-
-BacksideSnap-
Yo, why yoiu gotta be such a dick "jackass"???
I wrote that last post from work and I didnt have my code on me, but last night I DID in fact try it, I''ve actually been trying for a couple of days before I posted here asshole.
So why dont you just relax and watch your fuckin mouth
Btw, thanx for the info ;oP
------------------------------
fclose(fp)
------------------------------
I wrote that last post from work and I didnt have my code on me, but last night I DID in fact try it, I''ve actually been trying for a couple of days before I posted here asshole.
So why dont you just relax and watch your fuckin mouth
Btw, thanx for the info ;oP
------------------------------
fclose(fp)
------------------------------
ByteMe95::~ByteMe95()My S(h)ite
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement