PageRenderTime 33ms CodeModel.GetById 14ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Research/Example projects/AndroidGL_0.2/AndroidGL/src/edu/union/GLTutorialEight.java

https://bitbucket.org/DeveloperUX/behaviortree
Java | 216 lines | 166 code | 38 blank | 12 comment | 2 complexity | e8d6eff7f3e45700b81ecfce45de2908 MD5 | raw file
  1package edu.union;
  2
  3import java.nio.ByteBuffer;
  4import java.nio.ByteOrder;
  5import java.nio.FloatBuffer;
  6import java.nio.IntBuffer;
  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.View;
 18
 19public class GLTutorialEight extends View {
 20	private OpenGLContext glContext;
 21	
 22	float lightAmbient[] = new float[] { 0.2f, 0.3f, 0.6f, 1.0f };
 23	float lightDiffuse[] = new float[] { 0.2f, 0.3f, 0.6f, 1.0f };
 24
 25	float matAmbient[] = new float[] { 0.6f, 0.6f, 0.6f, 1.0f };
 26	float matDiffuse[] = new float[] { 0.6f, 0.6f, 0.6f, 1.0f };
 27	
 28	int tex;
 29	Bitmap bmp;
 30	
 31	float box[] = new float[] {
 32			// FRONT
 33			-0.5f, -0.5f,  0.5f,
 34			 0.5f, -0.5f,  0.5f,
 35			-0.5f,  0.5f,  0.5f,
 36			 0.5f,  0.5f,  0.5f,
 37			// BACK
 38			-0.5f, -0.5f, -0.5f,
 39			-0.5f,  0.5f, -0.5f,
 40			 0.5f, -0.5f, -0.5f,
 41			 0.5f,  0.5f, -0.5f,
 42			// LEFT
 43			-0.5f, -0.5f,  0.5f,
 44			-0.5f,  0.5f,  0.5f,
 45			-0.5f, -0.5f, -0.5f,
 46			-0.5f,  0.5f, -0.5f,
 47			// RIGHT
 48			 0.5f, -0.5f, -0.5f,
 49			 0.5f,  0.5f, -0.5f,
 50			 0.5f, -0.5f,  0.5f,
 51			 0.5f,  0.5f,  0.5f,
 52			// TOP
 53			-0.5f,  0.5f,  0.5f,
 54			 0.5f,  0.5f,  0.5f,
 55			 -0.5f,  0.5f, -0.5f,
 56			 0.5f,  0.5f, -0.5f,
 57			// BOTTOM
 58			-0.5f, -0.5f,  0.5f,
 59			-0.5f, -0.5f, -0.5f,
 60			 0.5f, -0.5f,  0.5f,
 61			 0.5f, -0.5f, -0.5f,
 62		};
 63	
 64	float texCoords[] = new float[] {
 65			// FRONT
 66			 0.0f, 0.0f,
 67			 1.0f, 0.0f,
 68			 0.0f, 1.0f,
 69			 1.0f, 1.0f,
 70			// BACK
 71			 1.0f, 0.0f,
 72			 1.0f, 1.0f,
 73			 0.0f, 0.0f,
 74			 0.0f, 1.0f,
 75			// LEFT
 76			 1.0f, 0.0f,
 77			 1.0f, 1.0f,
 78			 0.0f, 0.0f,
 79			 0.0f, 1.0f,
 80			// RIGHT
 81			 1.0f, 0.0f,
 82			 1.0f, 1.0f,
 83			 0.0f, 0.0f,
 84			 0.0f, 1.0f,
 85			// TOP
 86			 0.0f, 0.0f,
 87			 1.0f, 0.0f,
 88			 0.0f, 1.0f,
 89			 1.0f, 1.0f,
 90			// BOTTOM
 91			 1.0f, 0.0f,
 92			 1.0f, 1.0f,
 93			 0.0f, 0.0f,
 94			 0.0f, 1.0f
 95		};
 96
 97	FloatBuffer cubeBuff;
 98	FloatBuffer texBuff;
 99	
100	protected FloatBuffer makeFloatBuffer(float[] arr) {
101		ByteBuffer bb = ByteBuffer.allocateDirect(arr.length*4);
102		bb.order(ByteOrder.nativeOrder());
103		FloatBuffer fb = bb.asFloatBuffer();
104		fb.put(arr);
105		fb.position(0);
106		return fb;
107	}
108	
109	float[] pos = new float[] {0,0,3,0};
110	
111	public int loadTexture(GL10 gl, Context c, Bitmap bmp) {
112		ByteBuffer bb = ByteBuffer.allocateDirect(bmp.height()*bmp.width()*4);
113		bb.order(ByteOrder.nativeOrder());
114		IntBuffer ib = bb.asIntBuffer();
115		
116		for (int y=0;y<bmp.height();y++)
117			for (int x=0;x<bmp.width();x++) {
118					ib.put(bmp.getPixel(x,y));
119			}
120		ib.position(0);
121		bb.position(0);
122		
123		int[] tmp_tex = new int[1];
124		
125		gl.glGenTextures(1, tmp_tex, 0);
126		int tex = tmp_tex[0];
127		gl.glBindTexture(GL10.GL_TEXTURE_2D, this.tex);
128		gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, bmp.width(), bmp.height(), 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
129		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
130		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
131	
132		return tex;
133	}
134	
135	public GLTutorialEight(Context c) {
136		super(c);
137		bmp = BitmapFactory.decodeResource(c.getResources(), R.drawable.zeus);
138		
139		glContext = new OpenGLContext(OpenGLContext.DEPTH_BUFFER);
140		GL11 gl = (GL11)glContext.getGL();
141		
142		gl.glEnable(GL10.GL_LIGHTING);
143		gl.glEnable(GL10.GL_LIGHT0);
144		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, matAmbient, 0);
145		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, matDiffuse, 0);
146		
147		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient,	0);
148		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuse,	0);
149		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, pos, 0);
150		gl.glEnable(GL10.GL_DEPTH_TEST);
151		gl.glDepthFunc(GL10.GL_LEQUAL);
152		
153		gl.glEnable(GL10.GL_TEXTURE_2D);
154		
155		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
156		gl.glClearDepthf(1.0f);
157		
158		cubeBuff = makeFloatBuffer(box);
159		texBuff = makeFloatBuffer(texCoords);
160		
161		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, cubeBuff);
162		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
163		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuff);
164		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
165		
166		gl.glEnable(GL10.GL_CULL_FACE);
167		gl.glShadeModel(GL10.GL_SMOOTH);	
168	
169		tex = loadTexture(gl, c, bmp);
170	}
171	
172	float xrot = 0.0f;
173	float yrot = 0.0f;
174	
175	protected void onDraw(Canvas canvas) {
176		GL11 gl = (GL11)glContext.getGL();
177		int w = getWidth();
178		int h = getHeight();
179		
180		glContext.waitNative(canvas, this);
181		
182		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
183		
184		gl.glMatrixMode(GL10.GL_PROJECTION);
185		gl.glLoadIdentity();
186		gl.glViewport(0,0,w,h);
187		GLU.gluPerspective(gl, 45.0f, ((float)w)/h, 1f, 100f);
188			
189		gl.glMatrixMode(GL10.GL_MODELVIEW);
190		gl.glLoadIdentity();
191		GLU.gluLookAt(gl, 0, 0, 3, 0, 0, 0, 0, 1, 0);
192	
193		gl.glRotatef(30.0f, 1, 0, 0);
194		gl.glRotatef(40.0f, 0, 1, 0);
195	
196		gl.glColor4f(1.0f, 1, 1, 1.0f);
197		gl.glNormal3f(0,0,1);
198		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
199		gl.glNormal3f(0,0,-1);
200		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);
201	
202		gl.glColor4f(1, 1.0f, 1, 1.0f);
203		gl.glNormal3f(-1,0,0);
204		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
205		gl.glNormal3f(1,0,0);
206		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);
207		
208		gl.glColor4f(1, 1, 1.0f, 1.0f);
209		gl.glNormal3f(0,1,0);
210		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);
211		gl.glNormal3f(0,-1,0);
212		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4);
213	
214		glContext.waitGL();
215	}
216}