private boolean initTexture(GL4 gl4) {

    try {
      jgli.Texture2d texture = new Texture2d(jgli.Load.load(TEXTURE_ROOT + "/" + TEXTURE_DIFFUSE));
      jgli.Gl.Format format = jgli.Gl.translate(texture.format());

      gl4.glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

      gl4.glGenTextures(1, textureName);
      gl4.glActiveTexture(GL_TEXTURE0);
      gl4.glBindTexture(GL_TEXTURE_2D_ARRAY, textureName.get(0));
      gl4.glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_R, GL_RED);
      gl4.glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
      gl4.glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
      gl4.glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
      gl4.glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BASE_LEVEL, 0);
      gl4.glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, texture.levels() - 1);
      gl4.glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
      gl4.glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

      gl4.glTexStorage3D(
          GL_TEXTURE_2D_ARRAY,
          texture.levels(),
          format.internal.value,
          texture.dimensions(0)[0],
          texture.dimensions(0)[1],
          1);

      for (int level = 0; level < texture.levels(); ++level) {

        gl4.glTexSubImage3D(
            GL_TEXTURE_2D_ARRAY,
            level,
            0,
            0,
            0,
            texture.dimensions(level)[0],
            texture.dimensions(level)[1],
            1,
            format.external.value,
            format.type.value,
            texture.data(level));
      }

      gl4.glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

    } catch (IOException ex) {
      Logger.getLogger(Gl_430_texture_storage.class.getName()).log(Level.SEVERE, null, ex);
    }
    return true;
  }
Example #2
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
  }
  @Override
  protected boolean render(GL gl) {

    GL4 gl4 = (GL4) gl;

    {
      gl4.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.TRANSFORM));
      ByteBuffer pointer =
          gl4.glMapBufferRange(
              GL_UNIFORM_BUFFER, 0, Mat4.SIZE, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);

      Mat4 projection =
          glm.perspectiveFov_((float) Math.PI * 0.25f, windowSize.x, windowSize.y, 0.1f, 100.0f);
      Mat4 model = new Mat4(1.0f);

      pointer.asFloatBuffer().put(projection.mul(viewMat4()).mul(model).toFa_());

      // Make sure the uniform buffer is uploaded
      gl4.glUnmapBuffer(GL_UNIFORM_BUFFER);
    }

    gl4.glViewportIndexedf(0, 0, 0, windowSize.x, windowSize.y);
    gl4.glClearBufferfv(GL_COLOR, 0, new float[] {1.0f, 0.5f, 0.0f, 1.0f}, 0);

    gl4.glBindProgramPipeline(pipelineName.get(0));
    gl4.glActiveTexture(GL_TEXTURE0);
    gl4.glBindTexture(GL_TEXTURE_2D_ARRAY, textureName.get(0));
    gl4.glBindVertexArray(vertexArrayName.get(0));
    gl4.glBindBufferBase(
        GL_UNIFORM_BUFFER, Semantic.Uniform.TRANSFORM0, bufferName.get(Buffer.TRANSFORM));

    gl4.glDrawElementsInstancedBaseVertexBaseInstance(
        GL_TRIANGLES, elementCount, GL_UNSIGNED_SHORT, 0, 1, 0, 0);

    return true;
  }