Newbie
Newbie

Reputation: 1613

OpenGL: Sending RGBA color struct into glColor*() as one parameter?

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.

Upvotes: 1

Views: 3626

Answers (3)

elimad
elimad

Reputation: 1152

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.
Ex:

class MyClr
{
public:
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;} 

private:
float m_dat[4];
};

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

// to send to OpenGL
glColor4fv(clrYellow);

Upvotes: 1

Viktor Sehr
Viktor Sehr

Reputation: 13099

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;
glColor4fv(&color.r);

Upvotes: 6

Dr. Snoopy
Dr. Snoopy

Reputation: 56347

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

Upvotes: 3

Related Questions