Android Open Source - Look Renderer3 D






From Project

Back to project page Look.

License

The source code is released under:

====================== LOOK! LICENSING TERMS ====================== look! is licensed under the BSD 3-Clause (also known as "BSD New" or "BSD Simplified"), as follows: Copyright (c) 2010-2012, Look...

If you think the Android project Look listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
*-----------------------------------------------------------------------------
* Copyright (c) 2012, Look! Development Team
* All rights reserved./*from ww w.j a  v  a  2  s  . c  o m*/
*
* Distributed under the terms of the BSD Simplified License.
*
* The full license is in the LICENSE file, distributed with this software.
*-----------------------------------------------------------------------------
*/
package es.ucm.look.ar.ar3D;

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

import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import android.util.Log;
import es.ucm.look.ar.LookAR;
import es.ucm.look.ar.ar3D.core.TextureFactory;
import es.ucm.look.ar.ar3D.core.camera.Camera3D;
import es.ucm.look.ar.ar3D.core.camera.OrientedCamera;
import es.ucm.look.ar.util.LookARUtil;
import es.ucm.look.data.LookData;

/**
 * Renderer3D is the general renderer for the GLSurfaceView
 * 
 * @author ??ngel Serrano
 * 
 */
public class Renderer3D implements Renderer {

  /**
   * Some values for
   * {@link GLU#gluPerspective(GL10, float, float, float, float)}. We store
   * them cause they'll be required to do some calculations
   */
  private float fov, ratio, nearDist, farDist;

  private int fps;

  private int counter;

  private int accTime;

  private long lastTime = -1;

  private LookAR context;

  private boolean transparent;

  private Camera3D camera;

  /**
   * Constructs a Renderer
   * 
   * @param transparent
   *            if the background must be transparent
   * @param maxDist
   *            max distance to be shown
   */
  public Renderer3D(boolean transparent, float maxDist) {
    this.context = LookARUtil.getApp();
    this.transparent = transparent;
    this.camera = new OrientedCamera();

    fov = (float) Math.toDegrees(LookARUtil.CAMERA_VIEW_ANGLE);
    nearDist = 0.1f;
    farDist = maxDist;

    fps = 0;
    counter = 0;
    accTime = 0;
  }

  /**
   * Constructs a render with opaque background
   * 
   */
  public Renderer3D() {
    this(false, 50.0f);
  }

  @Override
  public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    initLights(gl);

    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glShadeModel(GL10.GL_SMOOTH);

    if (transparent) {
      gl.glClearColor(0, 0, 0, 0);
    } else {
      gl.glClearColor(0, 0, 0, 1);
    }

    gl.glClearDepthf(1.0f);
    gl.glEnable(GL10.GL_DEPTH_TEST);
    gl.glDepthFunc(GL10.GL_LEQUAL);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);

    TextureFactory.init(context, gl);
  }

  /**
   * Init the lights
   * 
   * @param gl
   *            GL10 context
   */
  private void initLights(GL10 gl) {
    gl.glEnable(GL10.GL_LIGHTING);
    gl.glLightModelfv(GL10.GL_LIGHT_MODEL_AMBIENT, new float[] { 0.3f,
        0.3f, 0.3f, 1.0f }, 0);
    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, new float[] { 1.0f,
        1.0f, 0.0f, 0.0f }, 0);
    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT_AND_DIFFUSE, new float[] {
        1.0f, 1.0f, 1.0f, 1.0f }, 0);
    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, new float[] { 1.0f,
        1.0f, 1.0f, 1.0f }, 0);
    gl.glEnable(GL10.GL_LIGHT0);

  }

  public void onDrawFrame(GL10 gl) {
    updateFPS();
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    camera.setCamera(gl);
    LookData.getInstance().getWorld().draw( gl );

  }

  private void updateFPS() {
    long now = System.currentTimeMillis();
    counter++;
    if (lastTime != -1) {
      accTime += now - lastTime;
      if (accTime > 1000) {
        if (fps != 0)
          fps = (fps + counter) / 2;
        else
          fps = counter;

        counter = 0;
        accTime = 0;
        Log.i("fps", "FPS: " + fps);
      }
    }
    lastTime = now;
  }

  /**
   * If the surface changes, reset the view
   */
  public void onSurfaceChanged(GL10 gl, int width, int height) {
    if (height == 0) {
      height = 1;
    }

    ratio = (float) width / (float) height;

    gl.glViewport(0, 0, width, height);
    set3D(gl);
  }

  /**
   * Sets 3D projection
   * 
   * @param gl
   *            GL10 context
   */
  private void set3D(GL10 gl) {
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();

    GLU.gluPerspective(gl, fov, ratio, nearDist, farDist);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();

  }

  /**
   * Returns the fps
   * 
   * @return the fps
   */
  public int getFPS() {
    return fps;
  }

  public void setCamera(Camera3D camera) {
    this.camera = camera;
  }

}




Java Source Code List

