Beispiel #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);
  }
Beispiel #2
0
 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
   }
 }
Beispiel #3
0
  private void renderRedTeapot(GL2 gl, float x, float y, float z) {
    float mat[] = new float[4];

    gl.glPushMatrix();
    gl.glTranslatef(x, y, z);
    mat[0] = 0.1745f;
    mat[1] = 0.01175f;
    mat[2] = 0.01175f;
    mat[3] = 1.0f;
    gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat, 0);
    mat[0] = 0.61424f;
    mat[1] = 0.04136f;
    mat[2] = 0.04136f;
    gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat, 0);
    mat[0] = 0.727811f;
    mat[1] = 0.626959f;
    mat[2] = 0.626959f;
    gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat, 0);
    gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 0.6f * 128.0f);
    glut.glutSolidTeapot(1.0);
    gl.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();
  }