Take the 2-minute tour ×

Is there some way to send a struct like:

struct COLOR {
    float r, g, b, a;

directly into glColor*() function as one parameter? Would make the code nicer.

I could make own function and send separetely each R,G,B,A values onto glColor4f() but that wouldnt be that nice. So im looking a way to send it the most optimal way as possible.

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted
COLOR color;
glColor4fv((GLfloat*) &color);

Update: I wouldn't recommend creating an inline function, however, you could use GLfloat in your struct to get the expression clearer. Use &color.r to avoid a compiler warning.

struct COLOR {
  GLfloat r,g,b,a;
COLOR color;
share|improve this answer
hmm, is it possible to make it any cleaner from that? –  Newbie Oct 17 '10 at 19:41
actually, I think it's as clear as it can possibly be. –  Viktor Sehr Oct 17 '10 at 19:47
@Newbie: You can just do glColor4fv(&color.r), assuming GLfloat is just float. –  GManNickG Oct 17 '10 at 19:48
should i make inline void glColor(const COLOR &color){ return glColor4fv((GLfloat*)&color); } ? would clean up the code, but im not so sure about using gl function looking function names... also does it make it faster if i use &color.r rather than (GLfloat*)&color ? if i make my struct use GLfloat instead could i just use &color ? –  Newbie Oct 18 '10 at 15:30
updated the answer –  Viktor Sehr Oct 18 '10 at 17:15
show 2 more comments

The most optimal way of sending vertex data is Vertex Arrays, it will also make your code look nicer, you should take a look.

share|improve this answer
i dont always want to create vertex arrays for little things since the optimizing would be useless –  Newbie Oct 18 '10 at 15:27
add comment

to make the code of calling glColor4fv simpler,
i prefer writing a small class to encapsulate the color values and
use operator overloading to to convert to float * automatically.

class MyClr
MyClr(float r, float g, float b, float a)
    m_dat[0] = r;
    m_dat[1] = g;
    m_dat[2] = b;
    m_dat[3] = a;
// if needed, we can 
// overload constructors to take other types of input like 0-255 RGBA vals
// and convert to 0.0f to 1.0f values

// wherever a float* is needed for color, this will kick-in
operator const float* ()const { return (float*)m_dat;} 

float m_dat[4];

// usage
MyClr clrYellow (1.0f, 1.0f, 0.0f, 1.0f);

// to send to OpenGL
share|improve this answer
add comment

Your Answer


