oliver_mpt
Posts: 10
Joined: Tue Dec 06, 2016 2:41 pm

setting colors in shader,

Mon Nov 20, 2017 5:49 pm

Hello everybody,

I am working on a code where I need to set the color of my objects, in all vertices and I also draw text, using textures, where the text colors are indexed colors which are loaded in a table, using uniform vec4 array, in the shader.
The colors don't show up correctly, some are OK, many not.
Here is the code of my shaders :

Code: Select all

char vShaderStr[] =
    "attribute vec3 vPosition;\n"
    "attribute vec2 a_st;\n"
    "attribute vec4 a_color;\n"
    "attribute float a_colorindex;\n"
    "uniform vec4 u_colorset[32];\n"
    "uniform mat4 u_mvp;\n"
    "varying vec2 v_frag_uv;\n"
    "varying vec4 v_textcolor;\n"
    "varying vec4 v_color;\n"
    "void main()\n"
    "{\n"
    "   v_frag_uv = a_st;\n"
    "   gl_Position = u_mvp * vec4(vPosition,1); \n"
    "   v_color = a_color;\n"
    "   int idx = int(a_colorindex);\n"
    "   v_textcolor = u_colorset[idx];\n"
    "}\n";



char fShaderStr[] =
    "uniform sampler2D texture_uniform;\n"
    "uniform float textureFlag;\n"
    "varying vec4 v_color;\n"
    "varying vec4 v_textcolor;\n"
    "varying vec2 v_frag_uv;\n"
    "void main()\n"
    "{\n"
    "   vec4 c1 = vec4(v_textcolor.xyz, v_textcolor * texture2D(texture_uniform, v_frag_uv).a);\n"
    "   vec4 c2 = v_color;\n"
    "   vec4 c;\n"
    "   if (textureFlag > 0.5) \n"
    "      c = c1;\n"
    "   else \n"
    "      c = c2;\n"
    "   gl_FragColor = c;\n"
    "}\n";

There is a flag called "textureFlag" to switch between geometry and text color rendering. uniform vec4 u_colorset[32] is my color table, containing the (r,g,b,a) values and "a_colorindex" is the color index to look up in the color table, this is a vertex attribute for the text, along with position and uv coordinates (attribute a_st).

All attributes are located using :

Code: Select all

   m_vertexHandle = glGetAttribLocation ( state->programObject, "vPosition" );
   m_colorHandle = glGetAttribLocation ( state->programObject, "a_color" );
   m_colorIndexHandle = glGetAttribLocation ( state->programObject, "a_colorindex" );
   m_colorTableHandle = glGetUniformLocation(state->programObject,"u_colorset");
   m_texHandle = glGetAttribLocation ( state->programObject, "a_st" );
   m_texflag = glGetUniformLocation(state->programObject,"textureFlag");
   m_samplerHandle = glGetUniformLocation( state->programObject, "texture_uniform" );
and the color table is initialized using a 1D array of floats (float colors[128] ) where all the 32 colors are given by their rgba values as :
colors[] = (r1,g1,b1,a1, ......, r32,g32,b32,a32);

Code: Select all

glUniform4fv(m_colorTableHandle, 128, colors);
Can anyone tell me why I'm failing here ?

User avatar
Paeryn
Posts: 2055
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: setting colors in shader,

Mon Nov 20, 2017 8:13 pm

oliver_mpt wrote:
Mon Nov 20, 2017 5:49 pm

Code: Select all

    "   vec4 c1 = vec4(v_textcolor.xyz, v_textcolor * texture2D(texture_uniform, v_frag_uv).a);\n"
This line looks suspect, you've said the fourth component is a vec4 of v_textcolor times the alpha of the texture lookup, so the alpha of c1 will be the red component of that. Did you mean?

Code: Select all

    "   vec4 c1 = vec4(v_textcolor.xyz, v_textcolor.a * texture2D(texture_uniform, v_frag_uv).a);\n"
She who travels light — forgot something.

oliver_mpt
Posts: 10
Joined: Tue Dec 06, 2016 2:41 pm

Re: setting colors in shader,

Tue Nov 21, 2017 6:39 am

Hi Paeryn !

Thanks so much ! you've made my day !
I went over the shader a hundred of times and couldn't see it. Of course you're absolutely right : at some point I have edited the line and dropped the ".a".

oliver

User avatar
Paeryn
Posts: 2055
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: setting colors in shader,

Tue Nov 21, 2017 11:30 am

It's always harder to see when you know what you meant to or originally had put there, the brain is very good at filling in missing details automatically.

Strange that the shader compiler didn't complain, I thought it was supposed to be an error to have unused components in a constructor. Maybe that only applies to whole parameters and not partials?
She who travels light — forgot something.

Return to “OpenGLES”

Who is online

Users browsing this forum: No registered users and 1 guest