Search code examples
androidopengl-estexturing

opengl es texturing problem android


i want to stretch or tile an opengl object from some bitmap. i use image of a part of bitmap. but my ring and middle textured with a average color of image. if i change image color change, but picture is not showing.

and my full code here

  //  ..in init///


           gl.glEnable(GL10.GL_TEXTURE_2D);   
           textures = new int[3];
           bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.pizzaf);
           bitmap = Bitmap.createBitmap(   bitmap, 0, 0,    bitmap.getWidth(),
                   bitmap.getHeight(), null, true);
           bitmap1 = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.pizslice);
           bitmap1 = Bitmap.createBitmap(   bitmap1, 0, 0,    bitmap1.getWidth(),
                   bitmap1.getHeight(), null, true);
        // Tell OpenGL to generate textures.
           gl.glTexParameterf(GL10.GL_TEXTURE_2D,
                   GL10.GL_TEXTURE_WRAP_S,
                   GL10.GL_CLAMP_TO_EDGE);
//         gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
//                  GL10.GL_LINEAR);
//          gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
//                  GL10.GL_LINEAR);

           gl.glGenTextures(1, textures, 0);

    }

and draw function

GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap1, 0);
            gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);

                 GLUT.glutSolidTorus(gl,0.175f, 0.95f, 12, 48);
                 gl.glTranslatef(0,0, 0.2f);
            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
                GLUT.glutSolidCone(gl, 1,0.01f, 12, 48);
            gl.glTranslatef(0,0, -0.4f);
            gl.glRotatef (180.0f, 1.0f, 0.0f, 0.0f);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap1, 0);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
        GLUT.glutSolidCone(gl, 1,0.01f, 12, 48);

please help anyone. i want to make a 3d pizza


Solution

  • There are 3 problems in your code:

    1. You don't use texture objects properly
    2. You reinitialize textures on each draw call
    3. glutSolidCone doesn't provide texture coordinates (means: you can't texture it properly)

    Fixing (1)

    A texture is always loaded according to this scheme:

    GLuint texture_object_name;
    
    glGenTextures(1, 
                  &texture_object_name); // you can create texture object names in batches
    
    glBindTexture(GL_TEXTURE_2D, // may differ, depending on texture target (i.e. kind of texture)
                  texture_object_name);
    
    glPixelStorei(GL_UNPACK_ALIGNMENT, …);   // </ depends on data format
    glPixelStorei(GL_UNPACK_ROW_LENGTH, …);  // <|
    glPixelStorei(GL_UNPACK_SKIP_PIXELS, …); // <|
    glPixelStorei(GL_UNPACK_SKIP_ROWS, …);   // <|
    
    glTexImage2D(GL_TEXTURE_2D, // again the target, depends on the kind of texture creates
                 0, // mipmap level
                 GL_RGBA, // internal format, GL_RGBA is one possibility, there are others
                 width, height, border, // border is usually 0
                 GL_BGRA,                  // format of the data input
                 GL_UNSIGNED_INT_8_8_8_8,  // type of the data input -- those must match the data
                 texture_data );
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, …); // mipmapping on/off and other settings
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, …); // you should really set those
    

    Fixing (2)

    Once you fixed (1), you can fix (2). To switch textures while rendering you just have to call

    glBindTexture(GL_TEXTURE_2D, texture_object_name);
    

    to use it in subsequent drawing calls.

    Fixing (3)

    (3) is fixed by defining the geometry yourself, supplying proper texture coordinates.