HappyEngineer
HappyEngineer

Reputation: 4055

Setting OpenGL ES maximum distance on Android

In OpenGL ES, how do I specify what the maximum visibility distance is? I've found that when I do:

gl.glTranslatef(0,0,-10f);

everything I draw is invisible because it's too far away from the camera. How do I specify that it should draw things farther away?

Upvotes: 0

Views: 932

Answers (1)

user562566
user562566

Reputation:

The solution to your problem HappyEngineer is simply tinkering with the static GLU.gluPerspective() method. Here is the code I am using for rendering a simple cube (just the view setup code) in my project (I'm also learning):

public void onSurfaceChanged(GL10 gl, int width, int height)
    {
        //Reset The Projection Matrix
        Log.v("Home", String.valueOf(height));
        gl.glViewport(0, 0, width, height);     //Reset The Current Viewport
        gl.glMatrixMode(GL10.GL_PROJECTION);    //Select The Projection Matrix
        gl.glLoadIdentity();    
        //Calculate The Aspect Ratio Of The Window
        GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 1000.0f);

        gl.glMatrixMode(GL10.GL_MODELVIEW);     //Select The Modelview Matrix
        gl.glLoadIdentity(); 
    }

So even if you're new to GL the names of these functions + the parameters should be fairly obvious if you have any background in 3D. For the function of interest, gluPerspective, you're passing a reference to your GL instance, the angle of the FOV or field of view, aspect ratio and then the far plane or maximum view distance. So that you understand the context in which this code is used, I'm using it inside of a GLWallpaperService (GL LiveWallpaper) within the onSurfaceChanged event. For more nfo on that see the following:

http://mindtherobot.com/blog/376/android-ui-making-a-live-wallpaper-fire-simulation/

http://www.rbgrn.net/content/354-glsurfaceview-adapted-3d-live-wallpapers

Also some nice tutorials for working with glES on droid:

http://blog.jayway.com/2009/12/03/opengl-es-tutorial-for-android-part-i/

http://insanitydesign.com/wp/projects/nehe-android-ports/

Upvotes: 5

Related Questions