Exemplo n.º 1
0
  @Override
  public void render() {
    int width = App.getScreenWidth();
    int height = App.getScreenHeight();

    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluOrtho2D(0, width, 0, height);
    glScalef(1, -1, 1);
    glTranslatef(0, -height, 0);
    glMatrixMode(GL_MODELVIEW);
    glDisable(GL_DEPTH_TEST);
    // Draw background
    glBegin(GL_QUADS);
    {
      if (backgroundColor != null) GeneralUtils.glColorShortcut(backgroundColor);

      glVertex2f(0, 0);
      glVertex2f(0, height);
      glVertex2f(width, height);
      glVertex2f(width, 0);
    }
    glEnd();

    for (GuiButton b : buttons) {
      b.render();
      // System.out.println(b.isMouseOver());

    }

    // I don't want to set the same opengl settings for every button.
    // We can reduce a least a tiny bit of overhead by doing it in one go
    font.renderBegin();
    for (GuiButton b : buttons) {
      b.renderText();
    }
    font.renderEnd();

    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_DEPTH_TEST);
  }
Exemplo n.º 2
0
 public static int prepareSquare(int texId, float size) {
   int list = glGenLists(1);
   glNewList(list, GL_COMPILE);
   glDisable(GL_CULL_FACE);
   glBindTexture(GL_TEXTURE_2D, texId);
   glTranslatef(-size / 2, -size / 2, 0);
   // glRotatef(180.0f, 0.0f, 0.0f, 0.0f);
   glBegin(GL_QUADS);
   glTexCoord2f(0.0f, 0.0f);
   glVertex2f(0.0f, 0.0f);
   glTexCoord2f(1.0f, 0.0f);
   glVertex2f(size, 0.0f);
   glTexCoord2f(1.0f, 1.0f);
   glVertex2f(size, size);
   glTexCoord2f(0.0f, 1.0f);
   glVertex2f(0.0f, size);
   glEnd();
   glEnable(GL_CULL_FACE);
   glEndList();
   return list;
 }
