Ejemplo n.º 1
0
  private void initVertexArray(GL3 gl3) {

    gl3.glGenVertexArrays(1, vertexArrayName);
    gl3.glBindVertexArray(vertexArrayName.get(0));
    {
      gl3.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(Buffer.VERTEX));
      {
        int stride = (2 + 3) * Float.BYTES;
        int offset = 0 * Float.BYTES;

        gl3.glEnableVertexAttribArray(Semantic.Attr.POSITION);
        gl3.glVertexAttribPointer(Semantic.Attr.POSITION, 2, GL_FLOAT, false, stride, offset);

        offset = 2 * Float.BYTES;
        gl3.glEnableVertexAttribArray(Semantic.Attr.COLOR);
        gl3.glVertexAttribPointer(Semantic.Attr.COLOR, 3, GL_FLOAT, false, stride, offset);
      }
      gl3.glBindBuffer(GL_ARRAY_BUFFER, 0);

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

    checkError(gl3, "initVao");
  }
Ejemplo n.º 2
0
  @Override
  protected boolean render(GL gl) {

    GL3 gl3 = (GL3) gl;

    Mat4 projection = glm.ortho_(-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f);
    Mat4 model = new Mat4(1.0f);
    Mat4 mvp = projection.mul(new Mat4(1.0f)).mul(model);

    gl3.glViewport(0, 0, windowSize.x, windowSize.y);
    gl3.glClearBufferfv(GL_COLOR, 0, clearColor.put(0, 0).put(1, 0).put(2, 0).put(3, 0));

    // Bind the program for use
    gl3.glUseProgram(programName);
    gl3.glUniform1i(uniformDiffuse, 0);
    gl3.glUniformMatrix4fv(uniformMvp, 1, false, mvp.toFa_(), 0);

    gl3.glActiveTexture(GL_TEXTURE0);
    gl3.glBindTexture(GL_TEXTURE_RECTANGLE, textureRectName.get(0));

    gl3.glBindVertexArray(vertexArrayName.get(0));
    gl3.glDrawArraysInstanced(GL_TRIANGLES, 0, vertexCount, 1);

    return true;
  }
Ejemplo n.º 3
0
 @Override
 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
   System.out.println("reshape");
   GL3 gl3 = drawable.getGL().getGL3();
   /** Just the glViewport for this sample, normally here you update your projection matrix. */
   gl3.glViewport(x, y, width, height);
 }
Ejemplo n.º 4
0
  @Override
  public void dispose(GLAutoDrawable glad) {
    GL3 gl3 = glad.getGL().getGL3();

    gl3.glDeleteVertexArrays(1, VAO, 0);
    gl3.glDeleteBuffers(1, VBO, 0);
    for (int shader : shaders) gl3.glDeleteShader(shader);
    gl3.glDeleteProgram(program);
  }
Ejemplo n.º 5
0
  @Override
  public void init(GLAutoDrawable glad) {
    GL3 gl3 = glad.getGL().getGL3();

    try {
      shaders.add(
          GLSLHelpers.createShader(gl3, getClass(), GL3.GL_VERTEX_SHADER, "glsl/simple_vs"));
      shaders.add(
          GLSLHelpers.createShader(gl3, getClass(), GL3.GL_FRAGMENT_SHADER, "glsl/simple_fs"));
      program = GLSLHelpers.createProgram(gl3, shaders);

      gl3.glGenVertexArrays(1, VAO, 0);
      gl3.glBindVertexArray(VAO[0]);

      gl3.glGenBuffers(1, VBO, 0);
      gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, VBO[0]);
      FloatBuffer buffer = GLBuffers.newDirectFloatBuffer(TRIANGLE);
      gl3.glBufferData(GL3.GL_ARRAY_BUFFER, buffer.capacity() * 4, buffer, GL3.GL_STATIC_DRAW);
      gl3.glEnableVertexAttribArray(0);
      gl3.glVertexAttribPointer(0, 2, GL3.GL_FLOAT, false, 0, 0);

      gl3.glBindVertexArray(0);
    } catch (Throwable t) {
      t.printStackTrace();
    }
  }
