Question

I'm following a real complete tutorial for 2D game developing on android using OpenGL, and I encountered the following

static final String VERTEX_SHADER_CODE =
        "uniform mat4 u_mvpMatrix;" +
        "attribute vec4 a_position;" +

        "void main() {" +
        "  gl_Position = u_mvpMatrix * a_position;" +
        "}";

static final String FRAGMENT_SHADER_CODE =
        "precision mediump float;" +
        "uniform vec4 u_color;" +

        "void main() {" +
        "  gl_FragColor = u_color;" +
        "}";

I'm new to OpenGL so I'd like to know what does that code means? or where can I read more about it.

I also read about GL Program, I'd like to know what is that too.

Was it helpful?

Solution

These are two pieces of code that will get executed by the graphics chip. The code is written in GLSL ES, a language based on GLSL 1.20 from desktop OpenGL.

The first piece of code is the vertex shader; it is executed for each vertex. The second is the fragment shader; it is executed for each pixel of rasterized geometry. Your examples do just the bare minimum, or maybe even less than that. For example, shader code usually performs lighting computations. These computations are missing here; the rendered output will not look like 3D objects yet. Later tutorials will add these computations.

If you start with OpenGL and GLSL on Android, it is a good idea to read an OpenGL ES2.0 book like this one. There are also books specific for GLSL, but general OpenGL books cover everything you need when you get started.

One more note: Some Android tutorials and example apps store shader code in string literals like you posted. It is actually quite a pain to write code like this (no line breaks, no syntax highlighting). You might want to read you shader programs from separate text files instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top