Exemplo n.º 3
0
  /**
   * draws a cylinder oriented along the z axis. The base of the cylinder is placed at z = 0, and
   * the top at z=height. Like a sphere, a cylinder is subdivided around the z axis into slices, and
   * along the z axis into stacks.
   *
   * <p>Note that if topRadius is set to zero, then this routine will generate a cone.
   *
   * <p>If the orientation is set to GLU.OUTSIDE (with glu.quadricOrientation), then any generated
   * normals point away from the z axis. Otherwise, they point toward the z axis.
   *
   * <p>If texturing is turned on (with glu.quadricTexture), then texture coordinates are generated
   * so that t ranges linearly from 0.0 at z = 0 to 1.0 at z = height, and s ranges from 0.0 at the
   * +y axis, to 0.25 at the +x axis, to 0.5 at the -y axis, to 0.75 at the -x axis, and back to 1.0
   * at the +y axis.
   *
   * @param baseRadius Specifies the radius of the cylinder at z = 0.
   * @param topRadius Specifies the radius of the cylinder at z = height.
   * @param height Specifies the height of the cylinder.
   * @param slices Specifies the number of subdivisions around the z axis.
   * @param stacks Specifies the number of subdivisions along the z axis.
   */
  public void draw(float baseRadius, float topRadius, float height, int slices, int stacks) {

    float da, r, dr, dz;
    float x, y, z, nz, nsign;
    int i, j;

    if (super.orientation == GLU_INSIDE) {
      nsign = -1.0f;
    } else {
      nsign = 1.0f;
    }

    da = 2.0f * PI / slices;
    dr = (topRadius - baseRadius) / stacks;
    dz = height / stacks;
    nz = (baseRadius - topRadius) / height;
    // Z component of normal vectors

    if (super.drawStyle == GLU_POINT) {
      glBegin(GL_POINTS);
      for (i = 0; i < slices; i++) {
        x = cos((i * da));
        y = sin((i * da));
        normal3f(x * nsign, y * nsign, nz * nsign);

        z = 0.0f;
        r = baseRadius;
        for (j = 0; j <= stacks; j++) {
          glVertex3f((x * r), (y * r), z);
          z += dz;
          r += dr;
        }
      }
      glEnd();
    } else if (super.drawStyle == GLU_LINE || super.drawStyle == GLU_SILHOUETTE) {
      // Draw rings
      if (super.drawStyle == GLU_LINE) {
        z = 0.0f;
        r = baseRadius;
        for (j = 0; j <= stacks; j++) {
          glBegin(GL_LINE_LOOP);
          for (i = 0; i < slices; i++) {
            x = cos((i * da));
            y = sin((i * da));
            normal3f(x * nsign, y * nsign, nz * nsign);
            glVertex3f((x * r), (y * r), z);
          }
          glEnd();
          z += dz;
          r += dr;
        }
      } else {
        // draw one ring at each end
        if (baseRadius != 0.0) {
          glBegin(GL_LINE_LOOP);
          for (i = 0; i < slices; i++) {
            x = cos((i * da));
            y = sin((i * da));
            normal3f(x * nsign, y * nsign, nz * nsign);
            glVertex3f((x * baseRadius), (y * baseRadius), 0.0f);
          }
          glEnd();
          glBegin(GL_LINE_LOOP);
          for (i = 0; i < slices; i++) {
            x = cos((i * da));
            y = sin((i * da));
            normal3f(x * nsign, y * nsign, nz * nsign);
            glVertex3f((x * topRadius), (y * topRadius), height);
          }
          glEnd();
        }
      }
      // draw length lines
      glBegin(GL_LINES);
      for (i = 0; i < slices; i++) {
        x = cos((i * da));
        y = sin((i * da));
        normal3f(x * nsign, y * nsign, nz * nsign);
        glVertex3f((x * baseRadius), (y * baseRadius), 0.0f);
        glVertex3f((x * topRadius), (y * topRadius), (height));
      }
      glEnd();
    } else if (super.drawStyle == GLU_FILL) {
      float ds = 1.0f / slices;
      float dt = 1.0f / stacks;
      float t = 0.0f;
      z = 0.0f;
      r = baseRadius;
      for (j = 0; j < stacks; j++) {
        float s = 0.0f;
        glBegin(GL_QUAD_STRIP);
        for (i = 0; i <= slices; i++) {
          if (i == slices) {
            x = sin(0.0f);
            y = cos(0.0f);
          } else {
            x = sin((i * da));
            y = cos((i * da));
          }
          if (nsign == 1.0f) {
            normal3f((x * nsign), (y * nsign), (nz * nsign));
            TXTR_COORD(s, t);
            glVertex3f((x * r), (y * r), z);
            normal3f((x * nsign), (y * nsign), (nz * nsign));
            TXTR_COORD(s, t + dt);
            glVertex3f((x * (r + dr)), (y * (r + dr)), (z + dz));
          } else {
            normal3f(x * nsign, y * nsign, nz * nsign);
            TXTR_COORD(s, t);
            glVertex3f((x * r), (y * r), z);
            normal3f(x * nsign, y * nsign, nz * nsign);
            TXTR_COORD(s, t + dt);
            glVertex3f((x * (r + dr)), (y * (r + dr)), (z + dz));
          }
          s += ds;
        } // for slices
        glEnd();
        r += dr;
        t += dt;
        z += dz;
      } // for stacks
    }
  }
