我被这个错误难住了:
.MainActivity (server)' ~ Consumer closed input channel or
an error occurred. events=0x8
项目编译并开始运行,我看着其他人有这个错误,它似乎有时发生当你的图纸超出可接受的范围,但我找不到任何错误在我的代码。
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;
class Square {
public Square()
{
float vertices[] =
{
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f
};
byte maxColor = (byte)255;
byte colors[] =
{
maxColor, maxColor, 0,maxColor,
0, maxColor,maxColor,maxColor,
0, 0, 0,maxColor,
maxColor, 0,maxColor,maxColor,
maxColor, 0, 0,maxColor,
0, maxColor, 0,maxColor,
0, 0,maxColor,maxColor,
0, 0, 0,maxColor,
};
//byte indices[] =
//{
// 0, 3, 1,
// 0, 2, 3
//};
byte tfan1[] =
{
1,0,3,
1,3,2,
1,2,6,
1,6,5,
1,5,4,
1,4,0
};
byte tfan2[] =
{
7,4,5,
7,5,6,
7,6,2,
7,2,3,
7,3,0,
7,0,4
};
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
mFVertexBuffer = vbb.asFloatBuffer();
mFVertexBuffer.put(vertices);
mFVertexBuffer.position(0);
mColorBuffer = ByteBuffer.allocateDirect(colors.length);
mColorBuffer.put(colors);
mColorBuffer.position(0);
mIndexBuffer = ByteBuffer.allocateDirect(tfan1.length);
mIndexBuffer.put(tfan1);
mIndexBuffer.position(0);
mTfan2 = ByteBuffer.allocateDirect(tfan2.length);
mTfan2.put(tfan2);
mTfan2.position(0);
}
public void draw(GL10 gl)
{
gl.glVertexPointer(3, GL11.GL_FLOAT, 0, mFVertexBuffer);
gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer);
gl.glDrawElements(GL10.GL_TRIANGLE_FAN, 6*3, GL10.GL_UNSIGNED_BYTE, mTfan1);
gl.glDrawElements(GL10.GL_TRIANGLE_FAN, 6*3, GL10.GL_UNSIGNED_BYTE, mTfan2);
gl.glFrontFace(GL11.GL_CCW);
}
private FloatBuffer mFVertexBuffer;
private ByteBuffer mColorBuffer;
private ByteBuffer mIndexBuffer;
private ByteBuffer mTfan1;
private ByteBuffer mTfan2;
}
那么下面的代码:
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView;
import java.lang.Math;
public class SquareRenderer implements GLSurfaceView.Renderer
{
public SquareRenderer(boolean useTranslucentBackground)
{
mTranslucentBackground = useTranslucentBackground;
mSquare = new Square();
}
public void onDrawFrame(GL10 gl)
{
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(0.0f, 0.5f, 0.5f, 1.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glRotatef(mAngle, 0.0f, 1.0f, 0.0f);
gl.glRotatef(mAngle, 1.0f, 0.0f, 0.0f);
gl.glTranslatef(0.0f,(float)Math.sin(mTransY), -7.0f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
mSquare.draw(gl);
mTransY += 0.075f;
mAngle += 0.4;
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
gl.glViewport(0,0,width,height);
float aspectRatio;
float zNear = 0.1f;
float zFar = 1000;
float fieldOfView = 30.0f/57.3f;
float size;
gl.glEnable(GL10.GL_NORMALIZE);
aspectRatio = (float)width/(float)height;
// float ratio = (float) width / height;
gl.glMatrixMode(GL10.GL_PROJECTION);
size = zNear * (float)(Math.tan((double)(fieldOfView/2.0f)));
gl.glFrustumf(-size, size, -size / aspectRatio, size / aspectRatio, zNear, zFar);
gl.glMatrixMode(GL10.GL_MODELVIEW);
// gl.glLoadIdentity();
// gl.glFrustumf(-ratio, ratio, -1,1,1,10);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
if (mTranslucentBackground)
{
gl.glClearColor(0, 0, 0, 0);
} else {
gl.glClearColor(1, 1, 1, 1);
}
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
}
private boolean mTranslucentBackground;
private Square mSquare;
private float mTransY;
private float mAngle;
}
我用这个来显示它:
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.WindowManager;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
GLSurfaceView view = new GLSurfaceView(this);
view.setRenderer(new SquareRenderer(true));
setContentView(view);
}
}
我发现这是因为我关闭了模拟器,或者在程序运行之前它没有启动。