Example #1
0
 public Renderer(
     final String vShader, final String fShader, final String texture, final String meshName)
     throws Exception {
   final HashMap<String, Shader.Type> dat = new HashMap<>();
   dat.put(vShader, Shader.Type.Vertex_Shader);
   dat.put(fShader, Shader.Type.Fragment_Shader);
   this.program = AssetManager.loadProgram(dat);
   if (texture.equals("")) {
     this.texture = -1;
   } else {
     this.texture = AssetManager.loadTexture(texture);
   }
   this.lastRot = new Quaternion();
   this.lastPosition = new Vector3<>(0f, 0f, 0f);
   this.lastSize = new Vector3<>(0f, 0f, 0f);
   this.lastMat = Matrix4x4.identityMatrix();
   this.mesh = AssetManager.loadMesh(meshName);
   this.glPos = glGetUniformLocation(AssetManager.getProgram(this.program), "mv_matrix");
 }
Example #2
0
  /**
   * Renders the various attributes on screen
   *
   * @param size
   * @param position
   * @param rotMat
   */
  public void render(
      final Vector3<Float> size, final Vector3<Float> position, final Quaternion rotation) {
    try {
      boolean recalculate = false;
      if (!size.equals(lastSize)) {
        this.lastSize.set(size);
        recalculate = true;
      }
      if (!this.lastPosition.equals(position)) {
        this.lastPosition.set(position);
        recalculate = true;
      }
      if (!this.lastRot.equals(rotation)) {
        this.lastRot.set(rotation);
        recalculate = true;
      }
      // Only do the calculations when it has been changed
      if (recalculate) {
        Matrix4x4 mat = Matrix4x4.identityMatrix();
        mat = Matrix4x4.scale(mat, size);
        Matrix4x4 pos = Matrix4x4.identityMatrix();
        pos.translate(position);
        mat = Matrix4x4.multiply(pos, mat);
        this.lastMat.set(Matrix4x4.multiply(mat, Quaternion.quaternionToMatrix(rotation)));
      }

      glUseProgram(AssetManager.getProgram(this.program));
      glBindVertexArray(AssetManager.getMeshVao(this.mesh));

      glEnableVertexAttribArray(0);

      glUniformMatrix4fv(this.glPos, true, this.lastMat.getRawData());
      if (this.texture != -1) {
        glBindTexture(GL_TEXTURE_2D, AssetManager.getTexture(this.texture));
      }

      glDrawArrays(GL_TRIANGLES, 0, AssetManager.getMeshSize(this.mesh));
      glDisableVertexAttribArray(0);
      glBindVertexArray(0);
    } catch (AssetManager.AssetNotFoundException ex) {
      Logger.getLogger(Renderer.class.getName()).log(Level.SEVERE, null, ex);
    }
  }