Example #1
0
  /**
   * The main function opens a 3D rendering window, constructs a simple 3D scene, and starts a timer
   * task to generate an animation.
   *
   * @throws IOException
   */
  public static void main(String[] args) throws IOException {

    sceneManager = new SimpleSceneManager();

    Landscape landscape = new Landscape(7, 50, 20, 10, 5);
    // Set up the vertex data and integrate
    VertexData vLandscape = landscape.getVertexData();
    sLandscape = new Shape(vLandscape);
    sceneManager.addShape(sLandscape);

    // adjust the camera
    camera = sceneManager.getCamera();

    Vector3f centerOfProjection = new Vector3f(0, 0, 40);
    Vector3f lookAtPoint = new Vector3f(0, 0, 0);
    Vector3f upVector = new Vector3f(0, 1, 0);
    camera.setCameraMatrix(centerOfProjection, lookAtPoint, upVector);

    // adjust the frustum
    Frustum frustum = sceneManager.getFrustum();

    float nearPlane = 1;
    float farPlane = 100;
    float aspectRatio = 1;
    float vFieldOfView = 60;
    frustum.setFrustum(nearPlane, farPlane, aspectRatio, vFieldOfView);

    // Make a render panel. The init function of the renderPanel
    // (see above) will be called back for initialization.
    renderPanel = new SimpleRenderPanel();

    // Make the main window of this application and add the renderer to it
    // JFrame
    jframe = new JFrame("simple");
    jframe.setSize(750, 750);
    jframe.setLocationRelativeTo(null); // center of screen
    jframe.getContentPane().add(renderPanel.getCanvas()); // put the canvas into a JFrame window

    // Add a mouse listener
    SimpleMouseListener mouse = new SimpleMouseListener();
    renderPanel.getCanvas().addMouseListener(mouse); // change proposed in forum
    renderPanel.getCanvas().addMouseMotionListener(mouse);
    renderPanel.getCanvas().addKeyListener(new MyKeyListener());

    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jframe.setVisible(true); // show window
  }
Example #2
0
  void setup_camera(float fov, float ratio, float view_distance) {
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GLU.gluPerspective(fov, ratio, 0.01f, view_distance);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);

    camera.set_attribs(fov, ratio, 0.01f, view_distance);
  }
Example #3
0
  private void render_batch(ChunkNode.Batch batch) {
    if (!camera.collides(batch.box)) return;

    Renderer.draw_calls++;

    // Use the shader the batch needs
    GL20.glUseProgram(batch.shader);

    // Bind VBO to vertex pointer
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, batch.vert_buf);
    GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);

    // Bind the texture coord VBO to texture pointer
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, batch.tex_buf);
    GL11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0);

    // Bind the texture for this batch
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, batch.tex);

    // Bind index array
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, batch.indices);

    // Draw the block
    GL12.glDrawRangeElements(
        GL11.GL_QUADS,
        0,
        Constants.Chunk.block_number * 24 - 1,
        batch.num_elements,
        GL11.GL_UNSIGNED_INT,
        0);

    Renderer.batch_draw_calls++;
    Renderer.quads += batch.num_elements;

    // Unbind the texture
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);

    // Unbind all buffers
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

    GL20.glUseProgram(0);
  }
Example #4
0
File: Box.java Project: RMMJR/g3m
 public final boolean touchesFrustum(Frustum frustum) {
   return frustum.touchesWithBox(this);
 }
Example #5
0
  private void projectObject(Object3D parent) {

    for (Object3D object : parent.children) {

      if (object.visible == false) continue;

      if (object instanceof Light) {

        renderData.lights.add((Light) object);

      } else if (object instanceof Mesh || object instanceof Line) {

        if (object.frustumCulled == false || frustum.intersectObject(object) == true) {

          this.object = getNextObjectInPool();
          this.object.object = object; // TODO fix casting here.

          if (object.renderDepth != Float.NaN) {

            this.object.z = object.renderDepth;

          } else {

            vector3.getPositionFromMatrix(object.matrixWorld);
            vector3.applyProjection(viewProjectionMatrix);
            this.object.z = vector3.z;
          }

          renderData.objects.add(this.object);
        }

      } else if (object instanceof Sprite || object instanceof Particle) {

        this.object = getNextObjectInPool();
        this.object.object = object;

        if (object.renderDepth != Float.NaN) {

          this.object.z = object.renderDepth;

        } else {

          vector3.getPositionFromMatrix(object.matrixWorld);
          vector3.applyProjection(viewProjectionMatrix);
          this.object.z = vector3.z;
        }

        renderData.sprites.add(this.object);

      } else {

        this.object = getNextObjectInPool();
        this.object.object = object;

        if (object.renderDepth != Float.NaN) {

          this.object.z = object.renderDepth;

        } else {

          vector3.getPositionFromMatrix(object.matrixWorld);
          vector3.applyProjection(viewProjectionMatrix);
          this.object.z = vector3.z;
        }
      }
    }

    projectObject(object);
  }