Ejemplo n.º 6
0
  private void initProgram(GL3 gl3) {

    ShaderCode vertShader =
        ShaderCode.create(
            gl3,
            GL_VERTEX_SHADER,
            this.getClass(),
            SHADERS_ROOT,
            null,
            SHADERS_NAME,
            "vert",
            null,
            true);
    ShaderCode fragShader =
        ShaderCode.create(
            gl3,
            GL_FRAGMENT_SHADER,
            this.getClass(),
            SHADERS_ROOT,
            null,
            SHADERS_NAME,
            "frag",
            null,
            true);

    ShaderProgram shaderProgram = new ShaderProgram();
    shaderProgram.add(vertShader);
    shaderProgram.add(fragShader);

    shaderProgram.init(gl3);

    programName = shaderProgram.program();

    /**
     * These links don't go into effect until you link the program. If you want to change index, you
     * need to link the program again.
     */
    gl3.glBindAttribLocation(programName, Semantic.Attr.POSITION, "position");
    gl3.glBindAttribLocation(programName, Semantic.Attr.COLOR, "color");
    gl3.glBindFragDataLocation(programName, Semantic.Frag.COLOR, "outputColor");

    shaderProgram.link(gl3, System.out);
    /**
     * Take in account that JOGL offers a GLUniformData class, here we don't use it, but take a look
     * to it since it may be interesting for you.
     */
    modelToClipMatrixUL = gl3.glGetUniformLocation(programName, "modelToClipMatrix");

    vertShader.destroy(gl3);
    fragShader.destroy(gl3);

    checkError(gl3, "initProgram");
  }
Ejemplo n.º 7
0
  @Override
  public void dispose(GLAutoDrawable drawable) {
    System.out.println("dispose");

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

    gl3.glDeleteProgram(programName);
    gl3.glDeleteVertexArrays(1, vertexArrayName);
    gl3.glDeleteBuffers(Buffer.MAX, bufferName);

    System.exit(0);
  }
Ejemplo n.º 8
0
  private boolean initBuffer(GL3 gl3) {

    FloatBuffer vertexBuffer = GLBuffers.newDirectFloatBuffer(vertexData);

    gl3.glGenBuffers(1, bufferName);

    gl3.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(0));
    gl3.glBufferData(GL_ARRAY_BUFFER, vertexSize, vertexBuffer, GL_STATIC_DRAW);
    gl3.glBindBuffer(GL_ARRAY_BUFFER, 0);

    BufferUtils.destroyDirectBuffer(vertexBuffer);

    return checkError(gl3, "initBuffer");
  }
Ejemplo n.º 9
0
  private boolean initProgram(GL3 gl3) {

    boolean validated = true;

    if (validated) {

      ShaderProgram shaderProgram = new ShaderProgram();

      ShaderCode vertShaderCode =
          ShaderCode.create(
              gl3,
              GL_VERTEX_SHADER,
              this.getClass(),
              SHADERS_ROOT,
              null,
              SHADERS_SOURCE,
              "vert",
              null,
              true);
      ShaderCode fragShaderCode =
          ShaderCode.create(
              gl3,
              GL_FRAGMENT_SHADER,
              this.getClass(),
              SHADERS_ROOT,
              null,
              SHADERS_SOURCE,
              "frag",
              null,
              true);

      shaderProgram.init(gl3);

      shaderProgram.add(vertShaderCode);
      shaderProgram.add(fragShaderCode);

      programName = shaderProgram.program();

      shaderProgram.link(gl3, System.out);
    }

    // Get variables locations
    if (validated) {

      uniformMvp = gl3.glGetUniformLocation(programName, "mvp");
      uniformDiffuse = gl3.glGetUniformLocation(programName, "diffuse");
    }

    return validated & checkError(gl3, "initProgram");
  }
