移动opengl的视点以在屏幕上找到对象

时间:2015-10-19 03:21:07

标签: android opengl-es-2.0 perspectivecamera

我有一个物体在我的opengl表面移动,它有时会离开屏幕,所以我想知道你如何改变你在世界的哪个地方,所以我可以找到我的物体,因为它正在移动。

这是我的方形对象的渲染器

class SquareRenderer implements GLSurfaceView.Renderer {

private int counter = 0;
private boolean mTranslucentBackground;
private Square mSquare;
private float mTransY;
private float mTransX;
private float mAngle;
private Context context;

private float xPoint = 0.0f;
private float yPoint = 0.0f;


public SquareRenderer(boolean useTranslucentBackground,Context context) {
    mTranslucentBackground = useTranslucentBackground;
    this.context=context;
    mSquare = new Square();
}

public void onDrawFrame(GL10 gl) {
    gl.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    gl.glMatrixMode(GL11.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glTranslatef(mTransX, mTransY, -10.0f);


    mSquare.draw(gl);
    GLU.gluLookAt(gl, 0.0f, 0.0f, 0.0f, (float) MainActivity.azimuth, (float) MainActivity.roll, 0.0f, 0.0f, 1, 0.0f);
    Random rnd = new Random();

    float maxx = 180.0f;
    float minx = -180.0f;
    float maxy = 90.0f;
    float miny = -90.0f;

    if( xPoint == 0.0f && yPoint == 0.0f){
        xPoint = rnd.nextFloat()*(maxx-minx)+minx;
        xPoint = round(xPoint,1);
        yPoint = rnd.nextFloat()*(maxy-miny)+miny;
        yPoint = round(yPoint,1);

        Log.d("XPOINT    YPOINT",xPoint +"      " +yPoint);
    }
    if(mTransX != xPoint && xPoint>mTransX) {
        mTransX += 0.1f;
        mTransX = round(mTransX,1);
    }else if(mTransX != xPoint && xPoint<mTransX){
        mTransX -= 0.1f;
        mTransX = round(mTransX,1);
    }else if(mTransX == xPoint){
        xPoint = rnd.nextFloat()*(maxx-minx)+minx;
        xPoint = round(xPoint,1);
    }
    if(mTransY != yPoint && yPoint >mTransY) {
        mTransY += 0.1f;
        mTransY = round(mTransY,1);
    }else if(mTransY != yPoint && yPoint < mTransY){
        mTransY -= 0.1f;
        mTransY = round(mTransY,1);
    }else if(mTransY == yPoint){
        yPoint = rnd.nextFloat()*(maxy-miny)+miny;
        yPoint = round(yPoint,1);
    }
}

public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);
    float ratio = (float) width / height;
    gl.glMatrixMode(GL11.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glDisable(GL11.GL_DITHER);
    gl.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_FASTEST);
    gl.glClearColor(0,0,0,0);
    gl.glEnable(GL11.GL_CULL_FACE);
    gl.glShadeModel(GL11.GL_SMOOTH);
    gl.glEnable(GL11.GL_DEPTH_TEST);
}

0 个答案:

没有答案
相关问题