0
votes

I am writing a simple android program using OpenGL to display simple per-vertex colored shapes and a counter displaying the framerate.

I found out that displaying the framerate is tricky due to the lack of built-in text rendering in OpenGL ... I searched a long time to find a simple code that would do that with OpenGL ES 2.0, and I found this code on Github. For that purpose, it uses a dedicated vertex and fragment shader. The following shows the fragment shader:

uniform sampler2D u_Texture;
precision mediump float;
uniform vec4 u_Color;
varying vec2 v_TexCoordinate;
void main()
{
    gl_FragColor = texture2D(u_Texture, v_TexCoordinate).w * u_Color;
}

However, in order to render my per-vertex colored shapes, I use the following fragment shader:

precision mediump float;            
varying vec4 v_Color;             
void main()
{
    gl_FragColor = v_Color;
}

I am a little confused by this...

Can my Android application use both shaders and switch between one and the other ? If yes, how ?

Or should I use one single shader doing both tasks ? In this case, what would the shader look like ?

Also, I would appreciate any hint on how to easily implement an FPS display in OpenGL ES 2.0 (by easily, I mean a few classes dedicated to FPS display, not general text rendering).

2

2 Answers

1
votes

I am not sure about how do we do it in android but in OpenGL its possible to use different shaders for different geometry. It goes something like this.

 glUseProgram(textShaderProgram) 
 /* draw text /*
 glUseProgram(otherStuffProgram) 
 /* draw other stuff */

so basic idea is to activate the shader before drawing the geometry on which its should be applied. Hope you can find something similar for android.

1
votes

In general, it is a bad practice to use any 'universal' shaders in OpenGL (ES) at all. They are way too inefficient. They have too many instructions. They don't optimize well on GPUs. You should create as many shaders for specific needs as you need.