首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓openGL ES 2.0场景滚动

安卓openGL ES 2.0场景滚动
EN

Stack Overflow用户
提问于 2012-11-03 01:27:06
回答 1查看 1.7K关注 0票数 0

我正在为使用openGL es 2.0的安卓开发一个简单的游戏。游戏将是2D的,有点像旧的超级马里奥,玩家可以左右移动/上下移动,但没有深度。

这就是问题所在。

举例说明。当我向右移动屏幕时,当背景的右边缘到达屏幕边缘时,我希望屏幕停止滚动。

我现在能做的最好的事情,就是当“视图”的中心在背景的边缘时停止滚动。但这并不好,因为四分之三的屏幕都是空白的。

这是渲染器的代码。

代码语言:javascript
复制
package com.huntedseas;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.Matrix;
import android.util.Log;


public class GameRenderer implements Renderer {
    protected LevelGenerator generator;
    protected SquareGL squareGL;

    protected static float angleX;
    protected static float angleY;
    protected static float angleZ;
    protected static float viewX = 0;
    protected static float viewY = 0;
    //private float viewZ = 0;

    private float[] mProjectionMatrix = new float[16]; //Projekcijska matrika
    private float[] mVMatrix = new float[16];
    private float[] mMVPMatrix = new float[16];

    @Override
    public void onSurfaceCreated(GL10 unused, EGLConfig conunused) {
        GLES20.glClearColor(1.0f,1.0f,1.0f,1.0f);

        GLES20.glDisable(GLES20.GL_CULL_FACE); //No culling of back faces  \\ To nevem al je treba da je uklopljeno al ne
        GLES20.glDisable(GLES20.GL_DEPTH_TEST); //No depth testing          \\  --||--
        GLES20.glEnable(GLES20.GL_BLEND); //Blending
        GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA,GLES20.GL_ONE_MINUS_SRC_ALPHA); //Interpolated blending

        generator = new LevelGenerator();
    }

    @Override
    public void onDrawFrame(GL10 unused) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        moveView();
        Matrix.setLookAtM(mVMatrix, 0, viewY, viewX, -10, viewY, viewX, 0, 0.0f, 1.0f, 0.0f); //set view
        Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mVMatrix, 0); //calculate view transformation

        generator.draw(mMVPMatrix);     
    }

    @Override
    public void onSurfaceChanged(GL10 unused, int width, int height) {
        GLES20.glViewport(0, 0, width, height);

        float ratio = (float) width/height;
        float left =-ratio;
        float right = ratio;
        float bottom = -1.0f;
        float top = 1.0f;
        float near = 1.0f;
        float far = 20.0f;      
        Log.d("ratio","ratio: "+left+"  r: "+right+"  w: "+width+"  h: "+height);
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);

    }

    long lastTime = System.currentTimeMillis();
    public void moveView(){
        if((System.currentTimeMillis()-lastTime) >= 33){
            lastTime = System.currentTimeMillis();
            Log.d("viewX","viewX: "+viewX + "viewY:"+viewY);
            if( (viewX - angleX/10) < Level1.viewXP && (viewX - angleX/10) > Level1.viewXM) viewX -= angleX/10; 
            if( (viewY - angleY/10) < Level1.viewYP && (viewY - angleY/10) > Level1.viewYM) viewY -= angleY/10;

            //if(Math.abs(viewX - angleX/10) < 10) viewX-=angleX/10;
            //if(Math.abs(viewY - angleY/10) < 10) viewY-=angleY/10;
            //if(Math.abs(viewZ - (angleZ-5)/10) < 10) viewZ+=(angleZ-5)/10;
        }
    }


}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-07 21:08:45

我已经解决了这个问题,修改了Matrix.frustumM的参数,使它们表示FOV。然后当你知道从中边到边的角度时,你可以用切线函数来计算偏移。

代码语言:javascript
复制
 GLES20.glViewport(0, 0, width, height);

    float ratio = (float) width/height;
    float near = 1.0f;
    float far = 150.0f;
    float fov = 80;
    float top = (float) (Math.tan(fov * Math.PI / 360.0f) * near);
    float bottom = -top;
    float left = ratio * bottom;
    float right = ratio * top;

    offsetY = (float) (Math.tan(Math.PI/180*fov/2) * Math.abs(cameraAway));
    offsetX = offsetY * ratio; //because we know ratio between x and y

    Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);

也可以使用Matrix.perspectivM(),但只对14+接口有效。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13200245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档