Esempio n. 1
0
    /**
     * Draws the body.
     *
     * <p>Only coded for polygons.
     *
     * @param gl the OpenGL graphics context
     */
    public void render(GL2 gl) {
      // save the original transform
      gl.glPushMatrix();

      // transform the coordinate system from world coordinates to local coordinates
      gl.glTranslated(this.transform.getTranslationX(), this.transform.getTranslationY(), 0.0);
      // rotate about the z-axis
      gl.glRotated(Math.toDegrees(this.transform.getRotation()), 0.0, 0.0, 1.0);

      // loop over all the body fixtures for this body
      for (BodyFixture fixture : this.fixtures) {
        // get the shape on the fixture
        Convex convex = fixture.getShape();
        // check the shape type
        if (convex instanceof Polygon) {
          // since Triangle, Rectangle, and Polygon are all of
          // type Polygon in addition to their main type
          Polygon p = (Polygon) convex;

          // set the color
          gl.glColor4fv(this.color, 0);

          // fill the shape
          gl.glBegin(GL2.GL_POLYGON);
          for (Vector2 v : p.getVertices()) {
            gl.glVertex3d(v.x, v.y, 0.0);
          }
          gl.glEnd();

          // set the color
          gl.glColor4f(this.color[0] * 0.8f, this.color[1] * 0.8f, this.color[2] * 0.8f, 1.0f);

          // draw the shape
          gl.glBegin(GL.GL_LINE_LOOP);
          for (Vector2 v : p.getVertices()) {
            gl.glVertex3d(v.x, v.y, 0.0);
          }
          gl.glEnd();
        }
        // circles and other curved shapes require a little more work, so to keep
        // this example short we only include polygon shapes; see the RenderUtilities
        // in the Sandbox application
      }

      // set the original transform
      gl.glPopMatrix();
    }
Esempio n. 2
0
  public void generateDisplaylist(GL2 gl) {
    displayList = gl.glGenLists(1);
    gl.glNewList(displayList, GL2.GL_COMPILE); // TODO Possible bug GL2? or
    // GL
    gl.glBegin(GL2.GL_TRIANGLES);

    for (int faceIndex = 0; faceIndex < triangulatedMesh.getNumberOfTriangles(); faceIndex++) {
      TriangleFacet f1 = triangulatedMesh.getFacet(faceIndex);
      Vector3 normalFace = f1.getNormal();
      Vector3 color = f1.getHalfEdge().getStartVertex().getColor();

      gl.glNormal3d(normalFace.get(0), normalFace.get(1), normalFace.get(2));
      gl.glColor3d(color.get(0), color.get(1), color.get(2));

      Vertex vx1 = f1.getHalfEdge().getStartVertex();
      Vector3 v1 = vx1.getPosition();

      Vertex vx2 = f1.getHalfEdge().getNextHalfEdge().getStartVertex();
      Vector3 v2 = vx2.getPosition();

      Vertex vx3 = f1.getHalfEdge().getNextHalfEdge().getNextHalfEdge().getStartVertex();
      Vector3 v3 = vx3.getPosition();

      gl.glNormal3d(vx1.getNormal().get(0), vx1.getNormal().get(1), vx1.getNormal().get(2));
      gl.glVertex3d(v1.get(0), v1.get(1), v1.get(2));

      gl.glNormal3d(vx2.getNormal().get(0), vx2.getNormal().get(1), vx2.getNormal().get(2));
      gl.glVertex3d(v2.get(0), v2.get(1), v2.get(2));

      gl.glNormal3d(vx3.getNormal().get(0), vx3.getNormal().get(1), vx3.getNormal().get(2));
      gl.glVertex3d(v3.get(0), v3.get(1), v3.get(2));
    }

    gl.glEnd();

    gl.glEndList();
  }
Esempio n. 3
0
  /**
   * Draw a line-based box with sides parallel to the x-y-z axes.
   *
   * @param aXMin
   * @param aXMax
   * @param aYMin
   * @param aYMax
   * @param aZMin
   * @param aZMax
   */
  public static void jDrawWireBox(
      final double aXMin,
      final double aXMax,
      final double aYMin,
      final double aYMax,
      final double aZMin,
      final double aZMax) {

    GL2 gl = GLContext.getCurrent().getGL().getGL2();
    // render lines for each edge of the box
    gl.glBegin(GL2.GL_LINES);
    gl.glVertex3d(aXMin, aYMin, aZMin);
    gl.glVertex3d(aXMax, aYMin, aZMin);
    gl.glVertex3d(aXMin, aYMax, aZMin);
    gl.glVertex3d(aXMax, aYMax, aZMin);
    gl.glVertex3d(aXMin, aYMin, aZMax);
    gl.glVertex3d(aXMax, aYMin, aZMax);
    gl.glVertex3d(aXMin, aYMax, aZMax);
    gl.glVertex3d(aXMax, aYMax, aZMax);

    gl.glVertex3d(aXMin, aYMin, aZMin);
    gl.glVertex3d(aXMin, aYMax, aZMin);
    gl.glVertex3d(aXMax, aYMin, aZMin);
    gl.glVertex3d(aXMax, aYMax, aZMin);
    gl.glVertex3d(aXMin, aYMin, aZMax);
    gl.glVertex3d(aXMin, aYMax, aZMax);
    gl.glVertex3d(aXMax, aYMin, aZMax);
    gl.glVertex3d(aXMax, aYMax, aZMax);

    gl.glVertex3d(aXMin, aYMin, aZMin);
    gl.glVertex3d(aXMin, aYMin, aZMax);
    gl.glVertex3d(aXMax, aYMin, aZMin);
    gl.glVertex3d(aXMax, aYMin, aZMax);
    gl.glVertex3d(aXMin, aYMax, aZMin);
    gl.glVertex3d(aXMin, aYMax, aZMax);
    gl.glVertex3d(aXMax, aYMax, aZMin);
    gl.glVertex3d(aXMax, aYMax, aZMax);
    gl.glEnd();
  }
  @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();
  }