🔒 Locked
vector class....
Advertisement
Original Post
Does anybody here use the Vector3 class that microsoft provides for their directX stuff??? or do you all take pride in writing your own?? Just wondering if they could possibly be faster... lots of them look useful if you don''t feel like writing your own... like:
length()
normalize()
scale()
dot()
cross()
lerp()
project()
unproject()
blah blah blah blah....
Write your own, it has many advantages being able to precicely define what''s in your vector class, as you then know (how easy it might be) how all the used functions work and you can shape.
For an example, i have, in my vector (vertex) class a normalize() function and a normalize(double nv).
The second version comes in handy when you want a vertex to emit light, according to the total (or per direction) light recieved.
This will allow you to create pretty wierd looking glowing objects, or even lasers.
There are many other advantages, and, what''s the problem of writing your own?
For an example, i have, in my vector (vertex) class a normalize() function and a normalize(double nv).
The second version comes in handy when you want a vertex to emit light, according to the total (or per direction) light recieved.
This will allow you to create pretty wierd looking glowing objects, or even lasers.
There are many other advantages, and, what''s the problem of writing your own?
hehe, the only problem with writing my own is I only remember the cross product from college, its the determinate of...
& I know what it gives me... a vector orthogonal to U & V & it points in a direction according to the right hand rule.
UxV = | X Y Z |
| Ux Uy Uz |
| Vx Vy Vz |
& I know what it gives me... a vector orthogonal to U & V & it points in a direction according to the right hand rule.
Make your own, it is quite handy to have a specialized class for doing what you need.
An easy and quick way to do crossproduct is:
v3d v3dCross(v3d & a, v3d & b)
{
v3d c;
c.x = b.z * a.y - b.y * a.z;
c.y = b.x * a.z - b.z * a.x;
c.z = b.y * a.x - b.x * a.y;
return c;
}
It''s just expanding what the deturminate does.
An easy and quick way to do crossproduct is:
v3d v3dCross(v3d & a, v3d & b)
{
v3d c;
c.x = b.z * a.y - b.y * a.z;
c.y = b.x * a.z - b.z * a.x;
c.z = b.y * a.x - b.x * a.y;
return c;
}
It''s just expanding what the deturminate does.
I think my method is better.. inheriting from D3DXVECTOR3 (or whatever the actual name is). Use Direct3D's special optimized code, and still add the functions you want.
It also makes cross-platforming somewhat easier; instead of changing *all* your code, you can just change the definition of your Vector3 class (except for where you use the D3DX functions, of course.)
edit: Made it sound less like an advertisement.
[edited by - psykr on January 3, 2004 3:52:15 PM]
It also makes cross-platforming somewhat easier; instead of changing *all* your code, you can just change the definition of your Vector3 class (except for where you use the D3DX functions, of course.)
edit: Made it sound less like an advertisement.
[edited by - psykr on January 3, 2004 3:52:15 PM]
psykr, you are just a salesman at heart
I'm not sure the D3D vector is really all that optimised.
and writing your own class does have some nice uses. and is VERY good for learning. Don't under estimate this. And don't forget about little things like exceptions (divide by zero, etc), if you do your own handling of these it's a good idea to write this code yourself when this could happen.
I have Vector2,Vector3, Vector4 iVector2, iVector3, iVector4 classes.
Vector3: (copied from VS)
[pimp]
[/pimp]
if anyone is interested in specifics about what these do and their code I'll be happy to share
[edit]
yeah. also, vectors can be other things to. A Vector4 class is very useful as it can be treated as a plane equation, so having the likes of distance code, intercept code, ect, in a Vector4 class is _very_ useful without needing a seperate Plane class.
hehehehe
[edited by - RipTorn on January 4, 2004 1:02:15 AM]
and writing your own class does have some nice uses. and is VERY good for learning. Don't under estimate this. And don't forget about little things like exceptions (divide by zero, etc), if you do your own handling of these it's a good idea to write this code yourself when this could happen.
I have Vector2,Vector3, Vector4 iVector2, iVector3, iVector4 classes.
Vector3: (copied from VS)
[pimp]
Vector3::asVector2
Vector3::closestPointToLineOnLine
Vector3::closestPontOnLineCapped
Vector3::closestPontOnLine
Vector3::CROSS
Vector3::DOT
Vector3::extendToLength
Vector3::generateNormal
Vector3::getLengthSquared
Vector3::getLength
Vector3::inBounds
Vector3::INVERSE_CROSS
Vector3::invertLength
Vector3::isZeroVector
Vector3::lineBoxIntercept
Vector3::operator * float
Vector3::operator *= float
Vector3::operator / float
Vector3::operator /= float
Vector3::operator + float
Vector3::operator += float
Vector3::operator - float
Vector3::operator -= float
Vector3::operator * Matrix
Vector3::operator *= Matrix
Vector3::operator / Matrix
Vector3::operator /= Matrix
Vector3::operator + Matrix
Vector3::operator += Matrix
Vector3::operator - Matrix
Vector3::operator -= Matrix
Vector3::operator * Vector3
Vector3::operator *= Vector3
Vector3::operator / Vector3
Vector3::operator /= Vector3
Vector3::operator + Vector3
Vector3::operator += Vector3
Vector3::operator - Vector3
Vector3::operator -= Vector3
Vector3::operator < Vector3
Vector3::operator == Vector3
Vector3::operator != Vector3
Vector3::operator > Vector3
Vector3::operator []
Vector3::reflect
Vector3::REVERSE_CROSS
Vector3::setLength
Vector3::shortenToLength
Vector3::Vector3
Vector3::Vector3
[/pimp]
if anyone is interested in specifics about what these do and their code I'll be happy to share
[edit]
yeah. also, vectors can be other things to. A Vector4 class is very useful as it can be treated as a plane equation, so having the likes of distance code, intercept code, ect, in a Vector4 class is _very_ useful without needing a seperate Plane class.
hehehehe
[edited by - RipTorn on January 4, 2004 1:02:15 AM]
RipTorn,
I'm very interested in the specifics of your vector class, I think have most of the code for it (you linked to it in a previous post somewhere) but I would actually like to learn the crap. This is of course related to my other post about billboarding. All I know about the modelview matrix is this:
[NEWBIE]
but I don't know what any of that means, & I certainly don't know what adding a matrix to a vector does (or multiplying, etc)
I do understand vectors, & I can visualize them, & I know what a cross product gives me, & know VERY simple vector math. Any help or mini-tuts you can give me would be GREATLY appreciated.
[/NEWBIE]
thanks, luke
[edited by - Luke Miklos on January 4, 2004 2:37:30 AM]
I'm very interested in the specifics of your vector class, I think have most of the code for it (you linked to it in a previous post somewhere) but I would actually like to learn the crap. This is of course related to my other post about billboarding. All I know about the modelview matrix is this:
//The matrix for glTranslatef(x,y,z) looks like this:
1 , 0 , 0 , 0
0 , 1 , 0 , 0
0 , 0 , 1 , 0
x , y , z , 1
//The matrix for glRotatef(theta,x,y,z) is much more complex:
float c = cosf(theta);
float s = sinf(theta);
x*x(1-c)+c , x*y(1-c)+z*s , x*z(1-c)-y*s , 0
y*x(1-c)-z*s , y*y(1-c)+c , y*z(1-c)+x*s , 0
z*x(1-c)+y*s , z*y(1-c)-x*s , z*z(1-c)+c , 0
0 , 0 , 0 , 1
[NEWBIE]
but I don't know what any of that means, & I certainly don't know what adding a matrix to a vector does (or multiplying, etc)
I do understand vectors, & I can visualize them, & I know what a cross product gives me, & know VERY simple vector math. Any help or mini-tuts you can give me would be GREATLY appreciated.
[/NEWBIE]
thanks, luke
[edited by - Luke Miklos on January 4, 2004 2:37:30 AM]
//The matrix for glTranslatef(x,y,z) looks like this:
1 , 0 , 0 , 0
0 , 1 , 0 , 0
0 , 0 , 1 , 0
x , y , z , 1
The first matrix is the standard matrix, 1 in x stands for 1 at the first coordinate(row) (the x coordinate), the 0s in y and z mean that every unit in the first coordinate stands for 0 units in those dimensions.
So, if we'll transform an x coordinate of 3.7 using this matrix (1st row as it is the x) the x coordinate will stand for:
3.7*1 in the x dimension, 3.7*0 in y and 3.7&\*0 in z
So, i don't know if you know anything about drawing a circle with cos/sin, but this works the same.
When you rotate around the z axis (3rd row) you'll change all rows except that one. And to calculate the value of both other rows, you'll use the current values of those rows:
// new matrix:
cos(a)*row0[0]+sin(a)*row1[0],cos(a)*row0[1]+sin(a)*row1[1],row0[2]
cos(a)*row1[0]+sin(a)*row0[0],cos(a)*row1[1]+sin(a)*row0[1],row0[2]
row2[0],row2[1],row2[2]
I think this is what it should be.
Note : all sin/cos/tan operations on the computer work with radiants, that means a full circle doesn't exist out of 360.0 (degrees) but 2*PI (about 6.28), keep that in mind using sin/cos/tan and corresponding functions!
[edited by - Tree Penguin on January 4, 2004 7:31:53 AM]
[edited by - Tree Penguin on January 4, 2004 7:33:31 AM]
psykr:
Using inheritence for the vector class sounds a lot like overhead to me. One should generally avoid inheritence with classes frequently used in the inner loops. A vector class is typically such a class since it will be used zillions of times every loop.
You can make a vector class of your own that is compatible with the D3D one without using inheritence.
/Q
Using inheritence for the vector class sounds a lot like overhead to me. One should generally avoid inheritence with classes frequently used in the inner loops. A vector class is typically such a class since it will be used zillions of times every loop.
You can make a vector class of your own that is compatible with the D3D one without using inheritence.
/Q
I know all about the trig & now the matrix makes perfect sense. I''m curious... when I call the glscale() function... does that modify the modelview-matrix or are the scaled umm... "computations" applied behind it?
When you call glScale(x,y,z) all parameters in the x coordinate row of the matrix will be multiplied by the x value, and so for the y and z.
quote:
Original post by Quirk
psykr:
Using inheritence for the vector class sounds a lot like overhead to me. One should generally avoid inheritence with classes frequently used in the inner loops. A vector class is typically such a class since it will be used zillions of times every loop.
You can make a vector class of your own that is compatible with the D3D one without using inheritence.
/Q
Is this true? I dont use inherited classes for vecrors; but I always assumed the compiler will do a good job to prevent the overhead.
All,
About C++, I have had some huge successes (& failures) in converting my recent project over to OOP, I love modular designs & am actually taking a grad CS course at Washington U. on Modular Design (in java BOOOO), BUT... the friggin glgentextures() & algensources() have given me some headaches. Its apparent to me that I can''t get them in working in a contructor of a class/object. Anybody have any info on these guys besides what MSDN spits out at us? thanks.
Orbano,
I am 100% American. My great grandfather came over here from Slovakia (was Czechoslovakia before ''93) & I think he had a hungarian background, but lets not tell any slovaks that.
Luke
About C++, I have had some huge successes (& failures) in converting my recent project over to OOP, I love modular designs & am actually taking a grad CS course at Washington U. on Modular Design (in java BOOOO), BUT... the friggin glgentextures() & algensources() have given me some headaches. Its apparent to me that I can''t get them in working in a contructor of a class/object. Anybody have any info on these guys besides what MSDN spits out at us? thanks.
Orbano,
I am 100% American. My great grandfather came over here from Slovakia (was Czechoslovakia before ''93) & I think he had a hungarian background, but lets not tell any slovaks that.
Luke
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement