Jump to content
  • Advertisement
Sign in to follow this  
Kazade

Whats the best way of doing this?

This topic is 4349 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

Hey, I have a Vector class which looks something like this:

class Vec3 {
public:
     float x, y, z;

public:
     ///operators, methods go here

    ...
}

This is of course pretty standard, now rather than supplying xyz to glVertex calls etc. I want to use the glVertex3fv() array counterparts functions. I was wondering if someone knew the best way to write a method that would return the values in the correct format for the 3fv methods. I thought perhaps returning a reference to x might work but im not sure its a good way of doing it, do all compilers store the variables one after the other in memory? I'll be honest, I know I'm being slightly lazy just asking here when I could probably work this out myself, but I flew home from Portugal last nite and im knackered :) so thanks in advance to anyone that replies, Cheers Luke.

Share this post


Link to post
Share on other sites
Advertisement
Floats are 4-bytes long, so on any relatively sane platform they will be tightly packed. So yes, declare an operator float () which returns a pointer to x, and it will be called automatically:

class vector
{
public:
float x, y, z;
public:
operator float *() {return &x;}
};

vector v1;

glVertex3fv(v1);

Share this post


Link to post
Share on other sites
Another way is to use non-standard extensions as the nameless union:

class Vector3
{
public:

union
{
// A struct makes sure x, y and z don't share the same memory of the union
struct
{
float m_x, m_y, m_z;
}

// The array uses the same memory as the struct
float m_elements[3];
}
};


This is what I use on MS Visual C++. Greetz,

Illco

Share this post


Link to post
Share on other sites
If you use swifcoder's code, you should at least make them GLfloats instead of floats, because GLfloats are guaranteed to be four bytes. If it was me though, I would just store the x, y, and z in an array.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Simian Man
If you use swifcoder's code, you should at least make them GLfloats instead of floats, because GLfloats are guaranteed to be four bytes. If it was me though, I would just store the x, y, and z in an array.


If they are IEEE floats then they are 4 bytes, so I would not worry about the above quote.

As for swiftcoder's code I would add another operator

operator float const*() {return &x;}


Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
As for swiftcoder's code I would add another operator
*** Source Snippet Removed ***


i'd prefer:


struct vector {

float x, y, z;

typedef float (&array_ref)[3];
typedef const float (&const_array_ref)[3];

//....

operator array_ref() {
return reinterpret_cast<array_ref>(*this);
}

operator const_array_ref() const {
return reinterpret_cast<const_array_ref>(*this);
}

};


But that is being abit too pedantic.

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!