我的程序是在触摸屏幕时出现一个白色方块并沿 za 轴移动。
我的目标和问题是,我还希望在一次又一次地触摸屏幕时再次发生这种情况。
出于某种原因添加 invalidate(); square.draw 后不起作用。我还尝试在活动类中创建一个线程来锁定和解锁画布。我不太确定我在做什么,如果有人能指出我正确的方向,那就太好了!
这是我的活动课程中的 GLRendering 课程
public class GL1Activity extends Activity implements OnTouchListener {
GLSurfaceView ourSurface;
float x,y;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ourSurface = new GLSurfaceView(this);
ourSurface.setOnTouchListener(this);
x=0;
y=0;
ourSurface.setRenderer(new GL1Renderer()); //links to GL1Renderer
setContentView(ourSurface);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSurface.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
ourSurface.onResume();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
x = event.getX();
y = event.getY();
return false;
}
public class GL1Renderer implements Renderer {
private GL1Square square;
float zoomz;
public GL1Renderer (){
square = new GL1Square();
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig eglconfig) {
// TODO Auto-generated method stub
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glClearColor(.1f, .1f, .1f, 1); //painting background
gl.glClearDepthf(1f);
}
@Override
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 0, -5, 0, 0, 0, 0, 3, 0);
if (x != 0 && y != 0){
gl.glTranslatef(0, 0, zoomz);
if (zoomz < 50) {
zoomz-=.1;
}
if (zoomz > 50){
zoomz = 0;
}
square.draw(gl);
};
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
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, 25);
}
}
这是方码;
public class GL1Square {
private float vertices[] = {
-1.0f, 1.0f,
1.0f, 1.0f,
1.0f, -1.0f,
-1.0f, -1.0f,
};
private short[] pIndex = { 0, 1, 2, 0, 2, 3};
private ShortBuffer pBuff;
private FloatBuffer vertBuff;
public GL1Square(){
ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length * 4);
bBuff.order(ByteOrder.nativeOrder());
vertBuff = bBuff.asFloatBuffer();
vertBuff.put(vertices);
vertBuff.position(0);
ByteBuffer pbBuff = ByteBuffer.allocateDirect(pIndex.length * 2);
pbBuff.order(ByteOrder.nativeOrder());
pBuff = pbBuff.asShortBuffer();
pBuff.put(pIndex);
pBuff.position(0);
}
public void draw(GL10 gl){
gl.glFrontFace(GL10.GL_CW);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glCullFace(GL10.GL_FRONT);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertBuff);
gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length, GL10.GL_UNSIGNED_SHORT, pBuff);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisable(GL10.GL_CULL_FACE);
}
问候