Ejemplo n.º 10
0
  private boolean initProgram(GL3 gl3) {

    boolean validated = true;

    ShaderCode[] shaderCodes = new ShaderCode[Shader.MAX];

    if (validated) {

      shaderCodes[Shader.VERT] =
          ShaderCode.create(
              gl3,
              GL_VERTEX_SHADER,
              this.getClass(),
              SHADERS_ROOT,
              null,
              SHADERS_SOURCE,
              "vert",
              null,
              true);
      shaderCodes[Shader.FRAG] =
          ShaderCode.create(
              gl3,
              GL_FRAGMENT_SHADER,
              this.getClass(),
              SHADERS_ROOT,
              null,
              SHADERS_SOURCE,
              "frag",
              null,
              true);

      ShaderProgram shaderProgram = new ShaderProgram();
      shaderProgram.add(shaderCodes[Shader.VERT]);
      shaderProgram.add(shaderCodes[Shader.FRAG]);

      shaderProgram.init(gl3);

      programName = shaderProgram.program();

      gl3.glBindAttribLocation(programName, Semantic.Attr.POSITION, "position");
      gl3.glBindAttribLocation(programName, Semantic.Attr.TEXCOORD, "texCoord");
      gl3.glBindFragDataLocation(programName, Semantic.Frag.COLOR, "color");

      shaderProgram.link(gl3, System.out);
    }
    if (validated) {

      gl3.glUniformBlockBinding(
          programName,
          gl3.glGetUniformBlockIndex(programName, "Transform"),
          Semantic.Uniform.TRANSFORM0);

      gl3.glUseProgram(programName);
      gl3.glUniform1i(gl3.glGetUniformLocation(programName, "diffuse"), 0);
      gl3.glUseProgram(0);
    }

    return validated & checkError(gl3, "initProgram");
  }
Ejemplo n.º 11
0
  @Override
  public void init(GLAutoDrawable drawable) {
    System.out.println("init");

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

    initBuffers(gl3);

    initVertexArray(gl3);

    initProgram(gl3);

    gl3.glEnable(GL_DEPTH_TEST);

    start = System.currentTimeMillis();
  }
Ejemplo n.º 12
0
  @Override
  protected boolean end(GL gl) {

    GL3 gl3 = (GL3) gl;

    gl3.glDeleteBuffers(1, bufferName);
    gl3.glDeleteProgram(programName);
    gl3.glDeleteTextures(1, textureName);
    gl3.glDeleteVertexArrays(1, vertexArrayName);

    BufferUtils.destroyDirectBuffer(bufferName);
    BufferUtils.destroyDirectBuffer(textureName);
    BufferUtils.destroyDirectBuffer(vertexArrayName);

    return checkError(gl3, "end");
  }
Ejemplo n.º 13
0
 public void bind(GL3 gl, int textureUnit) {
   if (!isInitialised) {
     logger.error("Attempted to bind an uninitalised Vertex Buffer");
     return;
   }
   gl.glActiveTexture(GL3.GL_TEXTURE0 + textureUnit);
   texture.bind(gl);
 }
Ejemplo n.º 14
0
  private boolean initTexture(GL3 gl3) {

    IntBuffer textureSize = GLBuffers.newDirectIntBuffer(1);

    try {
      gl3.glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

      gl3.glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE, textureSize);

      gl3.glGenTextures(1, textureRectName);

      gl3.glActiveTexture(GL_TEXTURE0);
      gl3.glBindTexture(GL_TEXTURE_RECTANGLE, textureRectName.get(0));
      gl3.glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_BASE_LEVEL, 0);
      gl3.glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAX_LEVEL, 0);
      gl3.glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      gl3.glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

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

      assert (texture.dimensions()[0] <= textureSize.get(0)
          && texture.dimensions()[1] <= textureSize.get(0));

      gl3.glTexImage2D(
          GL_TEXTURE_RECTANGLE,
          0,
          format.internal.value,
          texture.dimensions()[0],
          texture.dimensions()[1],
          0,
          format.external.value,
          format.type.value,
          texture.data());

      gl3.glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    } catch (IOException ex) {
      Logger.getLogger(Gl_330_texture_rect.class.getName()).log(Level.SEVERE, null, ex);
    }

    BufferUtils.destroyDirectBuffer(textureSize);

    return true;
  }
Ejemplo n.º 15
0
  @Override
  protected boolean end(GL gl) {

    GL3 gl3 = (GL3) gl;

    gl3.glDeleteProgram(programName);
    gl3.glDeleteBuffers(Buffer.MAX, bufferName);
    gl3.glDeleteTextures(1, textureName);
    gl3.glDeleteVertexArrays(1, vertexArrayName);

    BufferUtils.destroyDirectBuffer(bufferName);
    BufferUtils.destroyDirectBuffer(textureName);
    BufferUtils.destroyDirectBuffer(vertexArrayName);

    BufferUtils.destroyDirectBuffer(clearColor);

    return true;
  }
