Android Open Source - jmini3d Renderer3d






From Project

Back to project page jmini3d.

License

The source code is released under:

Copyright 2012 Mobialia http://www.mobialia.com/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to ...

If you think the Android project jmini3d 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

package jmini3d.android;
// www. j a v  a 2s. c om
import android.opengl.GLES20;
import android.util.Log;

import jmini3d.Blending;
import jmini3d.MatrixUtils;
import jmini3d.Object3d;
import jmini3d.Scene;

public class Renderer3d {
  public static final String TAG = "Renderer";

  // stats-related
  public static final int FRAMERATE_SAMPLEINTERVAL_MS = 10000;
  private boolean logFps = false;
  private long frameCount = 0;
  private float fps = 0;
  private long timeLastSample;

  private ResourceLoader resourceLoader;
  private GpuUploader gpuUploader;

  public float[] ortho = new float[16];

  Blending blending = null;
  Program currentProgram = null;
  Integer mapTextureId = -1;
  Integer envMapTextureId = -1;
  Integer normalMapTextureId = -1;
  int activeTexture = -1;

  int width = -1;
  int height = -1;

  public Renderer3d(ResourceLoader resourceLoader) {
    this.resourceLoader = resourceLoader;
    gpuUploader = new GpuUploader(resourceLoader);
  }

  public void reset() {
    mapTextureId = -1;
    envMapTextureId = -1;
    gpuUploader.reset();

    GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    GLES20.glClearDepthf(1f);
    GLES20.glDepthFunc(GLES20.GL_LEQUAL);
    GLES20.glDepthRangef(0, 1f);
    GLES20.glDepthMask(true);

    // For performance
    GLES20.glDisable(GLES20.GL_DITHER);

    // For transparency
    setBlending(Blending.NoBlending);

    // CCW frontfaces only, by default
    GLES20.glFrontFace(GLES20.GL_CCW);
    GLES20.glCullFace(GLES20.GL_BACK);
    GLES20.glEnable(GLES20.GL_CULL_FACE);
  }

  public void render(Scene scene) {
    scene.camera.updateMatrices();

    if (width != scene.camera.getWidth() || height != scene.camera.getHeight()) {
      width = scene.camera.getWidth();
      height = scene.camera.getHeight();
      GLES20.glViewport(0, 0, width, height);
      MatrixUtils.ortho(ortho, 0, width, height, 0, -5, 1);
    }

    for (int i = 0; i < scene.unload.size(); i++) {
      gpuUploader.unload(scene.unload.get(i));
    }
    scene.unload.clear();

    GLES20.glClearColor(scene.getBackgroundColor().r, scene.getBackgroundColor().g, scene.getBackgroundColor().b, scene.getBackgroundColor().a);
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

    currentProgram = null;

    for (int i = 0; i < scene.children.size(); i++) {
      Object3d o3d = scene.children.get(i);
      if (o3d.visible) {
        o3d.updateMatrices();
        drawObject(scene, o3d, scene.camera.perspectiveMatrix);

        if (o3d.clearDepthAfterDraw) {
          GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT);
        }
      }
    }

