0
\$\begingroup\$

I am learning open GL ES 2.0 and I just made 1 easy game for 1.0

I am following tutorials but I cant understand this thing

In OpenGL ES 1.0 if I define a square that way:

private float[] vertices = { // Vertices for a face 0.0f, 0.0f, 0.2f, // 0. left-bottom-front 1.0f, 0.0f, 0.2f, // 1. right-bottom-front 0.0f, 1.0f, 0.2f, // 2. left-top-front 1.0f, 1.0f, 0.2f // 3. right-top-front }; 

and draw it that way it Works

public void draw(GL10 gl) { gl.glFrontFace(GL10.GL_CCW); // Front face in counter-clockwise // orientation gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face gl.glCullFace(GL10.GL_BACK); // Cull the back face (don't display) gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // Enable // texture-coords-array // (NEW) gl.glTexCoordPointer(3, GL10.GL_FLOAT, 0, texBuffer); // Define // texture-coords // buffer (NEW) gl.glEnable(GL10.GL_BLEND); gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); // front gl.glBindTexture(GL10.GL_TEXTURE_2D,TextureLoader.TeclaBlancatextureIDs[0]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // Disable // texture-coords-array // (NEW) gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); gl.glDisable(GL10.GL_CULL_FACE); gl.glDisable(GL10.GL_BLEND); } 

but in OpenGL ES 2.0 if I do this that way( with the same vertex) it only write a triangle:

private void drawSuelo(Suelo suelo) { // Pass in the position information suelo.mPositions.position(0); GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 0, suelo.mPositions); GLES20.glEnableVertexAttribArray(mPositionHandle); // Pass in the color information suelo.mColors.position(0); GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false, 0, suelo.mColors); GLES20.glEnableVertexAttribArray(mColorHandle); // Pass in the normal information cubo.mCubeNormals.position(0); GLES20.glVertexAttribPointer(mNormalHandle, mNormalDataSize, GLES20.GL_FLOAT, false, 0, cubo.mCubeNormals); GLES20.glEnableVertexAttribArray(mNormalHandle); // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix // (which currently contains model * view). Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); // Pass in the modelview matrix. GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mMVPMatrix, 0); // This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix // (which now contains model * view * projection). Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); // Pass in the combined matrix. GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); // Pass in the light position in eye space. GLES20.glUniform3f(mLightPosHandle, mLightPosInEyeSpace[0], mLightPosInEyeSpace[1], mLightPosInEyeSpace[2]); // Draw the cube. GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6); } 

It seems to need all of this vertex in order to draw a square

final float[] PositionData = { // Top face -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f }; 

Why is happening this? Why I am missing? Is there any way in Open GL ES 2.0 to write a square with 4 vertex?

\$\endgroup\$
1
  • \$\begingroup\$ where is the glsl shader ? as far as i know gles2 does not have fixed function pipeline so you need shader to draw something \$\endgroup\$ Commented Mar 20, 2015 at 12:16

1 Answer 1

0
\$\begingroup\$

The correct answer was answered by Reto Koradi

You're using different primitive types. In the working ES 1.0 code, you have this:

gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); and in the ES 2.0 code that renders only a triangle when you give it 4 vertices:

GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6); So in the ES 1.0 code, you're specifying that you want to draw a triangle strip (primitive type GL_TRIANGLE_STRIP) with 4 vertices. This corresponds to 2 triangles. While in the ES 2.0 code, you specify that you want to draw 2 separate triangles (primitive type GL_TRIANGLES), which indeed needs 6 vertices.

If you want to use the original 4 vertices for the ES 2.0 code, you simply have to use the equivalent draw call with the same primitive type you used in the ES 1.0 version:

GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.