예제 #1
0
  private void applyLight(GL2 gl) {
    // Prepare light parameters.
    float SHINE_ALL_DIRECTIONS = 1;
    float[] lightPos = {0, 40, -100, SHINE_ALL_DIRECTIONS};
    float[] lightColorAmbient = {0.2f, 0.2f, 0.2f, 0.5f};
    float[] lightColorDiffuse = {1f, 1f, 1f, 0.5f};
    float[] lightColorSpecular = {0.8f, 0.8f, 0.8f, 0.5f};

    // Set light parameters.
    gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, lightPos, 0);
    gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_AMBIENT, lightColorAmbient, 0);
    gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_SPECULAR, lightColorSpecular, 0);
    gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_DIFFUSE, lightColorDiffuse, 0);

    gl.glLightfv(GL2.GL_LIGHT2, GL2.GL_POSITION, new float[] {0, 15, -40, SHINE_ALL_DIRECTIONS}, 0);
    gl.glLightfv(GL2.GL_LIGHT2, GL2.GL_AMBIENT, lightColorAmbient, 0);
    gl.glLightfv(GL2.GL_LIGHT2, GL2.GL_SPECULAR, lightColorSpecular, 0);
    gl.glLightfv(GL2.GL_LIGHT2, GL2.GL_DIFFUSE, lightColorDiffuse, 0);

    // Enable lighting in GL.
    gl.glEnable(GL2.GL_LIGHT1);
    // gl.glEnable(GL2.GL_LIGHT2);
    gl.glEnable(GL2.GL_LIGHTING);

    // Set material properties.
    //        float[] rgba = {0.3f, 0.5f, 1f};
    float[] rgba = {0.8f, 0.8f, 1f, 1f};
    gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT, rgba, 0);
    gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_SPECULAR, rgba, 0);
    gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_DIFFUSE, rgba, 0);
    gl.glMaterialf(GL2.GL_FRONT, GL2.GL_SHININESS, 20f);
  }
예제 #2
0
파일: Teapot.java 프로젝트: since2014/jogl
 private void enableStates(final GL2 gl, final boolean enable) {
   if (enable) {
     if (null != tex) {
       tex.bind(gl);
     }
     gl.glEnable(GL.GL_DEPTH_TEST);
     gl.glDepthFunc(GL.GL_LESS); // default
     // gl.glEnable(GL2.GL_TEXTURE_GEN_S);
     // gl.glEnable(GL2.GL_TEXTURE_1D);
     gl.glEnable(GL.GL_TEXTURE_2D);
     gl.glEnable(GL.GL_CULL_FACE);
     gl.glEnable(GLLightingFunc.GL_LIGHTING);
     gl.glEnable(GLLightingFunc.GL_LIGHT0);
     gl.glEnable(GL2.GL_AUTO_NORMAL);
     gl.glEnable(GLLightingFunc.GL_NORMALIZE);
     gl.glFrontFace(GL.GL_CW);
     gl.glCullFace(GL.GL_BACK); // default
     gl.glMaterialf(GL.GL_FRONT, GLLightingFunc.GL_SHININESS, 64.0f);
     gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
   } else {
     if (null != tex) {
       gl.glBindTexture(tex.getTarget(), 0);
     }
     gl.glDisable(GL.GL_DEPTH_TEST);
     // gl.glDisable(GL2.GL_TEXTURE_GEN_S);
     // gl.glDisable(GL2.GL_TEXTURE_1D);
     gl.glDisable(GL.GL_TEXTURE_2D);
     gl.glDisable(GL.GL_CULL_FACE);
     gl.glDisable(GLLightingFunc.GL_LIGHTING);
     gl.glDisable(GLLightingFunc.GL_LIGHT0);
     gl.glDisable(GL2.GL_AUTO_NORMAL);
     gl.glDisable(GLLightingFunc.GL_NORMALIZE);
     gl.glFrontFace(GL.GL_CCW); // default
   }
 }
 @Override
 public void init(GLAutoDrawable drawable) {
   GL2 gl = drawable.getGL().getGL2();
   //		java.nio.IntBuffer maxlights = Buffers.newDirectIntBuffer(1);
   int[] maxlights = new int[1];
   gl.glGetIntegerv(GL2.GL_MAX_LIGHTS, maxlights, 0);
   System.out.println(maxlights[0]);
   // 背景を白く塗りつぶす.
   gl.glClearColor(1f, 1f, 1f, 1.0f);
   gl.glEnable(GL_DEPTH_TEST);
   gl.glEnable(GL_CULL_FACE);
   gl.glCullFace(GL_FRONT);
   gl.glEnable(GL_LIGHTING);
   gl.glEnable(GL_LIGHT0);
   gl.glEnable(GL_LIGHT1);
   //		gl.glLightfv(GL_LIGHT1, GL_DIFFUSE, green, 0);
   //		gl.glLightfv(GL_LIGHT1, GL_SPECULAR, green, 0);
 }
