PDA

View Full Version : changing only the alpha-channel of a set of colors



guihazan
10-05-2012, 10:23 AM
Hi,

I have a shader that is used to draw a font. I know that people use texture to draw points, but here the point drawing shader is so fast that it does not make a difference to change it to texture. However, the way i implemented takes a lot of memory to hold the pixel values.

Currently i use an array of coordinates to draw the text and an array of ARGB to set the color. But the color is always the same, i just change the A-channel of the color, replicating the RGB in all pixels. The shader is below:



#define POINTS_VERTEX_CODE \
"attribute vec4 a_Position; attribute vec4 a_Color; varying vec4 v_Color; " \
"uniform mat4 projectionMatrix; " \
"void main() {v_Color = a_Color; gl_Position = a_Position * projectionMatrix;}"

#define POINTS_FRAGMENT_CODE \
"precision mediump float;" \
"varying vec4 v_Color;" \
"void main() {gl_FragColor = v_Color;}"


What i have to change in the code above to let i upload just the RGB and pass an array of A-channels? This would save a lot of memory, because instead of passing 4 bytes for each pixel, i would pass just 1 byte for the pixel.

thanks in advance,

guich

mhagain
10-05-2012, 11:45 AM
The following vertex shader should do it:
#define POINTS_VERTEX_CODE \
"attribute vec4 a_Position; attribute float a_Alpha; varying vec4 v_Color; " \
"uniform mat4 projectionMatrix; uniform vec3 a_RGB;" \
"void main() {v_Color = vec4 (a_RGB, a_Alpha); gl_Position = a_Position * projectionMatrix;}"

You'll need to change your glVertexAttribPointer call of course (the alpha attribute becomes glVertexAttribPointer (index, 1, GL_UNSIGNED_BYTE, GL_TRUE, stride, pointer)), as well as add a glUniform3fv for setting a_RGB. Not too sure how well your hardware and/or driver is going to like getting a 1-byte normalized attrib array though; the spec says it's possible, even D3D10 and 11 support it (normally a good indication that it is actually supported in hardware with GL too) but it may punt you back to a software emulated path on older hardware (in which case you've certainly saved a lot of memory but are also running a lot slower as a result - a common enough risk).

guihazan
10-05-2012, 12:32 PM
Hi mhagain,

I thought about another solution, but i don't know how to implement it.

vec4 color = (r,g,b,1);
vec4 alpha = (1,1,1,a);

v_Color = color * alpha;

Is it possible?

thanks

guich