示例#1
0
  public GLShader(PApplet pa) {
    // pgl = (PGraphicsOpenGL)pa.g;
    gl = PlatformUtil.getGL20();

    if (this.gl == null) {
      System.out.println("Shaders not supported");
    }

    System.out.println("GLSL Version : " + gl.glGetString(GL20.GL_SHADING_LANGUAGE_VERSION));
    String extensions = gl.glGetString(GL20.GL_EXTENSIONS);
    vertexShaderSupported = extensions.indexOf("GL_ARB_vertex_shader") != -1;
    vertexShaderEnabled = true;

    glslProgram = gl.glCreateProgram();

    vertexShader = -1;
    fragmentShader = -1;
  }
  /**
   * Instantiates a new model display scene.
   *
   * @param mtApplication the mt application
   * @param name the name
   */
  public Space3DScene(AbstractMTApplication mtApplication, String name) {
    super(mtApplication, name);
    this.pa = mtApplication;

    if (!MT4jSettings.getInstance().isOpenGlMode()) {
      System.err.println("Scene only usable when using the OpenGL renderer! - See settings.txt");
      return;
    }

    this.setClearColor(new MTColor(200, 200, 200, 255));
    this.registerGlobalInputProcessor(new CursorTracer(pa, this));

    // Add a background image for the scene
    this.getCanvas()
        .addChild(new MTBackgroundImage(pa, pa.loadImage(imagesPath + "3040.jpg"), true));

    // Init light settings
    MTLight.enableLightningAndAmbient(pa, 150, 150, 150, 255);
    // Create a light source //I think GL_LIGHT0 is used by processing!
    //		MTLight light = new MTLight(pa, GL.GL_LIGHT3, new Vector3D(0,0,0));
    MTLight light = new MTLight(pa, GL.GL_LIGHT3, new Vector3D(pa.width / 5f, -pa.height / 10f, 0));

    // Set up a material to react to the light
    GLMaterial material = new GLMaterial(PlatformUtil.getGL());
    material.setAmbient(new float[] {.1f, .1f, .1f, 1f});
    material.setDiffuse(new float[] {1.0f, 1.0f, 1.0f, 1f});
    material.setEmission(new float[] {.0f, .0f, .0f, 1f});
    material.setSpecular(new float[] {1.0f, 1.0f, 1.0f, 1f}); // almost white: very reflective
    material.setShininess(127); // 0=no shine,  127=max shine

    // Create the earth
    earth = new MTSphere(pa, "earth", 40, 40, 80, TextureMode.Projected); // TextureMode.Polar);
    earth.setLight(light);
    earth.setMaterial(material);
    earth.rotateX(earth.getCenterPointRelativeToParent(), -90);
    earth.setTexture(
        new GLTexture(
            pa,
            imagesPath + "worldMap.jpg",
            new GLTextureSettings(
                TEXTURE_TARGET.TEXTURE_2D,
                SHRINKAGE_FILTER.Trilinear,
                EXPANSION_FILTER.Bilinear,
                WRAP_MODE.CLAMP_TO_EDGE,
                WRAP_MODE.CLAMP_TO_EDGE)));
    earth.generateAndUseDisplayLists();
    earth.setPositionGlobal(
        new Vector3D(
            pa.width / 2f,
            pa.height / 2f,
            250)); // earth.setPositionGlobal(new Vector3D(200, 200, 250));
    // Animate earth rotation
    new Animation(
            "rotation animation", new MultiPurposeInterpolator(0, 360, 17000, 0, 1, -1), earth)
        .addAnimationListener(
            new IAnimationListener() {
              public void processAnimationEvent(AnimationEvent ae) {
                earth.rotateY(earth.getCenterPointLocal(), ae.getDelta(), TransformSpace.LOCAL);
              }
            })
        .start();

    // Put planets in a group that can be manipulated by gestures
    // so the rotation of the planets doesent get changed by the gestures
    MTComponent group = new MTComponent(mtApplication);
    group.setComposite(
        true); // This makes the group "consume" all picking and gestures of the children
    group.registerInputProcessor(new DragProcessor(mtApplication));
    group.addGestureListener(DragProcessor.class, new DefaultDragAction());
    group.addGestureListener(DragProcessor.class, new InertiaDragAction(80, 0.8f, 10));
    group.registerInputProcessor(new RotateProcessor(mtApplication));
    group.addGestureListener(RotateProcessor.class, new DefaultRotateAction());
    // Scale the earth from the center. Else it might get distorted
    group.registerInputProcessor(new ScaleProcessor(mtApplication));
    group.addGestureListener(
        ScaleProcessor.class,
        new IGestureEventListener() {
          public boolean processGestureEvent(MTGestureEvent ge) {
            ScaleEvent se = (ScaleEvent) ge;
            earth.scaleGlobal(
                se.getScaleFactorX(),
                se.getScaleFactorY(),
                se.getScaleFactorX(),
                earth.getCenterPointGlobal());
            return false;
          }
        });
    this.getCanvas().addChild(group);
    group.addChild(earth);

    // Create the moon
    final MTSphere moonSphere = new MTSphere(pa, "moon", 35, 35, 25, TextureMode.Polar);
    moonSphere.setMaterial(material);
    moonSphere.translate(new Vector3D(earth.getRadius() + moonSphere.getRadius() + 50, 0, 0));
    moonSphere.setTexture(
        new GLTexture(
            pa,
            imagesPath + "moonmap1k.jpg",
            new GLTextureSettings(
                TEXTURE_TARGET.RECTANGULAR,
                SHRINKAGE_FILTER.Trilinear,
                EXPANSION_FILTER.Bilinear,
                WRAP_MODE.CLAMP_TO_EDGE,
                WRAP_MODE.CLAMP_TO_EDGE)));
    moonSphere.generateAndUseDisplayLists();
    moonSphere.unregisterAllInputProcessors();
    // Rotate the moon around the earth
    new Animation(
            "moon animation", new MultiPurposeInterpolator(0, 360, 12000, 0, 1, -1), moonSphere)
        .addAnimationListener(
            new IAnimationListener() {
              public void processAnimationEvent(AnimationEvent ae) {
                moonSphere.rotateZ(
                    earth.getCenterPointLocal(), ae.getDelta(), TransformSpace.RELATIVE_TO_PARENT);
              }
            })
        .start();
    // Rotate the moon around ints own center
    new Animation(
            "moon animation around own axis",
            new MultiPurposeInterpolator(0, 360, 9000, 0, 1, -1),
            moonSphere)
        .addAnimationListener(
            new IAnimationListener() {
              public void processAnimationEvent(AnimationEvent ae) {
                moonSphere.rotateZ(
                    moonSphere.getCenterPointLocal(), -3 * ae.getDelta(), TransformSpace.LOCAL);
                moonSphere.rotateY(
                    moonSphere.getCenterPointLocal(), 0.5f * ae.getDelta(), TransformSpace.LOCAL);
              }
            })
        .start();
    earth.addChild(moonSphere);
  }