예제 #4
0
  /*
   * Initialize z-buffer, projection matrix, light source, and lighting model.
   * Do not specify a material property here.
   */
  public void init(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();
    glut = new GLUT();
    //
    float position[] = {0.0f, 3.0f, 3.0f, 0.0f};
    float local_view[] = {0.0f};

    gl.glEnable(GL.GL_DEPTH_TEST);
    gl.glDepthFunc(GL.GL_LESS);

    gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, position, 0);
    gl.glLightModelfv(GL2.GL_LIGHT_MODEL_LOCAL_VIEWER, local_view, 0);

    gl.glFrontFace(GL.GL_CW);
    gl.glEnable(GL2.GL_LIGHTING);
    gl.glEnable(GL2.GL_LIGHT0);
    gl.glEnable(GL2.GL_AUTO_NORMAL);
    gl.glEnable(GL2.GL_NORMALIZE);
    gl.glEnable(GL2.GL_FOG);
    {
      float fogColor[] = {0.5f, 0.5f, 0.5f, 1.0f};

      fogMode = GL2.GL_EXP;
      gl.glFogi(GL2.GL_FOG_MODE, fogMode);
      gl.glFogfv(GL2.GL_FOG_COLOR, fogColor, 0);
      gl.glFogf(GL2.GL_FOG_DENSITY, 0.35f);
      gl.glHint(GL2.GL_FOG_HINT, GL.GL_DONT_CARE);

      gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    }
  }
예제 #5
0
  @Override
  public void init(GLAutoDrawable drawable) {
    final GL2 gl = drawable.getGL().getGL2();
    gl.glClearColor(0f, 0f, 0f, 0f);
    gl.glClearDepth(1.0f);

    gl.glShadeModel(GL2.GL_SMOOTH);
    gl.glEnable(GL2.GL_DEPTH_TEST);

    // gl.glEnable(GL2.GL_CULL_FACE); // don't render face backside of triangles
    // gl.glCullFace(GL2.GL_BACK); // backside

    gl.glDepthFunc(GL2.GL_LEQUAL);
    gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);

    loadTextures();
    loadModels();
    setQuadParams();

    addTestListData();
    PassiveObjectMovement.getInstance().startThread(s);
  }
  @Override
  public void init(GLAutoDrawable pDrawable) {
    // Use debug pipeline
    // drawable.setGL(new DebugGL(drawable.getGL()));

    GL2 gl = pDrawable.getGL().getGL2();
    // System.err.println("INIT GL IS: " + gl.getClass().getName());

    checkRequiredExtensions(gl);

    // Enable VSync
    gl.setSwapInterval(1);

    // Setup the drawing area and shading mode
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    gl.glClearDepth(1.0);
    // sky blue color
    gl.glClearColor(0.17f, 0.65f, 0.92f, 0.0f);

    gl.glEnable(GL.GL_DEPTH_TEST);
    // int[] depth_bits = new int[1];
    // gl.glGetIntegerv(GL.GL_DEPTH_BITS, depth_bits, 0);

    gl.glShadeModel(GLLightingFunc.GL_SMOOTH);

    float[] grayCol = {0.8f, 0.8f, 0.8f, 1.0f};
    // float[] blueCol = {0.0f, 0.0f, 0.8f, 1.0f};
    gl.glMaterialfv(GL.GL_FRONT, GLLightingFunc.GL_AMBIENT_AND_DIFFUSE, grayCol, 0);

    // this.modelRender = ModelRender.getInstance();

    axisLabels.init();
    renderJosm.init(gl);
    // selectionDrawUtil.init(gl);
    compass.init(gl);

    lightRender.init(gl);
  }
