• Advertisement
Sign in to follow this  

OpenGL ES Texturing

This topic is 1393 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I noticed some strange behavior when trying to texture in OpenGL-ES for Android. I've followed some tutorials and applied the implementations to my code. Where things differ is where I encapsulate the drawing code in an entity class, and point the code to my existing (but altered to fit) shader. This is the texture that I'm trying to draw:




Just needed something with vertical and horizontal lines. Here's how it is drawn:



It looks to be the texture coordinates, But nothing seems to happen when I switch to trianglefan either.

Below is the full Entity class:

Additionally, I call the constructors in the render class as such:

    public void draw(float[] mMVPMatrix) {
        // Add program to OpenGL environment

        // get handle to vertex shader's vPosition member
        mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

        // Enable a handle to the triangle vertices

        // Prepare the triangle coordinate data
                mPositionHandle, COORDS_PER_VERTEX,
                GLES20.GL_FLOAT, false,
                vertexStride, vertexBuffer);

        // get handle to fragment shader's vColor member
        mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

        // Set color for drawing the triangle
        GLES20.glUniform4fv(mColorHandle, 1, color, 0);
        // get handle to shape's transformation matrix
        mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
        //Texture start
        mTextureUniformHandle = GLES20.glGetUniformLocation(mProgram, "u_Texture");
        mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgram, "a_TexCoordinate");
        // Set the active texture unit to texture unit 0.
        // Bind the texture to this unit.
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle);
        // Tell the texture uniform sampler to use this texture in the shader by binding to texture unit 0.
        GLES20.glUniform1i(mTextureUniformHandle, 0);
        Matrix.translateM(mMVPMatrix, 0, Tx, Ty, Tz);
        Matrix.scaleM(mMVPMatrix, 0, Sx, Sy, Sz);
        Matrix.rotateM(mMVPMatrix, 0, Rangle, Rx, Ry, Rz);
        // Apply the projection and view transformation
        GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);

        // Draw the square
                GLES20.GL_TRIANGLES, drawOrder.length,
                GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

        // Disable vertex array

//Render Class
private Entity mSquare;

Class constructor...


mSquare = new Entity (mMVPMatrix, mActivityContext);

And for drawing:

Edited by InfinityMachine

Share this post

Link to post
Share on other sites

It was the glEnableVertexAttribArray for texture coords.  I told him in chat.

Edited by LoneDwarf

Share this post

Link to post
Share on other sites

As LoneDrawf has mentioned … setting up the second vertex attribute is missing. Something like

        GLES20.glVertexAttribPointer(mTextureCoordinateHandle, ...);


BTW: Performance-wise it isn't good to resolve attribute and uniform locations on every draw call.

Share this post

Link to post
Share on other sites

Very true.  I actually keep a bitmask around to represent what is set but if you're starting out I would set it redundantly until you're rock solid.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement