在虚拟设备上一切正常。哪里会出问题?设备 - Nexus。也许我需要清单中的一些功能,或者一些 glFunction 错误或纹理格式。我使用 PNG 纹理(无论有无 alpha) 代码的主要部分:
活动:
package com.example;
//imports
public class wheel extends Activity {
private GLSurfaceView mGLSurfaceView;
// some buttons, views
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
mGLSurfaceView = (TouchSurfaceView)findViewById(R.id.glsurfaceview);
mGLSurfaceView.requestFocus();
mGLSurfaceView.setFocusableInTouchMode(true);
mGLSurfaceView.requestRender();
// other interface
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
}
表面视图:
package com.example;
//imports
public class TouchSurfaceView extends GLSurfaceView {
public TouchSurfaceView(Context context) {
super(context);
mRenderer = new CylinderRenderer(context);
setEGLConfigChooser(8, 8, 8, 8, 16, 0);
getHolder().setFormat(PixelFormat.TRANSLUCENT);
setRenderer(mRenderer);
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
public TouchSurfaceView(Context context, AttributeSet attr) {
super(context, attr);
mRenderer = new CylinderRenderer(context);
setEGLConfigChooser(8, 8, 8, 8, 16, 0);
getHolder().setFormat(PixelFormat.TRANSLUCENT);
setRenderer(mRenderer);
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
@Override
public boolean onTrackballEvent(MotionEvent e) {
// some rotation
requestRender();
return true;
}
@Override
public boolean onTouchEvent(MotionEvent e) {
// some rotation
return true;
}
private class CylinderRenderer implements GLSurfaceView.Renderer {
public CylinderRenderer(Context context) {
mCylinder = new Cylinder(0);
}
public void onDrawFrame(GL10 gl) {
gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0, 0.3f, -2.5f);
gl.glScalef(1.0f, 1.0f, 0.5f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glActiveTexture(GL10.GL_TEXTURE0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID[5]);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
mCylinder.draw(gl);
requestRender();
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
float ratio = (float) width / height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
}
private void loadTextures(GL10 gl) {
int[] textures = new int[6];
mTextureID = new int[6];
int[] resources = {R.raw.a, R.raw.b, R.raw.c, R.drawable.d, R.drawable.e, R.drawable.f};
gl.glGenTextures(6, textures, 0);
for(int i = 0; i < 6; i++)
{
mTextureID[i] = textures[i];
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID[i]);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);
InputStream is = mContext.getResources().openRawResource(resources[i]);
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(is);
} finally {
try {
is.close();
} catch(IOException e) {
}
}
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
}
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(0.0f, 0.0f, 0.0f, 0);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_TEXTURE_2D);
loadTextures(gl);
requestRender();
}
private Cylinder mCylinder;
private Context mContext;
private int mTextureID[];
}
private CylinderRenderer mRenderer;
}
圆筒:
package com.example;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
public class Cylinder
{
private final static int verticesCount = 40;
private final static int indicesCount = verticesCount * 3;
private FloatBuffer mVertexBuffer;
private FloatBuffer mTexBuffer;
private ByteBuffer mIndexBuffer;
private float vertices[];
public Cylinder(int type)
{
vertices = new float[(verticesCount + 1) * 12];
byte indices[] = new byte[indicesCount * 2];
float texture[] = new float[verticesCount * 8 + 1];
float tx, ty, dx, size, dz, dy;
// generate indices, texture, vertices
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
mVertexBuffer = vbb.asFloatBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);
ByteBuffer tbb = ByteBuffer.allocateDirect(texture.length * 4);
tbb.order(ByteOrder.nativeOrder());
mTexBuffer = tbb.asFloatBuffer();
mTexBuffer.put(texture);
mTexBuffer.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_FLOAT, 0, mVertexBuffer);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, indicesCount * 2, GL10.GL_UNSIGNED_BYTE, mIndexBuffer);
}
}