Thread: Treat members as an array? (opengl)

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    7

    Treat members as an array? (opengl)

    I guess you've herad this before, but please bear with me.

    I'm doing some opengl programming and've made this class:

    Code:
    struct Color 
    { 
       Color() {} 
       Color(float r_, float g_, float b_) : r(r_), g(g_), b(b_) {} 
       void Set(float r_, float g_, float b_); 
       void Invert(); 
       static Color Invert(Color& color); 
       void AdjustBrightness(float adjustment);   // in percent e.g. (1.3 - 0.3) 
       float r, g, b, a; 
        
       static Color GetPredefined(int color); 
       void SetToPredefined(int color); 
       enum 
       { 
          Red, 
          Green, 
          Blue, 
          Yellow, 
          Oranage, 
          Brown, 
          White, 
          Black 
       }; 
       static Color PredefinedColors[]; 
       void operator=(int predefined_color); 
       bool operator==(Color& c); 
    };
    Can I safely do this?
    Code:
    Color a; 
    glColor4fv(&a.r); //expects an 4 sized GLfloat array with r,g,b,a
    If I can, can I do this on more complex classes too?

    Thanks!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> Can I safely do this?
    Not in a portable manner. You may be able to use compiler-specific options and pragma's to achieve the desired memory layout of your classes.

    If you want to be portable, simply replace r, g, b, and a with a float array.

    For more info on object memory layout: http://groups-beta.google.com/groups...+memory+layout

    gg

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    Thanks

    ...

    That sucks

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    Ok, I go with the array.

    Code:
    struct Color 
    { 
       Color() {} 
       Color(float r_, float g_, float b_, float a_) 
       { 
          r() = r_; 
          g() = g_; 
          b() = b_; 
          a() = a_; 
       } 
       float arr[4]; 
       float& r() { return arr[0]; } 
       float& g() { return arr[1]; } 
       float& b() { return arr[2]; } 
       float& a() { return arr[3]; } 
    };
    Will the compiler (a good) be able to optimize a call to r(), leaving no overhead?

    And is there any way of doing somthing like this:
    Code:
    Color() : arr( {0,0,0,0} ) 
    {}

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    1. I don't know. I know that you can use the inline keyword to inform the compiler to attempt to inline the code.

    2. Well, kind of. I was surprised, but it looks like using {} inside () is a syntax error. But you can do something like this: (Forgive the renaming of variables; it was easier for me to work with)
    Code:
    struct Color 
    { 
       Color() {} 
       Color(float r_, float g_, float b_, float a_)
       { 
          m_arr[0] = r_; 
          m_arr[1] = g_; 
          m_arr[2] = b_; 
          m_arr[3] = a_; 
       } 
       Color(float arr[4])
       { 
          m_arr[0] = arr[0]; 
          m_arr[1] = arr[1];
          m_arr[2] = arr[2];
          m_arr[3] = arr[3];
       } 
       float& r();
       float& g();
       float& b();
       float& a();
    private:
       float m_arr[4]; 
    };
    
    inline float& Color::r() {
    	return m_arr[0];
    } 
    inline float& Color::g() {
    	return m_arr[1];
    } 
    inline float& Color::b() {
    	return m_arr[2];
    } 
    inline float& Color::a() {
    	return m_arr[3];
    } 
    
    int main()
    {
    	float init[4] = {1,2,3,4};
    	Color a(init);
    	return 0;
    }
    Of course, that doesn't really gain you anything over the Color a(1,2,3,4) syntax.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    I know I can use simple assignment inside the constructor, but I've heard that initalizer lists / preample (?) is alot quicker.

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    According to my compiler, you cannot specify an explicit initializer for an array. Unless there's some clever way that I'm not seeing, I think it has to be done as assignment.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    Ok, does anyone know more about the performance differences between preample and assignment?

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I found a pretty good explanation Here

    gg

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    Yup, that's were I had seen it a long time ago.

    Since I probably will create many objects like this it is unaceptable to create 4 unneccesarily floats each time, but it's a lot better to use a array to store the values when I'm using opengl.

    I can't see why you shouldn't be able to avoid this problem when the array has a fixed size.
    It is exacly the same as if I'd used 4 members, exept the array must be in correct sequence.

    There must be a way...

  11. #11
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Zen_id3b
    Ok, does anyone know more about the performance differences between preample and assignment?
    Quote Originally Posted by Zen_id3b
    Since I probably will create many objects like this it is unaceptable to create 4 unneccesarily floats each time
    Well, according to Codeplug's link, it's the same with either initialization lists or assignment.
    From www.parashift.com/c++-faq-lite/ctors.html#faq-10.6
    Note: There is no performance difference if the type of x_ is some built-in/intrinsic type, such as int or char* or float.
    So you can pass your fixed array in and use the assignment without any performance loss.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    oh.. I didn't see that

    Well, then probløem solved.

    Thanks

    PS: will it be quicker to not initalize the array? ( in Color() ctor )

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quicker, yes. But is it a good idea?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static array of initialized members?
    By Blackroot in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2009, 09:36 PM
  2. placing struct members into an array
    By droseman in forum C Programming
    Replies: 5
    Last Post: 01-27-2009, 09:18 AM
  3. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM