Beispiel #1
0
  /**
   * Applies parameters that should be set on the shaders. These are parameters like time, color,
   * buffer handles, etc.
   */
  public void applyParams() {
    mVertexShader.setColor(mColor);
    mVertexShader.setTime(mTime);
    mVertexShader.applyParams();

    mFragmentShader.setColorInfluence(mColorInfluence);
    mFragmentShader.applyParams();
  }
Beispiel #2
0
 /**
  * The material's diffuse color. This can be overwritten by {@link Object3D#setColor(int)}. This
  * color will be applied to the whole object. For vertex colors use {@link
  * Material#useVertexColors(boolean)} and {@link Material#setVertexColors(int)}.
  *
  * @param color A float array containing the colors to be used. These are normalized values
  *     containing values for the red, green, blue and alpha channels.
  */
 public void setColor(float[] color) {
   mColor[0] = color[0];
   mColor[1] = color[1];
   mColor[2] = color[2];
   mColor[3] = color[3];
   if (mVertexShader != null) mVertexShader.setColor(mColor);
 }
Beispiel #3
0
 /**
  * The material's diffuse color. This can be overwritten by {@link Object3D#setColor(int)}. This
  * color will be applied to the whole object. For vertex colors use {@link
  * Material#useVertexColors(boolean)} and {@link Material#setVertexColors(int)}.
  *
  * @param int color The color to be used. Color.RED for instance. Or 0xffff0000.
  */
 public void setColor(int color) {
   mColor[0] = (float) Color.red(color) / 255.f;
   mColor[1] = (float) Color.green(color) / 255.f;
   mColor[2] = (float) Color.blue(color) / 255.f;
   mColor[3] = (float) Color.alpha(color) / 255.f;
   if (mVertexShader != null) mVertexShader.setColor(mColor);
 }
Beispiel #4
0
 /**
  * Checks if any {@link IMaterialPlugin}s have been added. If so they will be added to the vertex
  * and/or fragment shader.
  *
  * @param location Where to insert the vertex and/or fragment shader
  */
 private void checkForPlugins(PluginInsertLocation location) {
   if (mPlugins == null) return;
   for (IMaterialPlugin plugin : mPlugins) {
     if (plugin.getInsertLocation() == location) {
       mVertexShader.addShaderFragment(plugin.getVertexShaderFragment());
       mFragmentShader.addShaderFragment(plugin.getFragmentShaderFragment());
     }
   }
 }
Beispiel #5
0
  /**
   * Sets the model matrix. The model matrix holds the object's local coordinates.
   *
   * @param modelMatrix
   */
  public void setModelMatrix(Matrix4 modelMatrix) {
    mModelMatrix = modelMatrix; // .getFloatValues();
    mVertexShader.setModelMatrix(mModelMatrix);

    mNormalMatrix.setAll(modelMatrix).setToNormalMatrix();
    float[] matrix = mNormalMatrix.getFloatValues();

    mNormalFloats[0] = matrix[0];
    mNormalFloats[1] = matrix[1];
    mNormalFloats[2] = matrix[2];
    mNormalFloats[3] = matrix[4];
    mNormalFloats[4] = matrix[5];
    mNormalFloats[5] = matrix[6];
    mNormalFloats[6] = matrix[8];
    mNormalFloats[7] = matrix[9];
    mNormalFloats[8] = matrix[10];

    mVertexShader.setNormalMatrix(mNormalFloats);
  }
Beispiel #6
0
 /**
  * Sets the model view matrix. The model view matrix is used to transform vertices to eye
  * coordinates
  *
  * @param modelViewMatrix
  */
 public void setModelViewMatrix(Matrix4 modelViewMatrix) {
   mModelViewMatrix = modelViewMatrix.getFloatValues();
   mVertexShader.setModelViewMatrix(mModelViewMatrix);
 }
Beispiel #7
0
 /**
  * Sets the model view projection matrix. The model view projection matrix is used to transform
  * vertices to screen coordinates.
  *
  * @param mvpMatrix
  */
 public void setMVPMatrix(Matrix4 mvpMatrix) {
   mVertexShader.setMVPMatrix(mvpMatrix.getFloatValues());
 }
Beispiel #8
0
 /**
  * Set the vertex color buffer handle. This is passed to {@link VertexShader#setColorBuffer(int)}
  *
  * @param vertexColorBufferHandle
  */
 public void setVertexColors(final int vertexColorBufferHandle) {
   mVertexShader.setVertexColors(vertexColorBufferHandle);
 }
