Allegro GL color mismatch
anto80

Hello all,

I am experiencing problem with colors in Allegro GL.
The red appears blue and vice versa.
Likewise the yellow appears cyan.
It acts like the RGB was actually BGR. :-[
Something must be wrong in my Allegro GL color initialization or when loading the texture.

Here is a sample code that illustrate the problem.

Any idea please??

1#include <stdio.h>
2#include <allegro.h>
3#include <alleggl.h>
4#include <winalleg.h>
5#include <windows.h>
6 
7GLuint tex_desk;
8FONT* allegro_fnt;
9 
10int main(void)
11{
12 allegro_init();
13 install_allegro_gl();
14 
15 install_timer();
16 install_keyboard();
17 install_mouse();
18 set_color_depth(24);
19 set_trans_blender(255, 255, 255, 128);
20 
21 /*--- Alleg GL inits --------------------------------------------------*/
22 allegro_gl_clear_settings();
23 allegro_gl_set(AGL_COLOR_DEPTH, 32);
24 allegro_gl_set(AGL_Z_DEPTH, 8);
25 allegro_gl_set(AGL_WINDOWED, true);
26 allegro_gl_set(AGL_DOUBLEBUFFER, 1);
27 allegro_gl_set(AGL_RENDERMETHOD, 1);
28 allegro_gl_set(AGL_SUGGEST, AGL_Z_DEPTH | AGL_COLOR_DEPTH);
29 allegro_gl_set(AGL_REQUIRE, AGL_DOUBLEBUFFER);
30
31 if (set_gfx_mode(GFX_OPENGL, 320, 240, 0, 0))
32 { /* set the resolution to what you want,
33 disregarding 640,480 which is the coordinate system */
34 exit(1);
35 }
36 
37 glMatrixMode(GL_PROJECTION); /* I am setting a state where I am editing the projection matrix... */
38 glLoadIdentity(); /* Clearing the projection matrix... */
39 glOrtho(0, 640, 0, 480, -1, 1); /* Creating an orthoscopic view matrix going from -1 -> 1 in each dimension on the screen (x, y, z). we will always draw our 2D objects at z equal to zero. */
40 glMatrixMode(GL_MODELVIEW); /* Now editing the model-view matrix. */
41 glLoadIdentity(); /* Clearing the model-view matrix. */
42 glDisable(GL_DEPTH_TEST); /* Disabling the depth test (z will not be used to tell what object will be shown above another, only the order in which I draw them.) */
43
44 glEnable(GL_TEXTURE_2D); /* Enable texturing on all models for now on. */
45 glEnable(GL_BLEND);
46
47 glClearColor(0.0, 0.0, 0.0, 0.0); /* Set the background color to black */
48 allegro_fnt = allegro_gl_convert_allegro_font(font, AGL_FONT_TYPE_TEXTURED, 1.0); // 16.0);
49
50 text_mode(-1);
51 
52 /*--- Create image ---------------------------------------------------*/
53 BITMAP* toto = create_bitmap(8,8);
54 clear_to_color(toto, makecol(255,0,0)); // RED?
55 textprintf(toto, font, 0,0, makecol(255,255,0), "%c", 'a'); // YELLOW?
56 tex_desk = allegro_gl_make_texture_ex(
57 AGL_TEXTURE_FLIP,
58 toto,
59 GL_RGB // GL_RGB, GL_RGBA, or -1
60 );
61 destroy_bitmap(toto);
62 
63 /*--- Display image --------------------------------------------------*/
64 glClear(GL_COLOR_BUFFER_BIT);
65 
66 glLoadIdentity();
67 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
68 glBindTexture(GL_TEXTURE_2D, tex_desk);
69
70 glBegin(GL_QUADS);
71 
72 glColor4ub(255, 255, 255, 255);
73 
74 glTexCoord2f(0, 0); glVertex3f(0 , 0 , 0.0f);
75 glTexCoord2f(1, 0); glVertex3f(32, 0 , 0.0f);
76 glTexCoord2f(1, 1); glVertex3f(32, 32, 0.0f);
77 glTexCoord2f(0, 1); glVertex3f(0 , 32, 0.0f);
78 
79 glEnd();
80 
81 allegro_gl_flip(); // this displays a texture with the letter 'a' in cyan on a blue background!
82 
83 while (!keypressed());
84 
85 allegro_gl_destroy_font(allegro_fnt);
86 return 0;
87}
88END_OF_MAIN()

:'(

razor

try using GL_BGR instead of GL_RGB, that would switch the order wouldn't it?

Kitty Cat

set_color_depth(24);
and
allegro_gl_set(AGL_COLOR_DEPTH, 32);

You're confusing Allegro, there. :P

anto80

Thank you for your feedback.

Kitty Cat said:

set_color_depth(24); and allegro_gl_set(AGL_COLOR_DEPTH, 32);
You're confusing Allegro, there.

OK i have now set_color_depth(32); and then, few lines of code later... allegro_gl_set(AGL_COLOR_DEPTH, 32);
It does not change anything : my texture is still looking BGR instead of RGB.

razor said:

try using GL_BGR instead of GL_RGB, that would switch the order wouldn't it?

Wow! I didn't know I could even do that!
However if i load my texture by
tex_desk = allegro_gl_make_texture_ex(AGL_TEXTURE_FLIP, toto, GL_BGR);
The texture is now appearing full white(255,255,255) (?!)
I tried |AGL_TEXTURE_HAS_ALPHA and/or |AGL_TEXTURE_MASKED as for the flags and it does not help.

I have done another test:
1) Displaying red pixel : works fine (really red)
2) Displaying blue part of a texture : works fine (even if the original bitmap looks BGR, this is the blue part of the "badly loaded"-texture. So it looks really blue)
3) Displaying cyan text : works fine (really cyan)

=> Therefore, the problem comes from the loading of the texture.

How should i use allegro_gl_make_texture_ex ???

1 /*--- Display RED point -----------------------------------------------*/
2 glLoadIdentity();
3 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
4 glPointSize(4);
5 glBegin(GL_POINTS);
6 glColor4ub(255, 0, 0, 128); // red point, half transparent
7 glVertex3f(100, 200, 0.0);
8 glEnd();
9 
10 /*--- Display blue part of the texture --------------------------------*/
11 glLoadIdentity();
12 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
13 glBindTexture(GL_TEXTURE_2D, tex_desk);
14 glBegin(GL_QUADS);
15 glColor4ub(0, 0, 255, 255); // blue part
16 glTexCoord2f(0, 0); glVertex3f(0 , 0 , 0.0f);
17 glTexCoord2f(1, 0); glVertex3f(32, 0 , 0.0f);
18 glTexCoord2f(1, 1); glVertex3f(32, 32, 0.0f);
19 glTexCoord2f(0, 1); glVertex3f(0 , 32, 0.0f);
20 glEnd();
21 
22 /*--- display cyan text ------------------------------------------------*/
23 glLoadIdentity();
24 glTranslatef(240, 240, 0.0);
25 glScalef(4.0, 4.0, 0);
26 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
27 allegro_gl_printf(allegro_fnt, 0, 0, 0, makecol(0,255,255), "toto");

Milan Mimica

Have you installed manufacturer GFX card drivers or are you using MS ones?

anto80

hmm.

Having looked at the AllegroGL's texture.c code, and taking into account that i got an ATI Radeon, here is what i found:

01012     /* ATI Radeon 7000 inverts R and B components when generating mipmaps and
01013      * the internal format is GL_RGB8, bt only on mipmaps. Instead, we'll use
01014      * GL_RGBA8. This works for bitmaps of depth <= 24. For 32-bpp bitmaps,
01015      * some additional tricks are needed: We must fill in alpha with 255.
01016      */

So after having done the code below, all is working fine :) !!

1/*-------------------------------------------------------------------------*/
2/*---------------------------- Mygl_make_texture --------------------------*/
3/*-------------------------------------------------------------------------*/
4GLuint mygl_make_texture(int nFlags, BITMAP* bmpTex, GLint internal_format)
5{
6 BITMAP* bmpTmp;
7 bmpTmp = NULL;
8 int x,y,nCol;
9 unsigned char r,g,b,a;
10 GLuint tex_nb;
11 int nColPink;
12 
13 if (nMyGlRgbOrder != MYGL_RGBA)
14 {
15 bmpTmp = create_bitmap(bmpTex->w, bmpTex->h);
16 nColPink = makecol(255,0,255);
17 
18 for (y=0; y<bmpTex->h; y++)
19 {
20 for (x=0; x<bmpTex->w; x++)
21 {
22 nCol = getpixel(bmpTex, x, y);
23 r = getr(nCol);
24 g = getg(nCol);
25 b = getb(nCol);
26 a = (nFlags & AGL_TEXTURE_HAS_ALPHA)? geta(nCol) : 255;
27 
28 if (!(nFlags & AGL_TEXTURE_MASKED) ||
29 nCol != nColPink)
30 {
31 switch(nMyGlRgbOrder)
32 {
33 case MYGL_BGRA:
34 nCol = makeacol(b, g, r, a);
35 break;
36 }
37 }
38 
39 putpixel(bmpTmp, x, y, nCol);
40 }
41 }
42 }
43 
44 tex_nb = allegro_gl_make_texture_ex(
45 nFlags, // AGL_TEXTURE_FLIP, // |AGL_TEXTURE_MIPMAP,
46 (bmpTmp!=NULL)?bmpTmp:bmpTex,
47 internal_format // GL_RGB8 // GL_RGB, GL_RGBA, or -1
48 );
49 
50 if (bmpTmp != NULL)
51 {
52 destroy_bitmap(bmpTmp);
53 }
54 return tex_nb;
55}

Bob

What graphics card do you have? Which driver version are you using?

anto80

ATI Radeon X600. Driver v8.162.0.0 (03/08/2005)

Bob

If you hack FakeGL's detection code to always set is_ati_radeon_7000 to 1 (in glext.c, function __fill_in_info_struct), does this solve your problem? It might be a general ATI issue, but we only had a Radeon 7000 to test it with.

Thread #586159. Printed from Allegro.cc