1

我正在尝试制作小型 3D 场景,其中有 4 个可以用户旋转和放大/缩小的金字塔。

我写了以下代码。它工作得很好,但是当我将金字塔延迟到远处(-100 个单位)时,它们不会被绘制。我不知道为什么。glTranslatef() 有一些限制吗?

    public class PrespectiveView extends GLSurfaceView implements GLSurfaceView.Renderer
    {
        MainActivity thiz;
        Triag t = new Triag();
        public PrespectiveView(MainActivity a)
        {
            super(a);
            thiz = a;
            setRenderer(this);
        }

        float _width = 320, _height = 462;

        public void onSurfaceCreated(GL10 gl, EGLConfig arg1)
        {
            gl.glMatrixMode(GL10.GL_PROJECTION);
            float size = .01f * (float) Math.tan(Math.toRadians(45.0) / 2); 
            float ratio = _width / _height;
            gl.glFrustumf(-size, size, -size/ratio, size/ratio, 0.1f, 100.0f);
            gl.glMatrixMode(GL10.GL_MODELVIEW);
            gl.glEnable(GL10.GL_DEPTH_TEST);
            gl.glClearColor(0.5f, 0.8f, 0.8f, 1f);
            gl.glEnable(GL10.GL_CULL_FACE);
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        }

        public void onSurfaceChanged(GL10 gl, int arg1, int arg2)
        {
            _width = arg1;
            _height = arg2;
            gl.glViewport(0, 0, arg1, arg2);
        }

        float[][] transArr = {{-1f,1f, 0f},{1f,1f, -1f},{-1f,-1f, -2f},{1f,-1f, -3f}};
        public void onDrawFrame(GL10 gl)
        {
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
            gl.glLoadIdentity();
            gl.glTranslatef(0f, 0f, -50f+CameraDist);
            gl.glRotatef(CameraAX, 1f, 0f, 0f);
            gl.glRotatef(CameraAY, 0f, 1f, 0f);
            for(int i = 0; i < 4; i++)
            {
                gl.glPushMatrix();
                gl.glTranslatef(transArr[i][0], transArr[i][1], 0);
                t.draw(gl);
                gl.glPopMatrix();
            }
            thiz.write(""+CameraDist);
        }
        boolean UpDown;
        float CameraAX = 0, CameraAY = 0, CameraDist = 0, tempX, tempY;
        @Override public boolean onTouchEvent(MotionEvent e)
        {
            if(e.getAction() == MotionEvent.ACTION_DOWN)
            {
                tempX = e.getX();
                tempY = e.getY();
                UpDown = e.getY() > _width/2;
                return true;
            }

            if(UpDown)
            {
                CameraAY -= (tempX-e.getX())/5;
                CameraAX -= (tempY-e.getY())/5;
            }
            else
            {
                CameraDist -= (tempY-e.getY())/2;
            }
            tempX = e.getX();
            tempY = e.getY();

            return true;   
        }
    }

//Triag draw
public void draw(GL10 gl)
{
   gl.glColor4f(1, 1, 1, 1);
   gl.glVertexPointer(3, GL10.GL_FLOAT, 0, f);
   gl.glColorPointer(4, GL10.GL_FLOAT, 0, c);
   gl.glDrawElements(GL10.GL_TRIANGLES, 12, GL10.GL_UNSIGNED_SHORT, s);
}

感谢帮助。

4

1 回答 1

2

您正在将金字塔平移到视锥体的远剪切平面(gl.glFrustumf(-size, size, -size/ratio, size/ratio, 0.1f, 100.0f);线定义了这一点)。

如果您希望它更远可见,请增加远剪裁平面(当前100.0f),但请注意,如果您进行任何类型的深度测试,您的深度值将变得越远越不准确。如果您发现任何 z 轴冲突,请尽可能增加近剪裁平面而不剪裁世界中的对象(目前0.1f)。

于 2012-07-28T22:38:17.913 回答