How to load 565 bitmaps as textures

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

How to load 565 bitmaps as textures

Postby Hachaso » Thu Apr 08, 2010 2:08 pm

Hi!

I can't seem to find any information how to load 565 bitmaps.
Does anyone know how to do this?

Maybe some code examples.

Thanks
Hachaso
Developer
Developer
 
Posts: 43
Joined: Mon Jan 26, 2009 11:44 am

Top

Postby MichaelEGR » Thu Apr 08, 2010 3:14 pm

I'll have a bunch of tutorials and full streamlined middleware called TyphonRT launched soon... Here is a quick dump of some code though that should get you started in the right direction. You will have to substitute a few things, but this should get you started.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.    public static Texture loadTexture2D(Bitmap bitmap, int clampS, int clampT, int magFilter, int minFilter, int mode)
  3.  
  4.    {
  5.  
  6.       if (bitmap == null)
  7.  
  8.       {
  9.  
  10.          logger.error("TextureLoader - loadTexture2D - bitmap is null.");
  11.  
  12.          throw new IllegalArgumentException("TextureLoader - loadTexture2D - bitmap is null.");
  13.  
  14.       }
  15.  
  16.  
  17.  
  18.       if (bitmap.getConfig() == null)
  19.  
  20.       {
  21.  
  22.          logger.error("TextureLoader - loadTexture2D - bitmap config is null.");
  23.  
  24.          throw new IllegalArgumentException("TextureLoader - loadTexture2D - bitmap config is null.");
  25.  
  26.       }
  27.  
  28.      
  29.  
  30.       int textureID = glb.glGenTexture();
  31.  
  32.       gl.glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  33.  
  34.       gl.glBindTexture(GL_TEXTURE_2D, textureID);
  35.  
  36.  
  37.  
  38.       gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clampS);
  39.  
  40.       gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clampT);
  41.  
  42.       gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  43.  
  44.       gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  45.  
  46.       gl.glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mode);
  47.  
  48.  
  49.  
  50.       GLUtils.texImage2D(GL_TEXTURE_2D, 0, bitmap, 0);
  51.  
  52.  
  53.  
  54.       int type = GLUtils.getType(bitmap);
  55.  
  56.  
  57.  
  58.       return new Texture(textureID, bitmap.getWidth(), bitmap.getHeight(), type == GLImage.RGBA ||
  59.  
  60.        type == GLImage.BGRA, true, clampS, clampT, magFilter, minFilter, mode);
  61.  
  62.    }
  63.  
  64.  
Parsed in 0.044 seconds, using GeSHi 1.0.8.4


As things go this won't run out of the box for you, but will give you a start. For instance GLImage is a TyphonRT class and so is Texture. I also have a static reference to the GL context and don't pass a reference around all over the place.