Exemplo n.º 4
0
  public void render() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
    glLoadIdentity(); // Reset The matrix

    //////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** ////////////////////

    // Give OpenGL our position,	then view,		then up vector
    gluLookAt(0, 1.5f, 100, 0, .5f, 0, 0, 1, 0);

    // We want the model to rotate around the axis so we give it a rotation
    // value, then increase/decrease it. You can rotate right of left with the arrow keys.

    glRotatef(g_RotateX, 0, 1.0f, 0); // Rotate the object around the Y-Axis
    g_RotateX += g_RotationSpeed; // Increase the speed of rotation

    // Make sure we have valid objects just in case. (size() is in the vector class)
    if (g_World.getObject().size() <= 0) return;

    for (int i = 0; i < g_World.getObject().size(); i++) {
      // Get the current object that we are displaying
      Object3d pObject = g_World.getObject(i);

      glBindTexture(GL_TEXTURE_2D, pObject.getMaterialID());

      // Render lines or normal triangles mode, depending on the global variable
      glBegin(g_ViewMode);

      // Go through all of the faces (polygons) of the object and draw them
      for (int j = 0; j < pObject.getNumFaces(); j++) {
        // Go through each corner of the triangle and draw it.
        for (int whichVertex = 0; whichVertex < 3; whichVertex++) {
          // Get the index for each point in the face
          int index = pObject.getFace(j).getVertices(whichVertex);

          // Get the index for each texture coord in the face
          int index2 = pObject.getFace(j).getTexCoords(whichVertex);

          // Give OpenGL the normal for this vertex.  Notice that we put a
          // - sign in front.  It appears that because of the ordering of Quake2's
          // polygons, we need to invert the normal
          // glNormal3f(-pObject.getNormal(index).x, -pObject.getNormal(index).y,
          // -pObject.getNormal(index).z);

          // Make sure there was a UVW map applied to the object or else it won't have tex coords.
          if (pObject.getNumTexcoords() > 0) {
            glTexCoord2f(pObject.getTexcoords(index2).s, pObject.getTexcoords(index2).t);
          }

          // Pass in the current vertex of the object (Corner of current face)
          glVertex3f(
              pObject.getVertices(index).x,
              pObject.getVertices(index).y,
              pObject.getVertices(index).z);
        }
      }

      glEnd();
    }
    // Render the cubed nodes to visualize the octree (in wire frame mode)
    if (g_bDisplayNodes) {
      // TOctree.g_Debug.renderDebugLines();
      for (int j = 0; j < g_World.getObject().size(); j++) {
        g_World.getObject(j).drawBoundingBox();
      }
    }
  }
Exemplo n.º 5
0
  protected void render() {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer

    glLoadIdentity();

    camera.look();

    // Each frame we calculate the new frustum.  In reality you only need to
    // calculate the frustum when we move the camera.
    GameCore.gFrustum.calculateFrustum();

    // Initialize the total node count that is being draw per frame
    Octree.totalNodesDrawn = 0;

    glPushMatrix();
    // Here we draw the octree, starting with the root node and recursing down each node.
    // This time, we pass in the root node and just the original world model.  You could
    // just store the world in the root node and not have to keep the original data around.
    // This is up to you.  I like this way better because it's easy, though it could be
    // more error prone.
    octree.drawOctree(octree, g_World);
    glPopMatrix();

    // Render the cubed nodes to visualize the octree (in wire frame mode)
    if (g_bDisplayNodes) Octree.debug.drawBoundingBox();

    glPushMatrix();
    // If there was a collision, make the Orange ball Red.
    if (octree.isObjectColliding()) {
      glColor3f(1.0f, 0.0f, 0.0f);
    } else {
      glColor3f(1.0f, 0.5f, 0.0f); // Disable Lighting.
    }
    // Move the Ball into place.
    glTranslatef(g_BallEntity.x, g_BallEntity.y, g_BallEntity.z);

    glDisable(GL_LIGHTING);
    // Draw the Ground Intersection Line.
    glBegin(GL_LINES);
    glColor3f(1, 1, 1);
    glVertex3f(g_vGroundISector[0].x, g_vGroundISector[0].y, g_vGroundISector[0].z);
    glVertex3f(g_vGroundISector[1].x, g_vGroundISector[1].y, g_vGroundISector[1].z);
    glEnd();

    // Draw the Forward Intersection Line.
    glBegin(GL_LINES);
    glColor3f(1, 1, 0);
    glVertex3f(
        g_vForwardISector[0].x * 10.0f, g_vForwardISector[0].y, g_vForwardISector[0].z * 10.0f);
    glVertex3f(
        g_vForwardISector[1].x * 10.0f, g_vForwardISector[1].y, g_vForwardISector[1].z * 10.0f);
    glEnd();

    // Re-enable lighting.
    glEnable(GL_LIGHTING);

    // System.out.println("x " + g_BallEntity.x + " y " + g_BallEntity.y);
    // Draw it!
    pObj.draw(g_BallEntity.fRadius, 20, 20);

    glPopMatrix();

    screen.setTitle(
        "Triangles: "
            + Octree.maxTriangles
            + "  -Total Draw: "
            + Octree.totalNodesDrawn
            + "  -Subdivisions: "
            + Octree.maxSubdivisions
            + "  -FPS: "
            + FPSCounter.get()
            + "  -Node Collisions: "
            + Octree.numNodesCollided
            + "  -Object Colliding? "
            + octree.isObjectColliding());
  }