Reputation: 732
i am trying to make small 3D scene where are 4 pyramids that can user rotate and zoom in/out.
i wrote following code. it works good, but when i delay pyramids to far (-100 units), they are not drawed. i dont know why. is there some restriction for 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);
}
Thanks for help.
Upvotes: 1
Views: 246
Reputation: 14688
You're translating your pyramid past the far clipping plane of your viewing frustum (the gl.glFrustumf(-size, size, -size/ratio, size/ratio, 0.1f, 100.0f);
line defines this).
Increase the far clipping plane (currently 100.0f
) if you want it to be visible further, but take note that if you ever do any sort of depth testing, your depth values will get less accurate the further away you go. If you notice any z-fighting, increase the near clipping plane as much as you can without clipping objects in the world (currently 0.1f
).
Upvotes: 2