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

  • SEARCH
  • Home
  • 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 6709101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:50:25+00:00 2026-05-26T07:50:25+00:00

im trying to make a cube with a different texture on each face. I

  • 0

im trying to make a cube with a different texture on each face.

I have the front face and the rear face working now. Now i am trying to make the right face of the cube. But something is going wrong, because i got the right face done but the texture is showing with errors (it’s like stretched and shredded), i have something bad in my code and i dont know what.

This is my code

public class Cube {

private FloatBuffer vertexBuffer;//Vertices
private FloatBuffer textureBuffer;//Texture coordinates
private ByteBuffer indexBuffer;//Indices
private int[] textures = new int[6];//Texture pointer

private float vertices[] = { //8 vertices of the cube
        -1.0f, -1.0f, 1.0f, // 0
        1.0f, -1.0f, 1.0f,  // 1
        -1.0f, 1.0f, 1.0f,  // 2
        1.0f, 1.0f, 1.0f,   // 3

        -1.0f, -1.0f, -1.0f,// 4
        1.0f, -1.0f, -1.0f, // 5
        -1.0f, 1.0f, -1.0f, // 6
        1.0f, 1.0f, -1.0f,  // 7
};

private byte indices[] = { //Faces definition
        0,1,2, 1,3,2, //front face (*)
        6,7,5, 6,5,4, //rear face (**)
        1,5,3, 5,7,3, //right face (***) //problems here
};

private float texture[] = {//Mapping coordinates for the vertices
        0.0f, 1.0f,
        1.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 0.0f,

        0.0f, 1.0f,
        1.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 0.0f,

        0.0f, 1.0f,
        1.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 0.0f,
};

public Cube() 
{
    ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
    byteBuf.order(ByteOrder.nativeOrder());
    vertexBuffer = byteBuf.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);

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

    indexBuffer = ByteBuffer.allocateDirect(indices.length);
    indexBuffer.put(indices);
    indexBuffer.position(0);
}

public void draw(GL10 gl) {
    //Point to our buffers
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    //Set the face rotation
    gl.glFrontFace(GL10.GL_CCW);
    //Enable the vertex and texture state
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
    //para que no pinte los poligonos que no se ven
    //gl.glEnable(GL10.GL_CULL_FACE);
    for(int i=0; i<3; i++) //<6 por que tenemos 6 texturas que queremos poner en las 6 caras de un cubo.
    {
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[i]);
        indexBuffer.position(6*i); //como cada dos triangulos (cuadrado) forman una cara, estos dos triangulos son 6 indices del array de indices, por lo tanto avanzamos 6 posiciones en el indexBuffer para pintar el siguiente cuadrado.
        gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_BYTE, indexBuffer); //el segundo parametro es 6 por que solo queremos pintar una cara (cuadrado) por textura.
    }
    //gl.glDisable(GL10.GL_CULL_FACE); //para que no pinte los poligonos que no se ven
    //Disable the client state before leaving
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}

public void loadGLTexture(GL10 gl, Context context) {
    loadTexture(gl,context,R.drawable.s1,0);
    loadTexture(gl,context,R.drawable.s2,1);
    loadTexture(gl,context,R.drawable.s3,2);
    loadTexture(gl,context,R.drawable.s4,3);
    loadTexture(gl,context,R.drawable.s5,4);
    loadTexture(gl,context,R.drawable.s6,5);
}

public void loadTexture(GL10 gl, Context context, int drawable, int textureNumber)
{
    //Get the texture from the Android resource directory
    InputStream is = context.getResources().openRawResource(drawable);
    Bitmap bitmap = null;
    try {
        //BitmapFactory is an Android graphics utility for images
        bitmap = BitmapFactory.decodeStream(is);
    } finally {
        //Always clear and close
        try {
            is.close();
            is = null;
        } catch (IOException e) {
        }
    }
    //Generate one texture pointer...
    gl.glGenTextures(1, textures, textureNumber);
    //...and bind it to our array
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[textureNumber]);
    //Create Nearest Filtered Texture
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    //Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
    //Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
    //Clean up
    bitmap.recycle();
}
}
  • 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-26T07:50:25+00:00Added an answer on May 26, 2026 at 7:50 am

    In OpenGL a vertex is the combination of a location and/or a texture coordinate and/or a colour and/or arbitrarily more attributes. So although you have 12 texture coordinates listed, all OpenGL gets from your data is 8 distinct vertices, each with a position and a texture coordinate.

    Your right face is composed of two triangles, one with vertices 1, 5 and 3 and one with vertices 5, 7 and 3. So that’s conceptually the same as a quad with vertices 1, 5, 7 and 3.

    From your own data, that quad has vertices:

    location: 1.0f, -1.0f,  1.0f; coordinate: 1.0f, 1.0f
    location: 1.0f, -1.0f, -1.0f; coordinate: 1.0f, 1.0f
    location: 1.0f,  1.0f, -1.0f; coordinate: 1.0f, 0.0f
    location: 1.0f,  1.0f,  1.0f; coordinate: 1.0f, 0.0f
    

    You’d therefore expect it to display the one dimensional straight line that runs along the right hand side of your texture, stretched out across the entire face. Is that what you’re seeing?

    If you want to supply unique texture coordinates for the corners of the side faces, you need to give them unique vertices (albeit that they’ll be located exactly on top of other vertices).

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

Sidebar

Related Questions

Right now I'm trying make some browser based game. But I have little questions.
I am trying to use Three.js to render a cube with 6 different images
I have an application that I know would make a great cube and would
im trying make one replace in string from a array but this dont work
I have spent about 2 Days trying to find out how to make 3d
I'm trying make one treeView with infinite subgroups. I can add my groups but
trying to make 5 curl childs for curl handler and defining them, but can't
I am trying to make an Object move to a Cube wich is stored
I'm trying make some stuff in jQuery using ASP.NET. But the ID from runat=server
I am working with nHibernate, and trying make sense of bag collections. My data

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.