Пример #1
0
  @Override
  public void setShaders(String vertexShader, String fragmentShader) {
    StringBuffer fc = new StringBuffer();
    StringBuffer vc = new StringBuffer();
    fc.append("float normPower = 0.0;\n");

    for (int i = 0; i < mLights.size(); ++i) {
      ALight light = mLights.get(i);

      if (light.getLightType() == ALight.POINT_LIGHT) {
        fc.append("L = normalize(uLightPosition").append(i).append(" + vEyeVec);\n");

        vc.append("dist = distance(-vEyeVec, uLightPosition").append(i).append(");\n");
        vc.append("vAttenuation")
            .append(i)
            .append(" = 1.0 / (uLightAttenuation")
            .append(i)
            .append("[1] + uLightAttenuation")
            .append(i)
            .append("[2] * dist + uLightAttenuation")
            .append(i)
            .append("[3] * dist * dist);\n");
      } else if (light.getLightType() == ALight.DIRECTIONAL_LIGHT) {
        fc.append("L = normalize(-uLightDirection").append(i).append(");\n");
        vc.append("vAttenuation").append(i).append(" = 1.0;\n");
      }

      fc.append("NdotL = max(dot(bumpnormal, L), 0.1);\n");
      fc.append("normPower = NdotL * vAttenuation")
          .append(i)
          .append(" * uLightPower")
          .append(i)
          .append(";\n");
      fc.append("intensity += normPower;\n");
      fc.append("Kd.rgb += uLightColor").append(i).append(" * normPower;\n");
      fc.append("Ks += pow(NdotL, uShininess) * vAttenuation")
          .append(i)
          .append(" * uLightPower")
          .append(i)
          .append(";\n");
    }

    super.setShaders(
        vertexShader.replace("%LIGHT_CODE%", vc.toString()),
        fragmentShader.replace("%LIGHT_CODE%", fc.toString()));
  }
Пример #2
0
  public void setShaders(String vertexShader, String fragmentShader) {
    StringBuffer sb = new StringBuffer();
    StringBuffer vc = new StringBuffer();

    for (int i = 0; i < mLights.size(); ++i) {
      ALight light = mLights.get(i);

      if (light.getLightType() == ALight.POINT_LIGHT) {
        sb.append("L = normalize(uLightPosition").append(i).append(" - V.xyz);\n");
        vc.append("dist = distance(V.xyz, uLightPosition").append(i).append(");\n");
        vc.append("vAttenuation")
            .append(i)
            .append(" = 1.0 / (uLightAttenuation")
            .append(i)
            .append("[1] + uLightAttenuation")
            .append(i)
            .append("[2] * dist + uLightAttenuation")
            .append(i)
            .append("[3] * dist * dist);\n");
      } else if (light.getLightType() == ALight.DIRECTIONAL_LIGHT) {
        vc.append("vAttenuation").append(i).append(" = 1.0;\n");
        sb.append("L = -normalize(uLightDirection").append(i).append(");\n");
      }
      sb.append("intensity +=  uLightPower")
          .append(i)
          .append(" * max(dot(N, L), 0.1) * vAttenuation")
          .append(i)
          .append(";\n");
      sb.append("Kd += uLightColor")
          .append(i)
          .append(" * uLightPower")
          .append(i)
          .append(" * max(dot(N, L), 0.1) * vAttenuation")
          .append(i)
          .append(";\n");
    }

    super.setShaders(
        vertexShader.replace("%LIGHT_CODE%", vc.toString()),
        fragmentShader.replace("%LIGHT_CODE%", sb.toString()));
  }
