🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How to work with index buffer in LibGDX

Started by
1 comment, last by andriyndev 5 years, 6 months ago

Hello everyone. There is a simple program:


package com.xxxxx.yyyyy;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.glutils.IndexBufferObject;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.graphics.glutils.VertexBufferObject;
import com.badlogic.gdx.math.Matrix4;

public class YyYyY extends ApplicationAdapter {

   float vertices[] = {
         1.0f, 1.0f, 0.0f, 0.6f, 1.0f,
         1.0f, 0.0f, 0.0f, 0.6f, 0.95f,
         0.0f, 1.0f, 0.0f, 0.4f, 1.0f,
   };

   short indices[] =
         {
               0, 1, 2,
         };

   VertexBufferObject VBO;
   IndexBufferObject IBO;
   String vertexShader;
   String fragmentShader;
   ShaderProgram shaderProgram;
   Camera camera;
   int projectionUniform, viewUniform, modelUniform;
   Matrix4 transformations;
   Texture texture;
   
   @Override
   public void create () {
      transformations = new Matrix4().idt();
        VertexAttribute vP = new VertexAttribute(VertexAttributes.Usage.Position, 3, "a_Position");
        VertexAttribute vT = new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 2, "a_Texture");
        VertexAttributes vAs = new VertexAttributes(new VertexAttribute[]{vP, vT});
        VBO = new VertexBufferObject(true, vertices.length / 5, vAs);
        vertexShader = Gdx.files.internal("shaders/vertex_shader.glsl").readString();
      fragmentShader = Gdx.files.internal("shaders/fragment_shader.glsl").readString();
      shaderProgram = new ShaderProgram(vertexShader, fragmentShader);
      VBO.bind(shaderProgram);
      VBO.setVertices(vertices, 0, vertices.length);
      /*IBO = new IndexBufferObject(true, indices.length);
      IBO.setIndices(indices, 0, indices.length);
      IBO.bind();*/
      camera = new PerspectiveCamera(45, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
      camera.position.set(0.0f, 4.0f, -4.0f);
      camera.lookAt(0.0f, 1.0f, 1.0f);
      camera.up.set(0.0f, 1.0f, 0.0f);
      camera.near = 0.01f;
      camera.far = 1000.0f;
      camera.update();
      projectionUniform = shaderProgram.getUniformLocation("projection");
      viewUniform = shaderProgram.getUniformLocation("view");
      modelUniform = shaderProgram.getUniformLocation("model");
      texture = new Texture("texture.png");
      texture.bind(1);
   }

   @Override
   public void render () {
      Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

      shaderProgram.begin();
      shaderProgram.setUniformMatrix(projectionUniform, camera.projection);
      shaderProgram.setUniformMatrix(viewUniform, camera.view);
      shaderProgram.setUniformMatrix(modelUniform, transformations);
      shaderProgram.setUniformi("mainTexture", 1);
      //Gdx.gl.glDrawElements(GL20.GL_TRIANGLES, IBO.getNumIndices(), GL20.GL_SHORT, 0);
      Gdx.gl.glDrawArrays(GL20.GL_TRIANGLES, 0, VBO.getNumVertices());
      shaderProgram.end();
   }
   
   @Override
   public void dispose () {
      VBO.dispose();
      //IBO.dispose();
      shaderProgram.dispose();
      texture.dispose();
   }
}

It works fine without an index buffer, but it displays nothing with the index buffer (if uncomment all comments and comment out line with glDrawArrays). I haven't completely figured out with creating index buffers in LibGDX due to lack of the information on the Internet. May someone help me? What am I doing wrong?

Advertisement

Thanks everyone who helped. I've solved the issue by using Mesh class, but if someone know how to solve the issue with IndexBufferObject, I would be glad to find it out too.

This topic is closed to new replies.

Advertisement