Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Game Programming (http://www.go4expert.com/articles/game-programming/)
-   -   How to Handle RGBA Color Format in OpenGL (http://www.go4expert.com/articles/handle-rgba-color-format-opengl-t29558/)

david_BS 19Mar2013 11:17

How to Handle RGBA Color Format in OpenGL
 
1 Attachment(s)
I did a sample program that shows how you can handle colors in RGBA format with the purpose of using them in an Opengl32 program, by the way, the sample program is itself the Opengl32 program and it'll show the results of managing different colors.

First, you would like to read this manual - http://mediafire.com/view/?n4n9zyzo0uda7ci

It's brief, then I say some words about how a color is represented in programming.

A color is represented as a 32 bit value, which its 24 first bits are for colors red, green and blue, and the remaining 8 are for the transparency (known as 'alpha'). For example, we take color 'White' in integer mode it is:
Code:

255, 255, 255, 255
remember it means, red , green, blue, alpha

In floating point it is the same:
Code:

255.0, 255.0, 255.0, 255.0
But, Opengl32 functions (at least the ones I tested), have float type arguments, and a color could be from 0.0 to 1.0 so all this involves the need of a conversion. But still is needed to mention another mode of representation, the hexadecimal:

Code:

DWORD cWhite = 0xFFFFFFFF;
This means, you have one color in one variable (32 bits variable). As you know the byte FF is 255 in integer mode. so it's the same, but in one variable.

Well, I am not the expert, so I just tell you that I wrote 4 functions to handle the conversion from one mode to another. I mean by 'mode' either the color is represented in 4 integers , 4 floats, or just 1 variable that has a hexadecimal value.

What I know, is that the integer mode is used to load colors from a configuration file or maybe to load them in memory by user input. Floating point mode is for the Opengl32 arguments in some functions. They are just a way to handle each of the 4 components individually.

I used a table with 15 colors so when you run the program you can check them.

Now let's see the code:

main.cpp

Code:


//
// By 85
// Credits: OGC, opengl.org
// boyscout_etk@hotmail.com
// etalking.com.ar
// 2013
//

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#pragma comment (lib, "opengl32.lib")
#pragma comment (lib, "GLUT32/glut32.lib")
#include<windows.h>
#include<stdio.h>

#include "GLUT32/glut.h"
#include <gl\gl.h>
#include <gl\glu.h>

#include "rgba_funcs.h"

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

static float scale = 1.4;
static float transx, transy, rotx, roty;

int CURCOLOR =0;//Color actual (Se cambia con el teclado)
int TablaRGBA[15][4] = {//Tabla con 15 colores diferentes en RGBA

        {80,150, 30, 110},
        {255, 40, 40,200},
        {40, 80,255,200},
        {250,250, 22, 254},
        {0,  0,  0, 128},
        {80,150, 30, 110},
        {255,255,255,255},
        {15, 50,255, 50},//Consola del Hurricane XD
        {220,220,220,200},
        {0,128,0,200},
        {255,255,255,255},
        {220,220,220,200},
        {100,255,160,200},
        {100,255,160,200},
        {51,51,204,180},//Menu del Hurricane XD
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void bfunc(void)
{
        static int state;
        if(state ^= 1)
        {
                glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
                glEnable(GL_BLEND);
        }
        else
        {
                glDisable(GL_BLEND);
        }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void init(char* filename)
{
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void TestPluginRGBAfuncs(){// Probando el plugin

        int v1[4] = {51,51,204,180};
        float v2[4] = {0,0,0,0};
        rgbaInt_a_rgbaFloat(v1,v2);
        printf("%f %f %f %f\n", v2[0], v2[1], v2[2], v2[3]);
        system("pause");

        //=================================

        int vec1[4] = {51,51,204,180};
        DWORD color1 = 0xFFFFFFFF;
        color1=rgbaInt_a_rgbaHex(vec1);
        printf("0x%X\n", color1);
        system("pause");

        //=================================

        int vecca[4] = {51,51,204,180};
        int* vec2 = (int*)&vecca[0];
        DWORD color2 = 0x336699FF;
        vec2 = rgbaHex_a_rgbaInt(color2);
        printf("%d %d %d %d\n", vec2[0], vec2[1], vec2[2], vec2[3]);
        system("pause");

        //=================================

        DWORD color3 = 0xFFFFFF33;
        color3 = ComplementoAlpha(color3,85);
        printf("0x%X\n", color3);
        system("pause");
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void display(void)
{
        glClear(GL_COLOR_BUFFER_BIT);
        glTranslatef(transx, transy, 0.f);
        glRotatef(rotx, 0., 1., 0.);
        glRotatef(roty, 1., 0., 0.);
        glScalef(scale, scale, 0.);
        glLineWidth(5.5);
        ////////////////////////////////////////////////////
        ////////////////////////////////////////////////////
        float r = float(TablaRGBA[CURCOLOR][0])/255.0;
        float g = float(TablaRGBA[CURCOLOR][1])/255.0;
        float b = float(TablaRGBA[CURCOLOR][2])/255.0;
        float a = float(TablaRGBA[CURCOLOR][3])/255.0;
        glClearColor(r,g,b,a);
        glColor3f(TablaRGBA[CURCOLOR][0],TablaRGBA[CURCOLOR][1],TablaRGBA[CURCOLOR][2]);
        glBegin(GL_LINES);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(15, 0, 0);
        glEnd();
        glutSwapBuffers();
        //glFlush();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void reshape(int w, int h)
{
        glViewport(-50, -50, w+120, h+120);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void key(unsigned char key, int x, int y)
{
        if (key=='\033') exit(0);

        bfunc();
        glutPostRedisplay();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void special(int key, int x, int y)
{
  switch (key) {
  case GLUT_KEY_UP:
          {
                if(CURCOLOR==14) CURCOLOR=0;
                else CURCOLOR++;
          }
    break;
  case GLUT_KEY_DOWN:
          {
                if(CURCOLOR==0) CURCOLOR=14;
                else CURCOLOR--;
          }
    break;
  default:
    return;
  }
  glutPostRedisplay();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void tick(void)
{
        glutPostRedisplay();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int main(int argc, char** argv){

        SetConsoleTitle("glTest2");
        TestPluginRGBAfuncs();

        ////////////////////////////////////////////

        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
        glutInitWindowSize(450, 450);
        glutCreateWindow("SHOW COLORES");
//        glutFullScreen();
        init(argv[1]);
        glutDisplayFunc(display);
        glutKeyboardFunc(key);
        glutSpecialFunc(special);
        glutReshapeFunc(reshape);
        glutIdleFunc(tick);
//        SetActiveWindow(FindWindow(0,"SHOW COLORES"));
        SetForegroundWindow(FindWindow(0,"SHOW COLORES"));//Para que quede en primer plano ;D
        glutMainLoop();
        system("pause");
        return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

These are the 4 functions. Note that the 4th function is actually not making a conversion, instead it sets the transparency atribute to a color.

rgba_funcs.cpp

Code:

//
// By 85
// Credits: OGC, opengl.org
// boyscout_etk@hotmail.com
// etalking.com.ar
// 2013
//

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#pragma warning(disable: 4244)
typedef unsigned long DWORD;
int idef[4]={255,255,255,255};
float fdef[4]={255.0,255.0,255.0,255.0};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Para Opengl32 (Valores de 0.0 a 1.0)
void rgbaInt_a_rgbaFloat(int iVecRGBA[4]=reinterpret_cast<int*>(idef),
                                                float fVecRGBA[4]=reinterpret_cast<float*>(fdef)){

        float red = float(iVecRGBA[0])/255.0;
        float green = float(iVecRGBA[1])/255.0;
        float blue = float(iVecRGBA[2])/255.0;
        float alpha = float(iVecRGBA[3])/255.0;
        fVecRGBA[0]=red;
        fVecRGBA[1]=green;
        fVecRGBA[2]=blue;
        fVecRGBA[3]=alpha;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

DWORD rgbaInt_a_rgbaHex(int vecRGBA[4]=reinterpret_cast<int*>(idef)){

        int red=vecRGBA[0];
        int green=vecRGBA[1];
        int blue=vecRGBA[2];
        int alpha=vecRGBA[3];
        return ((red<<24) + (green<<16) + (blue<<8) + alpha);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int* rgbaHex_a_rgbaInt(DWORD color= 0xFFFFFFFF){

        int red  = (color>>24);
        int green = (color>>16)&0xFF;
        int blue  = (color>>8)&0xFF;
        int alpha = (color)&0xFF;
        static int vecRGBA[4];
        vecRGBA[0]=red;
        vecRGBA[1]=green;
        vecRGBA[2]=blue;
        vecRGBA[3]=alpha;
        return vecRGBA;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

DWORD ComplementoAlpha(DWORD color= 0xFFFFFFFF, int alpha=255){

        color &= 0xFFFFFF00;//Quita                                       
        color += alpha;//Pone
        return color;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The program opens 2 windows, the console window will only show you the result of some tests on the 4 functions before mentioned. ignore it if you want.

The other window is the Opengl32 application window, which will show you a background color , a background color which you can change by pressing arrow keys UP and DOWN.

http://imgs.g4estatic.com/rgba-color...l/gltest21.png

See attachment for full project.


All times are GMT +5.5. The time now is 06:06.