Thursday, May 26, 2011

glBindTexture example c c++ objc java

Name
       glBindTexture — bind a named texture to a texturing target

C Specification

       void glBindTexture(GLenum  target,  GLuint  texture);

Parameters

    target       Specifies the target to which the texture is bound.
                    Must be either
                    GL_TEXTURE_1D,
                    GL_TEXTURE_2D,
                    GL_TEXTURE_3D, or
                    GL_TEXTURE_CUBE_MAP.

   texture      Specifies the name of a texture.
 
Description
            glBindTexture lets you create or use a named texture. Calling glBindTexture
            with target set to GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D or
            GL_TEXTURE_CUBE_MAP and texture set to the name of the new texture
            binds the texture name to the target.
            When a texture is bound to a target, the previous binding for that
            target is automatically broken.
       
            Texture names are unsigned integers. The value zero is reserved to
            represent the default texture for each texture target.
            Texture names and the corresponding texture contents are local to
            the shared display-list space (see glXCreateContext) of the current
            GL rendering context;
            two rendering contexts share texture names only if they
            also share display lists.
       
            You may use glGenTextures to generate a set of new texture names.
       
            When a texture is first bound, it assumes the specified target:
            A texture first bound to GL_TEXTURE_1D becomes one-dimensional texture, a
            texture first bound to GL_TEXTURE_2D becomes two-dimensional texture, a
            texture first bound to GL_TEXTURE_3D becomes three-dimensional texture, and a
            texture first bound to GL_TEXTURE_CUBE_MAP
            becomes a cube-mapped texture. The state of a one-dimensional texture
            immediately after it is first bound is equivalent to the state of the
            default GL_TEXTURE_1D at GL initialization, and similarly for two-
            and three-dimensional textures and cube-mapped textures.
       
            While a texture is bound, GL operations on the target to which it is
            bound affect the bound texture, and queries of the target to which it
            is bound return state from the bound texture. If texture mapping is active
            on the target to which a texture is bound, the bound texture is used.
            In effect, the texture targets become aliases for the textures currently
            bound to them, and the texture name zero refers to the default textures
            that were bound to them at initialization.
       
            A texture binding created with glBindTexture remains active until a different
            texture is bound to the same target, or until the bound texture is
            deleted with glDeleteTextures.
       
            Once created, a named texture may be re-bound to its same original target as often as needed.
            It is usually much faster to use glBindTexture to bind an existing named
            texture to one of the texture targets than it is to reload the texture image
            using glTexImage1D, glTexImage2D, or glTexImage3D.
            For additional control over performance, use
            glPrioritizeTextures.
       
            glBindTexture is included in display lists.


Notes
            glBindTexture is available only if the GL version is 1.1 or greater.
       
            GL_TEXTURE_CUBE_MAP is available only if the GL version is 1.3 or greater.

 Errors
            GL_INVALID_ENUM is generated if target is not one of the allowable
            values.
       
            GL_INVALID_OPERATION is generated if texture was previously created with a target
            that doesn't match that of target.
       
            GL_INVALID_OPERATION is generated if glBindTexture is executed
            between the execution of glBegin and the corresponding
            execution of glEnd.
        Associated Gets
            glGet with argument GL_TEXTURE_BINDING_1D
       
            glGet with argument GL_TEXTURE_BINDING_2D
       
            glGet with argument GL_TEXTURE_BINDING_3D
        See Also
            glAreTexturesResident,
            glDeleteTextures,
            glGenTextures,
            glGet,
            glGetTexParameter,
            glIsTexture,
            glPrioritizeTextures,
            glTexImage1D,
            glTexImage2D,
            glTexParameter
        Copyright
            Copyright © 1991-2006
            Silicon Graphics, Inc. This document is licensed under the SGI
            Free Software B License. For details, see
            http://oss.sgi.com/projects/FreeB/.     

Examples

public class Text3d {

 private IntBuffer   mVertexBuffer;
    private IntBuffer   mColorBuffer;
    private ByteBuffer  mIndexBuffer;
    private FloatBuffer mfTexBuffer;
    private int mTextureID;
   
 public Text3d(){
     int one = 50;
         int vertices[] = {
                 0, one, 0,
                 one, one, 0,
                 0,  0, 0,
                 one,  0, 0,
              
         };
        
         float tex[] = {
           0.0f ,0.0f,
           1.0f,0.0f,
           0.0f ,1.0f,
           1.0f,1.0f,
        
         };
         one = 100000;
         int colors[] = {
           one,    one,    one,  one,
                 one,    one,    one,  one,
                 one,  one,    one,  one,
                 one,  one,    one,  one,
               
         };
         byte indices[] = {
                 0, 1, 2,
                 1, 3, 2,
         };
    
         ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
         vbb.order(ByteOrder.nativeOrder());
         mVertexBuffer = vbb.asIntBuffer();
         mVertexBuffer.put(vertices);
         mVertexBuffer.position(0);
        
         ByteBuffer tbb = ByteBuffer.allocateDirect(tex.length*4);
         tbb.order(ByteOrder.nativeOrder());
         mfTexBuffer = tbb.asFloatBuffer();
         mfTexBuffer.put(tex);
         mfTexBuffer.position(0);
         ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);
         cbb.order(ByteOrder.nativeOrder());
         mColorBuffer = cbb.asIntBuffer();
         mColorBuffer.put(colors);
         mColorBuffer.position(0);
         mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
         mIndexBuffer.put(indices);
         mIndexBuffer.position(0);
     
     }


  public void TexCreate(GL10 gl,Context mContext)
  {
   int[] textures = new int[1];
         gl.glGenTextures(1, textures, 0);
         mTextureID = textures[0];
         gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);
        
         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);
         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_WRAP_T,
                 GL10.GL_CLAMP_TO_EDGE);
         gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,
                 GL10.GL_REPLACE);
      
        
      Bitmap mBitmap;
     
      Canvas mCanvas;
     
      Bitmap.Config config =   Bitmap.Config.ARGB_8888;
     
      mBitmap = Bitmap.createBitmap(128, 128, config);
         mCanvas = new Canvas(mBitmap);
         mBitmap.eraseColor(0);
        
         mCanvas.drawColor(0x00ffffff);
        
         Paint Pnt = new Paint();
         Pnt.setColor(0xff00ff00);
         Pnt.setTextSize(128);
         Pnt.setAntiAlias(false);
         Pnt.setTextScaleX(1);
        
         mCanvas.drawText("B", 0, 128, Pnt);
         
         GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, mBitmap, 0);
        
         mBitmap.recycle();   
   }

     public void draw(GL10 gl)
     {
    
         gl.glFrontFace(GL10.GL_CW);
         gl.glEnable(GL10.GL_TEXTURE_2D);
         gl.glColor4f(1f, 1f, 1f, 1f);
         gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,
                 GL10.GL_MODULATE);
        
         gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);
        
         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
         gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
         gl.glActiveTexture(GL10.GL_TEXTURE0);
       
         gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mfTexBuffer);
         gl.glVertexPointer(3, GL10.GL_FIXED, 0, mVertexBuffer);
         gl.glColorPointer(4, GL10.GL_FIXED, 0, mColorBuffer);
         gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_BYTE, mIndexBuffer);
     }
}