Beispiel #1
0
  /**
   * //! Draw a pretty arrow on the z-axis using a cone and a cylinder (using GLUT)
   *
   * @param aArrowStart
   * @param aArrowTip
   * @param aWidth
   */
  public static void jDrawArrow(
      final JVector3d aArrowStart, final JVector3d aArrowTip, final double aWidth) {

    GL2 gl = GLContext.getCurrent().getGL().getGL2();

    gl.glPushMatrix();

    // We don't really care about the up vector, but it can't
    // be parallel to the arrow...
    JVector3d up = new JVector3d(0, 1, 0);
    // JVector3d arrow = aArrowTip-aArrowStart;
    JVector3d arrow = new JVector3d(0, 0, 0);
    arrow.normalize();
    double d = Math.abs(JMaths.jDot(up, arrow));
    if (d > .9) {
      up = new JVector3d(1, 0, 0);
    }

    JMatrixGL.jLookAt(gl, aArrowStart, aArrowTip, up);

    double distance = JMaths.jDistance(aArrowTip, aArrowStart);

    // This flips the z axis around
    gl.glRotatef(180, 1, 0, 0);

    // create a new OpenGL quadratic object
    GLUquadric quadObj;
    quadObj = glu.gluNewQuadric();

    // set rendering style
    glu.gluQuadricDrawStyle(quadObj, GLU.GLU_FILL);

    // set normal-rendering mode
    glu.gluQuadricNormals(quadObj, GLU.GLU_SMOOTH);

    // render a cylinder and a cone
    gl.glRotatef(180, 1, 0, 0);
    glu.gluDisk(quadObj, 0, aWidth, 10, 10);
    gl.glRotatef(180, 1, 0, 0);

    glu.gluCylinder(quadObj, aWidth, aWidth, distance * ARROW_CYLINDER_PORTION, 10, 10);
    gl.glTranslated(0, 0, ARROW_CYLINDER_PORTION * distance);

    gl.glRotatef(180, 1, 0, 0);
    glu.gluDisk(quadObj, 0, aWidth * 2.0, 10, 10);
    gl.glRotatef(180, 1, 0, 0);

    glu.gluCylinder(quadObj, aWidth * 2.0, 0.0, distance * ARRROW_CONE_PORTION, 10, 10);

    // delete our quadric object
    glu.gluDeleteQuadric(quadObj);

    gl.glPopMatrix();
  }
Beispiel #2
0
  @Override
  public void display(final GLAutoDrawable gLDrawable) {
    final GL2 gl = gLDrawable.getGL().getGL2();

    enableStates(gl, true);

    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
    gl.glPushMatrix();
    gl.glRotatef(angleZ, 0.0f, 1.0f, 0.0f);
    gl.glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
    glut.glutSolidTeapot(2.0f);
    gl.glPopMatrix();
    gl.glFlush();
    if (angleZ >= 180.0f) {
      rotDir = -1.0f;
    } else if (angleZ <= 0.0f) {
      rotDir = +1.0f;
    }
    angleZ += rotIncr * rotDir;

    enableStates(gl, false);
  }
 @Override
 public void display(GLAutoDrawable drawable) {
   GL2 gl = drawable.getGL().getGL2();
   gl.glClear(GL.GL_COLOR_BUFFER_BIT);
   // gl.glColor3f(1.0f, 0.0f, 0.0f); //ここは削除
   gl.glRotatef(25f, 0f, 1f, 0f);
   gl.glBegin(GL_POLYGON);
   gl.glColor3f(1.0f, 0.0f, 0.0f); // 赤
   gl.glVertex2f(-0.9f, -0.9f);
   gl.glColor3f(0.0f, 1.0f, 0.0f); // 緑
   gl.glVertex2f(0.9f, -0.9f);
   gl.glColor3f(0.0f, 0.0f, 1.0f); // 青
   gl.glVertex2f(0.9f, 0.9f);
   gl.glColor3f(1.0f, 1.0f, 0.0f); // 黄
   gl.glVertex2f(-0.9f, 0.9f);
   gl.glEnd();
 }
  @Override
  public void display(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();
    gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // GL_DEPTH_BUFFER_BITを追加
    gl.glLoadIdentity();

    // 視点位置と視線方向
    glu.gluLookAt(3.0f, 4.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

    // 光源の位置設定
    //		gl.glLightfv(GL_LIGHT0, GL_POSITION, light0pos, 0);
    //		gl.glLightfv(GL_LIGHT1, GL_POSITION, light1pos, 0);

    // 図形の回転
    gl.glTranslatef(0.5f, 0.5f, 0.5f);
    gl.glRotatef(r, 0.0f, 1.0f, 0.0f);
    gl.glTranslatef(-0.5f, -0.5f, -0.5f);
    // 図形の色 (赤)
    //		gl.glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red, 0);//追加
    //		gl.glColor3f(0.0f, 0.0f, 0.0f);

    // 図形の描画
    gl.glBegin(GL_QUADS);
    for (int j = 0; j < 6; ++j) {
      //			gl.glColor3fv(color[j], 0); //追加
      //			gl.glNormal3fv(normal[j], 0);
      for (int i = 0; i < 4; ++i) {
        gl.glNormal3fv(vertex[face[j][i]], 0);
        gl.glVertex3fv(vertex[face[j][i]], 0);
      }
    }
    gl.glEnd();

    // 一周回ったら回転角を 0 に戻す
    if (r++ >= 360.0f) r = 0;
    System.out.println("anim:" + animator.isAnimating() + ", r:" + r);
    if (willAnimatorPause) {
      animator.pause();
      System.out.println("animoator paused:");
      willAnimatorPause = false;
    }
  }
Beispiel #5
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);
  }
Beispiel #6
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();
  }