コード例 #1
0
ファイル: Geometry.java プロジェクト: mjs513/cs4620-1
  /*
   * Sends a quadrilateral to the OpenGL pipeline. Mimics the other quad
   * function.
   */
  public static void quad(
      Vector3f v0, Vector3f v1, Vector3f v2, Vector3f v3, Vector3f n, Color3f c, GLAutoDrawable d) {

    GL2 gl = d.getGL().getGL2();

    gl.glBegin(GL2.GL_QUADS);

    gl.glColor3f(c.x, c.y, c.z);
    gl.glNormal3f(n.x, n.y, n.z);

    gl.glTexCoord2f(t0.x, t0.y);
    gl.glVertex3f(v0.x, v0.y, v0.z);

    gl.glTexCoord2f(t1.x, t1.y);
    gl.glVertex3f(v1.x, v1.y, v1.z);

    gl.glTexCoord2f(t2.x, t2.y);
    gl.glVertex3f(v2.x, v2.y, v2.z);

    gl.glTexCoord2f(t3.x, t3.y);
    gl.glVertex3f(v3.x, v3.y, v3.z);

    gl.glEnd();
  }
コード例 #2
0
  /** Called back by the animator to perform rendering. */
  @Override
  public void display(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2(); // get the OpenGL 2 graphics context
    gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear color and depth buffers

    for (int i = 0; i < stars.length; i++) {
      // Reset the view (x, y, z axes back to normal)
      gl.glLoadIdentity();
      gl.glTranslatef(0.0f, 0.0f, z);

      // The stars are texture quad square drawn on x-y plane and
      // distributed on on x-z plane around the y-axis

      // Initial 90 degree tile in the x-axis, y-axis pointing out of screen
      gl.glRotatef(tilt, 1.0f, 0.0f, 0.0f);
      // Rotate about y-axis (pointing out of screen), initial angle is 0
      gl.glRotatef(stars[i].angle, 0.0f, 1.0f, 0.0f);
      // Translate about the x-axis (pointing right) to its current distance
      gl.glTranslatef(stars[i].distance, 0.0f, 0.0f);

      // The stars have initial angle of 0, and initial distance linearly
      // distributed between 0 and 5.0f

      // Rotate the axes back, so that z-axis is again facing us, to ensure that
      // the quad (with texture) on x-y plane is facing us
      gl.glRotatef(-stars[i].angle, 0.0f, 1.0f, 0.0f);
      gl.glRotatef(-tilt, 1.0f, 0.0f, 0.0f);

      // Take note that without the two rotations and undo, there is only one
      // translation along the x-axis
      // Matrix operation is non-commutative. That is, AB != BA.
      // Hence, ABCB'A' != C

      // Draw the star, which spins on the z axis (pointing out of the screen)
      gl.glRotatef(starSpinAngle, 0.0f, 0.0f, 1.0f);
      // Set the star's color using bytes (why bytes? not float or int?)
      gl.glColor4ub(stars[i].r, stars[i].g, stars[i].b, (byte) 255);
      gl.glBegin(GL_QUADS);
      // draw a square on x-y plane
      gl.glTexCoord2f(textureCoordLeft, textureCoordBottom);
      gl.glVertex3f(-1.0f, -1.0f, 0.0f);
      gl.glTexCoord2f(textureCoordRight, textureCoordBottom);
      gl.glVertex3f(1.0f, -1.0f, 0.0f);
      gl.glTexCoord2f(textureCoordRight, textureCoordTop);
      gl.glVertex3f(1.0f, 1.0f, 0.0f);
      gl.glTexCoord2f(textureCoordLeft, textureCoordTop);
      gl.glVertex3f(-1.0f, 1.0f, 0.0f);
      gl.glEnd();

      // If twinkling, overlay with another drawing of an arbitrary color
      if (twinkleOn) {
        // Assign a color using bytes
        gl.glColor4ub(
            stars[(numStars - i) - 1].r,
            stars[(numStars - i) - 1].g,
            stars[(numStars - i) - 1].b,
            (byte) 255);
        gl.glBegin(GL_QUADS);
        // draw a square on x-y plane
        gl.glTexCoord2f(textureCoordLeft, textureCoordBottom);
        gl.glVertex3f(-1.0f, -1.0f, 0.0f);
        gl.glTexCoord2f(textureCoordRight, textureCoordBottom);
        gl.glVertex3f(1.0f, -1.0f, 0.0f);
        gl.glTexCoord2f(textureCoordRight, textureCoordTop);
        gl.glVertex3f(1.0f, 1.0f, 0.0f);
        gl.glTexCoord2f(textureCoordLeft, textureCoordTop);
        gl.glVertex3f(-1.0f, 1.0f, 0.0f);
        gl.glEnd();
      }

      // Update for the next refresh
      // The star spins about the z-axis (pointing out of the screen), and spiral
      // inwards and collapse towards the center, by increasing the angle on x-y
      // plane and reducing the distance.

      starSpinAngle += 0.01f; // used to spin the stars about the z-axis
      // spiral pattern
      stars[i].angle += (float) i / numStars; // changes the angle of a star
      // collapsing the star to the center
      stars[i].distance -= 0.01f; // changes the distance of a star
      // re-bone at the edge
      if (stars[i].distance < 0.0f) { // Is the star collapsed to the center?
        stars[i].distance += 5.0f; // move to the outer ring
        stars[i].setRandomRGB(); // choose a random color for the star
      }
    }
  }