Пример #3
0
  @Override
  protected void parse(InputStream is) throws ParsingException {
    BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
    String line;
    try {
      while ((line = buffer.readLine()) != null) {
        String repl = line.replaceAll(REGEX_CLEAN, REPLACE_EMPTY);
        if (repl.length() == 0 || repl.charAt(0) == COMMENT) continue;

        readLine(buffer, line);
      }
      buffer.close();
    } catch (Exception e) {
      throw new ParsingException(e);
    }

    // -- get lights

    Stack<Model> lights = mFbx.objects.getModelsByType(FBXValues.MODELTYPE_LIGHT);
    int numLights = lights.size();

    RajawaliRenderer.setMaxLights(numLights == 0 ? 1 : numLights);
    Stack<ALight> sceneLights = new Stack<ALight>();

    for (int i = 0; i < numLights; ++i) {
      Model l = lights.get(i);
      // -- really need to add more light types

      sceneLights.add(buildLight(l));
    }

    if (numLights == 0) {
      ALight light = new DirectionalLight();
      light.setPosition(2, 0, -5);
      light.setPower(1);
      sceneLights.add(light);
    }

    // -- check fog
    // TODO: add fog support
    /*
     * if(mFbx.version5.fogOptions.fogEnable != null && mFbx.version5.fogOptions.fogEnable == 1) { FogOptions
     * fogOptions = mFbx.version5.fogOptions; mRenderer.setFogEnabled(true); Camera cam = mRenderer.getCamera();
     * cam.setFogEnabled(true); cam.setFogNear(fogOptions.fogStart); cam.setFogColor(fogOptions.fogColor.color);
     * mRenderer.setBackgroundColor(fogOptions.fogColor.color); }
     */

    // -- get meshes

    Stack<Model> models = mFbx.objects.getModelsByType(FBXValues.MODELTYPE_MESH);

    try {
      for (int i = 0; i < models.size(); ++i) {
        buildMesh(models.get(i), sceneLights);
      }
    } catch (TextureException tme) {
      throw new ParsingException(tme);
    }

    // -- get cameras

    Stack<Model> cameras = mFbx.objects.getModelsByType(FBXValues.MODELTYPE_CAMERA);
    Model camera = null;

    for (int i = 0; i < cameras.size(); ++i) {
      if (cameras.get(i).hidden == null || !cameras.get(i).hidden.equals("True")) {
        camera = cameras.get(i);
        break;
      }
    }

    if (camera != null) { // TODO: FIX
      Camera cam = mRenderer.getCurrentCamera();
      cam.setPosition(camera.position);
      cam.setX(mRenderer.getCurrentCamera().getX() * -1);
      cam.setRotation(camera.properties.lclRotation);
      Vector3 lookAt = camera.lookAt;
      // lookAt.x = -lookAt.x;
      cam.setLookAt(lookAt);
      cam.setNearPlane(camera.properties.nearPlane);
      cam.setFarPlane(camera.properties.farPlane);
      cam.setFieldOfView(camera.properties.fieldOfView);
    }
  }
Пример #4
0
  public void setShaders(String vertexShader, String fragmentShader) {
    StringBuffer fc = new StringBuffer();
    StringBuffer vc = new StringBuffer();

    for (int i = 0; i < mLights.size(); ++i) {
      ALight light = mLights.get(i);

      if (light.getLightType() == ALight.POINT_LIGHT) {
        vc.append("dist = distance(V.xyz, uLightPosition").append(i).append(");\n");
        vc.append("vAttenuation")
            .append(i)
            .append(" = 1.0 / (uLightAttenuation")
            .append(i)
            .append("[1] + uLightAttenuation")
            .append(i)
            .append("[2] * dist + uLightAttenuation")
            .append(i)
            .append("[3] * dist * dist);\n");
        fc.append("L = normalize(uLightPosition").append(i).append(" - V.xyz);\n");
      } else if (light.getLightType() == ALight.SPOT_LIGHT) {
        vc.append("dist = distance(V.xyz, uLightPosition").append(i).append(");\n");
        vc.append("vAttenuation")
            .append(i)
            .append(" = (uLightAttenuation")
            .append(i)
            .append("[1] + uLightAttenuation")
            .append(i)
            .append("[2] * dist + uLightAttenuation")
            .append(i)
            .append("[3] * dist * dist);\n");
        fc.append("L = normalize(uLightPosition").append(i).append(" - V.xyz);\n");
        fc.append("vec3 spotDir")
            .append(i)
            .append(" = normalize(-uLightDirection")
            .append(i)
            .append(");\n");
        fc.append("float spot_factor = dot( L, spotDir").append(i).append(" );\n");
        fc.append("if( uSpotCutoffAngle").append(i).append(" < 180.0 ) {\n");
        fc.append("if( spot_factor >= cos( radians( uSpotCutoffAngle")
            .append(i)
            .append(") ) ) {\n");
        fc.append(
                "spot_factor = (1.0 - (1.0 - spot_factor) * 1.0/(1.0 - cos( radians( uSpotCutoffAngle")
            .append(i)
            .append("))));\n");
        fc.append("spot_factor = pow(spot_factor, uSpotFalloff")
            .append(i)
            .append("* 1.0/spot_factor);\n");
        fc.append("}\n");
        fc.append("else {\n");
        fc.append("spot_factor = 0.0;\n");
        fc.append("}\n");
        fc.append("L = vec3(L.y, L.x, L.z);\n");
        fc.append("}\n");
      } else if (light.getLightType() == ALight.DIRECTIONAL_LIGHT) {
        vc.append("vAttenuation").append(i).append(" = 1.0;\n");
        fc.append("L = normalize(-uLightDirection").append(i).append(");\n");
      }

      fc.append("NdotL = max(dot(N, L), 0.1);\n");
      fc.append("power = uLightPower")
          .append(i)
          .append(" * NdotL * vAttenuation")
          .append(i)
          .append(";\n");
      fc.append("intensity += power;\n");

      if (light.getLightType() == ALight.SPOT_LIGHT)
        fc.append("Kd.rgb += uLightColor").append(i).append(" * spot_factor;\n");
      else fc.append("Kd.rgb += uLightColor").append(i).append(" * power;\n");
    }

    super.setShaders(
        vertexShader.replace("%LIGHT_CODE%", vc.toString()),
        fragmentShader.replace("%LIGHT_CODE%", fc.toString()));
  }