• 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:

 

deadtreenoisegrgr.png

 

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

Deadtreet.png

 

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
        GLES20.glUseProgram(mProgram);

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

        // Enable a handle to the triangle vertices
        GLES20.glEnableVertexAttribArray(mPositionHandle);

        // Prepare the triangle coordinate data
        GLES20.glVertexAttribPointer(
                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");
        GLRenderClass.checkGlError("glGetUniformLocation");
        
        //Texture start
        mTextureUniformHandle = GLES20.glGetUniformLocation(mProgram, "u_Texture");
        mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgram, "a_TexCoordinate");
        // Set the active texture unit to texture unit 0.
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        // 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);
        GLRenderClass.checkGlError("glUniformMatrix4fv");

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

        // Disable vertex array
        GLES20.glDisableVertexAttribArray(mPositionHandle);
    }

}
//Render Class
private Entity mSquare;
...

Class constructor...

...

mSquare = new Entity (mMVPMatrix, mActivityContext);

And for drawing:

mPlayer.draw(mMVPMatrix);
Edited by InfinityMachine

Share this post


Link to post
Share on other sites
Advertisement

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.glEnableVertexAttribArray(mTextureCoordinateHandle);
        GLES20.glVertexAttribPointer(mTextureCoordinateHandle, ...);
        ...
        GLES20.glDisableVertexAttribArray(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