예제 #1
0
  protected void doColorPicking(ColorPickerInfo pickerInfo) {
    ObjectColorPicker picker = pickerInfo.getPicker();
    picker.getRenderTarget().bind();

    // Set background color (to Object3D.UNPICKABLE to prevent any conflicts)
    GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

    // Configure depth testing
    GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    GLES20.glDepthFunc(GLES20.GL_LESS);
    GLES20.glDepthMask(true);
    GLES20.glClearDepthf(1.0f);

    // Clear buffers used for color-picking
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

    // Get the picking material
    Material pickingMaterial = picker.getMaterial();

    // Can't blend picking colors
    GLES20.glDisable(GLES20.GL_BLEND);

    // Render the Skybox first (no need for depth testing)
    if (mSkybox != null && mSkybox.isPickingEnabled()) {
      GLES20.glDisable(GLES20.GL_DEPTH_TEST);
      GLES20.glDepthMask(false);
      mSkybox.renderColorPicking(mCamera, pickingMaterial);
      GLES20.glEnable(GLES20.GL_DEPTH_TEST);
      GLES20.glDepthMask(true);
    }

    // Render all children using their picking colors
    synchronized (mChildren) {
      for (int i = 0, j = mChildren.size(); i < j; ++i) {
        mChildren.get(i).renderColorPicking(mCamera, pickingMaterial);
      }
    }

    // pickObject() unbinds the renderTarget's framebuffer...
    ObjectColorPicker.pickObject(pickerInfo);
  }
  private void drawParticles() {
    float currentTime = (System.nanoTime() - globalStartTime) / 1000000000f;

    redParticleShooter.addParticles(particleSystem, currentTime, 1);
    greenParticleShooter.addParticles(particleSystem, currentTime, 1);
    blueParticleShooter.addParticles(particleSystem, currentTime, 1);

    setIdentityM(modelMatrix, 0);
    updateMvpMatrix();

    glDepthMask(false);
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ONE);

    particleProgram.useProgram();
    particleProgram.setUniforms(modelViewProjectionMatrix, currentTime, particleTexture);
    particleSystem.bindData(particleProgram);
    particleSystem.draw();

    glDisable(GL_BLEND);
    glDepthMask(true);
  }
  @Override
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
    // Set rendering options
    GLES20.glEnable(GLES20.GL_BLEND);
    GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
    GLES20.glDisable(GLES20.GL_DITHER);

    // Face culling
    GLES20.glEnable(GLES20.GL_CULL_FACE);
    GLES20.glFrontFace(GL10.GL_CCW);
    GLES20.glCullFace(GLES20.GL_BACK);

    // Depth
    GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    GLES20.glDepthFunc(GLES20.GL_LEQUAL);
    GLES20.glDepthMask(true);
  }
예제 #4
0
 @Override
 public void glDepthMask(boolean flag) {
   GLES20.glDepthMask(flag);
 }
