我已经从Android下载并运行了opengl示例。现在,我尝试在用户移动手指的同时使用opengl es 2.0。到目前为止,这是我所做的:
在我的GlSurfaceView类的onTouchEvent方法上:
@Override
public boolean onTouchEvent(MotionEvent e) {
mScaleDetector.onTouchEvent(e);
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN: {
float x = e.getX();
float y = e.getY();
mPreviousX = x;
mPreviousY = y;
// Save the ID of this pointer
mActivePointerId = e.getPointerId(0);
break;
}
case MotionEvent.ACTION_MOVE: {
final int pointerIndex = e.findPointerIndex(mActivePointerId);
final float x = e.getX(pointerIndex);
final float y = e.getY(pointerIndex);
if (!mScaleDetector.isInProgress()) {
mRenderer.setLineCoordinates(mPreviousX, mPreviousY, x, y);
requestRender();
}
mPreviousX = x;
mPreviousY = y;
break;
}
case MotionEvent.ACTION_UP: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
final int pointerIndex = (e.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = e.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mPreviousX = e.getX(newPointerIndex);
mPreviousY = e.getY(newPointerIndex);
mActivePointerId = e.getPointerId(newPointerIndex);
}
break;
}
}
return true;
}
在我的渲染器类上:
public void onDrawFrame(GL10 gl) {
// Draw background color
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// Set GL_MODELVIEW transformation mode
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity(); // reset the matrix to its default state
// When using GL_MODELVIEW, you must set the view point
GLU.gluLookAt(gl, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
gl.glScalef(factor, factor, 0.0f);
// Draw triangle
mTriangle.draw(gl);
mLine.draw(gl);
}
渲染器类中的函数来更改坐标:
public void setLineCoordinates(float mPreviousX
, float mPreviousY, float x, float y) {
// TODO Auto-generated method stub
lineCoords[0] = (float) (mPreviousX * 2.0 / WIDTH - 1.0);
lineCoords[1] = (float) (mPreviousY * -2.0 / HEIGHT + 1.0);
lineCoords[2] = 0.0f;
lineCoords[3] = (float) (x * 2.0 / WIDTH - 1.0);
lineCoords[4] = (float) (y * -2.0 / HEIGHT + 1.0);
lineCoords[5] = 0.0f;
}
当我移动手指时,没有线,只有一条移动的细线...
在我不断移动手指的同时,如何绘制整条线条并保持线条绘制?当我再次触摸屏幕时,也开始换行。
更新
感谢@Gil Moshayof,我现在可以用我的版本画线
public void setLineCoordinates(float mPreviousX
, float mPreviousY, float x, float y) {
// TODO Auto-generated method stub
float lineCoords[] = new float[6];
lineCoords[0] = (float) (mPreviousX * 2.0 / WIDTH - 1.0);
lineCoords[1] = (float) (mPreviousY * -2.0 / HEIGHT + 1.0);
lineCoords[2] = 0.0f;
lineCoords[3] = (float) (x * 2.0 / WIDTH - 1.0);
lineCoords[4] = (float) (y * -2.0 / HEIGHT + 1.0);
lineCoords[5] = 0.0f;
bufferOfArrays.add(new Line(lineCoords));
if(isFirst) {
isFirst = false;
listOfArrays.addAll(bufferOfArrays);
view.requestRender();
}
}
public void setFirst(boolean isFirst) {
this.isFirst = isFirst;
}
接着:
@Override
public void onDrawFrame(GL10 gl) {
//... color, matrix, look at etc..
// Draw triangle
//mTriangle.draw(gl);
//TODO mLine.draw(gl);
for(Line line : listOfArrays) {
line.draw(gl);
}
isFirst = true;
}
但是我的应用程序在移动手指时开始变慢,但仍然不知道发生了什么
最佳答案
这里的问题是,您总是在每次移动时都设置mPreviousX和mPreviousY。实际上,问题始于为这些参数选择的名称。更好的名称是mLineStartX / Y。
在触地事件中设置mLineStartX / Y,然后在每次移动时更新x / y,并且不要更改mLineStartX / Y。
至于添加多行,您将需要实现更多的逻辑。您需要维护定义的某个类的数组列表,该类具有2点(2xPointFs)。用户抬起手指(触摸/取消触摸),使用2个点创建一个新类,然后在渲染方法上,确保您遍历所有此类线条并进行渲染。