OpenGL: Where shoud I place shaders?
Posted
by
mivic
on Game Development
See other posts from Game Development
or by mivic
Published on 2012-12-11T01:16:25Z
Indexed on
2012/12/11
5:20 UTC
Read the original article
Hit count: 601
I'm trying to learn OpenGL ES 2.0 and I'm wondering what is the most common practice to "manage" shaders.
I'm asking this question because in the examples I've found (like the one included in the API Demo provided with the android sdk), I usually see everything inside the GLRenderer class and I'd rather separate things so I can have, for example, a GLImage object that I can reuse whenever I want to draw a textured quad (I'm focusing on 2D only at the moment), just like I had in my OpenGL ES 1.0 code.
In almost every example I've found, shaders are just defined as class attributes. For example:
public class Square {
public final String vertexShader =
"uniform mat4 uMVPMatrix;\n" +
"attribute vec4 aPosition;\n" +
"attribute vec4 aColor;\n" +
"varying vec4 vColor;\n" +
"void main() {\n" +
" gl_Position = uMVPMatrix * aPosition;\n" +
" vColor = aColor;\n" +
"}\n";
public final String fragmentShader =
"precision mediump float;\n" +
"varying vec4 vColor;\n" +
"void main() {\n" +
" gl_FragColor = vColor;\n" +
"}\n";
// ...
}
I apologize in advance if some of these questions are dumb, but I've never worked with shaders before.
1) Is the above code the common way to define shaders (public final class properties)?
2) Should I have a separate Shader class?
3) If shaders are defined outside the class that uses them, how would I know the names of their attributes (e.g. "aColor" in the following piece of code) so I can bind them?
colorHandle = GLES20.glGetAttribLocation(program, "aColor");
© Game Development or respective owner