예제 #5
0
  public void render(
      long ellapsedTime, double deltaTime, RenderTarget renderTarget, Material sceneMaterial) {
    // Scene color-picking requests are relative to the prior frame's render
    // state, so handle any pending request before applying this frame's updates...
    if (mPickerInfo != null) {
      doColorPicking(mPickerInfo);
      // One-shot, once per frame at most
      mPickerInfo = null;
    }

    performFrameTasks(); // Handle the task queue

    synchronized (mFrameTaskQueue) {
      if (mLightsDirty) {
        updateMaterialsWithLights();
        mLightsDirty = false;
      }
    }

    synchronized (mNextSkyboxLock) {
      // Check if we need to switch the skybox, and if so, do it.
      if (mNextSkybox != null) {
        mSkybox = mNextSkybox;
        mNextSkybox = null;
      }
    }
    synchronized (mNextCameraLock) {
      // Check if we need to switch the camera, and if so, do it.
      if (mNextCamera != null) {
        mCamera = mNextCamera;
        mCamera.setProjectionMatrix(mRenderer.getViewportWidth(), mRenderer.getViewportHeight());
        mNextCamera = null;
      }
    }

    int clearMask = mAlwaysClearColorBuffer ? GLES20.GL_COLOR_BUFFER_BIT : 0;

    if (renderTarget != null) {
      renderTarget.bind();
      GLES20.glClearColor(mRed, mGreen, mBlue, mAlpha);
    } else {
      //			GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
      GLES20.glClearColor(mRed, mGreen, mBlue, mAlpha);
    }

    if (mEnableDepthBuffer) {
      clearMask |= GLES20.GL_DEPTH_BUFFER_BIT;
      GLES20.glEnable(GLES20.GL_DEPTH_TEST);
      GLES20.glDepthFunc(GLES20.GL_LESS);
      GLES20.glDepthMask(true);
      GLES20.glClearDepthf(1.0f);
    }
    if (mAntiAliasingConfig.equals(ISurface.ANTI_ALIASING_CONFIG.COVERAGE)) {
      clearMask |= GL_COVERAGE_BUFFER_BIT_NV;
    }

    GLES20.glClear(clearMask);

    // Execute onPreFrame callbacks
    // We explicitly break out the steps here to help the compiler optimize
    final int preCount = mPreCallbacks.size();
    if (preCount > 0) {
      synchronized (mPreCallbacks) {
        for (int i = 0; i < preCount; ++i) {
          mPreCallbacks.get(i).onPreFrame(ellapsedTime, deltaTime);
        }
      }
    }

    // Update all registered animations
    synchronized (mAnimations) {
      for (int i = 0, j = mAnimations.size(); i < j; ++i) {
        Animation anim = mAnimations.get(i);
        if (anim.isPlaying()) anim.update(deltaTime);
      }
    }

    // We are beginning the render process so we need to update the camera matrix before fetching
    // its values
    mCamera.onRecalculateModelMatrix(null);

    // Get the view and projection matrices in advance
    mVMatrix = mCamera.getViewMatrix();
    mPMatrix = mCamera.getProjectionMatrix();
    // Pre-multiply View and Projection matrices once for speed
    mVPMatrix.setAll(mPMatrix).multiply(mVMatrix);
    mInvVPMatrix.setAll(mVPMatrix).inverse();
    mCamera.updateFrustum(mInvVPMatrix); // Update frustum plane

    // Update the model matrices of all the lights
    synchronized (mLights) {
      final int numLights = mLights.size();
      for (int i = 0; i < numLights; ++i) {
        mLights.get(i).onRecalculateModelMatrix(null);
      }
    }

    // Execute onPreDraw callbacks
    // We explicitly break out the steps here to help the compiler optimize
    final int preDrawCount = mPreDrawCallbacks.size();
    if (preDrawCount > 0) {
      synchronized (mPreDrawCallbacks) {
        for (int i = 0; i < preDrawCount; ++i) {
          mPreDrawCallbacks.get(i).onPreDraw(ellapsedTime, deltaTime);
        }
      }
    }

    if (mSkybox != null) {
      GLES20.glDisable(GLES20.GL_DEPTH_TEST);
      GLES20.glDepthMask(false);

      mSkybox.setPosition(mCamera.getX(), mCamera.getY(), mCamera.getZ());
      // Model matrix updates are deferred to the render method due to parent matrix needs
      // Render the skybox
      mSkybox.render(mCamera, mVPMatrix, mPMatrix, mVMatrix, null);

      if (mEnableDepthBuffer) {
        GLES20.glEnable(GLES20.GL_DEPTH_TEST);
        GLES20.glDepthMask(true);
      }
    }

    if (sceneMaterial != null) {
      sceneMaterial.useProgram();
      sceneMaterial.bindTextures();
    }

    synchronized (mChildren) {
      for (int i = 0, j = mChildren.size(); i < j; ++i) {
        // Model matrix updates are deferred to the render method due to parent matrix needs
        mChildren.get(i).render(mCamera, mVPMatrix, mPMatrix, mVMatrix, sceneMaterial);
      }
    }

    if (mDisplaySceneGraph) {
      mSceneGraph.displayGraph(mCamera, mVPMatrix, mPMatrix, mVMatrix);
    }

    if (sceneMaterial != null) {
      sceneMaterial.unbindTextures();
    }

    synchronized (mPlugins) {
      for (int i = 0, j = mPlugins.size(); i < j; i++) mPlugins.get(i).render();
    }

    if (renderTarget != null) {
      renderTarget.unbind();
    }

    // Execute onPostFrame callbacks
    // We explicitly break out the steps here to help the compiler optimize
    final int postCount = mPostCallbacks.size();
    if (postCount > 0) {
      synchronized (mPostCallbacks) {
        for (int i = 0; i < postCount; ++i) {
          mPostCallbacks.get(i).onPostFrame(ellapsedTime, deltaTime);
        }
      }
    }
  }
  /** Initialization function */
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
    // initialize shaders
    try {
      _shaders[GOURAUD_SHADER] =
          new Shader(
              vShaders[GOURAUD_SHADER], fShaders[GOURAUD_SHADER], mContext, false, 0); // gouraud
      _shaders[PHONG_SHADER] =
          new Shader(vShaders[PHONG_SHADER], fShaders[PHONG_SHADER], mContext, false, 0); // phong
      _shaders[NORMALMAP_SHADER] =
          new Shader(
              vShaders[NORMALMAP_SHADER],
              fShaders[NORMALMAP_SHADER],
              mContext,
              false,
              0); // normal map
    } catch (Exception e) {
      Log.d("SHADER 0 SETUP", e.getLocalizedMessage());
    }

    GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    GLES20.glClearDepthf(1.0f);
    GLES20.glDepthFunc(GLES20.GL_LEQUAL);
    GLES20.glDepthMask(true);

    // cull backface
    GLES20.glEnable(GLES20.GL_CULL_FACE);
    GLES20.glCullFace(GLES20.GL_BACK);

    // light variables
    float[] lightP = {30.0f, 0.0f, 10.0f, 1};
    this.lightPos = lightP;

    float[] lightC = {0.5f, 0.5f, 0.5f};
    this.lightColor = lightC;

    // material properties
    float[] mA = {0.45f, 0.45f, 0.45f, 1.0f};
    matAmbient = mA;

    float[] mD = {0.6f, 0.6f, 0.6f, 1.0f};
    matDiffuse = mD;

    float[] mS = {0.9f, 0.9f, 0.9f, 1.0f};
    matSpecular = mS;

    matShininess = 5.0f;

    // setup textures for all objects
    for (int i = 0; i < _objects.length; i++) setupTextures(_objects[i]);

    // set the view matrix

    Matrix.setLookAtM(
        mVMatrix, 0, eyePos[0], eyePos[1], eyePos[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
    /*Matrix.setLookAtM(
    rm,
    rmOffset,
    eyeX, eyeY, eyeZ,
    centerX, centerY, centerZ,
    upX, upY, upZ)*/
  }
예제 #7
0
  public void DrawModel() {

    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, m.textureHandle[0]);
    Mat4 rot;
    if (!haslanded) {
      Vec3 cross = VecMath.CrossProduct(MainActivity.phone_n, MainActivity.init_phone_n);
      phone_n = MainActivity.phone_n;
      init_phone_n = MainActivity.init_phone_n;

      Vec3 ny = new Vec3(0, 0, -1);
      Vec3 cross2 = VecMath.CrossProduct(init_phone_n, ny);
      float ang2 = (float) Math.asin(VecMath.Norm(cross2) / (VecMath.Norm(init_phone_n)));
      ;
      Mat4 rotmat2 = VecMath.ArbRotate(cross2, ang2);
      // Mat4 rotmat = VecMath.ArbRotate(cross, MainActivity.phone_ang);
      Mat4 rotmat = VecMath.ArbRotate(VecMath.Normalize(cross), MainActivity.phone_ang);

      if (Math.abs(init_phone_n.z) > Math.abs(init_phone_n.y))
        rotmat =
            VecMath.ArbRotate(
                VecMath.MultVec3(VecMath.Rx(-(float) Math.PI / 2), VecMath.Normalize(cross)),
                MainActivity.phone_ang);

      Mat4 rotmatf = VecMath.Mult(rotmat, rotmat2);

      Ro = rotmat;
      // Ro = VecMath.ArbRotate( VecMath.MultVec3(VecMath.Rx(-(float) Math.PI/2),
      // VecMath.Normalize(cross)), MainActivity.phone_ang);
      rot = VecMath.Mult(T, Ro);
      GLES20.glUniformMatrix4fv(Shader.rothandle, 1, true, makefloatbuffer(rot.m));
    } else {
      rot = T;
      GLES20.glUniformMatrix4fv(Shader.rothandle, 1, true, makefloatbuffer(rot.m));
      // Ro = rot;
    }

    // GLES20.glUniformMatrix4fv(Shader.rothandle, 1, true, makefloatbuffer(Ro.m));
    m.DrawModel();
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);

    // draw debug spheres
    for (int i = 0; i < 3; i++) {
      float r = .3f;
      float coss = (float) (r * Math.cos(Math.PI / 6 + i * 2 * Math.PI / 3));
      float sinn = (float) (r * Math.sin(Math.PI / 6 + i * 2 * Math.PI / 3));
      Vec3 poss = new Vec3(coss, -.5f, sinn);
      Mat4 TT = VecMath.T(poss);

      Mat4 SS = VecMath.S(.2f, .2f, .2f);
      Mat4 tott = VecMath.Mult(T, VecMath.Mult(Ro, VecMath.Mult(TT, SS)));
      fin_pos[i] = VecMath.MultVec3(tott, poss); // fin position in absolute coords
      GLES20.glUniformMatrix4fv(Shader.rothandle, 1, true, makefloatbuffer(tott.m));
      sphere[i].DrawModel();
    }

    if (isthrusting && fuel > .0f) {
      GLES20.glUniform1i(GLES20.glGetUniformLocation(Shader.program, "draw_exhaustf"), 1);
      Mat4 Tf = VecMath.Mult(rot, VecMath.T(0, -.8f, 0));
      GLES20.glUniformMatrix4fv(Shader.rothandle, 1, true, makefloatbuffer(Tf.m));
      GLES20.glEnable(GLES20.GL_BLEND);
      GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
      GLES20.glDepthMask(false);
      fire.DrawModel();
      GLES20.glDepthMask(true);
      GLES20.glDisable(GLES20.GL_BLEND);
      GLES20.glUniform1i(GLES20.glGetUniformLocation(Shader.program, "draw_exhaustf"), 0);
    }
  }