예제 #7
0
 private void enableTransparency(final GL2 gl) {
   gl.glEnable(GL2.GL_BLEND);
   gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
 }
  @Override
  public void display(GLAutoDrawable pDrawable) {

    if (error) {
      return;
    }

    countFps();

    simpleMoveAnimator.updateState();

    GL2 gl = pDrawable.getGL().getGL2();
    // System.err.println("INIT GL IS: " + gl.getClass().getName());

    GLU glu = new GLU();

    lightRender.draw(gl);

    // _direction_
    // gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_POSITION,
    // this.lightPos, 0);

    // // Clear the drawing area
    // gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
    //
    //
    //
    //
    // // Reset the current matrix to the "identity"
    // gl.glLoadIdentity();
    //
    // gl.glMatrixMode(GL2.GL_MODELVIEW);
    // gl.glLoadIdentity();

    // clear color and depth buffers
    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
    // gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
    gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
    gl.glLoadIdentity();

    setCamera(gl, glu, simpleMoveAnimator);

    gl.glEnable(GL.GL_MULTISAMPLE);

    skyBox.draw(gl, simpleMoveAnimator.getPoint());

    ground.draw(gl, simpleMoveAnimator.getPoint());

    if (modelRender.isDebugging()) {

      axisLabels.draw(gl);

      floor.draw(gl);

      // drawTextInfo(gl, simpleMoveAnimator.info());
    }

    renderJosm.draw(gl, simpleMoveAnimator);

    // selectionDrawUtil.draw(gl, objectSelectionListener,
    // simpleMoveAnimator);

    drawCompass(gl);

    // Flush all drawing operations to the graphics card
    gl.glFlush();
  }
예제 #9
0
  /**
   * //! Draw an x-y-z frame.
   *
   * @param aAxisLengthScale
   * @param aAxisThicknessScale
   * @param aModifyMaterialState
   */
  public static void jDrawFrame(
      final double aAxisLengthScale,
      final double aAxisThicknessScale,
      final boolean aModifyMaterialState) {
    // Triangle vertices:
    int nTriangles = 8;

    // Quad vertices:
    int nQuads = 16;

    GL2 gl = GLContext.getCurrent().getGL().getGL2();
    // set material properties
    float[] fnull = {0, 0, 0, 0};
    gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_SPECULAR, fnull, 0);
    gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_EMISSION, fnull, 0);
    gl.glColorMaterial(GL2.GL_FRONT_AND_BACK, GL2.GL_AMBIENT_AND_DIFFUSE);
    gl.glEnable(GL2.GL_COLOR_MATERIAL);
    gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_FILL);

    // enable vertex and normal arrays
    gl.glEnableClientState(GL2.GL_NORMAL_ARRAY);
    gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);

    if (aModifyMaterialState) {
      gl.glEnable(GL2.GL_COLOR_MATERIAL);
      gl.glColorMaterial(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE);
      gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL);
    }

    for (int k = 0; k < 3; k++) {
      gl.glPushMatrix();

      // Rotate to the appropriate axis
      if (k == 0) {
        gl.glRotatef(-90.0f, 0, 1, 0);
        gl.glColor3f(1.0f, 0.0f, 0.0f);
      } else if (k == 1) {
        gl.glRotatef(90.0f, 1, 0, 0);
        gl.glColor3f(0.0f, 1.0f, 0.0f);
      } else {
        gl.glRotatef(180.0f, 1, 0, 0);
        gl.glColor3f(0.0f, 0.0f, 1.0f);
      }

      // scaling
      gl.glScaled(aAxisThicknessScale, aAxisThicknessScale, aAxisLengthScale);

      // render frame object

      gl.glVertexPointer(3, GL2.GL_FLOAT, 0, triVerticesBuffer);
      gl.glNormalPointer(GL2.GL_FLOAT, 0, triNormalsBuffer);

      gl.glDrawArrays(GL2.GL_TRIANGLES, 0, nTriangles * 3);

      gl.glVertexPointer(3, GL2.GL_FLOAT, 0, quadVerticesBuffer);
      gl.glNormalPointer(GL2.GL_FLOAT, 0, quadNormalsBuffer);

      gl.glDrawArrays(GL2.GL_QUADS, 0, nQuads * 4);

      gl.glPopMatrix();
    }

    // disable vertex and normal arrays
    gl.glDisableClientState(GL2.GL_NORMAL_ARRAY);
    gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
  }