コード例 #3
0
ファイル: Geometry.java プロジェクト: mjs513/cs4620-1
  /*
   * Recursively generates a sphere using triangles and puts the resulting
   * polygons into the OpenGL pipeline. Mimics the other spheretri function.
   */
  private static void spheretri(
      int n, Vector3f v0, Vector3f v1, Vector3f v2, Color3f c, GLAutoDrawable d) {

    GL2 gl = d.getGL().getGL2();

    if (n == 0) {
      if (pipe.isFlatShaded()) {
        nrml.add(v0, v1);
        nrml.add(v2);
        nrml.normalize();

        gl.glBegin(GL.GL_TRIANGLES);

        gl.glColor3f(c.x, c.y, c.z);
        gl.glNormal3f(nrml.x, nrml.y, nrml.z);

        gl.glVertex3f(v0.x, v0.y, v0.z);
        gl.glVertex3f(v1.x, v1.y, v1.z);
        gl.glVertex3f(v2.x, v2.y, v2.z);

        gl.glEnd();
      } else {
        gl.glBegin(GL.GL_TRIANGLES);

        gl.glColor3f(c.x, c.y, c.z);
        xyTex(v0, texs[0]);
        xyTex(v1, texs[1]);
        xyTex(v2, texs[2]);

        gl.glNormal3f(v0.x, v0.y, v0.z);
        gl.glTexCoord2f(texs[0].x, texs[0].y);
        gl.glVertex3f(v0.x, v0.y, v0.z);

        gl.glNormal3f(v1.x, v1.y, v1.z);
        gl.glTexCoord2f(texs[1].x, texs[1].y);
        gl.glVertex3f(v1.x, v1.y, v1.z);

        gl.glNormal3f(v2.x, v2.y, v2.z);
        gl.glTexCoord2f(texs[2].x, texs[2].y);
        gl.glVertex3f(v2.x, v2.y, v2.z);

        gl.glEnd();
      }
    } else {
      Vector3f v01 = new Vector3f();
      Vector3f v12 = new Vector3f();
      Vector3f v20 = new Vector3f();

      v01.add(v0, v1);
      v01.normalize();
      v12.add(v1, v2);
      v12.normalize();
      v20.add(v2, v0);
      v20.normalize();

      spheretri(n - 1, v01, v12, v20, c, d);
      spheretri(n - 1, v0, v01, v20, c, d);
      spheretri(n - 1, v1, v12, v01, c, d);
      spheretri(n - 1, v2, v20, v12, c, d);
    }
  }