Ejemplo n.º 16
0
  public void display(GL3 gl) {
    model.bindBuffer(gl);
    // TODO separate shader uniforms
    for (Map.Entry<String, Object> entry : model.getShaderUniforms().entrySet()) {
      shader.setUniform(gl, entry.getKey(), entry.getValue());
    }
    // draw the triangles
    gl.glDrawElements(GL3.GL_TRIANGLES, model.getIndexCount(), GL3.GL_UNSIGNED_INT, 0);

    model.unbindBuffer(gl);
  }
Ejemplo n.º 17
0
  private boolean initTexture(GL3 gl3) {

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

      gl3.glGenTextures(1, textureName);

      gl3.glActiveTexture(GL_TEXTURE0);
      gl3.glBindTexture(GL_TEXTURE_2D, textureName.get(0));
      gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, swizzles.r.value);
      gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, swizzles.g.value);
      gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, swizzles.b.value);
      gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, swizzles.a.value);

      for (int level = 0; level < texture.levels(); ++level) {
        gl3.glTexImage2D(
            GL_TEXTURE_2D,
            level,
            format.internal.value,
            texture.dimensions(level)[0],
            texture.dimensions(level)[1],
            0,
            format.external.value,
            format.type.value,
            texture.data(level));
      }

    } catch (IOException ex) {
      Logger.getLogger(Gl_330_blend_index.class.getName()).log(Level.SEVERE, null, ex);
    }
    return checkError(gl3, "initTexture");
  }
Ejemplo n.º 18
0
  @Override
  public void display(GLAutoDrawable glad) {
    GL3 gl3 = glad.getGL().getGL3();

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

    gl3.glUseProgram(program);

    gl3.glBindVertexArray(VAO[0]);
    gl3.glDrawArrays(GL3.GL_TRIANGLES, 0, 3);
    gl3.glBindVertexArray(0);

    gl3.glUseProgram(0);
  }
Ejemplo n.º 19
0
  private boolean initVertexArray(GL3 gl3) {

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

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

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

    return true;
  }
Ejemplo n.º 20
0
  @Override
  public void display(GLAutoDrawable drawable) {
    //        System.out.println("display");

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

    /**
     * We set the clear color and depth (although depth is not necessary since it is 1 by default).
     */
    gl3.glClearColor(0f, .33f, 0.66f, 1f);
    gl3.glClearDepthf(1f);
    gl3.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    {
      // update matrix based on time
      now = System.currentTimeMillis();
      float diff = (float) (now - start) / 1000;
      /**
       * Here we build the matrix that will multiply our original vertex positions. We scale,
       * halving it, and rotate it.
       */
      scale = FloatUtil.makeScale(scale, true, 0.5f, 0.5f, 0.5f);
      zRotazion = FloatUtil.makeRotationEuler(zRotazion, 0, 0, 0, diff);
      modelToClip = FloatUtil.multMatrix(scale, zRotazion);
    }
    gl3.glUseProgram(programName);
    gl3.glBindVertexArray(vertexArrayName.get(0));

    gl3.glUniformMatrix4fv(modelToClipMatrixUL, 1, false, modelToClip, 0);

    gl3.glDrawElements(GL_TRIANGLES, elementSize, GL_UNSIGNED_SHORT, 0);
    /**
     * The following line binds VAO and program to the default values, this is not a cheaper
     * binding, it costs always as a binding. Every binding means additional validation and
     * overhead, this may affect your performances. So you should avoid these calls, but remember
     * that OpenGL is a state machine, so what you left bound remains bound!
     */
    //        gl3.glBindVertexArray(0);
    //        gl3.glUseProgram(0);
    /**
     * Check always any GL error, but keep in mind this is an implicit synchronization between CPU
     * and GPU, so you should use it only for debug purposes.
     */
    checkError(gl3, "display");
  }
