Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use ARGB color in opengl/SDL?

I am rendering SVG using Cairo. The Cairo output format is ARGB. Then I put rendered image in a SDL_Surface so I can use it as a openGL texture.

The rendered image was looking just fine when I use directly the SDL_Surface. But I had to use the surface as a texture in openGL because I needed some openGL function. The problem is, that all the color are flipped. OpenGL use RGBA and not ARGB.

I was wondering if anybody could help me converting a SDL_Surface ARGB to RGBA.

Usefull information:
I used this tutorial to render my SVG. http://tuxpaint.org/presentations/sdl_svg_svgopen2009_kendrick.pdf
My software is written in C.

EDIT: I used this tutorial to use a SDL_Surface as a openGL texture.
http://www.sdltutorials.com/sdl-tip-sdl-surface-to-opengl-texture

Both the rendering process and the opengl texture are the same as the tutorials.

like image 924
user1586263 Avatar asked Sep 04 '13 22:09

user1586263


1 Answers

Judging by your Tux example code, you can skip SDL completely and feed OpenGL the pixel data manually using the following code:

GLuint tex;

glGenTextures (1, &tex);
glBindTexture (GL_TEXTURE_2D, tex);

glTexImage2D  (GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, image);

The important details here are the GL_BGRA format for the pixel data and the GL_UNSIGNED_INT_8_8_8_8_REV data type (this reverses the order of the channels during pixel transfer operations). OpenGL will take care of converting the pixel data into the appropriate texel format for you. OpenGL ES, on the other hand, will not do this; to make this portable you may want to convert the pixel data to RGBA or BGRA yourself...

like image 96
Andon M. Coleman Avatar answered Oct 18 '22 17:10

Andon M. Coleman