drawing in OpenGL ES 2.0 on Android
This is the drawing part. Beware of texture uniform. It looks a bit strange but otherwise causes some error. With "glUniform1i(loc_myTex, texID);" instead of "glUniform1i(loc_myTex, 0);", no texture is seen.
void MyObject::draw() { // glBindFramebuffer(GL_FRAMEBUFFER, pFrameBufferId[0]); glBindTexture(GL_TEXTURE_2D, texID); // texID GLuint glUniform1i(loc_myTex, 0); glUniform1f(loc_myTime, myTimeRadian); glUniformMatrix4fv(loc_vmx, 1, GL_FALSE, mtrx.getM()); glUniformMatrix3fv(loc_tmx, 1, GL_FALSE, MyObject::tmx); glBindBuffer(GL_ARRAY_BUFFER, idbVtx); glVertexAttribPointer(loc_vtxc, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, idbTxt); glVertexAttribPointer(loc_txtc, 2, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, idbIdx); glDrawElements(GL_TRIANGLE_STRIP, n_elements, GL_UNSIGNED_SHORT, 0); }
The fragment shader part is shown below.
const GLchar* myFS0 = "uniform float t;" "uniform sampler2D myTex;" "varying vec2 tx2;" "void main(){" "vec4 c4 = texture2D(myTex, vec2(tx2.x, tx2.y));" "gl_FragColor = vec4(c4.xyz, 0.5);" "}";
And the initialization (i.e. GetUniformLocation()) part is here.
void MyObject::init(GLuint sp, GLuint tex, GLuint vtx, GLuint txt, GLuint idx) { texID = tex; loc_vtxc = glGetAttribLocation(sp, "vtxc"); loc_txtc = glGetAttribLocation(sp, "txtc"); ... loc_myTex = glGetUniformLocation(sp, "myTex"); mtrx.init(); ... }