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 5937133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:29:46+00:00 2026-05-22T15:29:46+00:00

I am trying to map a texture onto a square where the texture stretches

  • 0

I am trying to map a texture onto a square where the texture stretches to fit the square. Currently the texture maps, but it doesn’t map to the correct location and OGL performs some skewing or such that is unbeknownst to me. The image is 64×64 and the squares I’ve tried are from -2 to 2 (hor and ver) and -1 to 1. Here is the image:

enter image description here
The texture code is:


float texture[] = { 0,0,0,1, 1,0,1,1 };
ByteBuffer byteBuf = ByteBuffer.allocateDirect(texture.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
FloatBuffer textureBuffer = byteBuf.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.flip();
int[] buffer = new int[1];
gl11.glGenBuffers(1, buffer, 0);
textureCoordPointer = buffer[0];
gl11.glBindBuffer(GL11.GL_TEXTURE_COORD_ARRAY, textureCoordPointer);
gl11.glBufferData(GL11.GL_TEXTURE_COORD_ARRAY, textureBuffer.capacity() 
   * 4, textureBuffer, GL11.GL_STATIC_DRAW);
gl11.glBindBuffer(GL11.GL_TEXTURE_COORD_ARRAY, 0);
Bitmap bitmap = BitmapFactory.decodeResource(context
   .getResources(),R.drawable.afd);
int textures[] = new int[1];
gl.glGenTextures(1, textures, 0);
texturePointer = textures[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, texturePointer);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();

and the code for the rendering is:


gl11.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertexPointerSquare);
gl11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0);       
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexPointerSquare);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texturePointer);
gl11.glBindBuffer(GL11.GL_TEXTURE_COORD_ARRAY, textureCoordPointer);
gl11.glTexCoordPointer(2, GL10.GL_FLOAT, 0, 0);

gl11.glPushMatrix();
gl11.glScalef(.4f,.4f,0);
gl11.glColor4f(1,1,1,1);
gl11.glDrawElements(GL11.GL_TRIANGLE_STRIP, indicesSquare, 
   GL11.GL_UNSIGNED_SHORT, 0);
gl11.glPopMatrix();
gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);

And the square:


GL11 gl11 = (GL11) gl;
FloatBuffer vertexSquareBuffer = ByteBuffer.allocateDirect(verticesSquare * 3 * 4)
.order(ByteOrder.nativeOrder()).asFloatBuffer();

ShortBuffer indexSquareBuffer = ByteBuffer.allocateDirect(indicesSquare * 2)
.order(ByteOrder.nativeOrder()).asShortBuffer();

float vertices[] = { 1f, 1f, 0, 1f, -1f, 0, -1f, 1f, 0, -1f, -1f, 0 };
short indices[] = { 0, 1, 2, 3 };
vertexSquareBuffer.put(vertices);
indexSquareBuffer.put(indices);
indexSquareBuffer.flip();
vertexSquareBuffer.flip();
int[] bufferSquare = new int[1];
gl11.glGenBuffers(1, bufferSquare, 0);
vertexPointerSquare = bufferSquare[0];
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertexPointerSquare);
gl11.glBufferData(GL11.GL_ARRAY_BUFFER, vertexSquareBuffer.capacity() * 4,
    vertexSquareBuffer, GL11.GL_STATIC_DRAW);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);
gl11.glGenBuffers(1, bufferSquare, 0);
indexPointerSquare = bufferSquare[0];
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexPointerSquare);
gl11.glBufferData(GL11.GL_ELEMENT_ARRAY_BUFFER, indexSquareBuffer.capacity() * 2,
    indexSquareBuffer, GL11.GL_STATIC_DRAW);

I am using an ortho perspective
gl.glOrthof(-windowRatio, windowRatio, -1,1, -4, 4)
. I have tried drawArrays(), which makes no difference, and also using many combinations of glTexParameter. The problem seems twofold: (1) the image doesn’t stretch to fit, and (2) only the left triangle of the square correctly renders the texture. Something I’ve noticed is that changing the texture coordinates has no effect on the image. How do I get this to work as intended? Thanks for looking.

  • 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-05-22T15:29:47+00:00Added an answer on May 22, 2026 at 3:29 pm

    You use GL_TEXTURE_COORD_ARRAY as buffer binding point. That is just wrong. The glTexCoordPointer sources its data from the current GL_ARRAY_BUFFER, like the glVertexPointer. So in your first part substitute

    gl11.glBindBuffer(GL11.GL_TEXTURE_COORD_ARRAY, textureCoordPointer);
    gl11.glBufferData(GL11.GL_TEXTURE_COORD_ARRAY, textureBuffer.capacity() 
       * 4, textureBuffer, GL11.GL_STATIC_DRAW);
    gl11.glBindBuffer(GL11.GL_TEXTURE_COORD_ARRAY, 0);
    

    by

    gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, textureCoordPointer);
    gl11.glBufferData(GL11.GL_ARRAY_BUFFER, textureBuffer.capacity() 
       * 4, textureBuffer, GL11.GL_STATIC_DRAW);
    gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);
    

    and in the rendering code substitute

    gl11.glBindBuffer(GL11.GL_TEXTURE_COORD_ARRAY, textureCoordPointer);
    

    by

    gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, textureCoordPointer);
    

    This works because the gl...Pointer commands take their data from the buffer that is bound to GL_ARRAY_BUFFER at the moment they are called. So just select the appropriate buffer, then call gl...Pointer and then you can bind another buffer to GL_ARRAY_BUFFER (but the index buffer needs to be bound to GL_ELEMENT_ARRAY_BUFFER when calling glDrawElements, of course. But as Tommy already mentioned in a comment, you could also just go without the trivial index buffer and with glDrawArrays).

    So, as the call to glBindBuffer(GL_TEXTURE_COORD_ARRAY, ...) fails (as GL_TEXTURE_COORD_ARRAY is no valid binding point), you have the vertex buffer still bound when setting the texcoord pointer. But since you specify your vertices to have 3 components and your texcoords 2, you actually get { 1f, 1f, 0f, 1f, -1f, 0f, -1f, 1f } as texture coordinates (the first eight values of your vertex array). So as you render your array as

    2   0
    
    3   1
    

    you get as texture coords:

    -1,0   1,1
    
    -1,1   0,1
    

    After making the proposed changes you will get

    1,0   0,0
    
    1,1   0,1
    

    So your texture is still flipped horizontally and vertically (I don’t know if that’s intended). You have to make sure that your textrue coordinates match your vertices. But after changing your above mentioned buffer error, it should at least look a bit nicer.

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

Sidebar

Related Questions

I'm stuck at not being able to map texture to a square in openGLES.
I am trying to map a different texture on each side of a cube
I'm trying to map a List with an index column. This works fine, but
I'm trying to map two objects to each other using a ManyToMany association, but
I'm currently trying to map music related data using Doctrine2's POPO Annotations. I haven't
I am trying to map information about the User to several dto's, but I'm
I am currently trying to map my Java class hierarchy into my database using
I'm trying to zoom a map into the user's current location once the view
I'm trying to map a ManyToMany relationships between 2 tables, both having composite primary
I'm trying to map a struct definition using ctypes: struct attrl { struct attrl

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.