예제 #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");
  }
예제 #2
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();
    }
  }
예제 #3
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);
  }
예제 #4
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;
  }
예제 #5
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;
  }
예제 #6
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");
  }
예제 #7
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;
  }
예제 #8
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");
  }
예제 #9
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;
  }