Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9006205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:20:56+00:00 2026-06-16T01:20:56+00:00

I started from the google’s android tutorial on open gl and then used this

  • 0

I started from the google’s android tutorial on open gl and then used this tutorial:
to add textures. Since the class structures differ a bit, I had to do some code-moving-renaming. What i ended up is:

enter image description here

Here is the texture file i used.
As one can see, the texture is somehow stretched along (1, 1), although the underlying object is a square.
Here is my quad’s code, any help is appreciated.

public class GLSquare implements IGLObject {

private final String vertexShaderCode =
// This matrix member variable provides a hook to manipulate
// the coordinates of the objects that use this vertex shader
"uniform mat4 uMVPMatrix;" + "attribute vec2 a_TexCoordinate;"
        + "varying vec2 v_TexCoordinate;" + "attribute vec4 vPosition;"
        + "void main() {"
        +
        // the matrix must be included as a modifier of gl_Position
        "  gl_Position = uMVPMatrix * vPosition;"
        + "v_TexCoordinate = a_TexCoordinate;" + "}";

private final String fragmentShaderCode = "precision mediump float;"
        + "uniform sampler2D u_Texture;" + "varying vec2 v_TexCoordinate;"
        + "void main() {"
        + "  gl_FragColor = texture2D(u_Texture, v_TexCoordinate);" + "}";

private final FloatBuffer vertexBuffer;
private final ShortBuffer drawListBuffer;
private final int mProgramHandle;
private int mPositionHandle;
private int mMVPMatrixHandle;
private int mTextureDataHandle;
/** The texture pointer */

// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;
static float squareCoords[] = { -10f, 10f, 0.0f, // top left
        -10f, -10f, 0.0f, // bottom left
        10f, -10f, 0.0f, // bottom right
        10f, 10f, 0.0f }; // top right

private FloatBuffer textureBuffer; // buffer holding the texture coordinates
private float texture[] = {
        // Mapping coordinates for the vertices
        0.0f, 1.0f, // top left (V2)
        0.0f, 0.0f, // bottom left (V1)
        1.0f, 1.0f, // top right (V4)
        1.0f, 0.0f // bottom right (V3)
};

static float[] mTranslate = new float[16];
static float[] translatedMVP = new float[16];

private final short drawOrder[] = { 0, 1, 2, 0, 2, 3 }; 

public GLSquare(float x, float y, float z, Context context) {
    // initialize vertex byte buffer for shape coordinates
    ByteBuffer bb = ByteBuffer.allocateDirect(squareCoords.length * 4);
    bb.order(ByteOrder.nativeOrder());
    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(squareCoords);
    vertexBuffer.position(0);

    bb = ByteBuffer.allocateDirect(texture.length * 4);
    bb.order(ByteOrder.nativeOrder());
    textureBuffer = bb.asFloatBuffer();
    textureBuffer.put(texture);
    textureBuffer.position(0);

    // initialize byte buffer for the draw list
    ByteBuffer dlb = ByteBuffer.allocateDirect(
    // (# of coordinate values * 2 bytes per short)
            drawOrder.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);

    Matrix.setIdentityM(mTranslate, 0);
    Matrix.translateM(mTranslate, 0, x, y, z);
    // prepare shaders and OpenGL program
    final int vertexShaderHandle = GLTools.compileShader(
            GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    final int fragmentShaderHandle = GLTools.compileShader(
            GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

    mProgramHandle = GLTools.createAndLinkProgram(vertexShaderHandle,
            fragmentShaderHandle, new String[] { "a_Position", "a_Color",
                    "a_TexCoordinate" });
    // Load the texture
    mTextureDataHandle = GLTools.loadGLTexture(context, R.raw.stars1024);
}

public void draw(float[] vpMatrix) {
    // Add program to OpenGL environment
    GLES20.glUseProgram(mProgramHandle);

    // Pass in the position information
    vertexBuffer.position(0);
    GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT,
            false, 0, vertexBuffer);
    GLES20.glEnableVertexAttribArray(mPositionHandle);

    int mTextureCoordinateHandle = GLES20.glGetAttribLocation(
            mProgramHandle, "a_TexCoordinate");
    // Pass in the texture coordinate information
    textureBuffer.position(0);
    GLES20.glVertexAttribPointer(mTextureCoordinateHandle, 2,
            GLES20.GL_FLOAT, false, 0, textureBuffer);
    GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);

    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle,
            "uMVPMatrix");
    WideOpenRenderer.checkGlError("glGetUniformLocation");

    // Apply the projection and view transformation
    Matrix.multiplyMM(translatedMVP, 0, vpMatrix, 0, mTranslate, 0);
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, translatedMVP, 0);

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

}

}

here is the methode loading texture:

public static int loadGLTexture(Context context, final int resourceId) {
    Log.d("GLTools", "Loading texture...");
      final int[] textureHandle = new int[1];

        GLES20.glGenTextures(1, textureHandle, 0);

        if (textureHandle[0] != 0)
        {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inScaled = false;   // No pre-scaling

            // Read in the resource
            final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);

            // Bind to the texture in OpenGL
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);
            Log.d("GLTools", "Binding texture, setting parameter" + resourceId);
            // Set filtering
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

            // Load the bitmap into the bound texture.
            GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

            // Recycle the bitmap, since its data has been loaded into OpenGL.
            bitmap.recycle();
        }

        if (textureHandle[0] == 0)
        {
            throw new RuntimeException("Error loading texture.");
        }

        return textureHandle[0];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:20:57+00:00Added an answer on June 16, 2026 at 1:20 am

    Ok, i am now able to answer my own question.
    The texture was just scaled relative to its origin = left bottom point (u, v) = (0, 0), which has irritated me, since I am new to opengl, I was expecting that it would be scaled relative to at least world origin, or object’s centroid.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay I started to implement this horrible code from Google's android. The OnSharedPreferenceChangeListener is
I have a timer class..This timer will be started from various portions of my
I'm trying to finish the getting started steps on Google's GCM from Google's Android
I got the Hello, Android program to work after reading the tutorials from Google's
Hi I started programming a ToDo-List on android (in eclipse) which synchronises with Google
I have created an application which needs sign in from Facebook/Twitter/Google to get started.
I'm just getting started with Android Dev. Which Android phones should I add as
Our application uses the MapMaker class from Google collections, and we're getting the exception
I want android in app purchase without any transactions from google checkouts accounts,.I want
I am using the mxmlc to compile the examples from google to get started

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.