And here is the Texture class.. As things go, yes there is more TyphonRT specific code. VectorBuffer3f and VectorBuffer2f are essentially NIO FloatBuffers.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class Texture
  3.  
  4. {
  5.  
  6.    private static final boolean  s_DEFAULT_USE_MIPMAPS = false;
  7.  
  8.    private static final String   s_STR_EMPTY = "";
  9.  
  10.  
  11.  
  12.    private static VectorBuffer3f vertexBuffer;
  13.  
  14.    private static VectorBuffer2f textureBuffer;
  15.  
  16.  
  17.  
  18.    static
  19.  
  20.    {
  21.  
  22.       vertexBuffer = new VectorBuffer3f(4, true, true);
  23.  
  24.       textureBuffer = new VectorBuffer2f(4, true, true);
  25.  
  26.    }
  27.  
  28.  
  29.  
  30.    /**
  31.  
  32.     * The name or path of the texture
  33.  
  34.     */
  35.  
  36.    public String name = s_STR_EMPTY;
  37.  
  38.  
  39.  
  40.    /**
  41.  
  42.     * The binded id of the texture in opengl
  43.  
  44.     */
  45.  
  46.    public int id;
  47.  
  48.  
  49.  
  50.    /**
  51.  
  52.     * The binded images width and height
  53.  
  54.     */
  55.  
  56.    public int width, height;
  57.  
  58.  
  59.  
  60.    /**
  61.  
  62.     * True if texture is transparent. Transparent textures are stored as GL_RGBA, opaque as GL_RGB
  63.  
  64.     */
  65.  
  66.    public boolean transparent;
  67.  
  68.  
  69.  
  70.    /**
  71.  
  72.     * Whether texture uses mipmaps
  73.  
  74.     */
  75.  
  76.    public boolean useMipmaps = s_DEFAULT_USE_MIPMAPS;
  77.  
  78.  
  79.  
  80.    /**
  81.  
  82.     * The GL_TEXTURE_WRAP_S texture parameter
  83.  
  84.     */
  85.  
  86.    public int wrapS = OpenGL.GL_REPEAT;
  87.  
  88.  
  89.  
  90.    /**
  91.  
  92.     * The GL_TEXTURE_WRAP_T texture parameter
  93.  
  94.     */
  95.  
  96.    public int wrapT = OpenGL.GL_REPEAT;
  97.  
  98.  
  99.  
  100.    /**
  101.  
  102.     * The GL_TEXTURE_MAG_FILTER texture parameter
  103.  
  104.     */
  105.  
  106.    public int magFilter = OpenGL.GL_LINEAR;
  107.  
  108.  
  109.  
  110.    /**
  111.  
  112.     * The GL_TEXTURE_MIN_FILTER texture parameter
  113.  
  114.     */
  115.  
  116.    public int minFilter = OpenGL.GL_LINEAR;
  117.  
  118.  
  119.  
  120.    /**
  121.  
  122.     * The GL_TEXTURE_ENV_MODE texture parameter
  123.  
  124.     */
  125.  
  126.    public int mode = OpenGL.GL_MODULATE;
  127.  
  128.  
  129.  
  130.    /**
  131.  
  132.     * Creates a default Texture.
  133.  
  134.     */
  135.  
  136.    public Texture(int id)
  137.  
  138.    {
  139.  
  140.       this.id = id;
  141.  
  142.    }  
  143.  
  144.  
  145.  
  146.    /**
  147.  
  148.     * Creates a Texture.
  149.  
  150.     */
  151.  
  152.    public Texture(int id, int width, int height, boolean transparent, boolean useMipmaps,
  153.  
  154.     int wrapS, int wrapT, int magFilter, int minFilter, int mode)
  155.  
  156.    {
  157.  
  158.       this(null, id, width, height, transparent, useMipmaps, wrapS, wrapT, magFilter, minFilter, mode);
  159.  
  160.    }
  161.  
  162.  
  163.  
  164.    public Texture(String name, int id, int width, int height, boolean transparent, boolean useMipmaps,
  165.  
  166.     int wrapS, int wrapT, int magFilter, int minFilter, int mode)
  167.  
  168.    {
  169.  
  170.       this.name = name;
  171.  
  172.       this.id = id;
  173.  
  174.       this.width = width;
  175.  
  176.       this.height = height;
  177.  
  178.       this.transparent = transparent;
  179.  
  180.       this.useMipmaps = useMipmaps;
  181.  
  182.       this.wrapS = wrapS;
  183.  
  184.       this.wrapT = wrapT;
  185.  
  186.       this.magFilter = magFilter;
  187.  
  188.       this.minFilter = minFilter;
  189.  
  190.       this.mode = mode;
  191.  
  192.    }
  193.  
  194.  
  195.  
  196.    // Draws a texture quads at the specified bounds.
  197.  
  198.    public static void drawTexturedQuad(float x, float y, float width, float height, float tx, float ty, float tw,
  199.  
  200.     float th)
  201.  
  202.    {
  203.  
  204.       vertexBuffer.put(x, y, 0);
  205.  
  206.       vertexBuffer.put(x + width, y, 0);
  207.  
  208.       vertexBuffer.put(x, y + height, 0);
  209.  
  210.       vertexBuffer.put(x + width, y + height, 0);
  211.  
  212.       vertexBuffer.rewindWrite();
  213.  
  214.  
  215.  
  216.       textureBuffer.put(tx, ty);
  217.  
  218.       textureBuffer.put(tx + tw, ty);
  219.  
  220.       textureBuffer.put(tx, ty + th);
  221.  
  222.       textureBuffer.put(tx + tw, ty + th);
  223.  
  224.       textureBuffer.rewindWrite();
  225.  
  226.  
  227.  
  228.       gl.glVertexPointer(3, 0, vertexBuffer.getReadBuffer());
  229.  
  230.       gl.glTexCoordPointer(2, 0, textureBuffer.getReadBuffer());
  231.  
  232.       gl.glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  233.  
  234.    }
  235.  
  236.  
  237.  
  238.    // Draws a texture quads at the specified bounds.
  239.  
  240.    public static void drawTexturedQuad(float x, float y, float z, float width, float height, float tx, float ty,
  241.  
  242.     float tw, float th)
  243.  
  244.    {
  245.  
  246.       vertexBuffer.put(x, y, z);
  247.  
  248.       vertexBuffer.put(x + width, y, z);
  249.  
  250.       vertexBuffer.put(x, y + height, z);
  251.  
  252.       vertexBuffer.put(x + width, y + height, 0);
  253.  
  254.       vertexBuffer.rewindWrite();
  255.  
  256.  
  257.  
  258.       textureBuffer.put(tx, ty);
  259.  
  260.       textureBuffer.put(tx + tw, ty);
  261.  
  262.       textureBuffer.put(tx, ty + th);
  263.  
  264.       textureBuffer.put(tx + tw, ty + th);
  265.  
  266.       textureBuffer.rewindWrite();
  267.  
  268.  
  269.  
  270.       gl.glVertexPointer(3, 0, vertexBuffer.getReadBuffer());
  271.  
  272.       gl.glTexCoordPointer(2, 0, textureBuffer.getReadBuffer());
  273.  
  274.       gl.glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  275.  
  276.    }
  277.  
  278.  
  279.  
  280.    public void set(Texture t)
  281.  
  282.    {
  283.  
  284.       this.name = t.name;
  285.  
  286.       this.id = t.id;
  287.  
  288.       this.width = t.width;
  289.  
  290.       this.height = t.height;
  291.  
  292.       this.transparent = t.transparent;
  293.  
  294.       this.useMipmaps = t.useMipmaps;
  295.  
  296.       this.wrapS = t.wrapS;
  297.  
  298.       this.wrapT = t.wrapT;
  299.  
  300.       this.magFilter = t.magFilter;
  301.  
  302.       this.minFilter = t.minFilter;
  303.  
  304.       this.mode = t.mode;
  305.  
  306.    }
  307.  
  308.  
  309.  
  310.    /**
  311.  
  312.     * Sets the texture parameters.
  313.  
  314.     */
  315.  
  316.    public void setTexParameters()
  317.  
  318.    {
  319.  
  320.       gl.glBindTexture(GL_TEXTURE_2D, id);
  321.  
  322.       gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
  323.  
  324.       gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
  325.  
  326.       gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  327.  
  328.       gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  329.  
  330.       gl.glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mode);
  331.  
  332.    }
  333.  
  334.  
  335.  
  336.    private static final String s_STR_NAME = "[name=";
  337.  
  338.    private static final String s_STR_ID = " id=";
  339.  
  340.    private static final String s_STR_WIDTH = " width=";
  341.  
  342.    private static final String s_STR_HEIGHT = " height=";
  343.  
  344.    private static final String s_STR_WRAPS = " wrapS=";
  345.  
  346.    private static final String s_STR_WRAPT = " wrapT=";
  347.  
  348.    private static final String s_STR_MAGFILTER = " magFilter=";
  349.  
  350.    private static final String s_STR_MINFILTER = " minFilter=";
  351.  
  352.    private static final String s_STR_MODE = " mode=";
  353.  
  354.    private static final String s_STR_TRANSPARENT = " transparent=";
  355.  
  356.    private static final String s_STR_YES = "yes]";
  357.  
  358.    private static final String s_STR_NO = "no]";
  359.  
  360.  
  361.  
  362.    public String toString()
  363.  
  364.    {
  365.  
  366.       StringBuilder sb = new StringBuilder(s_STR_NAME);
  367.  
  368.  
  369.  
  370.       sb.append(name).append(s_STR_ID).append(id).append(s_STR_WIDTH).append(width).append(s_STR_HEIGHT).append(
  371.  
  372.        height).append(s_STR_WRAPS).append(wrapS).append(s_STR_WRAPT).append(wrapT).append(s_STR_MAGFILTER).append(
  373.  
  374.         magFilter).append(s_STR_MINFILTER).append(minFilter).append(s_STR_MODE).append(mode).append(
  375.  
  376.          s_STR_TRANSPARENT).append(transparent ? s_STR_YES : s_STR_NO);
  377.  
  378.  
  379.  
  380.       return sb.toString();
  381.  
  382.    }
  383.  
  384. }
  385.  
  386.  
Parsed in 0.056 seconds, using GeSHi 1.0.8.4
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests