Exemple #1
0
  @Override
  public void draw(GLAutoDrawable glAutoDrawable, MatrixStack mvStack, Matrix3D pMat) {
    GL4 gl = (GL4) glAutoDrawable.getGL();

    mvStack.pushMatrix(); // push translate
    translate(dxTranslate, dyTranslate, dzTranslate);
    mvStack.multMatrix(getTranslation());
    mvStack.pushMatrix(); // push rotate

    rotate(dxRotate, dyRotate, dzRotate);
    mvStack.multMatrix(getRotation());
    mvStack.multMatrix(getScale());

    gl.glUniformMatrix4fv(IdentityLocs.getMvLoc(), 1, false, mvStack.peek().getFloatValues(), 0);
    gl.glUniformMatrix4fv(IdentityLocs.getProjLoc(), 1, false, pMat.getFloatValues(), 0);
    gl.glUniformMatrix4fv(
        IdentityLocs.getnlocation(),
        1,
        false,
        (mvStack.peek().inverse().transpose().getFloatValues()),
        0);

    // bind vertex values
    gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo[index]);
    gl.glVertexAttribPointer(0, 3, GL.GL_FLOAT, false, 0, 0);
    gl.glEnableVertexAttribArray(0);

    // bind normal values
    gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo[index + 2]);
    gl.glVertexAttribPointer(1, 3, GL.GL_FLOAT, false, 0, 0);
    gl.glEnableVertexAttribArray(1);

    // bind texture values
    gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo[index + 1]);
    gl.glVertexAttribPointer(2, 2, GL.GL_FLOAT, false, 0, 0);
    gl.glEnableVertexAttribArray(2);

    gl.glActiveTexture(gl.GL_TEXTURE0);
    gl.glBindTexture(gl.GL_TEXTURE_2D, texture);

    gl.glEnable(GL_CULL_FACE);
    gl.glFrontFace(GL_CCW);
    gl.glEnable(GL_DEPTH_TEST);
    gl.glDepthFunc(GL_LEQUAL);
    // draw arrays
    gl.glDrawArrays(GL_TRIANGLES, 0, myShape.getIndices().length);
    mvStack.popMatrix(); // pop rotate
    mvStack.popMatrix(); // pop translate
  }
  private boolean initVertexArray(GL4 gl4) {

    boolean validated = true;

    gl4.glGenVertexArrays(1, vertexArrayName);
    gl4.glBindVertexArray(vertexArrayName.get(0));
    {
      gl4.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(Buffer.VERTEX));
      gl4.glVertexAttribPointer(Semantic.Attr.POSITION, 2, GL_FLOAT, false, 2 * Vec2.SIZE, 0);
      gl4.glVertexAttribPointer(
          Semantic.Attr.TEXCOORD, 2, GL_FLOAT, false, 2 * Vec2.SIZE, Vec2.SIZE);
      gl4.glBindBuffer(GL_ARRAY_BUFFER, 0);

      gl4.glEnableVertexAttribArray(Semantic.Attr.POSITION);
      gl4.glEnableVertexAttribArray(Semantic.Attr.TEXCOORD);

      gl4.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferName.get(Buffer.ELEMENT));
    }
    gl4.glBindVertexArray(0);

    return validated;
  }