Beispiel #9
0
 /**
  * Set the normal buffer handle. This is passed to {@link VertexShader#setNormals(int)}
  *
  * @param normalBufferHandle
  */
 public void setNormals(final int normalBufferHandle) {
   mVertexShader.setNormals(normalBufferHandle);
 }
Beispiel #10
0
 /**
  * Set the texture coordinates buffer handle. This is passed to {@link
  * VertexShader#setTextureCoords(int)}
  *
  * @param textureCoordBufferHandle
  */
 public void setTextureCoords(final int textureCoordBufferHandle) {
   mVertexShader.setTextureCoords(textureCoordBufferHandle);
 }
Beispiel #11
0
 /**
  * Set the vertex buffer handle. This is passed to {@link VertexShader#setVertices(int)}
  *
  * @param vertexBufferHandle
  */
 public void setVertices(final int vertexBufferHandle) {
   mVertexShader.setVertices(vertexBufferHandle);
 }
Beispiel #12
0
  /**
   * Takes all material parameters and creates the vertex shader and fragment shader and then
   * compiles the program. This method should only be called on initialization or when parameters
   * have changed.
   */
  private void createShaders() {
    if (!mIsDirty) return;

    if (mCustomVertexShader == null && mCustomFragmentShader == null) {
      //
      // -- Check textures
      //

      List<ATexture> diffuseTextures = null;
      List<ATexture> normalMapTextures = null;
      List<ATexture> envMapTextures = null;
      List<ATexture> skyTextures = null;
      List<ATexture> specMapTextures = null;
      List<ATexture> alphaMapTextures = null;

      boolean hasCubeMaps = false;
      boolean hasVideoTexture = false;

      for (int i = 0; i < mTextureList.size(); i++) {
        ATexture texture = mTextureList.get(i);

        switch (texture.getTextureType()) {
          case VIDEO_TEXTURE:
            hasVideoTexture = true;
            // no break statement, add the video texture to the diffuse textures
          case DIFFUSE:
          case RENDER_TARGET:
            if (diffuseTextures == null) diffuseTextures = new ArrayList<ATexture>();
            diffuseTextures.add(texture);
            break;
          case NORMAL:
            if (normalMapTextures == null) normalMapTextures = new ArrayList<ATexture>();
            normalMapTextures.add(texture);
            break;
          case CUBE_MAP:
            hasCubeMaps = true;
          case SPHERE_MAP:
            boolean isSkyTexture = false;
            boolean isEnvironmentTexture = false;

            if (texture.getClass() == SphereMapTexture.class) {
              isSkyTexture = ((SphereMapTexture) texture).isSkyTexture();
              isEnvironmentTexture = ((SphereMapTexture) texture).isEnvironmentTexture();
            } else if (texture.getClass() == CubeMapTexture.class) {
              isSkyTexture = ((CubeMapTexture) texture).isSkyTexture();
              isEnvironmentTexture = ((CubeMapTexture) texture).isEnvironmentTexture();
            }

            if (isSkyTexture) {
              if (skyTextures == null) skyTextures = new ArrayList<ATexture>();
              skyTextures.add(texture);
            } else if (isEnvironmentTexture) {
              if (envMapTextures == null) envMapTextures = new ArrayList<ATexture>();
              envMapTextures.add(texture);
            }
            break;
          case SPECULAR:
            if (specMapTextures == null) specMapTextures = new ArrayList<ATexture>();
            specMapTextures.add(texture);
            break;
          case ALPHA:
            if (alphaMapTextures == null) alphaMapTextures = new ArrayList<ATexture>();
            alphaMapTextures.add(texture);
            break;
          default:
            break;
        }
      }

      mVertexShader = new VertexShader();
      mVertexShader.enableTime(mTimeEnabled);
      mVertexShader.hasCubeMaps(hasCubeMaps);
      mVertexShader.useVertexColors(mUseVertexColors);
      mVertexShader.initialize();
      mFragmentShader = new FragmentShader();
      mFragmentShader.enableTime(mTimeEnabled);
      mFragmentShader.hasCubeMaps(hasCubeMaps);
      mFragmentShader.initialize();

      if (diffuseTextures != null && diffuseTextures.size() > 0) {
        DiffuseTextureFragmentShaderFragment fragment =
            new DiffuseTextureFragmentShaderFragment(diffuseTextures);
        mFragmentShader.addShaderFragment(fragment);
      }

      if (normalMapTextures != null && normalMapTextures.size() > 0) {
        NormalMapFragmentShaderFragment fragment =
            new NormalMapFragmentShaderFragment(normalMapTextures);
        mFragmentShader.addShaderFragment(fragment);
      }

      if (envMapTextures != null && envMapTextures.size() > 0) {
        EnvironmentMapFragmentShaderFragment fragment =
            new EnvironmentMapFragmentShaderFragment(envMapTextures);
        mFragmentShader.addShaderFragment(fragment);
      }

      if (skyTextures != null && skyTextures.size() > 0) {
        SkyTextureFragmentShaderFragment fragment =
            new SkyTextureFragmentShaderFragment(skyTextures);
        mFragmentShader.addShaderFragment(fragment);
      }

      if (hasVideoTexture)
        mFragmentShader.addPreprocessorDirective("#extension GL_OES_EGL_image_external : require");

      checkForPlugins(PluginInsertLocation.PRE_LIGHTING);

      //
      // -- Lighting
      //

      if (mLightingEnabled && mLights != null && mLights.size() > 0) {
        mVertexShader.setLights(mLights);
        mFragmentShader.setLights(mLights);

        mLightsVertexShaderFragment = new LightsVertexShaderFragment(mLights);
        mLightsVertexShaderFragment.setAmbientColor(mAmbientColor);
        mLightsVertexShaderFragment.setAmbientIntensity(mAmbientIntensity);
        mVertexShader.addShaderFragment(mLightsVertexShaderFragment);
        mFragmentShader.addShaderFragment(new LightsFragmentShaderFragment(mLights));

        checkForPlugins(PluginInsertLocation.PRE_DIFFUSE);

        //
        // -- Diffuse method
        //

        if (mDiffuseMethod != null) {
          mDiffuseMethod.setLights(mLights);
          IShaderFragment fragment = mDiffuseMethod.getVertexShaderFragment();
          if (fragment != null) mVertexShader.addShaderFragment(fragment);
          fragment = mDiffuseMethod.getFragmentShaderFragment();
          mFragmentShader.addShaderFragment(fragment);
        }

        checkForPlugins(PluginInsertLocation.PRE_SPECULAR);

        //
        // -- Specular method
        //

        if (mSpecularMethod != null) {
          mSpecularMethod.setLights(mLights);
          mSpecularMethod.setTextures(specMapTextures);
          IShaderFragment fragment = mSpecularMethod.getVertexShaderFragment();
          if (fragment != null) mVertexShader.addShaderFragment(fragment);

          fragment = mSpecularMethod.getFragmentShaderFragment();
          if (fragment != null) mFragmentShader.addShaderFragment(fragment);
        }
      }

      checkForPlugins(PluginInsertLocation.PRE_ALPHA);

      if (alphaMapTextures != null && alphaMapTextures.size() > 0) {
        AlphaMapFragmentShaderFragment fragment =
            new AlphaMapFragmentShaderFragment(alphaMapTextures);
        mFragmentShader.addShaderFragment(fragment);
      }

      checkForPlugins(PluginInsertLocation.PRE_TRANSFORM);
      checkForPlugins(PluginInsertLocation.POST_TRANSFORM);

      mVertexShader.buildShader();
      mFragmentShader.buildShader();
    } else {
      mVertexShader = mCustomVertexShader;
      mFragmentShader = mCustomFragmentShader;

      if (mVertexShader.needsBuild()) mVertexShader.initialize();
      if (mFragmentShader.needsBuild()) mFragmentShader.initialize();

      if (mVertexShader.needsBuild()) mVertexShader.buildShader();
      if (mFragmentShader.needsBuild()) mFragmentShader.buildShader();
    }

    if (debug) {
      RajLog.d("-=-=-=- VERTEX SHADER -=-=-=-");
      RajLog.d(mVertexShader.getShaderString());
      RajLog.d("-=-=-=- FRAGMENT SHADER -=-=-=-");
      RajLog.d(mFragmentShader.getShaderString());
    }

    mProgramHandle =
        createProgram(mVertexShader.getShaderString(), mFragmentShader.getShaderString());
    if (mProgramHandle == 0) {
      mIsDirty = false;
      return;
    }

    mVertexShader.setLocations(mProgramHandle);
    mFragmentShader.setLocations(mProgramHandle);

    for (int i = 0; i < mTextureList.size(); i++) {
      ATexture texture = mTextureList.get(i);
      setTextureParameters(texture);
    }

    mIsDirty = false;
  }