Ejemplo n.º 21
0
  private boolean initVertexArray(GL3 gl3) {

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

      gl3.glEnableVertexAttribArray(Semantic.Attr.POSITION);
      gl3.glEnableVertexAttribArray(Semantic.Attr.TEXCOORD);
    }
    gl3.glBindVertexArray(0);

    return checkError(gl3, "initVertexArray");
  }
  public LitProgram(
      GL3 gl3,
      String shadersFilepath,
      String vertexShader,
      String fragmentShader,
      int projectionUBB) {

    super(gl3, shadersFilepath, vertexShader, fragmentShader);

    unLocModelToCameraMatrix = gl3.glGetUniformLocation(getProgramId(), "modelToCameraMatrix");
    unLocLightDiffuseIntensity = gl3.glGetUniformLocation(getProgramId(), "lightDiffuseIntensity");
    unLocLightAmbientIntensity = gl3.glGetUniformLocation(getProgramId(), "lightAmbientIntensity");

    unLocNormalModelToCameraMatrix =
        gl3.glGetUniformLocation(getProgramId(), "normalModelToCameraMatrix");
    unLocLightCameraSpacePosition =
        gl3.glGetUniformLocation(getProgramId(), "cameraSpaceLightPosition");
    unLocLightAttenuation = gl3.glGetUniformLocation(getProgramId(), "lightAttenuation");
    unLocShininessFactor = gl3.glGetUniformLocation(getProgramId(), "shininessFactor");
    unLocBaseDiffuseColor = gl3.glGetUniformLocation(getProgramId(), "baseDiffuseColor");

    int projectionUBI = gl3.glGetUniformBlockIndex(getProgramId(), "Projection");
    gl3.glUniformBlockBinding(getProgramId(), projectionUBI, projectionUBB);
  }
Ejemplo n.º 23
0
  private void initBuffers(GL3 gl3) {

    FloatBuffer vertexBuffer = GLBuffers.newDirectFloatBuffer(vertexData);
    ShortBuffer elementBuffer = GLBuffers.newDirectShortBuffer(elementData);

    gl3.glGenBuffers(Buffer.MAX, bufferName);
    gl3.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(Buffer.VERTEX));
    {
      gl3.glBufferData(GL_ARRAY_BUFFER, vertexSize, vertexBuffer, GL_STATIC_DRAW);
    }
    gl3.glBindBuffer(GL_ARRAY_BUFFER, 0);

    gl3.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferName.get(Buffer.ELEMENT));
    {
      gl3.glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementSize, elementBuffer, GL_STATIC_DRAW);
    }
    gl3.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    BufferUtils.destroyDirectBuffer(vertexBuffer);
    BufferUtils.destroyDirectBuffer(elementBuffer);

    checkError(gl3, "initBuffers");
  }
Ejemplo n.º 24
0
  @Override
  protected boolean render(GL gl) {

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

      Mat4 projection = glm.perspective_((float) Math.PI * 0.25f, 4.0f / 3.0f, 0.1f, 100.0f);
      Mat4 model = new Mat4(1.0f);

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

      gl3.glUnmapBuffer(GL_UNIFORM_BUFFER);
    }

    gl3.glDrawBuffer(GL_BACK);

    gl3.glViewport(0, 0, windowSize.x, windowSize.y);
    gl3.glDisable(GL_FRAMEBUFFER_SRGB);
    clearColor.put(new float[] {1.0f, 0.5f, 0.0f, 1.0f}).rewind();
    gl3.glClearBufferfv(GL_COLOR, 0, clearColor);

    gl3.glUseProgram(programName);

    gl3.glDisable(GL_FRAMEBUFFER_SRGB);
    gl3.glActiveTexture(GL_TEXTURE0);
    gl3.glBindTexture(GL_TEXTURE_2D, textureName.get(0));
    gl3.glBindBufferBase(
        GL_UNIFORM_BUFFER, Semantic.Uniform.TRANSFORM0, bufferName.get(Buffer.TRANSFORM));
    gl3.glBindVertexArray(vertexArrayName.get(0));

    gl3.glDrawElementsInstancedBaseVertex(GL_TRIANGLES, elementCount, GL_UNSIGNED_SHORT, 0, 1, 0);

    return true;
  }
