PageRenderTime 39ms CodeModel.GetById 15ms app.highlight 20ms RepoModel.GetById 2ms app.codeStats 0ms

/Research/Example projects/Android OGL/ModelRenderer/src/edu/union/android/ModelView.java

https://bitbucket.org/DeveloperUX/behaviortree
Java | 231 lines | 179 code | 45 blank | 7 comment | 17 complexity | 20b410554c6795a9aee4a24e7d1cf2d7 MD5 | raw file
  1package edu.union.android;
  2
  3import java.nio.ByteBuffer;
  4import java.nio.ByteOrder;
  5import java.nio.IntBuffer;
  6import java.nio.ShortBuffer;
  7
  8import javax.microedition.khronos.opengles.GL10;
  9import javax.microedition.khronos.opengles.GL11;
 10
 11import android.content.Context;
 12import android.graphics.Bitmap;
 13import android.graphics.BitmapFactory;
 14import android.graphics.Canvas;
 15import android.graphics.OpenGLContext;
 16import android.opengl.GLU;
 17import android.view.KeyEvent;
 18import android.view.View;
 19
 20import edu.union.graphics.Model;
 21import edu.union.graphics.Mesh;
 22import edu.union.graphics.FixedPointUtils;
 23
 24public class ModelView extends View {
 25	private Model m;
 26	private OpenGLContext context;
 27	ViewAnimator animator;
 28
 29	int tex;
 30	int verts;
 31
 32	float lightAmbient[] = new float[] { 0.2f, 0.3f, 0.6f, 1.0f };
 33	float lightDiffuse[] = new float[] { 1f, 1f, 1f, 1.0f };
 34
 35	float matAmbient[] = new float[] { 1f, 1f, 1f, 1.0f };
 36	float matDiffuse[] = new float[] { 1f, 1f, 1f, 1.0f };
 37
 38	float[] pos = new float[] {0,20,20,1};
 39
 40	int frame_ix = 0;
 41
 42	private IntBuffer[] vertices;
 43	private IntBuffer normals;
 44	private IntBuffer texCoords;
 45	private ShortBuffer indices;
 46
 47	protected static int loadTexture(GL10 gl, Bitmap bmp) {
 48		ByteBuffer bb = ByteBuffer.allocateDirect(bmp.height()*bmp.width()*4);
 49		bb.order(ByteOrder.nativeOrder());
 50		IntBuffer ib = bb.asIntBuffer();
 51
 52		for (int y=0;y<bmp.height();y++)
 53			for (int x=0;x<bmp.width();x++) {
 54				ib.put(bmp.getPixel(x,y));
 55			}
 56		ib.position(0);
 57		bb.position(0);
 58
 59		int[] tmp_tex = new int[1];
 60
 61		gl.glGenTextures(1, tmp_tex, 0);
 62		int tx = tmp_tex[0];
 63		gl.glBindTexture(GL10.GL_TEXTURE_2D, tx);
 64		gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, bmp.width(), bmp.height(), 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
 65		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
 66		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
 67		return tx;
 68	}
 69
 70	public ModelView(edu.union.graphics.Model m, Context c) {
 71		super(c);
 72		setFocusable(true);
 73		this.m = m;
 74		context = new OpenGLContext(OpenGLContext.DEPTH_BUFFER);
 75
 76		GL10 gl = (GL10)context.getGL();
 77		gl.glShadeModel(GL10.GL_SMOOTH);
 78		gl.glClearColor(1,1,1,1);
 79
 80		gl.glClearDepthf(1.0f);
 81		gl.glEnable(GL10.GL_DEPTH_TEST);
 82		gl.glDepthFunc(GL10.GL_LEQUAL);
 83
 84		gl.glEnable(GL10.GL_LIGHTING);
 85		gl.glEnable(GL10.GL_LIGHT0);
 86		gl.glEnable(GL10.GL_NORMALIZE);
 87
 88
 89		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient,	0);
 90		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuse,	0);
 91		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, pos, 0);
 92
 93		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, matAmbient, 0);
 94		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, matDiffuse, 0);
 95
 96		// Pretty perspective
 97		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
 98		gl.glEnable(GL10.GL_CULL_FACE);
 99		// Turn on these for textures.