es.ucm.look.ar.LookAR.java
es.ucm.look.ar.Preview.java
es.ucm.look.ar.ar2D.AR2D.java
es.ucm.look.ar.ar2D.Drawable2D.java
es.ucm.look.ar.ar2D.HUDElement.java
es.ucm.look.ar.ar2D.drawables.Circle2D.java
es.ucm.look.ar.ar2D.drawables.Image2D.java
es.ucm.look.ar.ar2D.drawables.Text2D.java
es.ucm.look.ar.ar3D.Drawable3D.java
es.ucm.look.ar.ar3D.Renderer3D.java
es.ucm.look.ar.ar3D.core.Color4.java
es.ucm.look.ar.ar3D.core.TextureFactory.java
es.ucm.look.ar.ar3D.core.camera.Camera3D.java
es.ucm.look.ar.ar3D.core.camera.OrientedCamera.java
es.ucm.look.ar.ar3D.core.drawables.DrawablesDataBase.java
es.ucm.look.ar.ar3D.core.drawables.Entity3D.java
es.ucm.look.ar.ar3D.core.drawables.Mesh3D.java
es.ucm.look.ar.ar3D.core.drawables.primitives.CirclePrimitive.java
es.ucm.look.ar.ar3D.core.drawables.primitives.Cube.java
es.ucm.look.ar.ar3D.core.drawables.primitives.Grid.java
es.ucm.look.ar.ar3D.core.drawables.primitives.LinePrimitive.java
es.ucm.look.ar.ar3D.core.drawables.primitives.LinesLoopPrimitive.java
es.ucm.look.ar.ar3D.core.drawables.primitives.ObjMesh3D.java
es.ucm.look.ar.ar3D.core.drawables.primitives.PointPrimitive.java
es.ucm.look.ar.ar3D.core.drawables.primitives.Ring.java
es.ucm.look.ar.ar3D.core.drawables.primitives.SquarePrimitive.java
es.ucm.look.ar.ar3D.core.drawables.primitives.TrianglePrimitive.java
es.ucm.look.ar.ar3D.core.drawables.primitives.extra.ImagePrimitive.java
es.ucm.look.ar.ar3D.parser.MeshObjParser.java
es.ucm.look.ar.hud.ActionListener.java
es.ucm.look.ar.hud.BasicHud.java
es.ucm.look.ar.hud.Button.java
es.ucm.look.ar.hud.HUD.java
es.ucm.look.ar.listeners.CameraListener.java
es.ucm.look.ar.listeners.TouchListener.java
es.ucm.look.ar.math.collision.Armature.java
es.ucm.look.ar.math.collision.SphericalArmature.java
es.ucm.look.ar.math.collision.SquareArmature.java
es.ucm.look.ar.math.collision.debug.DebugArmature.java
es.ucm.look.ar.math.collision.debug.SphericalDebugArmature.java
es.ucm.look.ar.math.collision.debug.SquareDebugArmature.java
es.ucm.look.ar.math.geom.Matrix3.java
es.ucm.look.ar.math.geom.Plane.java
es.ucm.look.ar.math.geom.Point2.java
es.ucm.look.ar.math.geom.Point3.java
es.ucm.look.ar.math.geom.Ray.java
es.ucm.look.ar.math.geom.Triangle.java
es.ucm.look.ar.math.geom.Vector3.java
es.ucm.look.ar.util.CameraParametersHelper.java
es.ucm.look.ar.util.DeviceOrientation.java
es.ucm.look.ar.util.LookARUtil.java
es.ucm.look.ar.util.PositionTimerTask.java
es.ucm.look.data.EntityData.java
es.ucm.look.data.LookData.java
es.ucm.look.data.WorldEntityFactory.java
es.ucm.look.data.WorldEntity.java
es.ucm.look.data.World.java
es.ucm.look.data.filesManager.LookFilesManager.java
es.ucm.look.data.interfaces.DataGetter.java
es.ucm.look.data.interfaces.DataHandler.java
es.ucm.look.data.interfaces.DataSetter.java
es.ucm.look.data.local.BasicDataHandler.java
es.ucm.look.data.local.DBDataHandler.java
es.ucm.look.data.local.contentprovider.LookContentProvider.java
es.ucm.look.data.local.contentprovider.sql.LookSQLContentProvider.java
es.ucm.look.data.local.contentprovider.sql.LookSQLHelper.java
es.ucm.look.data.remote.ConfigNet.java
es.ucm.look.data.remote.LookProperties.java
es.ucm.look.data.remote.RemoteDBHandler.java
es.ucm.look.data.remote.restful.LookService.java
es.ucm.look.data.remote.restful.RestMethod.java
es.ucm.look.data.remote.restful.ServiceManager.java
es.ucm.look.location.LocationManager.java
es.ucm.look.locationProvider.DeviceSensor.java
es.ucm.look.locationProvider.InertialNavigationSystem.java
es.ucm.look.locationProvider.LocationProvider.java
es.ucm.look.locationProvider.Motion.java
es.ucm.look.locationProvider.Positioning.java
es.ucm.look.locationProvider.Util.java
es.ucm.look.locationProviderWifi.Cliente.java
es.ucm.look.locationProviderWifi.WifiLocation.java
es.ucm.look.locationProviderWifi.WifiService.java
es.ucm.look.locationProviderWifi.util.DateUtils.java
es.ucm.look.locationProviderWifi.util.DeviceReader.java
es.ucm.look.locationProviderWifi.util.DeviceWriter.java
es.ucm.look.locationProviderWifi.wifi.Lugar.java
es.ucm.look.locationProviderWifi.wifi.Lugares.java
es.ucm.look.locationProviderWifi.wifi.NodoWifi.java
es.ucm.look.locationProvider.map.Mapa.java
es.ucm.look.locationProvider.test.java