예제 #10
0
  @Override
  public void display(GLAutoDrawable drawable) {
    GL2 gl2 = drawable.getGL().getGL2();
    GLU glu = GLU.createGLU();

    gl2.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
    gl2.glEnable(GL2.GL_DEPTH_TEST);
    gl2.glDisable(GL2.GL_CULL_FACE);

    gl2.glMatrixMode(GL2.GL_PROJECTION);
    gl2.glLoadIdentity();
    glu.gluPerspective(60, drawable.getSurfaceWidth() / drawable.getSurfaceHeight(), 0.1f, 100);

    gl2.glMatrixMode(GL2.GL_MODELVIEW);
    gl2.glLoadIdentity();

    camera_.apply(gl2, glu);
    light_.apply(gl2, glu);

    terrain_.draw(gl2, glu);

    Runnable triangle =
        () -> {
          gl2.glBegin(GL2.GL_TRIANGLES);
          {
            gl2.glColor3f(1, 1, 1);
            gl2.glNormal3f(0, 0, 1);
            gl2.glVertex3f(0, 1, 0);
            gl2.glColor3f(1, 0, 0);
            gl2.glNormal3f(0, 0, 1);
            gl2.glVertex3f(-0.87f, -0.5f, 0);
            gl2.glColor3f(0, 0, 1);
            gl2.glNormal3f(0, 0, 1);
            gl2.glVertex3f(0.87f, -0.5f, 0);

            gl2.glColor3f(1, 1, 1);
            gl2.glNormal3f(0.71898836f, 0.4170133f, -0.5560177f);
            gl2.glVertex3f(0, 1, 0);
            gl2.glColor3f(0, 0, 1);
            gl2.glNormal3f(0.71898836f, 0.4170133f, -0.5560177f);
            gl2.glVertex3f(0.87f, -0.5f, 0);
            gl2.glColor3f(0, 1, 0);
            gl2.glNormal3f(0.71898836f, 0.4170133f, -0.5560177f);
            gl2.glVertex3f(0, 0, -0.75f);

            gl2.glColor3f(1, 0, 0);
            gl2.glNormal3f(-0.7189883f, 0.41701326f, -0.5560177f);
            gl2.glVertex3f(-0.87f, -0.5f, 0);
            gl2.glColor3f(1, 1, 1);
            gl2.glNormal3f(-0.7189883f, 0.41701326f, -0.5560177f);
            gl2.glVertex3f(0, 1, 0);
            gl2.glColor3f(0, 1, 0);
            gl2.glNormal3f(-0.7189883f, 0.41701326f, -0.5560177f);
            gl2.glVertex3f(0, 0, -0.75f);

            gl2.glColor3f(0, 0, 1);
            gl2.glNormal3f(0, -1.305f, -0.87f);
            gl2.glVertex3f(0.87f, -0.5f, 0);
            gl2.glColor3f(1, 0, 0);
            gl2.glNormal3f(0, -1.305f, -0.87f);
            gl2.glVertex3f(-0.87f, -0.5f, 0);
            gl2.glColor3f(0, 1, 0);
            gl2.glNormal3f(0, -1.305f, -0.87f);
            gl2.glVertex3f(0, 0, -0.75f);
          }
          gl2.glEnd();
        };

    gl2.glPushMatrix();
    {
      gl2.glPushMatrix();
      gl2.glTranslatef(0, 0, 0);
      gl2.glRotatef(angle__, 0, 1, 0);
      triangle.run();
      gl2.glPopMatrix();

      gl2.glPushMatrix();
      gl2.glTranslatef(0, 3.2f, 0);
      gl2.glRotatef(angle__ + 30, 0, 1, 0);
      triangle.run();
      gl2.glPopMatrix();

      gl2.glPushMatrix();
      gl2.glTranslatef(3.2f, 0, 0);
      gl2.glRotatef(angle__ + 60, 0, 1, 0);
      triangle.run();
      gl2.glPopMatrix();
    }
    gl2.glPopMatrix();
    angle__ += 1;

    gl2.glPushMatrix();
    gl2.glTranslatef(0.05f, -1.5f, 5);
    gl2.glScalef(0.05f, 0.05f, 0.05f);
    for (Face face : glock3__.faces) {
      gl2.glBegin(GL2.GL_POLYGON);
      for (FaceVertex vertex : face.vertices) {
        gl2.glColor3f(1, 1, 1);
        gl2.glNormal3f(vertex.n.x, vertex.n.y, vertex.n.z);
        gl2.glVertex3f(vertex.v.x, vertex.v.y, vertex.v.z);
      }
      gl2.glEnd();
    }
    gl2.glPopMatrix();
  }
  @Override
  public void display(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();
    gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

    gl.glShadeModel(GL2.GL_SMOOTH); // Enable Smooth Shading (Gouraud)

    // Display the scene according to the current trackball orientation, scale the scene to fit
    // better
    tbc.prepareForDisplay(drawable);
    gl.glScaled(3, 3, 3);

    // Add a light
    int lightNumber = 0;
    float[] position = {lightPos.x, lightPos.y, lightPos.z, 1};
    float[] colour = {
      lightColR.getFloatValue(), lightColG.getFloatValue(), lightColB.getFloatValue(), 1
    };
    float[] acolour = {
      colour[0] * ambient.getFloatValue(),
      colour[1] * ambient.getFloatValue(),
      colour[2] * ambient.getFloatValue()
    };
    gl.glLightfv(GL2.GL_LIGHT0 + lightNumber, GL2.GL_SPECULAR, colour, 0);
    gl.glLightfv(GL2.GL_LIGHT0 + lightNumber, GL2.GL_DIFFUSE, colour, 0);
    gl.glLightfv(GL2.GL_LIGHT0 + lightNumber, GL2.GL_AMBIENT, acolour, 0);
    gl.glLightfv(
        GL2.GL_LIGHT0 + lightNumber,
        GL2.GL_POSITION,
        position,
        0); // transformed by the modelview matrix when glLight is called
    gl.glEnable(GL2.GL_LIGHT0 + lightNumber);

    // Determine which shader to display the scene with

    ShaderState customShader = null;

    if (viewingMode == 1) {
      stateCheckerboard.useProgram(gl, false);
      stateWoodcut.useProgram(gl, false);
      stateToon.useProgram(gl, false);
      statePerFragment.useProgram(gl, usePerFragment);
      if (usePerFragment) customShader = statePerFragment;
      else customShader = null;
    } else if (viewingMode == 2) {
      statePerFragment.useProgram(gl, false);
      stateWoodcut.useProgram(gl, false);
      stateToon.useProgram(gl, false);
      stateCheckerboard.useProgram(gl, useCheckerboard);
      if (useCheckerboard) customShader = stateCheckerboard;
      else customShader = null;
    } else if (viewingMode == 3) {
      statePerFragment.useProgram(gl, false);
      stateCheckerboard.useProgram(gl, false);
      stateToon.useProgram(gl, false);
      stateWoodcut.useProgram(gl, useWoodcut);
      if (useWoodcut) customShader = stateWoodcut;
      else customShader = null;
    } else if (viewingMode == 4) {
      statePerFragment.useProgram(gl, false);
      stateCheckerboard.useProgram(gl, false);
      stateWoodcut.useProgram(gl, false);
      stateToon.useProgram(gl, useToon);
      if (useToon) customShader = stateToon;
      else customShader = null;
    }

    // Setup the uniform values that may be used within the current shader
    if (customShader != null) {
      // Blinn-Phong uniforms
      int specExponentUniformLocation = customShader.getUniformLocation(gl, "shininess");
      gl.glUniform1f(specExponentUniformLocation, shininess.getFloatValue());
      int ambientUniformLocation = customShader.getUniformLocation(gl, "ambient");
      gl.glUniform1f(ambientUniformLocation, ambient.getFloatValue());

      // Checkerboard uniforms
      int colour1UniformLocation = customShader.getUniformLocation(gl, "Color1");
      float[] colour1 = {col1R.getFloatValue(), col1G.getFloatValue(), col1B.getFloatValue()};
      gl.glUniform3fv(colour1UniformLocation, 1, colour1, 0);
      int colour2UniformLocation = customShader.getUniformLocation(gl, "Color2");
      float[] colour2 = {col2R.getFloatValue(), col2G.getFloatValue(), col2B.getFloatValue()};
      gl.glUniform3fv(colour2UniformLocation, 1, colour2, 0);
      int avgColourUniformLocation = customShader.getUniformLocation(gl, "AvgColor");
      float[] avgColour = {
        (colour1[0] + colour2[0]) / 2, (colour1[1] + colour2[1]) / 2, (colour1[2] + colour2[2]) / 2
      };
      gl.glUniform3fv(avgColourUniformLocation, 1, avgColour, 0);
      int frequencyUniformLocation = customShader.getUniformLocation(gl, "Frequency");
      double frequencyValue = frequency.getValue();
      frequencyValue = useIntFrequency.getValue() ? Math.round(frequencyValue) : frequencyValue;
      gl.glUniform1f(frequencyUniformLocation, (float) frequencyValue);
      int useAveragingUniformLocation = customShader.getUniformLocation(gl, "UseAveraging");
      gl.glUniform1i(useAveragingUniformLocation, useAveraging.getValue() ? 1 : 0);
      int useSmoothStepUniformLocation = customShader.getUniformLocation(gl, "UseSmoothStep");
      gl.glUniform1i(useSmoothStepUniformLocation, useSmoothStep.getValue() ? 1 : 0);

      // Woodcut uniforms
      int timeUniformLocation = customShader.getUniformLocation(gl, "Time");
      gl.glUniform1f(timeUniformLocation, time.getFloatValue());
      int lightPositionUniformLocation = customShader.getUniformLocation(gl, "LightPosition");
      float[] lightPosition = {lightPos.x, lightPos.y, lightPos.z};
      gl.glUniform3fv(lightPositionUniformLocation, 1, lightPosition, 0);

      // Toon uniforms
      int threshHighUniformLocation = customShader.getUniformLocation(gl, "ThresholdHigh");
      gl.glUniform1f(threshHighUniformLocation, thresholdHigh.getFloatValue());
      int threshMedUniformLocation = customShader.getUniformLocation(gl, "ThresholdMedium");
      gl.glUniform1f(threshMedUniformLocation, thresholdMedium.getFloatValue());
      int threshLowUniformLocation = customShader.getUniformLocation(gl, "ThresholdLow");
      gl.glUniform1f(threshLowUniformLocation, thresholdLow.getFloatValue());
    }

    // Draw teapot
    gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, new float[] {1, 1, 0, 1}, 0);
    gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL2.GL_SPECULAR, new float[] {1, 1, 1, 1}, 0);
    gl.glMaterialf(GL.GL_FRONT_AND_BACK, GL2.GL_SHININESS, 50);
    glut.glutSolidTeapot(1);

    // Draw table
    gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, new float[] {0, 1, 1, 1}, 0);
    gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL2.GL_SPECULAR, new float[] {1, 1, 1, 1}, 0);
    gl.glMaterialf(GL.GL_FRONT_AND_BACK, GL2.GL_SHININESS, 50);
    gl.glPushMatrix();
    gl.glTranslated(0, -0.8, 0);
    gl.glBegin(GL2.GL_TRIANGLE_FAN);
    gl.glNormal3d(0, 1, 0);
    gl.glTexCoord2d(0, 0);
    gl.glVertex3d(-10, 0, -10);
    gl.glTexCoord2d(0, 1);
    gl.glVertex3d(-10, 0, 10);
    gl.glTexCoord2d(1, 1);
    gl.glVertex3d(10, 0, 10);
    gl.glTexCoord2d(1, 0);
    gl.glVertex3d(10, 0, -10);
    gl.glEnd();
    gl.glPopMatrix();
  }
  @Override
  public void init(GLAutoDrawable drawable) {
    drawable.setGL(new DebugGL2(drawable.getGL().getGL2()));
    GL2 gl = drawable.getGL().getGL2();

    gl.glClearColor(0, 0, 0, 1); // Black Background
    gl.glClearDepth(1); // Depth Buffer Setup
    gl.glEnable(GL.GL_DEPTH_TEST); // Enables Depth Testing
    gl.glDepthFunc(GL.GL_LEQUAL); // The Type Of Depth Testing To Do

    gl.glEnable(GL2.GL_LIGHTING);
    gl.glEnable(GL2.GL_LIGHT0);
    gl.glEnable(GL2.GL_NORMALIZE); // normalize normals before lighting

    // Setup perFragmentLighting shader
    String shaderNamePerFragment = "perFragmentLighting";
    ShaderCode vsCodePerFragment =
        ShaderCode.create(
            gl,
            GL2.GL_VERTEX_SHADER,
            this.getClass(),
            "shaders",
            "shader/bin",
            shaderNamePerFragment,
            false);
    ShaderCode fsCodePerFragment =
        ShaderCode.create(
            gl,
            GL2.GL_FRAGMENT_SHADER,
            this.getClass(),
            "shaders",
            "shader/bin",
            shaderNamePerFragment,
            false);
    ShaderProgram programPerFragment = new ShaderProgram();
    programPerFragment.add(vsCodePerFragment);
    programPerFragment.add(fsCodePerFragment);
    if (!programPerFragment.link(gl, System.err)) {
      throw new GLException("Couldn't link program: " + programPerFragment);
    }
    statePerFragment.attachShaderProgram(gl, programPerFragment, false);

    // Setup perVertexLighting shader
    String shaderNameCheckerboard = "checkerboard";
    ShaderCode vsCodeCheckerboard =
        ShaderCode.create(
            gl,
            GL2.GL_VERTEX_SHADER,
            this.getClass(),
            "shaders",
            "shader/bin",
            shaderNameCheckerboard,
            false);
    ShaderCode fsCodeCheckerboard =
        ShaderCode.create(
            gl,
            GL2.GL_FRAGMENT_SHADER,
            this.getClass(),
            "shaders",
            "shader/bin",
            shaderNameCheckerboard,
            false);
    ShaderProgram programCheckerboard = new ShaderProgram();
    programCheckerboard.add(vsCodeCheckerboard);
    programCheckerboard.add(fsCodeCheckerboard);
    if (!programCheckerboard.link(gl, System.err)) {
      throw new GLException("Couldn't link program: " + programCheckerboard);
    }
    stateCheckerboard.attachShaderProgram(gl, programCheckerboard, false);

    // Setup woodcut shader
    String shaderNameWoodcut = "woodcut";
    ShaderCode vsCodeWoodcut =
        ShaderCode.create(
            gl,
            GL2.GL_VERTEX_SHADER,
            this.getClass(),
            "shaders",
            "shader/bin",
            shaderNameWoodcut,
            false);
    ShaderCode fsCodeWoodcut =
        ShaderCode.create(
            gl,
            GL2.GL_FRAGMENT_SHADER,
            this.getClass(),
            "shaders",
            "shader/bin",
            shaderNameWoodcut,
            false);
    ShaderProgram programWoodcut = new ShaderProgram();
    programWoodcut.add(vsCodeWoodcut);
    programWoodcut.add(fsCodeWoodcut);
    if (!programWoodcut.link(gl, System.err)) {
      throw new GLException("Couldn't link program: " + programWoodcut);
    }
    stateWoodcut.attachShaderProgram(gl, programWoodcut, false);

    // Setup toon shader
    String shaderNameToon = "toon";
    ShaderCode vsCodeToon =
        ShaderCode.create(
            gl,
            GL2.GL_VERTEX_SHADER,
            this.getClass(),
            "shaders",
            "shader/bin",
            shaderNameToon,
            false);
    ShaderCode fsCodeToon =
        ShaderCode.create(
            gl,
            GL2.GL_FRAGMENT_SHADER,
            this.getClass(),
            "shaders",
            "shader/bin",
            shaderNameToon,
            false);
    ShaderProgram programToon = new ShaderProgram();
    programToon.add(vsCodeToon);
    programToon.add(fsCodeToon);
    if (!programToon.link(gl, System.err)) {
      throw new GLException("Couldn't link program: " + programToon);
    }
    stateToon.attachShaderProgram(gl, programToon, false);
  }
