ArViewer  Android Version
Arvos - Augmented reality viewer open source
 All Classes Namespaces Files Functions Variables
Square.java
Go to the documentation of this file.
1 /*
2  Copyright (C) 2013, Peter Graf
3 
4  This file is part of Arvos - AR Viewer Open Source for Android.
5  Arvos is free software.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 
21  For more information on the AR Viewer Open Source or Peter Graf,
22  please see: http://www.mission-base.com/.
23  */
24 
25 package com.mission_base.arviewer_android.viewer.opengl;
26 
27 import java.nio.ByteBuffer;
28 import java.nio.ByteOrder;
29 import java.nio.FloatBuffer;
30 
31 import javax.microedition.khronos.opengles.GL10;
32 
33 import com.mission_base.arviewer_android.Arvos;
34 
35 import android.graphics.Bitmap;
36 import android.graphics.BitmapFactory;
37 import android.opengl.GLUtils;
38 
45 public class Square
46 {
47  private FloatBuffer vertexBuffer; // buffer holding the vertices
48 
49  public static final float vertices[] = {
50  //
51  -0.5f, -0.5f, 0.0f, // V1 - bottom left
52  -0.5f, 0.5f, 0.0f, // V2 - top left
53  0.5f, -0.5f, 0.0f, // V3 - bottom right
54  0.5f, 0.5f, 0.0f // V4 - top right
55  };
56 
57  private FloatBuffer textureBuffer; // buffer holding the texture coordinates
58 
59  private static final float texture[] = {
60  // Mapping coordinates for the vertices
61  0.0f, 1.0f, // top left (V2)
62  0.0f, 0.0f, // bottom left (V1)
63  1.0f, 1.0f, // top right (V4)
64  1.0f, 0.0f // bottom right (V3)
65  };
66 
67  public Square()
68  {
69  ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
70  byteBuffer.order(ByteOrder.nativeOrder());
71  vertexBuffer = byteBuffer.asFloatBuffer();
72  vertexBuffer.put(vertices);
73  vertexBuffer.position(0);
74 
75  byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
76  byteBuffer.order(ByteOrder.nativeOrder());
77  textureBuffer = byteBuffer.asFloatBuffer();
78  textureBuffer.put(texture);
79  textureBuffer.position(0);
80  }
81 
83  private int[] textures = new int[1];
84 
85  public void loadGLTexture(GL10 gl, int textureId)
86  {
87  // loading texture
88  Bitmap bitmap = BitmapFactory.decodeResource(Arvos.getInstance().getActivity().getResources(), textureId);
89 
90  loadGLTexture(gl, bitmap);
91 
92  // Clean up
93  bitmap.recycle();
94  }
95 
96  public void loadGLTexture(GL10 gl, Bitmap bitmap)
97  {
98  // generate one texture pointer
99  gl.glGenTextures(1, textures, 0);
100  // ...and bind it to our array
101  gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
102 
103  // create nearest filtered texture
104  gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
105  gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
106 
107  // Use Android GLUtils to specify a two-dimensional texture image from
108  // our bitmap
109  GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
110  }
111 
113  public void draw(GL10 gl)
114  {
115  // bind the previously generated texture
116  gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
117 
118  // Point to our buffers
119  gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
120  gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
121 
122  // Set the face rotation
123  gl.glFrontFace(GL10.GL_CW);
124 
125  // Point to our vertex buffer
126  gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
127  gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
128 
129  // Draw the vertices as triangle strip
130  gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
131 
132  // Disable the client state before leaving
133  gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
134  gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
135  }
136 }