开发者

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

开发者 https://www.devze.com 2023-01-20 03:32 出处:网络
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 nice开发者_如何学运维r.

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 nice开发者_如何学运维r.

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.


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);


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


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消