Ejemplo n.º 25
0
  private boolean initTexture(GL3 gl3) {

    try {
      jgli.Texture2d texture = new Texture2d(jgli.Load.load(TEXTURE_ROOT + "/" + TEXTURE_DIFFUSE));
      assert (!texture.empty());

      gl3.glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

      gl3.glGenTextures(1, textureName);

      gl3.glActiveTexture(GL_TEXTURE0);
      gl3.glBindTexture(GL_TEXTURE_2D, textureName.get(0));
      gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
      gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, texture.levels() - 1);
      gl3.glTexParameteri(
          GL_TEXTURE_2D,
          GL_TEXTURE_MIN_FILTER,
          texture.levels() == 1 ? GL_NEAREST : GL_NEAREST_MIPMAP_NEAREST);
      gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
      gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
      gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
      gl3.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, -1000.f);
      gl3.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 1000.f);
      gl3.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, 0.0f);

      jgli.Gl.Format format = jgli.Gl.translate(texture.format());
      for (int level = 0; level < texture.levels(); ++level) {
        gl3.glTexImage2D(
            GL_TEXTURE_2D,
            level,
            format.internal.value,
            texture.dimensions(level)[0],
            texture.dimensions(level)[1],
            0,
            format.external.value,
            format.type.value,
            texture.data(level));
      }

      if (texture.levels() == 1) {
        gl3.glGenerateMipmap(GL_TEXTURE_2D);
      }

      gl3.glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

    } catch (IOException ex) {
      Logger.getLogger(Gl_320_texture_2d.class.getName()).log(Level.SEVERE, null, ex);
    }
    return true;
  }
Ejemplo n.º 26
0
  private boolean initBuffer(GL3 gl3) {

    ShortBuffer elementBuffer = GLBuffers.newDirectShortBuffer(elementData);
    FloatBuffer vertexBuffer = GLBuffers.newDirectFloatBuffer(vertexData);
    IntBuffer uniformBufferOffset = GLBuffers.newDirectIntBuffer(1);

    gl3.glGenBuffers(Buffer.MAX, bufferName);

    gl3.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferName.get(Buffer.ELEMENT));
    gl3.glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementSize, elementBuffer, GL_STATIC_DRAW);
    gl3.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    gl3.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(Buffer.VERTEX));
    gl3.glBufferData(GL_ARRAY_BUFFER, vertexSize, vertexBuffer, GL_STATIC_DRAW);
    gl3.glBindBuffer(GL_ARRAY_BUFFER, 0);

    gl3.glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, uniformBufferOffset);
    int uniformBlockSize = Math.max(Mat4.SIZE, uniformBufferOffset.get(0));

    gl3.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.TRANSFORM));
    gl3.glBufferData(GL_UNIFORM_BUFFER, uniformBlockSize, null, GL_DYNAMIC_DRAW);
    gl3.glBindBuffer(GL_UNIFORM_BUFFER, 0);

    BufferUtils.destroyDirectBuffer(elementBuffer);
    BufferUtils.destroyDirectBuffer(vertexBuffer);
    BufferUtils.destroyDirectBuffer(uniformBufferOffset);

    return checkError(gl3, "initBuffer");
  }
Ejemplo n.º 27
0
  @Override
  protected boolean render(GL gl) {

    GL3 gl3 = (GL3) gl;

    // Setup blending
    gl3.glEnable(GL_BLEND);
    gl3.glBlendEquation(GL_FUNC_ADD);
    gl3.glBlendFunc(GL_ONE, GL_SRC1_COLOR);

    Mat4 projection = glm.perspective_((float) Math.PI * 0.25f, 4.0f / 3.0f, 0.1f, 100.0f);
    Mat4 model = new Mat4(1.0f);
    Mat4 mvp = projection.mul(viewMat4()).mul(model);

    gl3.glViewport(0, 0, windowSize.x, windowSize.y);
    gl3.glClearBufferfv(GL_COLOR, 0, clearColor.put(0, 1).put(1, 1).put(2, 1).put(3, 1));

    gl3.glUseProgram(programName);
    gl3.glUniformMatrix4fv(uniformMvp, 1, false, mvp.toFa_(), 0);
    gl3.glUniform1i(uniformDiffuse, 0);

    gl3.glActiveTexture(GL_TEXTURE0);
    gl3.glBindTexture(GL_TEXTURE_2D, textureName.get(0));

    gl3.glBindVertexArray(vertexArrayName.get(0));
    gl3.glDrawArraysInstanced(GL_TRIANGLES, 0, vertexCount, 1);

    return true;
  }