fabioramirezs / andar

Automatically exported from code.google.com/p/andar
0 stars 0 forks source link

Facing problem to load texture image in andar?? #29

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Dear all,

Last copule of days, I am trying to load a texture image(.bmp) inside andar 
sample project(downlod from trunk) application's coding.but every time white 
texture come to screen on marker.here is my coding for texture:

package edu.dhbw.andar.pub;
public class CustomObject extends ARObject {

    public CustomObject(String name, String patternName,
            double markerWidth, double[] markerCenter) {
        super(name, patternName, markerWidth, markerCenter);

    }

......
.....

    int[]  textureIDs = new int[5];
    private Resources res;

public final void draw(GL10 gl) {
        super.draw(gl);
.....
.....
       gl.glEnable(GL10.GL_TEXTURE_2D); 

        //Enable vertex buffer
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

        //Bind our only previously generated texture in this case
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[0]);

        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices1.length / 3);
}

        @Override
        public void init(GL10 gl) {

            gl.glEnable(GL10.GL_TEXTURE_2D);

            gl.glGenTextures(1, textureIDs, 0);
            //load the textures into the graphics memory

    Bitmap bm = BitmapFactory.decodeResource(res, R.drawable.image);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[0]);
            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm,0);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

            bm.recycle();

        }
}

here the object is four square and image will be bind in the four sqaure 
object. the coding can compile and install in phone. the four square draw but 
with white texture.I also add image.bmp in drawble folder. but still same 
result.

please help me to sort out this problem...

Thanks

Rassall

Original issue reported on code.google.com by rassall....@gmail.com on 21 Jul 2010 at 9:35

GoogleCodeExporter commented 9 years ago
well when I upgraded my sample work with andar from G1 to Samsung Galaxy 9000 
my textures got white as well, I put the texture files in res/drawable-hdpi 
instead of drawable and it worked agein so maybe that's a possibility for u as 
well

Original comment by bansch...@gmail.com on 21 Jul 2010 at 1:37

GoogleCodeExporter commented 9 years ago
and u don't use vertex coordinates so that'll be a prob for sure

that's how I do it

public void draw(GL10 gl, float x, float y, float w, float h)
    {
        float vertex[] = new float[] {
                 x - w/2, y - h/2, 0,
                 x + w/2, y - h/2, 0,
                 x - w/2, y + h/2, 0,
                 x + w/2, y + h/2, 0
            };

         float texel[] = new float[] {
                 0, 1,
                 1, 1,
                 0, 0,
                 1, 0
           };

          ByteBuffer vb = ByteBuffer.allocateDirect(vertex.length * 4);
          vb.order(ByteOrder.nativeOrder());
          FloatBuffer vertexBuffer = vb.asFloatBuffer();
          vertexBuffer.put(vertex);
          vertexBuffer.position(0);

          ByteBuffer tb = ByteBuffer.allocateDirect(texel.length * 4);
          tb.order(ByteOrder.nativeOrder());
          FloatBuffer texelBuffer = tb.asFloatBuffer();
          texelBuffer.put(texel);
          texelBuffer.position(0);

          gl.glDisable(GL10.GL_BLEND);
          gl.glEnable(GL10.GL_TEXTURE_2D);
          bindTexture(gl); // this is my own funtction but just binds an allocated tex
          gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
          gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
          gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texelBuffer);
          gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
          gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
    }

Original comment by bansch...@gmail.com on 21 Jul 2010 at 7:04

GoogleCodeExporter commented 9 years ago
Thanks banschare for your promt reply. basically in my coding I used vertex 
coordinates.like:

        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, verticesBuffer);
        gl.glNormalPointer(GL10.GL_FLOAT, 0, normalsBuffer);    
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureCoordsBuffer);
        gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 6);

after that a four square drawn and also can coloring it by using 
glcolor4f..anyway i still trouble bind it with texture image.

I already tried your drawable-hdpi method but got error:

Bitmap bm = BitmapFactory.decodeResource(res, 
R.drawable-hdpi.nehe1);//error:hdpi cannot be resolved and also R.drawable 
cannot be resolved

I also tried your coding but program exit when marker detect.if i ignored 
super.draw(gl);then not exit but nonthing draw in the scree.

if you are free can you check my coiding again..i am afraid whatever i have 
decalared is right or not.like:
package edu.dhbw.andar.pub;
public class CustomObject extends ARObject {
....
....
    @Override
    public void draw (GL10) {
        super.draw(gl);
         ....
         ....
            gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 6);
}
    @Override
    public void init(GL10 gl) {

        gl.glEnable(GL10.GL_TEXTURE_2D);

        gl.glGenTextures(1, textureIDs, 0);
        //load the textures into the graphics memory

        Bitmap bm = BitmapFactory.decodeResource(res, R.drawable.nehe1);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[0]);
        //GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm,0);
        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

        bm.recycle();

    }

I think there is something mismatch my declaration/wrong in my coding.So I am 
trying to figure out.If anybody can help me I will be very much thankful...

Thanks
Rassall

Original comment by rassall....@gmail.com on 22 Jul 2010 at 5:08

GoogleCodeExporter commented 9 years ago
have you debugged the reference of Bitmap bm is there actually a valid 
reference or is it 0, non square texture will make problems as well, so your 
texture should be for example 256x256

and why have you commented this line //GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 
0, bm,0);

this is very important otherwise your bitmap doesn't get loaded in texture 
memory

best,
banschare

Original comment by bansch...@gmail.com on 22 Jul 2010 at 9:53

