setting colors in shader,
Posted: 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 :
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 :
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);
Can anyone tell me why I'm failing here ?
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";
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" );
colors[] = (r1,g1,b1,a1, ......, r32,g32,b32,a32);
Code: Select all
glUniform4fv(m_colorTableHandle, 128, colors);