Esempio n. 1
0
    private int initVAO(GL3 gl) {
      int[] buff = new int[1];
      gl.glGenVertexArrays(1, buff, 0);
      int vao = buff[0];
      Assert.assertTrue("Invalid VAO: " + vao, vao > 0);

      gl.glUseProgram(progID);
      final int posLoc = gl.glGetAttribLocation(progID, "vPosition");
      final int colorLoc = gl.glGetAttribLocation(progID, "vColor");
      gl.glUseProgram(0);

      gl.glBindVertexArray(vao);
      gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vbo);
      gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, ibo);

      gl.glEnableVertexAttribArray(posLoc);
      gl.glEnableVertexAttribArray(colorLoc);

      final int stride = 6 * Buffers.SIZEOF_FLOAT;
      final int cOff = 3 * Buffers.SIZEOF_FLOAT;
      gl.glVertexAttribPointer(posLoc, 3, GL3.GL_FLOAT, false, stride, 0L);
      gl.glVertexAttribPointer(colorLoc, 3, GL3.GL_FLOAT, false, stride, cOff);

      gl.glBindVertexArray(0);
      // See class documentation above!
      gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0);
      gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, 0);
      return vao;
    }
  @Override
  public void display(GLAutoDrawable glad) {
    System.out.println("display");

    GL3 gl3 = glad.getGL().getGL3();

    gl3.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    gl3.glClear(GL3.GL_COLOR_BUFFER_BIT);

    programObject.bind(gl3);
    {
      programObject.setUniform(gl3, "offset", new float[] {0.5f, 0.5f}, 2);

      gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, vertexBufferObject[0]);

      gl3.glEnableVertexAttribArray(0);
      gl3.glEnableVertexAttribArray(1);
      {
        gl3.glVertexAttribPointer(0, 4, GL3.GL_FLOAT, false, 0, 0);
        gl3.glVertexAttribPointer(1, 4, GL3.GL_FLOAT, false, 0, 36 * 4 * 4);

        gl3.glDrawArrays(GL3.GL_TRIANGLES, 0, 36);
      }
      gl3.glDisableVertexAttribArray(0);
      gl3.glDisableVertexAttribArray(1);
    }
    programObject.unbind(gl3);

    glad.swapBuffers();
  }
Esempio n. 3
0
    private void displayVBOOnly(final GL3 gl) {
      final int posLoc = gl.glGetAttribLocation(progID, "vPosition");
      final int colorLoc = gl.glGetAttribLocation(progID, "vColor");
      gl.glEnableVertexAttribArray(posLoc);
      gl.glEnableVertexAttribArray(colorLoc);

      gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vbo);
      final int stride = 6 * Buffers.SIZEOF_FLOAT;
      final int cOff = 3 * Buffers.SIZEOF_FLOAT;
      gl.glVertexAttribPointer(posLoc, 3, GL3.GL_FLOAT, false, stride, 0L);
      gl.glVertexAttribPointer(colorLoc, 3, GL3.GL_FLOAT, false, stride, cOff);
      gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, ibo);
      gl.glDrawElements(GL3.GL_TRIANGLES, 3, GL3.GL_UNSIGNED_SHORT, 0L);

      gl.glDisableVertexAttribArray(posLoc);
      gl.glDisableVertexAttribArray(colorLoc);
      gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0);
      gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, 0);
    }
  /**
   * The main rendering method.
   *
   * @param renderItem the object that needs to be drawn
   */
  private void draw(RenderItem renderItem) {

    // Set the material of the shape to be rendered
    setMaterial(renderItem.getShape().getMaterial());

    // Get reference to the vertex data of the render item to be rendered
    GLVertexData vertexData = (GLVertexData) renderItem.getShape().getVertexData();

    // Check if the vertex data has been uploaded to OpenGL via a
    // "vertex array object" (VAO). The VAO will store the vertex data
    // in several "vertex buffer objects" (VBOs) on the GPU. We do this
    // only once for performance reasons. Once the data is in the VBOs
    // asscociated with a VAO, it is stored on the GPU and rendered more
    // efficiently.
    if (vertexData.getVAO() == null) {
      initArrayBuffer(vertexData);
    }

    // Set modelview and projection matrices in shader (has to be done in
    // every step, since they usually have changed)
    setTransformation(renderItem.getT());

    // Bind the VAO of this shape. This activates the VBOs that we
    // associated with the VAO. We already loaded the vertex data into the
    // VBOs on the GPU, so we do not have to send them again.
    vertexData.getVAO().bind();

    // Try to connect the vertex buffers to the corresponding variables
    // in the current vertex shader.
    // Note: This is not part of the vertex array object, because the active
    // shader may have changed since the vertex array object was initialized.
    // We need to make sure the vertex buffers are connected to the right
    // variables in the shader
    ListIterator<VertexData.VertexElement> itr = vertexData.getElements().listIterator(0);
    vertexData.getVAO().rewindVBO();
    while (itr.hasNext()) {
      VertexData.VertexElement e = itr.next();
      int dim = e.getNumberOfComponents();

      // Bind the next vertex buffer object
      gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vertexData.getVAO().getNextVBO());

      // Tell OpenGL which "in" variable in the vertex shader corresponds
      // to the current vertex buffer object.
      // We use our own convention to name the variables, i.e.,
      // "position", "normal", "color", "texcoord", or others if
      // necessary.
      int attribIndex = -1;
      switch (e.getSemantic()) {
        case POSITION:
          attribIndex = gl.glGetAttribLocation(activeShaderID, "position");
          break;
        case NORMAL:
          attribIndex = gl.glGetAttribLocation(activeShaderID, "normal");
          break;
        case COLOR:
          attribIndex = gl.glGetAttribLocation(activeShaderID, "color");
          break;
        case TEXCOORD:
          attribIndex = gl.glGetAttribLocation(activeShaderID, "texcoord");
          break;
      }

      gl.glVertexAttribPointer(attribIndex, dim, GL3.GL_FLOAT, false, 0, 0);
      gl.glEnableVertexAttribArray(attribIndex);
    }

    // Render the vertex buffer objects
    gl.glDrawElements(
        GL3.GL_TRIANGLES,
        renderItem.getShape().getVertexData().getIndices().length,
        GL3.GL_UNSIGNED_INT,
        0);

    // We are done with this shape, bind the default vertex array
    gl.glBindVertexArray(0);

    cleanMaterial(renderItem.getShape().getMaterial());
  }