我正在尝试制作小型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);
}
感谢您的帮助。
答案 0 :(得分:2)
您正在将金字塔翻译过视锥体的远剪裁平面(gl.glFrustumf(-size, size, -size/ratio, size/ratio, 0.1f, 100.0f);
行定义了这个)。
如果希望它进一步可见,请增加远剪裁平面(当前为100.0f
),但请注意,如果您进行任何类型的深度测试,您的深度值将越远越不准确走。如果您注意到任何z-fighting,请尽可能多地增加近剪裁平面而不剪切世界中的对象(目前为0.1f
)。