GoogleCodeExporter commented 9 years ago
Thanks Banschare for continuous supporting me..

how can i can do debug to understand bitmap bm is 0 or 1?

Yes I used 256x256 texture for this case...

basically in main coding I used also

GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm,0);

but still same white box texture on the marker..Even I tried to load texture in 
CustomActivity.java also  but still same result white box...such that

public class CustomActivity extends AndARActivity {

....
....
    @Override
    public void init(GL10 gl) {
        gl.glGenTextures(1, textureIDs, 0);
        Bitmap bm = BitmapFactory.decodeResource(res, R.drawable.image);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[0]);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm,0);
        bm.recycle();

    }
}

For me I think the problem is the loading texture. and this line under 

public void init(GL10 gl) {

gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[0]);///has no effect in my 
coding

}
has no effect in my coding

So I am really not understand what is the problem here...If you are free can i 
send this application to you for check...

Thanks again..

Regards
Rassall

Original comment by rassall....@gmail.com on 23 Jul 2010 at 3:53

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Rassall,

After the execution of this line:
Bitmap bm = BitmapFactory.decodeResource(res, R.drawable.image);

you should check the value of bm to make sure it is non-zero. It is possible 
that your bitmap isn't loading correctly. 

also, have you examined the output from logcat to see if there are any errors 
in it? 

Original comment by suzy...@gmail.com on 19 Aug 2010 at 1:25

GoogleCodeExporter commented 9 years ago
Thanks Suzyque,

Actaully I don't understand how i can check value of bm ?because in logcat 
there is no information of about bm.

Anyway I tried to load texture followed by pingpong game(Tdomahan).And now 
problem seems in following two line:

Bitmap bm = BitmapFactory.decodeResource(res, R.drawable.nehe1);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm,0); 

If i ignore this two line then program can open and draw white cube during 
marker detection time. otherwise if use these two functions then program open 
and exit automatically.

I did not find any thing about this two line in log.cat. Anyway I have attached 
my logcat of this program.

I have attached my program also..So if you check it will very helpful for me.

Thanks

Regards
Rassall

Original comment by rassall....@gmail.com on 20 Aug 2010 at 9:10

Attachments:

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Thanks 

I able to solved it.

Regards
Rassall

Original comment by rassall....@gmail.com on 27 Aug 2010 at 6:18

GoogleCodeExporter commented 9 years ago
alright so I will close this issue. It would be great if you could tell others 
what the problem was and how you solved it.
regards
tobi

Original comment by tdomhan on 2 Oct 2010 at 11:11

GoogleCodeExporter commented 9 years ago
// Get a new texture id:
    private static int newTextureID(GL10 gl) {
        int[] temp = new int[1];
        gl.glGenTextures(1, temp, 0);
        return temp[0];        
    }

    private int loadTexture(GL10 gl, Context context, int resource) {       

        gl.glEnable(GL10.GL_TEXTURE_2D);

        int id = newTextureID(gl);

        Matrix flip = new Matrix();
        flip.postScale(1f, -1f);

        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inScaled = true;

        // Load up, and flip the texture:
        //Bitmap temp = BitmapFactory.decodeResource(context.getResources(), resource, opts);
       // Bitmap bmp = Bitmap.createBitmap(temp, 0, 0, temp.getWidth(), temp.getHeight(), flip, true);
        //temp.recycle(); 

        InputStream is = context.getResources().openRawResource( resource);
        Bitmap bmp = null;
        try {           
            bmp = BitmapFactory.decodeStream(is);

        } finally {
            //Always clear and close
            try {
                is.close();
                is = null;
            } catch (IOException e) {
            }
        }

        gl.glBindTexture(GL10.GL_TEXTURE_2D, id);       

        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR_MIPMAP_NEAREST);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR_MIPMAP_NEAREST);
        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);

        int[] crop = {0, bmp.getWidth(), bmp.getHeight(), - bmp.getHeight()};

        ((GL11) gl).glTexParameteriv(GL10.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, crop, 0);

        for(int level=0, height = bmp.getHeight(), width = bmp.getWidth(); true; level++) {

            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, level, bmp, 0);

            if(height==1 && width==1) break;

            width >>= 1; height >>= 1;
            if(width<1)  width = 1;
            if(height<1) height = 1;

            Bitmap bmp2 = Bitmap.createScaledBitmap(bmp, width, height, true);
            bmp.recycle();
            bmp = bmp2;
        }

        bmp.recycle();

        return id;
    }
Please check this will work

Original comment by reju...@gmail.com on 11 Jan 2011 at 7:10

GoogleCodeExporter commented 9 years ago
Im having trouble with this section 

I finding it difficult to understand your solution did you place the load 
textures method inside your CustomObject  I'm sorta a noob at this and would 
appreciate any help with loading textures in AndAR

Original comment by Dsuleym...@googlemail.com on 17 Mar 2012 at 9:58

GoogleCodeExporter commented 9 years ago
How are you getting the context? And where are you finally calling loadtexture? 
The problem I'm having in my project is that I need access to the projects 
Context, but I don't have access to it (at least not in my CustomOjbect class). 

Original comment by masturba...@hotmail.com on 23 Mar 2012 at 1:31

GoogleCodeExporter commented 9 years ago
Hi guys, may I see the code that is working so that I can see it in my phone, 
I'm having a problem too in adding a texture in sample cube in the project, 
help me please

Original comment by arive...@gmail.com on 27 Oct 2012 at 12:38