won't be needing this anymore

This commit is contained in:
benfry
2010-01-10 23:28:40 +00:00
parent c4c6c897b6
commit 3092f6135a
8 changed files with 0 additions and 724 deletions
@@ -1,100 +0,0 @@
/*
* Copyright (C) 2007 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package processing.android.opengl;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import javax.microedition.khronos.opengles.GL10;
/**
* A vertex shaded cube.
*/
class Cube
{
public Cube()
{
int one = 0x10000;
int vertices[] = {
-one, -one, -one,
one, -one, -one,
one, one, -one,
-one, one, -one,
-one, -one, one,
one, -one, one,
one, one, one,
-one, one, one,
};
int colors[] = {
0, 0, 0, one,
one, 0, 0, one,
one, one, 0, one,
0, one, 0, one,
0, 0, one, one,
one, 0, one, one,
one, one, one, one,
0, one, one, one,
};
byte indices[] = {
0, 4, 5, 0, 5, 1,
1, 5, 6, 1, 6, 2,
2, 6, 7, 2, 7, 3,
3, 7, 4, 3, 4, 0,
4, 7, 6, 4, 6, 5,
3, 0, 1, 3, 1, 2
};
// Buffers to be passed to gl*Pointer() functions
// must be direct, i.e., they must be placed on the
// native heap where the garbage collector cannot
// move them.
//
// Buffers with multi-byte datatypes (e.g., short, int, float)
// must have their byte order set to native order
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
vbb.order(ByteOrder.nativeOrder());
mVertexBuffer = vbb.asIntBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);
ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);
cbb.order(ByteOrder.nativeOrder());
mColorBuffer = cbb.asIntBuffer();
mColorBuffer.put(colors);
mColorBuffer.position(0);
mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
mIndexBuffer.put(indices);
mIndexBuffer.position(0);
}
public void draw(GL10 gl)
{
gl.glFrontFace(GL10.GL_CW);
gl.glVertexPointer(3, GL10.GL_FIXED, 0, mVertexBuffer);
gl.glColorPointer(4, GL10.GL_FIXED, 0, mColorBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, 36, GL10.GL_UNSIGNED_BYTE, mIndexBuffer);
}
private IntBuffer mVertexBuffer;
private IntBuffer mColorBuffer;
private ByteBuffer mIndexBuffer;
}
@@ -1,114 +0,0 @@
package processing.android.opengl;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGL11;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;
import javax.microedition.khronos.opengles.GL;
import android.view.SurfaceHolder;
public class EglHelper {
EGL10 egl;
EGLDisplay eglDisplay;
EGLSurface eglSurface;
EGLConfig eglConfig;
EGLContext eglContext;
public EglHelper() { }
// Needs to be separate from the constructor, since it may be restarted.
public void start() {
// don't recreate if already started [fry]
if (egl == null) {
// Specify a configuration for our opengl session
// and grab the first configuration that matches is
int[] configSpec = {
EGL10.EGL_DEPTH_SIZE, 16,
EGL10.EGL_NONE
};
// Get an EGL instance
egl = (EGL10) EGLContext.getEGL();
// Get to the default display.
eglDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
// We can now initialize EGL for that display
int[] version = new int[2];
egl.eglInitialize(eglDisplay, version);
EGLConfig[] configs = new EGLConfig[1];
int[] num_config = new int[1];
egl.eglChooseConfig(eglDisplay, configSpec, configs, 1, num_config);
eglConfig = configs[0];
// Create an OpenGL ES context. This must be done only once, an
// OpenGL context is a somewhat heavy object.
eglContext = egl.eglCreateContext(eglDisplay, eglConfig,
EGL10.EGL_NO_CONTEXT, null);
eglSurface = null;
}
}
// Create and return an OpenGL surface
public GL createSurface(SurfaceHolder holder) {
// The window size has changed, so we need to create a new surface.
if (eglSurface != null) {
// Unbind and destroy the old EGL surface, if there is one.
egl.eglMakeCurrent(eglDisplay, EGL10.EGL_NO_SURFACE,
EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
egl.eglDestroySurface(eglDisplay, eglSurface);
}
// Create an EGL surface we can render into.
eglSurface = egl.eglCreateWindowSurface(eglDisplay,
eglConfig, holder, null);
// Before we can issue GL commands, we need to make sure
// the context is current and bound to a surface.
egl.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
return eglContext.getGL();
}
public boolean swap() {
egl.eglSwapBuffers(eglDisplay, eglSurface);
// Always check for EGL_CONTEXT_LOST, which means the context and all
// associated data were lost (For instance because the device went to
// sleep). We need to sleep until we get a new surface.
return egl.eglGetError() != EGL11.EGL_CONTEXT_LOST;
}
public void finish() {
if (eglSurface != null) {
egl.eglMakeCurrent(eglDisplay, EGL10.EGL_NO_SURFACE,
EGL10.EGL_NO_SURFACE,
EGL10.EGL_NO_CONTEXT);
egl.eglDestroySurface(eglDisplay, eglSurface);
eglSurface = null;
}
if (eglContext != null) {
egl.eglDestroyContext(eglDisplay, eglContext);
eglContext = null;
}
if (eglDisplay != null) {
egl.eglTerminate(eglDisplay);
eglDisplay = null;
}
// Clear this out so it's clear the restart is needed
egl = null;
}
}
@@ -1,48 +0,0 @@
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package processing.android.opengl;
public class GLColor {
public final int red;
public final int green;
public final int blue;
public final int alpha;
public GLColor(int red, int green, int blue, int alpha) {
this.red = red;
this.green = green;
this.blue = blue;
this.alpha = alpha;
}
public GLColor(int red, int green, int blue) {
this.red = red;
this.green = green;
this.blue = blue;
this.alpha = 0x10000;
}
public boolean equals(Object other) {
if (other instanceof GLColor) {
GLColor color = (GLColor)other;
return (red == color.red && green == color.green &&
blue == color.blue && alpha == color.alpha);
}
return false;
}
}
@@ -1,94 +0,0 @@
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package processing.android.opengl;
import android.util.Log;
import java.nio.ShortBuffer;
import java.util.ArrayList;
public class GLFace {
public GLFace() {
}
// for triangles
public GLFace(GLVertex v1, GLVertex v2, GLVertex v3) {
addVertex(v1);
addVertex(v2);
addVertex(v3);
}
// for quadrilaterals
public GLFace(GLVertex v1, GLVertex v2, GLVertex v3, GLVertex v4) {
addVertex(v1);
addVertex(v2);
addVertex(v3);
addVertex(v4);
}
public void addVertex(GLVertex v) {
mVertexList.add(v);
}
// must be called after all vertices are added
public void setColor(GLColor c) {
int last = mVertexList.size() - 1;
if (last < 2) {
Log.e("GLFace", "not enough vertices in setColor()");
} else {
GLVertex vertex = mVertexList.get(last);
// only need to do this if the color has never been set
if (mColor == null) {
while (vertex.color != null) {
mVertexList.add(0, vertex);
mVertexList.remove(last + 1);
vertex = mVertexList.get(last);
}
}
vertex.color = c;
}
mColor = c;
}
public int getIndexCount() {
return (mVertexList.size() - 2) * 3;
}
public void putIndices(ShortBuffer buffer) {
int last = mVertexList.size() - 1;
GLVertex v0 = mVertexList.get(0);
GLVertex vn = mVertexList.get(last);
// push triangles into the buffer
for (int i = 1; i < last; i++) {
GLVertex v1 = mVertexList.get(i);
buffer.put(v0.index);
buffer.put(v1.index);
buffer.put(vn.index);
v0 = v1;
}
}
private ArrayList<GLVertex> mVertexList = new ArrayList<GLVertex>();
private GLColor mColor;
}
@@ -1,102 +0,0 @@
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package processing.android.opengl;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.Iterator;
public class GLShape {
public GLShape(GLWorld world) {
mWorld = world;
}
public void addFace(GLFace face) {
mFaceList.add(face);
}
public void setFaceColor(int face, GLColor color) {
mFaceList.get(face).setColor(color);
}
public void putIndices(ShortBuffer buffer) {
Iterator<GLFace> iter = mFaceList.iterator();
while (iter.hasNext()) {
GLFace face = iter.next();
face.putIndices(buffer);
}
}
public int getIndexCount() {
int count = 0;
Iterator<GLFace> iter = mFaceList.iterator();
while (iter.hasNext()) {
GLFace face = iter.next();
count += face.getIndexCount();
}
return count;
}
public GLVertex addVertex(float x, float y, float z) {
// look for an existing GLVertex first
Iterator<GLVertex> iter = mVertexList.iterator();
while (iter.hasNext()) {
GLVertex vertex = iter.next();
if (vertex.x == x && vertex.y == y && vertex.z == z) {
return vertex;
}
}
// doesn't exist, so create new vertex
GLVertex vertex = mWorld.addVertex(x, y, z);
mVertexList.add(vertex);
return vertex;
}
public void animateTransform(M4 transform) {
mAnimateTransform = transform;
if (mTransform != null)
transform = mTransform.multiply(transform);
Iterator<GLVertex> iter = mVertexList.iterator();
while (iter.hasNext()) {
GLVertex vertex = iter.next();
mWorld.transformVertex(vertex, transform);
}
}
public void startAnimation() {
}
public void endAnimation() {
if (mTransform == null) {
mTransform = new M4(mAnimateTransform);
} else {
mTransform = mTransform.multiply(mAnimateTransform);
}
}
public M4 mTransform;
public M4 mAnimateTransform;
protected ArrayList<GLFace> mFaceList = new ArrayList<GLFace>();
protected ArrayList<GLVertex> mVertexList = new ArrayList<GLVertex>();
protected ArrayList<Integer> mIndexList = new ArrayList<Integer>(); // make more efficient?
protected GLWorld mWorld;
}
@@ -1,88 +0,0 @@
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package processing.android.opengl;
import java.nio.IntBuffer;
public class GLVertex {
public float x;
public float y;
public float z;
final short index; // index in vertex table
GLColor color;
GLVertex() {
this.x = 0;
this.y = 0;
this.z = 0;
this.index = -1;
}
GLVertex(float x, float y, float z, int index) {
this.x = x;
this.y = y;
this.z = z;
this.index = (short)index;
}
public boolean equals(Object other) {
if (other instanceof GLVertex) {
GLVertex v = (GLVertex)other;
return (x == v.x && y == v.y && z == v.z);
}
return false;
}
static public int toFixed(float x) {
return (int)(x*65536.0f);
}
public void put(IntBuffer vertexBuffer, IntBuffer colorBuffer) {
vertexBuffer.put(toFixed(x));
vertexBuffer.put(toFixed(y));
vertexBuffer.put(toFixed(z));
if (color == null) {
colorBuffer.put(0);
colorBuffer.put(0);
colorBuffer.put(0);
colorBuffer.put(0);
} else {
colorBuffer.put(color.red);
colorBuffer.put(color.green);
colorBuffer.put(color.blue);
colorBuffer.put(color.alpha);
}
}
public void update(IntBuffer vertexBuffer, M4 transform) {
// skip to location of vertex in mVertex buffer
vertexBuffer.position(index * 3);
if (transform == null) {
vertexBuffer.put(toFixed(x));
vertexBuffer.put(toFixed(y));
vertexBuffer.put(toFixed(z));
} else {
GLVertex temp = new GLVertex();
transform.multiply(this, temp);
vertexBuffer.put(toFixed(temp.x));
vertexBuffer.put(toFixed(temp.y));
vertexBuffer.put(toFixed(temp.z));
}
}
}
@@ -1,98 +0,0 @@
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package processing.android.opengl;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import java.util.Iterator;
import java.util.ArrayList;
import javax.microedition.khronos.opengles.GL10;
public class GLWorld {
public void addShape(GLShape shape) {
mShapeList.add(shape);
mIndexCount += shape.getIndexCount();
}
public void generate() {
ByteBuffer bb = ByteBuffer.allocateDirect(mVertexList.size()*4*4);
bb.order(ByteOrder.nativeOrder());
mColorBuffer = bb.asIntBuffer();
bb = ByteBuffer.allocateDirect(mVertexList.size()*4*3);
bb.order(ByteOrder.nativeOrder());
mVertexBuffer = bb.asIntBuffer();
bb = ByteBuffer.allocateDirect(mIndexCount*2);
bb.order(ByteOrder.nativeOrder());
mIndexBuffer = bb.asShortBuffer();
Iterator<GLVertex> iter2 = mVertexList.iterator();
while (iter2.hasNext()) {
GLVertex vertex = iter2.next();
vertex.put(mVertexBuffer, mColorBuffer);
}
Iterator<GLShape> iter3 = mShapeList.iterator();
while (iter3.hasNext()) {
GLShape shape = iter3.next();
shape.putIndices(mIndexBuffer);
}
}
public GLVertex addVertex(float x, float y, float z) {
GLVertex vertex = new GLVertex(x, y, z, mVertexList.size());
mVertexList.add(vertex);
return vertex;
}
public void transformVertex(GLVertex vertex, M4 transform) {
vertex.update(mVertexBuffer, transform);
}
int count = 0;
public void draw(GL10 gl)
{
mColorBuffer.position(0);
mVertexBuffer.position(0);
mIndexBuffer.position(0);
gl.glFrontFace(GL10.GL_CW);
gl.glShadeModel(GL10.GL_FLAT);
gl.glVertexPointer(3, GL10.GL_FIXED, 0, mVertexBuffer);
gl.glColorPointer(4, GL10.GL_FIXED, 0, mColorBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, mIndexCount, GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
count++;
}
static public float toFloat(int x) {
return x/65536.0f;
}
private ArrayList<GLShape> mShapeList = new ArrayList<GLShape>();
private ArrayList<GLVertex> mVertexList = new ArrayList<GLVertex>();
private int mIndexCount = 0;
private IntBuffer mVertexBuffer;
private IntBuffer mColorBuffer;
private ShortBuffer mIndexBuffer;
}
@@ -1,80 +0,0 @@
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package processing.android.opengl;
/**
*
* A 4x4 float matrix
*
*/
public class M4 {
public float[][] m = new float[4][4];
public M4() {
}
public M4(M4 other) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
m[i][j] = other.m[i][j];
}
}
}
public void multiply(GLVertex src, GLVertex dest) {
dest.x = src.x * m[0][0] + src.y * m[1][0] + src.z * m[2][0] + m[3][0];
dest.y = src.x * m[0][1] + src.y * m[1][1] + src.z * m[2][1] + m[3][1];
dest.z = src.x * m[0][2] + src.y * m[1][2] + src.z * m[2][2] + m[3][2];
}
public M4 multiply(M4 other) {
M4 result = new M4();
float[][] m1 = m;
float[][] m2 = other.m;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
result.m[i][j] = m1[i][0]*m2[0][j] + m1[i][1]*m2[1][j] + m1[i][2]*m2[2][j] + m1[i][3]*m2[3][j];
}
}
return result;
}
public void setIdentity() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
m[i][j] = (i == j ? 1f : 0f);
}
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder("[ ");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
builder.append(m[i][j]);
builder.append(" ");
}
if (i < 2)
builder.append("\n ");
}
builder.append(" ]");
return builder.toString();
}
}