Reputation: 2534
Situation: Android application supports two versions of OpenGL ES: 2.0 and 3.0. Differences are only in shaders. I do not want to make separate sets of classes for versions 2 and 3, but want merge them. That is, instead of two similar classes:
import android.opengl.GLES20;
class ObjectGLES20 {
public ObjectGLES20() {
...
// invoke static methods from GLES20
int positionLink = GLES20.glGetAttribLocation(programObject, "a_position");
int lightPositionLink = GLES20.glGetUniformLocation(programObject, "u_lightPosition");
...
}
}
import android.opengl.GLES30;
class ObjectGLES30 {
public ObjectGLES30() {
...
// invoke static methods from GLES30
int positionLink = GLES30.glGetAttribLocation(programObject, "a_position");
int lightPositionLink = GLES30.glGetUniformLocation(programObject, "u_lightPosition");
...
}
}
Do something like this:
// define supported version OpenGL ES
if (getVersionGLES() >= 3.0 && Build.VERSION.SDK_INT >= 18) {
objectAll = new ObjectAll(new GLES30(), true) // use polymorphism
} else objectAll = new ObjectAll(new GLES20(), false)
...
/** @param gles - instance of GLES20 or GLES30 */
class ObjectAll {
public ObjectAll(GLES20 gles, boolean isSupportGLES30) {
String pathToVertexShader;
String pathToFragmentShader;
if (isSupportGLES30) {
pathToVertexShader = "shaders/gles30/vertex_shader.glsl";
pathToFragmentShader = "shaders/gles30/fragment_shader.glsl";
} else {
pathToVertexShader = "shaders/gles20/vertex_shader.glsl";
pathToFragmentShader = "shaders/gles20/fragment_shader.glsl";
}
int programObject = linkProgram(pathToVertexShader, pathToFragmentShader);
...
// invoke static methods from GLES20 or GLES30 depending on supported version
// TIP!: static member accessed via instance reference
int positionLink = gles.glGetAttribLocation(programObject, "a_position");
int lightPositionLink = gles.glGetUniformLocation(programObject,
"u_lightPosition");
...
}
}
But this approach (create instances GLES) seems not good for calling static methods. Maybe there are other options, such as reflection or others? Thank you all.
Upvotes: 0
Views: 254
Reputation: 12229
But this approach seems not good for calling static methods GLES.
The Android GLES30
object is a subclass of GLES20
, and the underlying native API doesn't make such a distinction and the same native function will be used for both.
I strongly suspect it really doesn't matter and you can safely call GLES20.glFoo()
for the functions and constants that are only in GLES20
and it will work fine for both GLES20
and GLES30
content.
Upvotes: 1