PageRenderTime 21ms CodeModel.GetById 6ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Research/Example projects/Android OGL/AndroidGL/src/edu/union/GLTutorialEleven.java

https://bitbucket.org/DeveloperUX/behaviortree
Java | 192 lines | 141 code | 36 blank | 15 comment | 6 complexity | e653bfa42f49a5e114f54b7946997652 MD5 | raw file
  1package edu.union;
  2
  3import java.nio.FloatBuffer;
  4
  5import javax.microedition.khronos.opengles.GL10;
  6
  7import android.content.Context;
  8import android.opengl.GLU;
  9import android.view.KeyEvent;
 10
 11/**
 12 * Approximately corresponds to: http://www.zeuscmd.com/tutorials/opengles/25-Reflections.php
 13 * @author bburns
 14 *
 15 */
 16public class GLTutorialEleven extends GLTutorialBase {
 17	float y = 1.5f;
 18	
 19	float lightAmbient[] = new float[] { 0.2f, 0.3f, 0.6f, 1.0f };
 20	float lightDiffuse[] = new float[] { 0.6f, 0.6f, 0.6f, 1.0f };
 21	float[] lightPos = new float[] {0,0,3,1};
 22	
 23	float matAmbient[] = new float[] { 0.6f, 0.6f, 0.6f, 1.0f };
 24	float matDiffuse[] = new float[] { 0.6f, 0.6f, 0.6f, 1.0f };
 25	
 26	float white[] = new float[] { 1f, 1f, 1f, 1.0f};
 27	float trans[] = new float[] { 1f, 1f, 1f, 0.3f};
 28	
 29	float box[] = new float[] {
 30			// FRONT
 31			-0.5f, -0.5f,  0.5f,
 32			 0.5f, -0.5f,  0.5f,
 33			-0.5f,  0.5f,  0.5f,
 34			 0.5f,  0.5f,  0.5f,
 35			// BACK
 36			-0.5f, -0.5f, -0.5f,
 37			-0.5f,  0.5f, -0.5f,
 38			 0.5f, -0.5f, -0.5f,
 39			 0.5f,  0.5f, -0.5f,
 40			// LEFT
 41			-0.5f, -0.5f,  0.5f,
 42			-0.5f,  0.5f,  0.5f,
 43			-0.5f, -0.5f, -0.5f,
 44			-0.5f,  0.5f, -0.5f,
 45			// RIGHT
 46			 0.5f, -0.5f, -0.5f,
 47			 0.5f,  0.5f, -0.5f,
 48			 0.5f, -0.5f,  0.5f,
 49			 0.5f,  0.5f,  0.5f,
 50			// TOP
 51			-0.5f,  0.5f,  0.5f,
 52			 0.5f,  0.5f,  0.5f,
 53			 -0.5f,  0.5f, -0.5f,
 54			 0.5f,  0.5f, -0.5f,
 55			// BOTTOM
 56			-0.5f, -0.5f,  0.5f,
 57			-0.5f, -0.5f, -0.5f,
 58			 0.5f, -0.5f,  0.5f,
 59			 0.5f, -0.5f, -0.5f,
 60		};
 61
 62	float[] floorVertices = new float[] {
 63				-3.0f, 0.0f, 3.0f,
 64				 3.0f, 0.0f, 3.0f,
 65				-3.0f, 0.0f,-3.0f,
 66				 3.0f, 0.0f,-3.0f
 67			};
 68
 69	
 70	FloatBuffer cubeBuff;
 71	FloatBuffer floorBuff;
 72	
 73	public GLTutorialEleven(Context c) {
 74		super(c, 20);
 75
 76		cubeBuff = makeFloatBuffer(box);
 77		floorBuff = makeFloatBuffer(floorVertices);
 78	}
 79	
 80	protected void init(GL10 gl) {
 81		gl.glEnable(GL10.GL_LIGHTING);
 82		gl.glEnable(GL10.GL_LIGHT0);
 83		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, matAmbient, 0);
 84		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, matDiffuse, 0);
 85		
 86		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient,	0);
 87		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuse,	0);
 88		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPos, 0);
 89		gl.glEnable(GL10.GL_DEPTH_TEST);
 90		gl.glDepthFunc(GL10.GL_LEQUAL);
 91		
 92		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
 93		
 94		gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
 95		gl.glClearDepthf(1.0f);
 96		
 97		gl.glEnable(GL10.GL_CULL_FACE);
 98		gl.glShadeModel(GL10.GL_SMOOTH);
 99		
100		gl.glClearStencil(0);
101	}
102	
103	float xrot = 0.0f;
104	float yrot = 0.0f;
105	
106	public void onDrawFrame(GL10 gl) {
107		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT | GL10.GL_STENCIL_BUFFER_BIT);
108			
109		gl.glMatrixMode(GL10.GL_MODELVIEW);
110		gl.glLoadIdentity();
111		GLU.gluLookAt(gl, 0, 3, 7, 0, 0, 0, 0, 1, 0);
112		
113		// Draw the floor into the stencil buffer.
114		gl.glDisable(GL10.GL_DEPTH_TEST);
115		gl.glColorMask(false, false, false, false);
116		gl.glDepthMask(false);
117		
118		gl.glEnable(GL10.GL_STENCIL_TEST);
119		gl.glStencilOp(GL10.GL_REPLACE, GL10.GL_REPLACE, GL10.GL_REPLACE);
120		gl.glStencilFunc(GL10.GL_ALWAYS, 1, 0xffffffff);
121	
122		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, floorBuff);
123		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, white, 0);
124		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, white, 0);
125		gl.glNormal3f(0,1,0);
126		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
127		
128		// Draw the reflection
129		gl.glColorMask(true, true, true, true);
130		gl.glDepthMask(true);
131		gl.glEnable(GL10.GL_DEPTH_TEST);
132		gl.glStencilFunc(GL10.GL_EQUAL, 1, 0xffffffff);
133		gl.glStencilOp(GL10.GL_KEEP, GL10.GL_KEEP, GL10.GL_KEEP);
134		
135		gl.glPushMatrix();
136		gl.glScalef(1.0f, -1f, 1f);
137		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPos, 0);
138		gl.glCullFace(GL10.GL_FRONT);
139		
140		setupCube(gl);
141		
142		gl.glPushMatrix();
143		gl.glTranslatef(0.0f,y,-1.0f);
144		gl.glRotatef(xrot, 1, 0, 0);
145		gl.glRotatef(yrot, 0, 1, 0);
146		drawCube(gl);
147		gl.glPopMatrix();
148		
149		gl.glCullFace(GL10.GL_BACK);
150		gl.glPopMatrix();
151		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPos, 0);
152		
153		gl.glDisable(GL10.GL_STENCIL_TEST);
154		
155		// Draw the floor for real.
156		gl.glEnable(GL10.GL_BLEND);
157		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
158		
159		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, floorBuff);
160		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, trans, 0);
161		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, trans, 0);
162		gl.glNormal3f(0,1,0);
163		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
164		
165		gl.glDisable(GL10.GL_BLEND);
166		
167		setupCube(gl);
168		gl.glPushMatrix();
169		gl.glTranslatef(0.0f,y,-1.0f);
170		gl.glRotatef(xrot, 1, 0, 0);
171		gl.glRotatef(yrot, 0, 1, 0);
172		// Draw the non-reflected cube
173		drawCube(gl);
174		gl.glPopMatrix();
175		xrot+=1.0f;
176		yrot+=0.5f;
177	}
178
179	@Override
180	public boolean onKeyDown(int keyCode, KeyEvent event) {
181		if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
182			y+=0.1;
183			invalidate();
184		}
185		else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
186			if (y > 0.5)
187				y-=0.1;
188			invalidate();
189		}	
190		return super.onKeyDown(keyCode, event);
191	}
192}