Jump to content

  • Log In with Google      Sign In   
  • Create Account

OpenGL ES Texturing


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 InfinityMachine   Members   -  Reputation: 193

Like
0Likes
Like

Posted 26 March 2014 - 05:56 PM

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, 28 March 2014 - 12:26 AM.

Find yourself. If you can't, keep looking.


Sponsor:

#2 Stainless   Members   -  Reputation: 1069

Like
0Likes
Like

Posted 28 March 2014 - 03:12 AM

Can you post the shader please?



#3 LoneDwarf   Members   -  Reputation: 280

Like
1Likes
Like

Posted 29 March 2014 - 08:24 PM

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


Edited by LoneDwarf, 29 March 2014 - 08:24 PM.


#4 haegarr   Crossbones+   -  Reputation: 4586

Like
2Likes
Like

Posted 30 March 2014 - 03:54 AM

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.



#5 LoneDwarf   Members   -  Reputation: 280

Like
0Likes
Like

Posted 31 March 2014 - 08:49 AM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS