Ejemplo n.º 1
0
  @Override
  protected void init() {
    initializeProgram();

    try {
      cylinderMesh = new Mesh("UnitCylinder.xml");
      planeMesh = new Mesh("LargePlane.xml");
    } catch (Exception exception) {
      exception.printStackTrace();
      System.exit(-1);
    }

    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glFrontFace(GL_CW);

    glEnable(GL_DEPTH_TEST);
    glDepthMask(true);
    glDepthFunc(GL_LEQUAL);
    glDepthRange(0.0f, 1.0f);
    glEnable(GL_DEPTH_CLAMP);

    projectionUniformBuffer = glGenBuffers();
    glBindBuffer(GL_UNIFORM_BUFFER, projectionUniformBuffer);
    glBufferData(GL_UNIFORM_BUFFER, ProjectionBlock.SIZE, GL_DYNAMIC_DRAW);

    // Bind the static buffers.
    glBindBufferRange(
        GL_UNIFORM_BUFFER, projectionBlockIndex, projectionUniformBuffer, 0, ProjectionBlock.SIZE);

    glBindBuffer(GL_UNIFORM_BUFFER, 0);
  }
Ejemplo n.º 2
0
  protected boolean prepare(int fbufWidth, int fbufHeight) {
    if (ctx.useShader(this) && glIsProgram(program)) {
      glUseProgram(program);
      ctx.checkGLError("Shader.prepare useProgram");

      glUniform2f(uScreenSizeLoc, fbufWidth, fbufHeight);
      // ctx.checkGLError("Shader.prepare uScreenSizeLoc set to " + fbufWidth + " " + fbufHeight);

      glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
      ctx.checkGLError("Shader.prepare BindBuffer");

      glEnableVertexAttribArray(aMatrix);
      glEnableVertexAttribArray(aTranslation);
      glEnableVertexAttribArray(aPosition);
      if (aTexture != -1) glEnableVertexAttribArray(aTexture);
      ctx.checkGLError("Shader.prepare AttribArrays enabled");

      glVertexAttribPointer(aMatrix, 4, GL_FLOAT, false, VERTEX_STRIDE, 0);
      glVertexAttribPointer(aTranslation, 2, GL_FLOAT, false, VERTEX_STRIDE, 16);
      glVertexAttribPointer(aPosition, 2, GL_FLOAT, false, VERTEX_STRIDE, 24);
      if (aTexture != -1) glVertexAttribPointer(aTexture, 2, GL_FLOAT, false, VERTEX_STRIDE, 32);
      ctx.checkGLError("Shader.prepare AttribPointer");
      return true;
    }
    return false;
  }
  @Override
  protected void update(double time) {
    glClear(GL_COLOR_BUFFER_BIT);

    program.activate();

    // Bind to the VAO that has all the information about the vertices
    glBindVertexArray(vaoId);
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    // Bind to the index VBO that has all the information about the order of the vertices
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboiId);

    // Draw the vertices
    glDrawElements(GL_TRIANGLES, indicesCount, GL_UNSIGNED_BYTE, 0);

    // Put everything back to default (deselect)
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glBindVertexArray(0);

    Program.deactivate();
  }
Ejemplo n.º 4
0
 @Override
 public void draw() {
   checkCreated();
   // Bind the vao and enable all attributes
   GL30.glBindVertexArray(id);
   for (int i = 0; i < attributeBufferIDs.length; i++) {
     GL20.glEnableVertexAttribArray(i);
   }
   // Bind the indices buffer
   GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBufferID);
   // Draw all indices with the provided mode
   GL11.glDrawElements(
       drawingMode.getGLConstant(),
       indicesCount,
       GL11.GL_UNSIGNED_INT,
       indicesOffset * DataType.INT.getByteSize());
   // Unbind the indices buffer
   GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
   // Disable all attributes and unbind the vao
   for (int i = 0; i < attributeBufferIDs.length; i++) {
     GL20.glDisableVertexAttribArray(i);
   }
   GL30.glBindVertexArray(0);
   // Check for errors
   LWJGLUtil.checkForGLError();
 }
  public void destroyOpenGL() {
    // Delete the shaders
    GL20.glUseProgram(0);
    GL20.glDetachShader(pId, vsId);
    GL20.glDetachShader(pId, fsId);

    GL20.glDeleteShader(vsId);
    GL20.glDeleteShader(fsId);
    GL20.glDeleteProgram(pId);

    // Select the VAO
    GL30.glBindVertexArray(vaoId);

    // Disable the VBO index from the VAO attributes list
    GL20.glDisableVertexAttribArray(0);
    GL20.glDisableVertexAttribArray(1);

    // Delete the vertex VBO
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    GL15.glDeleteBuffers(vboId);

    // Delete the index VBO
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
    GL15.glDeleteBuffers(vboiId);

    // Delete the VAO
    GL30.glBindVertexArray(0);
    GL30.glDeleteVertexArrays(vaoId);

    Display.destroy();
  }
Ejemplo n.º 6
0
  public void RebuildMesh(float startX, float startY, float startZ) {

    VBOColorHandle = GL15.glGenBuffers();
    VBOVertexHandle = GL15.glGenBuffers();

    // Using 6 * 4 * 3 because 6 faces * 4 pts per face * 3 coords per point
    FloatBuffer VertexPositionData =
        BufferUtils.createFloatBuffer((CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE) * 6 * 4 * 3);
    FloatBuffer VertexColorData =
        BufferUtils.createFloatBuffer((CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE) * 6 * 4 * 3);

    for (float x = 0; x < CHUNK_SIZE; x += 1) {
      for (float y = 0; y < CHUNK_SIZE; y += 1) {
        for (float z = 0; z < CHUNK_SIZE; z += 1) {

          VertexPositionData.put(
              CreateCube(
                  (float) startX + x * CUBE_LENGTH,
                  (float) startY + y * CUBE_LENGTH,
                  (float) startZ + z * CUBE_LENGTH));
          VertexColorData.put(CreateCubeVertexCol(GetCubeColor(Blocks[(int) x][(int) y][(int) z])));
        }
      }
    }

    VertexColorData.flip();
    VertexPositionData.flip();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOVertexHandle);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, VertexPositionData, GL15.GL_STATIC_DRAW);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOColorHandle);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, VertexColorData, GL15.GL_STATIC_DRAW);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
  }
Ejemplo n.º 7
0
  public Mesh(float[] vertices, int[] indices) {

    this.vertices = vertices;
    this.indices = indices;

    vbo = glGenBuffers();
    ibo = glGenBuffers();

    FloatBuffer fb = BufferUtils.createFloatBuffer(vertices.length);
    fb.put(vertices);
    fb.flip();

    IntBuffer ib = BufferUtils.createIntBuffer(indices.length);
    ib.put(indices);
    ib.flip();

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, fb, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, ib, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  }
Ejemplo n.º 8
0
  public void render(int x, int y, float extra) {
    setExtraLevel(extra);
    glPushMatrix();
    shader.bind();
    glBindVertexArray(vao);
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    {
      glTranslatef(x, y, 0);
      glActiveTexture(GL_TEXTURE1);
      glBindTexture(GL_TEXTURE_2D, texture);
      glActiveTexture(GL_TEXTURE2);
      glBindTexture(GL_TEXTURE_2D, Texture.Lava);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vio);
      glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0);
      glActiveTexture(GL_TEXTURE1);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
      glBindTexture(GL_TEXTURE_2D, 0);

      /*
       * glTranslatef(x, y, 0); glBindTexture(GL_TEXTURE_2D, texture); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vio); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindTexture(GL_TEXTURE_2D, 0);
       */
    }
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);
    glBindVertexArray(0);
    shader.release();
    glPopMatrix();
  }
Ejemplo n.º 9
0
  private int createTubeVbo(int tube, Color color) {

    float[] c =
        new float[] {color.getBlue() / 255f, color.getGreen() / 255f, color.getRed() / 255f};

    totalNumVerts[tube] = tubes[tube].indeces.length;

    IntBuffer buf = BufferUtils.createIntBuffer(1);
    GL15.glGenBuffers(buf);
    int vbo = buf.get();

    FloatBuffer data = BufferUtils.createFloatBuffer(tubes[tube].indeces.length * 9);

    for (int i = 0; i < tubes[tube].indeces.length; i++) {
      data.put(c);

      Vector3D vertex = tubes[tube].vertices[tubes[tube].indeces[i]];
      Vector3D normal = tubes[tube].normals[tubes[tube].indeces[i]];

      float[] vertexf = new float[] {vertex.x, vertex.y, vertex.z};
      float[] normalf = new float[] {normal.x, normal.y, normal.z};

      data.put(vertexf);
      data.put(normalf);
    }
    data.rewind();

    int bytesPerFloat = Float.SIZE / Byte.SIZE;
    int numBytes = data.capacity() * bytesPerFloat;
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, data, GL15.GL_STATIC_DRAW);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

    return vbo;
  }
Ejemplo n.º 10
0
  public VertexArray(float[] vertices, byte[] indices, float[] textureCoordinates) {
    count = indices.length;

    vao = glGenVertexArrays();
    glBindVertexArray(vao);

    vbo = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(vertices), GL_STATIC_DRAW);
    glVertexAttribPointer(Shader.VERTEX_ATTRIB, 3, GL_FLOAT, false, 0, 0);
    glEnableVertexAttribArray(Shader.VERTEX_ATTRIB);

    tbo = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, tbo);
    glBufferData(
        GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(textureCoordinates), GL_STATIC_DRAW);
    glVertexAttribPointer(Shader.TCOORD_ATTRIB, 2, GL_FLOAT, false, 0, 0);
    glEnableVertexAttribArray(Shader.TCOORD_ATTRIB);

    ibo = glGenBuffers();
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, BufferUtils.createByteBuffer(indices), GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
  }
  public void setupQuad() {
    // We'll define our quad using 4 vertices of the custom 'Vertex' class
    Vertex v0 = new Vertex();
    v0.setXYZ(-0.5f, 0.5f, 0f);
    v0.setRGB(1, 0, 0);
    Vertex v1 = new Vertex();
    v1.setXYZ(-0.5f, -0.5f, 0f);
    v1.setRGB(0, 1, 0);
    Vertex v2 = new Vertex();
    v2.setXYZ(0.5f, -0.5f, 0f);
    v2.setRGB(0, 0, 1);
    Vertex v3 = new Vertex();
    v3.setXYZ(0.5f, 0.5f, 0f);
    v3.setRGB(1, 1, 1);

    Vertex[] vertices = new Vertex[] {v0, v1, v2, v3};
    // Put each 'Vertex' in one FloatBuffer
    FloatBuffer verticesBuffer =
        BufferUtils.createFloatBuffer(vertices.length * Vertex.elementCount);
    for (int i = 0; i < vertices.length; i++) {
      verticesBuffer.put(vertices[i].getXYZW());
      verticesBuffer.put(vertices[i].getRGBA());
    }
    verticesBuffer.flip();

    // OpenGL expects to draw vertices in counter clockwise order by default
    byte[] indices = {
      0, 1, 2,
      2, 3, 0
    };
    indicesCount = indices.length;
    ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(indicesCount);
    indicesBuffer.put(indices);
    indicesBuffer.flip();

    // Create a new Vertex Array Object in memory and select it (bind)
    vaoId = GL30.glGenVertexArrays();
    GL30.glBindVertexArray(vaoId);

    // Create a new Vertex Buffer Object in memory and select it (bind)
    vboId = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesBuffer, GL15.GL_STATIC_DRAW);
    // Put the positions in attribute list 0
    GL20.glVertexAttribPointer(0, 4, GL11.GL_FLOAT, false, Vertex.sizeInBytes, 0);
    // Put the colors in attribute list 1
    GL20.glVertexAttribPointer(
        1, 4, GL11.GL_FLOAT, false, Vertex.sizeInBytes, Vertex.elementBytes * 4);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

    // Deselect (bind to 0) the VAO
    GL30.glBindVertexArray(0);

    // Create a new VBO for the indices and select it (bind) - INDICES
    vboiId = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId);
    GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
  }
Ejemplo n.º 12
0
 private void storeDataInAttributeList(int attributeNumber, int attributeSize, float[] data) {
   int vboID = GL15.glGenBuffers();
   vbos.add(vboID);
   GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
   FloatBuffer buffer = storeDataInFloatBuffer(data);
   GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
   GL20.glVertexAttribPointer(attributeNumber, attributeSize, GL11.GL_FLOAT, false, 0, 0);
   GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
 }
Ejemplo n.º 13
0
  @Override
  public void draw(Program program) {
    glBindVertexArray(vao);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);

    glDrawArrays(GL_TRIANGLES, 0, 6 * 2 * 3);

    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
  }
Ejemplo n.º 14
0
 public void setupVIBO() {
   ByteBuffer indexBuffer = BufferUtils.createByteBuffer(indexElementCount * getNumObjects());
   for (int i = 0; i < getNumObjects(); ++i) {
     indexBuffer.put(Rectangle.getOrder(4 * i));
   }
   indexBuffer.flip();
   setVIBO(GL15.glGenBuffers());
   GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, getVIBO());
   GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL15.GL_DYNAMIC_DRAW);
   GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
 }
Ejemplo n.º 15
0
 @Override
 public void create() {
   if (isCreated()) {
     throw new IllegalStateException("Vertex array has already been created");
   }
   if (vertexData == null) {
     throw new IllegalStateException("Vertex data has not been set");
   }
   // Generate and bind the vao
   id = GL30.glGenVertexArrays();
   GL30.glBindVertexArray(id);
   // Generate, bind and fill the indices vbo then unbind
   indicesBufferID = GL15.glGenBuffers();
   GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBufferID);
   GL15.glBufferData(
       GL15.GL_ELEMENT_ARRAY_BUFFER, vertexData.getIndicesBuffer(), GL15.GL_STATIC_DRAW);
   GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
   // Save the count of indices to draw
   indicesCountCache = vertexData.getIndicesCount();
   resetIndicesCountAndOffset();
   // Create the map for attribute index to buffer ID
   attributeBufferIDs = new int[vertexData.getAttributeCount()];
   // For each attribute, generate, bind and fill the vbo, then setup the attribute in the vao and
   // save the buffer ID for the index
   for (int i = 0; i < vertexData.getAttributeCount(); i++) {
     final VertexAttribute attribute = vertexData.getAttribute(i);
     final int bufferID = GL15.glGenBuffers();
     GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, bufferID);
     GL15.glBufferData(GL15.GL_ARRAY_BUFFER, attribute.getData(), GL15.GL_STATIC_DRAW);
     attributeBufferIDs[i] = bufferID;
     // Three ways to interpret integer data
     if (attribute.getType().isInteger() && attribute.getUploadMode() == UploadMode.KEEP_INT) {
       // Directly as an int
       GL30.glVertexAttribIPointer(
           i, attribute.getSize(), attribute.getType().getGLConstant(), 0, 0);
     } else {
       // Or as a float, normalized or not
       GL20.glVertexAttribPointer(
           i,
           attribute.getSize(),
           attribute.getType().getGLConstant(),
           attribute.getUploadMode().normalize(),
           0,
           0);
     }
   }
   // Unbind the vbo and vao
   GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
   GL30.glBindVertexArray(0);
   // Update state
   super.create();
   // Check for errors
   LWJGLUtil.checkForGLError();
 }
  private int initializeVertexBuffer() {
    int positionBufferObject = glGenBuffers();

    glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);

    FloatBuffer vertexData = BufferUtils.createFloatBuffer(vertexDataAry.length).put(vertexDataAry);
    vertexData.flip();
    glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    return positionBufferObject;
  }
Ejemplo n.º 17
0
 /**
  * @param id
  * @param data @Info Stocke le FloatBuffer dans le GPU
  */
 public void bufferData() {
   buffer = BufferUtils.createFloatBuffer(floatlist.size());
   for (Float f : floatlist) {
     buffer.put(f);
   }
   buffer.flip();
   glBindBuffer(GL_ARRAY_BUFFER, vboID);
   glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
   //		glBufferSubData(vboID, 0, buffer);
   glBindBuffer(GL_ARRAY_BUFFER, 0);
   bufferSize = buffer.limit() / 7; // 7 = 3 vertex(x,y,z) + 4 color (r,g,b,a)
 }
Ejemplo n.º 18
0
  public void render() {
    if (((ViewerContainer3D) v.getContainer()).getPbuffer() != currentPbuffer) {
      for (int i = 0; i < vbo.length; i++) {
        vbo[i] = -1;
        selectedVbo[i] = -1;
      }
      currentPbuffer = ((ViewerContainer3D) v.getContainer()).getPbuffer();
    }

    for (int i = 0; i < tubes.length; i++) {
      if (hidden[i]) continue;

      int vbo;
      if (!selected[i]) {
        if (this.vbo[i] == -1) {
          Color c = colors[i];
          GL15.glDeleteBuffers(this.vbo[i]);
          this.vbo[i] = createTubeVbo(i, colors[i]);
        }
        vbo = this.vbo[i];
      } else {
        if (this.selectedVbo[i] == -1) {
          Color c = selectedColors[i];
          GL15.glDeleteBuffers(this.selectedVbo[i]);
          this.selectedVbo[i] = createTubeVbo(i, selectedColors[i]);
        }
        vbo = this.selectedVbo[i];
      }

      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);

      GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
      GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
      GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY);

      GL11.glColorMaterial(GL11.GL_FRONT_AND_BACK, GL11.GL_AMBIENT_AND_DIFFUSE);
      GL11.glEnable(GL11.GL_COLOR_MATERIAL);

      GL11.glColorPointer(3, GL11.GL_FLOAT, vertexStride, colorPointer);
      GL11.glVertexPointer(3, GL11.GL_FLOAT, vertexStride, vertexPointer);
      GL11.glNormalPointer(GL11.GL_FLOAT, vertexStride, normalPointer);

      GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, totalNumVerts[i]);

      GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
      GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);
      GL11.glDisableClientState(GL11.GL_NORMAL_ARRAY);
      GL11.glDisable(GL11.GL_COLOR_MATERIAL);

      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    }
  }
Ejemplo n.º 19
0
  public void draw() {

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glDrawElements(GL_TRIANGLES, indices.length, GL_UNSIGNED_INT, 0);

    glDisableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  }
Ejemplo n.º 20
0
  @Override
  protected void reshape(int width, int height) {
    MatrixStack persMatrix = new MatrixStack();
    persMatrix.perspective(45.0f, (width / (float) height), zNear, zFar);

    ProjectionBlock projData = new ProjectionBlock();
    projData.cameraToClipMatrix = persMatrix.top();

    glBindBuffer(GL_UNIFORM_BUFFER, projectionUniformBuffer);
    glBufferSubData(GL_UNIFORM_BUFFER, 0, projData.fillAndFlipBuffer(mat4Buffer));
    glBindBuffer(GL_UNIFORM_BUFFER, 0);

    glViewport(0, 0, width, height);
  }
Ejemplo n.º 21
0
  public static void main(String[] args) {
    Window window = new Window(1024, 768, false);

    window.setTitle("Hello OpenGL!");

    window.setVisible(true);

    int vertexCount = 3;
    int floatSize = 4;

    FloatBuffer buffer = createFloatBuffer(vertexCount * 3 + vertexCount * 4);
    buffer.put(0).put(0.5f).put(0);
    buffer.put(1).put(0).put(0).put(1);

    buffer.put(-0.5f).put(-0.5f).put(0);
    buffer.put(0).put(1).put(0).put(1);

    buffer.put(0.5f).put(-0.5f).put(0);
    buffer.put(0).put(0).put(1).put(1);
    buffer.flip();

    /*IntBuffer indices = ByteBuffer.allocateDirect(3 * 4).order(ByteOrder.nativeOrder()).asIntBuffer();
    indices.put(0).put(1).put(2);
    indices.flip();*/

    int vbo = glGenBuffers();

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);

    while (!window.isCloseRequested()) {
      window.pollEvents();

      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_COLOR_ARRAY);

      glBindBuffer(GL_ARRAY_BUFFER, vbo);

      glVertexPointer(3, GL_FLOAT, 7 * floatSize, 0);
      glColorPointer(4, GL_FLOAT, 7 * floatSize, 3 * floatSize);

      glDrawArrays(GL_TRIANGLES, 0, 3);

      window.swap();
      window.sleep(16);
    }

    window.destroy();
  }
  public void display(long deltaTime) {
    Display.sync(60);
    elapsedTime += deltaTime;

    if (Display.wasResized()) resize();

    float[] offsets = computePositionOffsets(0, 0, deltaTime);
    float xOffset = offsets[0];
    float yOffset = offsets[1];
    adjustVertexData(xOffset, yOffset);

    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);

    glUseProgram(program);

    glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, 0);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableVertexAttribArray(0);
    glUseProgram(0);

    Display.update(); // calls (among other things) swapBuffers()
  }
Ejemplo n.º 23
0
 private void bindIndicesBuffer(int[] indices) {
   int vboID = GL15.glGenBuffers();
   vbos.add(vboID);
   GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboID);
   IntBuffer buffer = storeDataInIntBuffer(indices);
   GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
 }
  public void adjustVertexData(float xOffset, float yOffset) {

    newData.clear();

    for (int i = 0; i < 12; i += 4) {
      newData.put(vertexDataAry[i] + xOffset); // new x
      newData.put(vertexDataAry[i + 1] + yOffset); // new y
      newData.put(0); // new z
      newData.put(1); // new w (clip space value)
    }

    newData.flip();

    glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
    glBufferSubData(GL_ARRAY_BUFFER, 0, newData);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
  }
Ejemplo n.º 25
0
  private void render_batch(ChunkNode.Batch batch) {
    if (!camera.collides(batch.box)) return;

    Renderer.draw_calls++;

    // Use the shader the batch needs
    GL20.glUseProgram(batch.shader);

    // Bind VBO to vertex pointer
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, batch.vert_buf);
    GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);

    // Bind the texture coord VBO to texture pointer
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, batch.tex_buf);
    GL11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0);

    // Bind the texture for this batch
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, batch.tex);

    // Bind index array
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, batch.indices);

    // Draw the block
    GL12.glDrawRangeElements(
        GL11.GL_QUADS,
        0,
        Constants.Chunk.block_number * 24 - 1,
        batch.num_elements,
        GL11.GL_UNSIGNED_INT,
        0);

    Renderer.batch_draw_calls++;
    Renderer.quads += batch.num_elements;

    // Unbind the texture
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);

    // Unbind all buffers
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

    GL20.glUseProgram(0);
  }
Ejemplo n.º 26
0
  public void draw() {
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer(0, 3, GL_FLOAT, false, Vertex.SIZE * 4, 0);

    glDrawArrays(GL_TRIANGLES, 0, size);

    glDisableVertexAttribArray(0);
  }
Ejemplo n.º 27
0
  private void renderVbo(int id) {
    if (lock.tryLock()) {
      try {
        if (vertexBuffers[id] <= 0 || disposed) {
          return;
        }

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);
        glEnableClientState(GL_NORMAL_ARRAY);

        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, idxBuffers[id]);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBuffers[id]);

        glVertexPointer(SIZE_VERTEX, GL11.GL_FLOAT, STRIDE, OFFSET_VERTEX);

        GL13.glClientActiveTexture(GL13.GL_TEXTURE0);
        glTexCoordPointer(SIZE_TEX0, GL11.GL_FLOAT, STRIDE, OFFSET_TEX_0);

        GL13.glClientActiveTexture(GL13.GL_TEXTURE1);
        glTexCoordPointer(SIZE_TEX1, GL11.GL_FLOAT, STRIDE, OFFSET_TEX_1);

        glColorPointer(SIZE_COLOR * 4, GL11.GL_UNSIGNED_BYTE, STRIDE, OFFSET_COLOR);

        glNormalPointer(GL11.GL_FLOAT, STRIDE, OFFSET_NORMAL);

        GL11.glDrawElements(GL11.GL_TRIANGLES, vertexCount[id], GL11.GL_UNSIGNED_INT, 0);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);

        glDisableClientState(GL_NORMAL_ARRAY);
        glDisableClientState(GL_COLOR_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
        glDisableClientState(GL_VERTEX_ARRAY);

      } finally {
        lock.unlock();
      }
    }
  }
Ejemplo n.º 28
0
  public void renderBuffer(OpenGLBuffer buffer, ITextureObject texture) {
    GL13.glActiveTexture(GL13.GL_TEXTURE0 + 0);
    texture.bind();
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);

    glBindBuffer(GL_ARRAY_BUFFER, buffer.getVboID());
    glVertexAttribPointer(0, 3, GL_FLOAT, false, Vertex.SIZE_IN_FLOATS * 4, 0);
    glVertexAttribPointer(1, 2, GL_FLOAT, false, Vertex.SIZE_IN_FLOATS * 4, 12);
    glVertexAttribPointer(2, 3, GL_FLOAT, false, Vertex.SIZE_IN_FLOATS * 4, 20);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer.getIboID());
    glDrawElements(GL_TRIANGLES, buffer.getIndicesCount(), GL_UNSIGNED_INT, 0);

    glDisableVertexAttribArray(2);
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);
  }
Ejemplo n.º 29
0
 private static void render() {
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glLoadIdentity();
   cam.applyTranslations();
   glUseProgram(shaderProgram);
   glLight(GL_LIGHT0, GL_POSITION, asFloatBuffer(cam.x(), cam.y(), cam.z(), 1));
   glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
   glVertexPointer(3, GL_FLOAT, 0, 0L);
   glBindBuffer(GL_ARRAY_BUFFER, vboNormalHandle);
   glNormalPointer(GL_FLOAT, 0, 0L);
   glEnableClientState(GL_VERTEX_ARRAY);
   glEnableClientState(GL_NORMAL_ARRAY);
   glColor3f(0.4f, 0.27f, 0.17f);
   glMaterialf(GL_FRONT, GL_SHININESS, 10f);
   glDrawArrays(GL_TRIANGLES, 0, model.faces.size() * 3);
   glDisableClientState(GL_VERTEX_ARRAY);
   glDisableClientState(GL_NORMAL_ARRAY);
   glBindBuffer(GL_ARRAY_BUFFER, 0);
   glUseProgram(0);
 }
Ejemplo n.º 30
0
  protected void compile() {
    vao = glGenVertexArrays();
    glBindVertexArray(vao);
    {
      vbo = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, vbo);
      {
        glBufferData(GL_ARRAY_BUFFER, Buffer.createFloatBuffer(vertices), GL_STATIC_DRAW);
        glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
      }
      glBindBuffer(GL_ARRAY_BUFFER, 0);

      vio = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, vio);
      {
        glBufferData(GL_ARRAY_BUFFER, Buffer.createByteBuffer(indices), GL_STATIC_DRAW);
      }
      glBindBuffer(GL_ARRAY_BUFFER, 0);

      vto = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, vto);
      {
        glBufferData(GL_ARRAY_BUFFER, Buffer.createByteBuffer(texCoords), GL_STATIC_DRAW);
        glVertexAttribPointer(1, 3, GL_UNSIGNED_BYTE, false, 0, 1);
      }
      glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
    glBindVertexArray(0);
  }