    if (scene.hud.size() > 0) {
      GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT);
    }

    for (int i = 0; i < scene.hud.size(); i++) {
      Object3d o3d = scene.hud.get(i);
      if (o3d.visible) {
        o3d.updateMatrices();
        drawObject(scene, o3d, ortho);
      }
    }
    if (logFps) {
      doFps();
    }
  }

  private void drawObject(Scene scene, Object3d o3d, float[] perspectiveMatrix) {
    Program program = gpuUploader.getProgram(scene, o3d.material);

    if (program != currentProgram) {
      GLES20.glUseProgram(program.webGLProgram);
      program.setSceneUniforms(scene);
      currentProgram = program;
    }

    if (blending != o3d.material.blending) {
      setBlending(o3d.material.blending);
    }

    program.drawObject(this, gpuUploader, o3d, perspectiveMatrix);
  }

  private void setBlending(Blending blending) {
    if (this.blending == null || !this.blending.equals(blending)) {
      this.blending = blending;

      switch (blending) {
        case NoBlending:
          GLES20.glDisable(GLES20.GL_BLEND);
          break;
        case NormalBlending:
          GLES20.glEnable(GLES20.GL_BLEND);
          GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
          break;
        case AdditiveBlending:
          GLES20.glEnable(GLES20.GL_BLEND);
          GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE);
          break;
        case SubtractiveBlending:
          GLES20.glEnable(GLES20.GL_BLEND);
          GLES20.glBlendFunc(GLES20.GL_ZERO, GLES20.GL_ONE_MINUS_SRC_COLOR);
          break;
        case MultiplyBlending:
          GLES20.glEnable(GLES20.GL_BLEND);
          GLES20.glBlendFunc(GLES20.GL_ZERO, GLES20.GL_SRC_COLOR);
          break;
      }
    }
  }

  public GpuUploader getGpuUploader() {
    return gpuUploader;
  }

  /**
   * If true, framerate and memory is periodically calculated and Log'ed, and
   * gettable thru fps()
   */
  public void setLogFps(boolean b) {
    logFps = b;

    if (logFps) { // init
      timeLastSample = System.currentTimeMillis();
      frameCount = 0;
    }
  }

  private void doFps() {
    frameCount++;

    long now = System.currentTimeMillis();
    long delta = now - timeLastSample;
    if (delta >= FRAMERATE_SAMPLEINTERVAL_MS) {
      fps = frameCount / (delta / 1000f);

      Log.v(TAG, "FPS: " + fps);

      timeLastSample = now;
      frameCount = 0;
    }
  }

  /**
   * Returns last sampled framerate (logFps must be set to true)
   */
  public float getFps() {
    return fps;
  }

  public ResourceLoader getResourceLoader() {
    return resourceLoader;
  }
}




Java Source Code List

cocoonjs.CocoonJsLinker.java
jmini3d.Blending.java
jmini3d.Camera.java
jmini3d.Color4.java
jmini3d.CubeMapTexture.java
jmini3d.Font.java
jmini3d.GpuObjectStatus.java
jmini3d.MatrixUtils.java
jmini3d.Object3d.java
jmini3d.Rect.java
jmini3d.SceneController.java
jmini3d.Scene.java
jmini3d.Texture.java
jmini3d.Utils.java
jmini3d.Vector3.java
jmini3d.android.Activity3d.java
jmini3d.android.GeometryBuffers.java
jmini3d.android.GlSurfaceView3d.java
jmini3d.android.GpuUploader.java
jmini3d.android.Program.java
jmini3d.android.Renderer3d.java
jmini3d.android.ResourceLoader.java
jmini3d.android.compat.CompatibilityWrapper5.java
jmini3d.android.demo.DemoActivity.java
jmini3d.android.input.InputController.java
jmini3d.demo.ArialFont.java
jmini3d.demo.CubeScene.java
jmini3d.demo.CubesScene.java
jmini3d.demo.DemoSceneController.java
jmini3d.demo.EnvMapCubeScene.java
jmini3d.demo.NormalMapScene.java
jmini3d.demo.ParentScene.java
jmini3d.demo.TeapotGeometry.java
jmini3d.demo.TeapotScene.java
jmini3d.geometry.BoxGeometry.java
jmini3d.geometry.Geometry.java
jmini3d.geometry.PlaneGeometry.java
jmini3d.geometry.SkyboxGeometry.java
jmini3d.geometry.SpriteGeometry.java
jmini3d.geometry.VariableGeometry.java
jmini3d.gwt.Canvas3d.java
jmini3d.gwt.EngineResources.java
jmini3d.gwt.EntryPoint3d.java
jmini3d.gwt.GeometryBuffers.java
jmini3d.gwt.GpuUploader.java
jmini3d.gwt.MyInt16Array.java
jmini3d.gwt.Program.java
jmini3d.gwt.Renderer3d.java
jmini3d.gwt.ResourceLoader.java
jmini3d.gwt.TextureLoadedListener.java
jmini3d.gwt.demo.DemoEntryPoint.java
jmini3d.gwt.input.InputController.java
jmini3d.input.KeyListener.java
jmini3d.input.TouchListener.java
jmini3d.input.TouchPointer.java
jmini3d.light.AmbientLight.java
jmini3d.light.DirectionalLight.java
jmini3d.light.Light.java
jmini3d.light.PointLight.java
jmini3d.material.Material.java
jmini3d.material.PhongMaterial.java
jmini3d.material.SpriteMaterial.java
jmini3d.utils.Fnt2Class.java
jmini3d.utils.Obj2Class.java