PageRenderTime 23ms CodeModel.GetById 6ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/Research/Example projects/android2.1_TestOGL/src/com/example/atogl/GLTutorialBase.java

https://bitbucket.org/DeveloperUX/behaviortree
Java | 252 lines | 182 code | 31 blank | 39 comment | 2 complexity | 2c9c2e8d1643d4b52cf2da78754f0557 MD5 | raw file
  1package com.example.atogl;
  2
  3import java.nio.ByteBuffer;
  4import java.nio.ByteOrder;
  5import java.nio.FloatBuffer;
  6import java.nio.IntBuffer;
  7import javax.microedition.khronos.egl.EGLConfig;
  8import javax.microedition.khronos.opengles.GL10;
  9
 10import android.content.Context;
 11import android.graphics.Bitmap;
 12import android.graphics.BitmapFactory;
 13import android.opengl.GLU;
 14import android.opengl.GLSurfaceView;
 15import android.os.Handler;
 16
 17public abstract class GLTutorialBase extends GLSurfaceView implements GLSurfaceView.Renderer {
 18	protected Context context;
 19	protected Handler handler;
 20	
 21	int width;
 22	int height;
 23	int fps;
 24	
 25	float box[] = new float[] {
 26			// FRONT
 27			 0.5f, -0.5f,  0.5f,
 28			 0.5f,  0.5f,  0.5f,
 29			-0.5f, -0.5f,  0.5f,
 30			-0.5f,  0.5f,  0.5f,
 31			// BACK
 32			-0.5f, -0.5f, -0.5f,
 33			-0.5f,  0.5f, -0.5f,
 34			 0.5f, -0.5f, -0.5f,
 35			 0.5f,  0.5f, -0.5f,
 36			// LEFT
 37			-0.5f, -0.5f,  0.5f,
 38			-0.5f,  0.5f,  0.5f,
 39			-0.5f, -0.5f, -0.5f,
 40			-0.5f,  0.5f, -0.5f,
 41			// RIGHT
 42			 0.5f, -0.5f, -0.5f,
 43			 0.5f,  0.5f, -0.5f,
 44			 0.5f, -0.5f,  0.5f,
 45			 0.5f,  0.5f,  0.5f,
 46			// TOP
 47			-0.5f,  0.5f,  0.5f,
 48			 0.5f,  0.5f,  0.5f,
 49			 -0.5f,  0.5f, -0.5f,
 50			 0.5f,  0.5f, -0.5f,
 51			// BOTTOM
 52			-0.5f, -0.5f,  0.5f,
 53			-0.5f, -0.5f, -0.5f,
 54			 0.5f, -0.5f,  0.5f,
 55			 0.5f, -0.5f, -0.5f,
 56		};
 57	
 58	float texCoords[] = new float[] {
 59			// FRONT
 60			 0.0f, 1.0f,
 61			 0.0f, 0.0f,
 62			 1.0f, 1.0f,
 63			 1.0f, 0.0f,
 64			// BACK
 65			 1.0f, 0.0f,
 66			 1.0f, 1.0f,
 67			 0.0f, 0.0f,
 68			 0.0f, 1.0f,
 69			// LEFT
 70			 1.0f, 0.0f,
 71			 1.0f, 1.0f,
 72			 0.0f, 0.0f,
 73			 0.0f, 1.0f,
 74			// RIGHT
 75			 1.0f, 0.0f,
 76			 1.0f, 1.0f,
 77			 0.0f, 0.0f,
 78			 0.0f, 1.0f,
 79			// TOP
 80			 0.0f, 0.0f,
 81			 1.0f, 0.0f,
 82			 0.0f, 1.0f,
 83			 1.0f, 1.0f,
 84			// BOTTOM
 85			 1.0f, 0.0f,
 86			 1.0f, 1.0f,
 87			 0.0f, 0.0f,
 88			 0.0f, 1.0f
 89		};
 90
 91	FloatBuffer cubeBuff;
 92	FloatBuffer texBuff;
 93	
 94	public void setHandler(Handler han)
 95	{
 96		handler = han;
 97	}
 98	
 99	/**
100	 * Make a direct NIO FloatBuffer from an array of floats
101	 * @param arr The array
102	 * @return The newly created FloatBuffer
103	 */
104	protected static FloatBuffer makeFloatBuffer(float[] arr) {
105		ByteBuffer bb = ByteBuffer.allocateDirect(arr.length*4);
106		bb.order(ByteOrder.nativeOrder());
107		FloatBuffer fb = bb.asFloatBuffer();
108		fb.put(arr);
109		fb.position(0);
110		return fb;
111	}
112
113	/**
114	 * Make a direct NIO IntBuffer from an array of ints
115	 * @param arr The array
116	 * @return The newly created IntBuffer
117	 */
118	protected static IntBuffer makeFloatBuffer(int[] arr) {
119		ByteBuffer bb = ByteBuffer.allocateDirect(arr.length*4);
120		bb.order(ByteOrder.nativeOrder());
121		IntBuffer ib = bb.asIntBuffer();
122		ib.put(arr);
123		ib.position(0);
124		return ib;
125	}
126	
127	protected static ByteBuffer makeByteBuffer(Bitmap bmp) {
128		ByteBuffer bb = ByteBuffer.allocateDirect(bmp.getHeight()*bmp.getWidth()*4);
129		bb.order(ByteOrder.BIG_ENDIAN);
130		IntBuffer ib = bb.asIntBuffer();
131
132		for (int y = 0; y < bmp.getHeight(); y++)
133			for (int x=0;x<bmp.getWidth();x++) {
134				int pix = bmp.getPixel(x, bmp.getHeight()-y-1);
135				// Convert ARGB -> RGBA
136				byte alpha = (byte)((pix >> 24)&0xFF);
137				byte red = (byte)((pix >> 16)&0xFF);
138				byte green = (byte)((pix >> 8)&0xFF);
139				byte blue = (byte)((pix)&0xFF);
140								
141				ib.put(((red&0xFF) << 24) | 
142					   ((green&0xFF) << 16) |
143					   ((blue&0xFF) << 8) |
144					   ((alpha&0xFF)));
145			}
146		ib.position(0);
147		bb.position(0);
148		return bb;
149	}
150	
151	protected int loadTexture(GL10 gl, int resource) {
152		return loadTexture(gl, BitmapFactory.decodeResource(context.getResources(), resource));
153	}
154	
155	/**
156	 * Create a texture and send it to the graphics system
157	 * @param gl The GL object
158	 * @param bmp The bitmap of the texture
159	 * @param reverseRGB Should the RGB values be reversed?  (necessary workaround for loading .pngs...)
160	 * @return The newly created identifier for the texture.
161	 */
162	protected static int loadTexture(GL10 gl, Bitmap bmp) {
163		int[] tmp_tex = new int[1];
164
165		gl.glGenTextures(1, tmp_tex, 0);
166		int tex = tmp_tex[0];
167		loadTexture(tex, GL10.GL_TEXTURE_2D, bmp, gl);
168		return tex;
169	}
170	
171
172	
173	public void loadTexture(int texture, int type,  int resource, GL10 gl) {
174		loadTexture(texture, type, BitmapFactory.decodeResource(context.getResources(), resource), gl);
175	}
176	
177	static public void loadTexture(int texture, int type, Bitmap bmp, GL10 gl) {
178		loadTexture(texture, type, bmp.getWidth(), bmp.getHeight(), makeByteBuffer(bmp), gl);
179	}
180	
181	static public void loadTexture(int texture, int type, int width, int height, ByteBuffer bb, GL10 gl) {
182		gl.glBindTexture(type, texture);
183		gl.glTexImage2D(type, 0, GL10.GL_RGBA, width, height, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, null);
184		gl.glTexSubImage2D(type, 0, 0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
185		gl.glTexParameterf(type, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
186		gl.glTexParameterf(type, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
187	}
188
189	/**
190	 * Constructor
191	 */
192	public GLTutorialBase(Context context) {
193		this(context, -1);
194	}
195	
196	/**
197	 * Constructor for animated views
198	 * @param c The View's context
199	 * @param fps The frames per second for the animation.
200	 */
201	public GLTutorialBase(Context context, int fps) {
202		super(context);
203		this.context = context;
204		this.fps = fps;
205		
206		cubeBuff = makeFloatBuffer(box);
207		texBuff = makeFloatBuffer(texCoords);
208	}
209
210	public void setupCube(GL10 gl) {
211		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, cubeBuff);
212		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
213		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuff);
214		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
215	}
216	
217	public void drawCube(GL10 gl) {
218		gl.glColor4f(1.0f, 1, 1, 1.0f);
219		gl.glNormal3f(0,0,1);
220		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
221		gl.glNormal3f(0,0,-1);
222		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);
223	
224		gl.glColor4f(1, 1.0f, 1, 1.0f);
225		gl.glNormal3f(-1,0,0);
226		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
227		gl.glNormal3f(1,0,0);
228		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);
229		
230		gl.glColor4f(1, 1, 1.0f, 1.0f);
231		gl.glNormal3f(0,1,0);
232		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);
233		gl.glNormal3f(0,-1,0);
234		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4);
235	}
236	
237    public void onSurfaceCreated(GL10 gl, EGLConfig config) {    
238    }
239
240    public void onSurfaceChanged(GL10 gl, int w, int h) {
241    	this.width = w;
242    	this.height = h;
243    	gl.glMatrixMode(GL10.GL_PROJECTION);
244		gl.glLoadIdentity();
245		gl.glViewport(0,0,w,h);
246		GLU.gluOrtho2D(gl, 0, w, h, 0);
247		//GLU.gluPerspective(gl, 45.0f, ((float)w)/h, 1f, 100f);
248		init(gl);
249	}
250	
251	protected void init(GL10 gl) {}	
252}