Augment Reality Apps on Android

Problems with Canvas, OpenGL, etc...

Augment Reality Apps on Android

Postby Ramana » Wed May 19, 2010 8:05 am

Iam trying to develop augment reality app for android.I have camera preview on SurfaceView and 3d grid on GLSurfaceView.Now i would like to plot some points as Markers(2d png images) on the 3d grid?
How do i do that?Both 3d grid and markers should move as per the sensor values?

Warm Regards,
Ramana.

Thanks seed for the reply.Just i will try to elaborate the problem.
1.I have camera preview on surface view.
2.I have a grid made from vertices and indices to connect them.I have rotated the grid by -45deg around x-axis.Now the grid is more or less parallel to earth.
3.Now i have a png image.I have used the following code to read the png image.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         //Get the texture from the Android resource directory
  2.                 InputStream is = context.getResources().openRawResource(R.drawable.icon);
  3.                 Bitmap bitmap = null;
  4.                 try {
  5.                         //BitmapFactory is an Android graphics utility for images
  6.                         bitmap = BitmapFactory.decodeStream(is);
  7.  
  8.                 } finally {
  9.                         //Always clear and close
  10.                         try {
  11.                                 is.close();
  12.                                 is = null;
  13.                         } catch (IOException e) {
  14.                         }
  15.                 }
  16.  
  17.                 //Generate one texture pointer...
  18.                 gl.glGenTextures(1, textures, 0);
  19.                 //...and bind it to our array
  20.                 gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
  21.                
  22.                 //Create Nearest Filtered Texture
  23.                 gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
  24.                 gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
  25.  
  26.                 //Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
  27.                 gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
  28.                 gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
  29.                
  30.                 //Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
  31.                 GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
  32.                
  33.                 //Clean up
  34.                 bitmap.recycle();
  35.         }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

These are my grid vertices and indices.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         private float vertices[] = {
  2.                            -3.0f,  3.0f, 0.0f,  //0
  3.                        -3.0f,  2.0f, 0.0f,  //1
  4.                        -3.0f,  1.0f, 0.0f,  //2
  5.                        -3.0f,  0.0f, 0.0f,  //3
  6.                        -3.0f, -1.0f, 0.0f,  //4
  7.                        -3.0f, -2.0f, 0.0f,  //5
  8.                        -3.0f, -3.0f, 0.0f,  //6
  9.                        -2.0f, -3.0f, 0.0f,  //7
  10.                        -2.0f, -2.0f, 0.0f,  //8
  11.                        -2.0f, -1.0f, 0.0f,  //9
  12.                        -2.0f,  0.0f, 0.0f,  //10
  13.                        -2.0f,  1.0f, 0.0f,  //11
  14.                        -2.0f,  2.0f, 0.0f,  //12
  15.                        //-2.0f,  3.0f, 0.0f,  //13
  16.                        -2.0f,  3.0f, 0.0f,  //14
  17.                        -1.0f,  3.0f, 0.0f,  //15
  18.                        -1.0f,  2.0f, 0.0f,  //16
  19.                        -1.0f,  1.0f, 0.0f,  //17
  20.                        //-1.0f,  1.0f, 0.0f,  //18
  21.                        -1.0f,  0.0f, 0.0f,  //19
  22.                        -1.0f, -1.0f, 0.0f,  //20
  23.                        -1.0f, -2.0f, 0.0f,  //21
  24.                        -1.0f, -3.0f, 0.0f,  //22
  25.                         0.0f, -3.0f, 0.0f,  //23
  26.                         0.0f, -2.0f, 0.0f,  //24
  27.                         0.0f, -1.0f, 0.0f,  //25
  28.                         0.0f,  0.0f, 0.0f,  //26
  29.                         0.0f,  1.0f, 0.0f,  //27
  30.                         0.0f,  2.0f, 0.0f,  //28
  31.                         0.0f,  3.0f, 0.0f,  //29
  32.                         1.0f,  3.0f, 0.0f,  //30
  33.                         1.0f,  2.0f, 0.0f,  //31
  34.                         1.0f,  1.0f, 0.0f,  //32
  35.                         1.0f,  0.0f, 0.0f,  //33
  36.                         1.0f, -1.0f, 0.0f,  //34
  37.                         1.0f, -2.0f, 0.0f,  //35
  38.                         1.0f, -3.0f, 0.0f,  //36
  39.                         2.0f, -3.0f, 0.0f,  //37
  40.                         2.0f, -2.0f, 0.0f,  //38
  41.                         2.0f, -1.0f, 0.0f,  //39
  42.                         2.0f,  0.0f, 0.0f,  //40
  43.                         2.0f,  1.0f, 0.0f,  //41
  44.                         2.0f,  2.0f, 0.0f,  //42
  45.                         2.0f,  3.0f, 0.0f,  //43
  46.                         3.0f,  3.0f, 0.0f,  //44
  47.                         3.0f,  2.0f, 0.0f,  //45
  48.                         3.0f,  1.0f, 0.0f,  //46
  49.                         3.0f,  0.0f, 0.0f,  //47
  50.                         //3.0f,  0.0f, 0.0f,  //48
  51.                         3.0f, -1.0f, 0.0f,  //49
  52.                         3.0f, -2.0f, 0.0f,  //50
  53.                         3.0f, -3.0f, 0.0f,  //51
  54.                         //3.0f, -2.0f, 0.0f,
  55.                 };
Parsed in 0.043 seconds, using GeSHi 1.0.8.4

private short[] indices = { 0,6,6,48,48,42,42,0,7,13,20,14,21,27,34,28,35,41,48,42,
1,43,2,44,3,45,4,46,5,47 };