100
101		ByteBuffer bb;
102		vertices = new IntBuffer[m.getFrameCount()];
103		for (int i=0;i<m.getFrameCount();i++) {
104			Mesh ms = m.getFrame(i).getMesh();
105			verts = ms.getFaceCount()*3;
106
107			//bb = ByteBuffer.allocateDirect(msh.getVertexCount()*3*4);
108			bb = ByteBuffer.allocateDirect(verts*3*4);
109			bb.order(ByteOrder.nativeOrder());
110			vertices[i] = bb.asIntBuffer();
111
112			for (int f=0;f<ms.getFaceCount();f++) {
113				int[] face = ms.getFace(f);
114				for (int j=0;j<3;j++) {
115					float[] v = ms.getVertexf(face[j]);
116					for (int k=0;k<3;k++) {
117						vertices[i].put(FixedPointUtils.toFixed(v[k]));
118					}
119				}
120			}
121			vertices[i].position(0);
122		}
123
124		Mesh msh = m.getFrame(0).getMesh();
125		if (msh.getTextureFile() != null) {
126			gl.glEnable(GL10.GL_TEXTURE_2D);
127			tex = loadTexture(gl, BitmapFactory.decodeResource(c.getResources(),R.drawable.skin));
128		}
129
130		//bb = ByteBuffer.allocateDirect(msh.getVertexCount()*3*4);
131		bb = ByteBuffer.allocateDirect(verts*3*4);
132		bb.order(ByteOrder.nativeOrder());
133		normals = bb.asIntBuffer();
134
135		bb = ByteBuffer.allocateDirect(verts*2*4);
136		bb.order(ByteOrder.nativeOrder());
137		texCoords = bb.asIntBuffer();
138
139		bb = ByteBuffer.allocateDirect(verts*2);
140		bb.order(ByteOrder.nativeOrder());
141		indices = bb.asShortBuffer();
142
143		short ct = 0;
144		for (int i=0;i<msh.getFaceCount();i++) {
145			int[] face_n = msh.getFaceNormals(i);
146			int[] face_tx = msh.getFaceTextures(i);
147			for (int j=0;j<3;j++) {
148				float[] n = msh.getNormalf(face_n[j]);
149				for (int k=0;k<3;k++) {
150					normals.put(FixedPointUtils.toFixed(n[k]));
151				}
152				float[] tx = msh.getTextureCoordinatef(face_tx[j]);
153				texCoords.put(FixedPointUtils.toFixed(tx[0]));
154				texCoords.put(FixedPointUtils.toFixed(tx[1]));				
155				indices.put(ct++);
156			}
157		}
158		normals.position(0);
159		texCoords.position(0);
160		indices.position(0);
161
162		animator = new ViewAnimator(this, 5);
163	}
164
165
166
167	@Override
168	protected void onAttachedToWindow() {
169		animator.start();
170		super.onAttachedToWindow();
171	}
172
173	@Override
174	protected void onDetachedFromWindow() {
175		animator.stop();
176		super.onDetachedFromWindow();
177	}
178
179	@Override
180	protected void onDraw(Canvas canvas) {
181		GL11 gl = (GL11)context.getGL();
182		int w = getWidth();
183		int h = getHeight();
184
185		context.waitNative(canvas, this);
186
187		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
188
189		gl.glMatrixMode(GL10.GL_PROJECTION);
190		gl.glLoadIdentity();
191		gl.glViewport(0,0,w,h);
192		GLU.gluPerspective(gl, 45.0f, ((float)w)/h, 1f, 100f);
193
194		gl.glMatrixMode(GL10.GL_MODELVIEW);
195		gl.glLoadIdentity();
196		GLU.gluLookAt(gl, 0, 0, 5, 0, 0, 0, 0, 1, 0);
197		gl.glTranslatef(0,0,-10);
198		gl.glRotatef(30.0f, 1, 0, 0);
199		//gl.glRotatef(40.0f, 0, 1, 0);
200
201		gl.glVertexPointer(3,GL10.GL_FIXED, 0, vertices[frame_ix]);
202		gl.glNormalPointer(GL10.GL_FIXED,0, normals);
203		gl.glTexCoordPointer(2,GL10.GL_FIXED,0,texCoords);
204		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
205		gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
206		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
207
208		//gl.glColor4f(1,0,0,1);
209		gl.glBindTexture(GL10.GL_TEXTURE_2D, tex);
210		//gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
211		gl.glDrawElements(GL10.GL_TRIANGLES, verts, GL10.GL_UNSIGNED_SHORT, indices);
212
213		frame_ix = (frame_ix+1)%m.getFrameCount();
214		context.waitGL();
215	}
216
217	@Override
218	public boolean onKeyDown(int keyCode, KeyEvent event) {
219		if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
220			frame_ix--;
221			if (frame_ix < 0)
222				frame_ix = m.getFrameCount()-1;
223			invalidate();
224		}
225		else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
226			frame_ix = (frame_ix+1)%m.getFrameCount();
227			invalidate();
228		}
229		return super.onKeyUp(keyCode, event);
230	}
231}