[GRASS-dev] Help with gsd_color_func() for NVIZ

Hi,

I am trying to understand the OpenGL color setting code in NVIZ for changing
the North Arrow text color. I am not very good with the bitwise/GL stuff,
so can anyone give me a hand at understanding how to feed gsd_color_func()
correctly?

this is in lib/ogsf/gsd_objs.c and lib/ogsf/gsd_prim.c

I was expecting 0x00000 to mean 0xRRGGBB, but it's backwards for me.
The least significant pair comes first.
(I'm running Pentium4 / 32bit / little endian)

some experiments:

gsd_color_func(0xFF0000); r=0 g=0 b=-1 a=0
gsd_color_func(0x00FF00); r=0 g=-1 b=0 a=0
gsd_color_func(0x0000FF); r=-1 g=0 b=0 a=0
gsd_color_func(0xFFFFFF); r=-1 g=-1 b=-1 a=0
gsd_color_func(0x00000000); r=0 g=0 b=0 a=0
gsd_color_func(0xFFFFFFFF); r=-1 g=-1 b=-1 a=-1
gsd_color_func(0xFF000000); r=0 g=0 b=0 a=-1
gsd_color_func(0x00FFFFFF); r=-1 g=-1 b=-1 a=0

gsd_color_func(0x000008); r=8 g=0 b=0 a=0
gsd_color_func(0x000010); r=16 g=0 b=0 a=0
gsd_color_func(0x000070); r=112 g=0 b=0 a=0
gsd_color_func(0x000080); r=-128 g=0 b=0 a=0
gsd_color_func(0x000090); r=-112 g=0 b=0 a=0
gsd_color_func(0x0000A0); r=-96 g=0 b=0 a=0
gsd_color_func(0x0000A8); r=-88 g=0 b=0 a=0
gsd_color_func(0x0000FE); r=-2 g=0 b=0 a=0

(am I doing %d with something that should be e.g. %c, %x?)

relevant code from gsd_prim.c:

#define RED_MASK 0x000000FF
#define GRN_MASK 0x0000FF00
#define BLU_MASK 0x00FF0000
#define ALP_MASK 0xFF000000

#define INT_TO_RED(i, r) (r = (i & RED_MASK))
#define INT_TO_GRN(i, g) (g = (i & GRN_MASK) >> 8)
#define INT_TO_BLU(i, b) (b = (i & BLU_MASK) >> 16)
#define INT_TO_ALP(i, a) (a = (i & ALP_MASK) >> 24)

/************************************************************************/
void gsd_color_func(unsigned int col)
{
    GLbyte r, g, b, a;

    /* OGLXXX
     * cpack: if argument is not a variable
     * might need to be:
     * glColor4b(($1)&0xff, ($1)>>8&0xff, ($1)>>16&0xff, ($1)>>24&0xff)
     */
    INT_TO_RED(col, r);
    INT_TO_GRN(col, g);
    INT_TO_BLU(col, b);
    INT_TO_ALP(col, a);
    glColor4ub(r, g, b, a);

/* HB DEBUG */
fprintf(stderr, "gsd_color_func: r=%d g=%d b=%d a=%d\n", r, g, b, a);

    return;
}

/************************************************************************/

thanks,
Hamish

Hamish wrote:

I am trying to understand the OpenGL color setting code in NVIZ for changing
the North Arrow text color. I am not very good with the bitwise/GL stuff,
so can anyone give me a hand at understanding how to feed gsd_color_func()
correctly?

this is in lib/ogsf/gsd_objs.c and lib/ogsf/gsd_prim.c

I was expecting 0x00000 to mean 0xRRGGBB, but it's backwards for me.
The least significant pair comes first.
(I'm running Pentium4 / 32bit / little endian)

That's how gsd_prim.c defines it:

#define RED_MASK 0x000000FF
#define GRN_MASK 0x0000FF00
#define BLU_MASK 0x00FF0000
#define ALP_MASK 0xFF000000

some experiments:

gsd_color_func(0xFF0000); r=0 g=0 b=-1 a=0

[snip]

(am I doing %d with something that should be e.g. %c, %x?)

No; the variables:

    GLbyte r, g, b, a;

should be GLubyte; GLbyte is signed, i.e. 0xFF == -1.

This won't matter to the glColor4ub() call, which will cast them to
GLubyte, but it will matter to the fprintf() call, which will cast
them to int.

--
Glynn Clements <glynn@gclements.plus.com>