Java openglgltranslatef和glScalef
我正在尝试实现一个平移和收缩功能,以便将其放大到我拥有的OpenGL应用程序中。我能够用一个简单的Java openglgltranslatef和glScalef,java,android,opengl-es,Java,Android,Opengl Es,我正在尝试实现一个平移和收缩功能,以便将其放大到我拥有的OpenGL应用程序中。我能够用一个简单的glTranslatef相对容易地实现pan功能,但是使用glScalef函数要困难得多。我有三个类,一个用于GLSurfaceView,一个用于GLTriangle,第三个(下面)用于GLRenderer,这是使用glScalef和glTranslatef的地方 GLRendererClass: public class GLRendererEx implements Renderer, OnTo
glTranslatef
相对容易地实现pan功能,但是使用glScalef
函数要困难得多。我有三个类,一个用于GLSurfaceView
,一个用于GLTriangle
,第三个(下面)用于GLRenderer
,这是使用glScalef
和glTranslatef
的地方
GLRenderer
Class:
public class GLRendererEx implements Renderer, OnTouchListener {
private GLTriangleEx tri;
float ratio;
float x = 0, y = 0;
float dx = 0, dy = 0;
float sx = 0, sy = 0;
float tx = 0, ty = 0;
float xtwo = 0, ytwo = 0;
float sxtwo = 0, sytwo = 0;
float screenX, screenY;
float xscale = 1, yscale = 1;
float xscaletwo = 1;
float xscaletotal = 1, yscaletotal = 1;
int NONE = 0, DRAG = 1, ZOOM = 2;
int mode = NONE;
int width, height;
boolean touched = true;
Display getOrient = getWindowManager().getDefaultDisplay();
int orientation;
public GLRendererEx() {
tri = new GLTriangleEx();
screenX = ourSurface.getWidth();
screenY = ourSurface.getHeight();
ourSurface.setOnTouchListener(this);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glClearColor(0f, 0f, 0f, 1f);
gl.glClearDepthf(1f);
orientation = getOrient.getRotation();
}
@Override
public void onDrawFrame(GL10 gl) {
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, 2, 0);
//warning - x is backwards
gl.glTranslatef(tx, ty, 0);
gl.glScalef(xscaletotal, yscaletotal, 1);
tri.draw(gl);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
ratio = ((float) width) / height;
screenX = width;
screenY = height;
orientation = getOrient.getRotation();
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 50);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
event.getActionIndex();
sx = x = event.getX(0);
sy = y = event.getY(0);
mode = DRAG;
break;
}
case MotionEvent.ACTION_POINTER_DOWN: {
event.getActionIndex();
sxtwo = xtwo = event.getX(1);
sytwo = ytwo = event.getY(1);
mode = ZOOM;
touched = true;
break;
}
case MotionEvent.ACTION_MOVE: {
x = event.getX(0);
y = event.getY(0);
xtwo = event.getX(1);
ytwo = event.getY(1);
if (mode == ZOOM)
midPoint(x, y, xtwo, ytwo);
if (mode == DRAG) {
dx = x - sx;
dy = y - sy;
dx *= -8;
dy *= -8;
if (orientation == Configuration.ORIENTATION_LANDSCAPE)
dx *= 2;
dx /= screenX;
dy /= screenY;
tx += dx;
ty += dy;
sx = x;
sy = y;
}
break;
}
case MotionEvent.ACTION_POINTER_UP: {
mode = DRAG;
break;
}
case MotionEvent.ACTION_UP: {
mode = NONE;
break;
}
}
return true;
}
private float midPoint(float x, float y, float xtwo, float ytwo) {
x -= xtwo;
y -= ytwo;
if (touched) {
sxtwo = x;
sytwo = y;
xscaletwo = FloatMath.sqrt(sxtwo * sxtwo + sytwo * sytwo);
touched = false;
}
xscale -= xscaletwo;
xscale = FloatMath.sqrt(x * x + y * y);
xscaletotal += xscale;
yscaletotal = xscaletotal;
return xscale;
}
}
x/y
是坐标,dx/dy
是坐标的变化,sx/sy
是起始坐标,tx/ty
是坐标的总变化(用于平移),xscale/yscale
是用于计算缩放比例的浮动,xscaletotal/yscalettal
是缩放比例的总变化,无拖动缩放和模式表示模式
就目前情况而言,我可以通过分开手指来放大,但捏一下也会放大而不是缩小。而且,变焦太敏感了
我真的不知道如何从这里解决这个问题。任何帮助都将不胜感激。看来你的“中点”还有一些工作要做。。尝试这样做:
float scale = 1.0; //factor used in glScalef
float previousPinchScale; //needed to remember from previous methode call
private float midPoint(float x, float y, float xtwo, float ytwo) {
float currentPinchScale = FloatMath.sqrt((x-xTwo)*(x-xTwo) + (y-yTwo)*(y-yTwo)); //current distance between both "touches"
if (touched) { //gesture just began:
previousPinchScale = currentPinchScale; //Save relative information about touch positions
touched = false;
}
else { //gesture in progress:
/*
At this point we have 2 values of "PinchScale" for this and previous "midPoint" method call.
The factor of relative change in this distance should be applied to current scene scale:
currentPinchScale/previousPinchScale is the "relative change", for instance if the distance between the 2 touches would be 10% this factor would return 1.1
Since we have to consider that scene might already be zoomed you need to take previous scale and multiply it with this change..
*/
scale *= currentPinchScale/previousPinchScale; //scale used in glScalef
previousPinchScale = currentPinchScale; //remember the new position of "midPoint" call
}
return scale;
}
我完全惊讶于这个答案的简单性,尽管现在我遇到了另一个我不太理解的问题。进行捏缩变焦的两次触屏必须同步到令人难以置信的程度,以至于我的测试设备(Galaxy S)通常无法检测到多点触屏,而我更快的实际手机(Galaxy S3)只能检测到每100次捏屏中的一次。我如何使“触摸时间”窗口变大以使其更容易挤压?我不认为这些事件与触摸时间窗口有多大关系。。这样做的最佳方式可能是在触摸开始、移动和结束时获取事件,或者能够在任何给定时间获取所有触摸及其位置,并不断检查更改。即使你得到了这两个动作中的任何一个,你仍然需要一个好的系统来识别手势本身。。。另一方面,这些手势识别器已经是为android编写的,那么为什么不使用它们呢?尽管有标题,问题实际上是正确计算捏缩/缩放手势的比例,而不是正确使用glScaleF。