예제 #13
0
  @Override
  public void draw(final GL2 gl, final boolean picking) {
    JOGLRenderer renderer = drawer.getRenderer();
    gl.glColor3d(1.0, 1.0, 1.0);
    if (picking) {
      if (renderer.colorPicking) {
        gl.glDisable(GL.GL_DITHER);
        gl.glDisable(GLLightingFunc.GL_LIGHTING);
        gl.glDisable(GL.GL_TEXTURE);

        int viewport[] = new int[4];
        gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);

        FloatBuffer pixels = FloatBuffer.allocate(4);
        gl.glReadPixels(
            renderer.camera.getLastMousePressedPosition().x,
            viewport[3] - renderer.camera.getLastMousePressedPosition().y,
            1,
            1,
            GL.GL_RGBA,
            GL.GL_FLOAT,
            pixels);

        gl.glEnable(GL.GL_DITHER);
        gl.glEnable(GLLightingFunc.GL_LIGHTING);
        gl.glEnable(GL.GL_TEXTURE);

        // Color index = new Color(pixels.get(0), pixels.get(1), pixels.get(2));
        // System.out.println("color picked " + index.toString());
      } else {
        gl.glPushMatrix();
        gl.glInitNames();
        gl.glPushName(0);
        double alpha = 0d;
        int size = objects.size();
        double delta = size == 0 ? 0 : 1d / size;
        for (final List<T> list : objects) {
          alpha = alpha + delta;
          for (T object : list) {
            if (isFading) {
              double originalAlpha = object.getAlpha();
              object.setAlpha(originalAlpha * alpha);
              object.draw(gl, drawer, picking);
              object.setAlpha(originalAlpha);
            } else {
              object.draw(gl, drawer, picking);
            }
          }
        }

        gl.glPopName();
        gl.glPopMatrix();
      }
    } else if (drawAsList) {
      Integer index = openGLListIndex;
      if (index == null) {

        index = gl.glGenLists(1);
        gl.glNewList(index, GL2.GL_COMPILE);
        double alpha = 0d;
        int size = objects.size();
        double delta = size == 0 ? 0 : 1d / size;
        for (final List<T> list : objects) {
          alpha = alpha + delta;
          for (T object : list) {
            if (isFading) {
              double originalAlpha = object.getAlpha();
              object.setAlpha(originalAlpha * alpha);
              object.draw(gl, drawer, picking);
              object.setAlpha(originalAlpha);
            } else {
              object.draw(gl, drawer, picking);
            }
          }
        }
        gl.glEndList();
      }
      gl.glCallList(index);
      openGLListIndex = index;
    } else if (drawAsVBO) {
      if (vah == null) {
        GLU glu = renderer.getGlu();
        vah = new VertexArrayHandler(gl, glu, renderer);
        vah.buildVertexArray(getObjects());
      } else {
        // vah.loadCollada(null);
      }

    } else {
      for (final T object : getObjects()) {
        object.draw(gl, drawer, picking);
      }
    }
  }