How do i define the texture coordinates for my image to appear on my grid?
If i do texture image will it be possible to make them move on the grid?

This is my OndrawFrame:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onDrawFrame(GL10 gl) {
  2.                 // TODO Auto-generated method stub
  3.                
  4.                 Log.i("Inside draw frame", "Ondraw frame");
  5.                 //gl.glEnable(GL10.GL_TEXTURE_2D);
  6.                 //gl.glClearColor(0, 0, 0, 0);
  7.                 gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  8.                 gl.glLoadIdentity();
  9.                 gl.glTranslatef(0,0,-4);
  10.                 gl.glScalef(2.0f, 1.0f, 1.0f);
  11.                 //gl.glScalef(0.9f, 0.9f, 0.9f);
  12.                 gl.glPushMatrix();
  13.                 gl.glRotatef(-45 , 1, 0, 0);
  14.                 //Square sq = new Square();
  15.                 sq.draw(gl);
  16.                 gl.glPopMatrix();
  17.                
  18.         }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


sq.draw method::
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void draw(GL10 gl) {
  2.                
  3.                 gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
  4.                 //gl.glLoadIdentity();
  5.                 // Counter-clockwise winding.
  6.                 gl.glFrontFace(GL10.GL_CCW);
  7.                 // Enable face culling.
  8.                 gl.glEnable(GL10.GL_CULL_FACE);
  9.                 // What faces to remove with the face culling.
  10.                 gl.glCullFace(GL10.GL_BACK);
  11.                
  12.                 // Enabled the vertices buffer for writing and to be used during
  13.                 // rendering.
  14.                 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  15.                 gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
  16.                 // Specifies the location and data format of an array of vertex
  17.                 // coordinates to use when rendering.
  18.                 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
  19.                 gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
  20.                
  21.                 gl.glDrawElements(GL10.GL_LINES, indices.length,
  22.                                 GL10.GL_UNSIGNED_SHORT, indexBuffer);
  23.                
  24. //              gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,
  25. //                              GL10.GL_UNSIGNED_SHORT, indexBuffer);
  26.                
  27.                 // Disable the vertices buffer.
  28.                 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
  29.                 // Disable face culling.
  30.                 gl.glDisable(GL10.GL_CULL_FACE);
  31.         }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


My image does not appear on the grid.
How do we actually define the texture coordinates.?
I have defined like this but no results
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private float texture[] = {            
  2.                 //Mapping coordinates for the vertices
  3.                  0.1f, 0.1f,
  4.                  1.0f, 1.0f,
  5.                  1.0f, 1.0f,
  6.                  1.0f, 0.0f,
  7.         };
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Hi ,
I didn't get why we need texture coordinates for both grid and markers?Iam newbee to opengl and as far as i know that texture is used when an image has to be overlayed on 3d surface.Cant we just draw 3d grid with vertices,indices and png images textured on to them.For markers we will define vertices ,indices and texture coordinates.Is my understanding right?
Last edited by Ramana on Wed May 26, 2010 12:29 pm, edited 2 times in total.
Ramana
Junior Developer
Junior Developer
 
Posts: 17
Joined: Wed May 19, 2010 6:57 am

Top

Re: Augment Reality Apps on Android

Postby seed » Wed May 19, 2010 1:56 pm

This is a bit like saying "I want to make Halo for Android. How do I do that?"

Maybe try narrowing your question some.

I will try to answer a little bit.

(1) Create your grid and objects as lists of vertices and indices and load them in buffers.
(2) Create matrix translations and rotations based on your sensor readings. Search the web for how to do this.
(3) Use OpenGl to apply your matrix translations and then to draw your grid/objects.
Visit Exit 4 Gaming - http://www.exit4games.com/
Home of LavaBall - http://exit4games.com/?page_id=3
Home of Rebound - http://exit4games.com/?page_id=138
Home of Tap Crazy - http://exit4games.com/?page_id=219
Download "Caveman Pool" From the Market Today!
seed
Senior Developer
Senior Developer
 
Posts: 103
Joined: Mon Mar 15, 2010 3:22 pm

Re: Augment Reality Apps on Android

Postby seed » Wed May 19, 2010 6:20 pm

Draw the grid and the markers as separate objects, each with their own set of vertex, texture, and index buffers.

You marker is a 2D texture. Give it vertices, texture coordinates, and indices like this:
0,0,0, 1,0,0, 1,1,0, 0,1,0 // Vertices.
0.0, 1,0, 1,1, 0,1 // Texture coordinates
0,1,2, 0,2,3 // Indices - two triangles.

Then in your draw routine, draw the grid, then draw the markers. You probably want to do some scaling on the markers that I don't show.

glPushMatrix()
glTranslate()
glRotate()
sq.draw() // Draw the grid
for (int i=0; i<markers.length; i++)
{
markers[i].draw();
}
glPopMatrix()

Hope this helps.
Visit Exit 4 Gaming - http://www.exit4games.com/
Home of LavaBall - http://exit4games.com/?page_id=3
Home of Rebound - http://exit4games.com/?page_id=138
Home of Tap Crazy - http://exit4games.com/?page_id=219
Download "Caveman Pool" From the Market Today!
seed
Senior Developer
Senior Developer
 
Posts: 103
Joined: Mon Mar 15, 2010 3:22 pm

Re: Augment Reality Apps on Android

Postby puxdroid » Tue Jun 21, 2011 2:19 pm

Maybe you could use this framework: beyondar.com
puxdroid
Developer
Developer
 
Posts: 49
Joined: Wed Nov 25, 2009 11:57 am

Top

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

Who is online

Users browsing this forum: No registered users and 4 guests