?? glcube.java
字號:
/*** * Excerpted from "Hello, Android!", * published by The Pragmatic Bookshelf. * Copyrights apply to this code. It may not be used to create training material, * courses, books, articles, and the like. Contact us if you are in doubt. * We make no guarantees that this code is fit for any purpose. * Visit http://www.pragmaticprogrammer.com/titles/eband for more book information.***/
package com.mot.opengl;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import javax.microedition.khronos.opengles.GL;import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;import javax.microedition.khronos.opengles.GL11Ext;import javax.microedition.khronos.opengles.GL11ExtensionPack;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;import android.widget.Toast;
public class GLCube {
private final IntBuffer mVertexBuffer;
private final IntBuffer mTextureBuffer;
private static int[] textures = new int[6];
public GLCube() {
int one = 65536;
int half = one / 2;
int vertices[] = { // FRONT
-half, -half, half, half, -half, half,
-half, half, half, half, half, half,
// BACK
-half, -half, -half, -half, half, -half,
half, -half, -half, half, half, -half,
// LEFT
-half, -half, half, -half, half, half,
-half, -half, -half, -half, half, -half,
// RIGHT
half, -half, -half, half, half, -half,
half, -half, half, half, half, half,
// TOP
-half, half, half, half, half, half,
-half, half, -half, half, half, -half,
// BOTTOM
-half, -half, half, -half, -half, -half,
half, -half, half, half, -half, -half, };
int texCoords[] = {
// FRONT
0, one, one, one, 0, 0, one, 0,
// BACK
one, one, one, 0, 0, one, 0, 0,
// LEFT
one, one, one, 0, 0, one, 0, 0,
// RIGHT
one, one, one, 0, 0, one, 0, 0,
// TOP
one, 0, 0, 0, one, one, 0, one,
// BOTTOM
0, 0, 0, one, one, 0, one, one, };
// 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 data types (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 tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
tbb.order(ByteOrder.nativeOrder());
mTextureBuffer = tbb.asIntBuffer();
mTextureBuffer.put(texCoords);
mTextureBuffer.position(0);
}
public void draw(GL10 gl) { //Bind diffirent face /* gl.glBindTexture(GL11ExtensionPack.GL_TEXTURE_CUBE_MAP_POSITIVE_X, textures[0]); // gl.glBindTexture(GL11ExtensionPack.GL_TEXTURE_CUBE_MAP_POSITIVE_Y, textures[1]); // gl.glBindTexture(GL11ExtensionPack.GL_TEXTURE_CUBE_MAP_POSITIVE_Z, textures[2]); // gl.glBindTexture(GL11ExtensionPack.GL_TEXTURE_CUBE_MAP_NEGATIVE_X, textures[3]); // gl.glBindTexture(GL11ExtensionPack.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, textures[4]); // gl.glBindTexture(GL11ExtensionPack.GL_TEXTURE_CUBE_MAP_POSITIVE_Z, textures[5]); // */ gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, mTextureBuffer); gl.glVertexPointer(3, GL10.GL_FIXED, 0, mVertexBuffer);
gl.glColor4f(1, 1, 1, 1);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); // gl.glNormal3f(0, 0, 1);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[1]); // gl.glNormal3f(0, 0, -1);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);
gl.glColor4f(1, 1, 1, 1);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[2]); // gl.glNormal3f(-1, 0, 0);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[3]); // gl.glNormal3f(1, 0, 0);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);
gl.glColor4f(1, 1, 1, 1);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[4]); // gl.glNormal3f(0, 1, 0);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[5]); // gl.glNormal3f(0, -1, 0);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4); }
public static ByteBuffer extract(Bitmap bmp) { ByteBuffer bb = ByteBuffer.allocateDirect(bmp.getHeight()
* bmp.getWidth() * 4);
bb.order(ByteOrder.BIG_ENDIAN);
IntBuffer ib = bb.asIntBuffer();
// Convert ARGB -> RGBA
for (int y = bmp.getHeight() - 1; y > -1; y--)
for (int x = 0; x < bmp.getWidth(); x++) {
int pix = bmp.getPixel(x, bmp.getHeight() - y - 1);
// int alpha = ((pix >> 24) & 0xFF);
int red = ((pix >> 16) & 0xFF);
int green = ((pix >> 8) & 0xFF);
int blue = ((pix) & 0xFF);
// Make up alpha for interesting effect
ib.put(red << 24 | green << 16 | blue << 8
| ((red + blue + green) / 3));
}
bb.position(0);
return bb;
}
public static void loadTexture(GL10 gl, Context context, int[] resource) { /* int[] cube_face = { GL11ExtensionPack.GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL11ExtensionPack.GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL11ExtensionPack.GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL11ExtensionPack.GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL11ExtensionPack.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL11ExtensionPack.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z }; int len = resource.length; ByteBuffer[] bb = new ByteBuffer[len]; int[] tmp_tex = new int[1]; gl.glGenTextures(1, tmp_tex, 0); int tex = tmp_tex[0]; int i; for ( i=0; i<len; i++ ) { Bitmap bmp = BitmapFactory.decodeResource( context.getResources(), resource[i]); bb[i] = extract(bmp); } int width = 128; int height = 128; // Load it up gl.glBindTexture(GL11ExtensionPack.GL_TEXTURE_CUBE_MAP, tex); gl.glTexParameterx(GL11ExtensionPack.GL_TEXTURE_CUBE_MAP, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE); gl.glTexParameterx(GL11ExtensionPack.GL_TEXTURE_CUBE_MAP, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE); //gl.glTexParameterx(GL11ExtensionPack.GL_TEXTURE_CUBE_MAP, // GL10.GL_TEXTURE_WRAP_R, GL10.GL_CLAMP_TO_EDGE); gl.glTexParameterx(GL11ExtensionPack.GL_TEXTURE_CUBE_MAP, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL11ExtensionPack.GL_TEXTURE_CUBE_MAP, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR_MIPMAP_LINEAR); gl.glTexParameterx(GL11ExtensionPack.GL_TEXTURE_CUBE_MAP, GL11.GL_GENERATE_MIPMAP, GL10.GL_TRUE); for ( i=0; i<len; i++ ) { gl.glTexImage2D(cube_face[i], 0, GL10.GL_RGBA, width, height, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb[i]); } */ int len = resource.length; ByteBuffer[] bb = new ByteBuffer[len]; int i; for ( i=0; i<len; i++ ) { Bitmap bmp = BitmapFactory.decodeResource( context.getResources(), resource[i]); bb[i] = extract(bmp); } int width = 128; int height = 128; for (i=0;i<6;i++) { gl.glGenTextures(1, textures, i); //gl.glGenTextures(1, tmp_tex, i); gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[i]); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, width, height, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb[i]); } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -