1
\$\begingroup\$

//EDIT: Found the solution: had to use GL11.glDrawElements instead of DrawArrays.

I'm working on a "voxel based world" engine. At the moment my approach follows these steps:

Creation of a Mesh:

  1. Create FloatBuffer
  2. Fill it with floats (24 * 3) per cube
  3. Generate vertex array (vaoID)
  4. Bind vertex array
  5. Generate buffer object
  6. Bind buffer object
  7. Bind buffer data(GL_ARRAY_BUFFER, vertexPositionData, GL_STATIC_DRAW)

Rendering process

  1. BindVertexArray(vaoID)
  2. EnableVertexAttribArray(0)
  3. glDrawArrays(GL11.GL_TRIANGLES, 0, numberOfFloats)

But somewhere here i'm doing something wrong. No matter what i fill in the floatbuffer, there are no cubes (or at least triangles) rendered.

Sourcecode of worldData

private void initMesh() {
    vaoID = GL30.glGenVertexArrays();
    GL30.glBindVertexArray(vaoID);

    vID = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, vID);
    glBufferData(GL_ARRAY_BUFFER, vertexPositionData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    GL30.glBindVertexArray(0);
}

/**
 * Generates World Array on world creation
 */
private void generateWorld(){
    this.world = new BlockType[worldSize][worldSize][worldSize];

    for(int i = 0; i < worldSize; i++){
        for(int k = 0; k< worldSize; k++){
            for(int j = 0; j< worldSize; j++){
                world[i][k][j] = BlockType.Grass;
            }               
        }
    }
}

/**
 * Creates a single cube for a greater mesh
 * @param tx
 * @param ty
 * @param tz
 */
public void putVertices(float tx, float ty, float tz) {
    vertexPositionData.put(new float[]{             
              // Front face
              -0.1f + tx, -0.1f + ty,  0.1f + tz,
               0.1f + tx, -0.1f + ty,  0.1f + tz,
               0.1f + tx,  0.1f + ty,  0.1f + tz,
              -0.1f + tx,  0.1f + ty,  0.1f + tz,

              // Back face
              -0.1f + tx, -0.1f + ty, -0.1f + tz,
              -0.1f + tx,  0.1f + ty, -0.1f + tz,
               0.1f + tx,  0.1f + ty, -0.1f + tz,
               0.1f + tx, -0.1f + ty, -0.1f + tz,

              // Top face
              -0.1f + tx,  0.1f + ty, -0.1f + tz,
              -0.1f + tx,  0.1f + ty,  0.1f + tz,
               0.1f + tx,  0.1f + ty,  0.1f + tz,
               0.1f + tx,  0.1f + ty, -0.1f + tz,

              // Bottom face
              -0.1f + tx, -0.1f + ty, -0.1f + tz,
               0.1f + tx, -0.1f + ty, -0.1f + tz,
               0.1f + tx, -0.1f + ty,  0.1f + tz,
              -0.1f + tx, -0.1f + ty,  0.1f + tz,

              // Right face
               0.1f + tx, -0.1f + ty, -0.1f + tz,
               0.1f + tx,  0.1f + ty, -0.1f + tz,
               0.1f + tx,  0.1f + ty,  0.1f + tz,
               0.1f + tx, -0.1f + ty,  0.1f + tz,

              // Left face
              -0.1f + tx, -0.1f + ty, -0.1f + tz,
              -0.1f + tx, -0.1f + ty,  0.1f + tz,
              -0.1f + tx,  0.1f + ty,  0.1f + tz,
              -0.1f + tx,  0.1f + ty, -0.1f + tz
    });
}

public int getVerticesCount(){
    return (24*3)*worldSize*worldSize*worldSize;
}

/**
 * Creates a floatbuffer of a complete mesh
 * @return
 */
public FloatBuffer createVerticesAsBuffer(){
    vertexPositionData = BufferUtils.createFloatBuffer((24*3)*worldSize*worldSize*worldSize);

    for (int x = 0; x < worldSize; x++) {
        for (int y = 0; y < worldSize; y++) {
            for (int z = 0; z < worldSize; z++) {
                    putVertices(x/10, y/10, z/10);
            }
        }
    }

    vertexPositionData.flip();
    return vertexPositionData;
}

Sourcecode of rendering

public void render(WorldData worldData) {

    GL30.glBindVertexArray(worldData.vaoID);
    GL20.glEnableVertexAttribArray(0);

    GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, worldData.getVerticesCount());

    GL20.glDisableVertexAttribArray(0);
    GL30.glBindVertexArray(0);
}

VertexShader

    #version 400 core

     in vec3 position;

     out vec3 colour;

     uniform mat4 transformationMatrix;
     uniform mat4 projectionMatrix;
     uniform mat4 viewMatrix;

     void main(void){

        gl_Position = projectionMatrix * viewMatrix * transformationMatrix * vec4(position, 1.0);
        colour = vec3(position.x+0.5, 1.0, position.y+0.5);
     }

FragmentShader

    #version 400 core

    in vec3 colour;
    out vec4 out_Colour;

    void main(void){
        out_Colour = vec4(colour, 1.0);
    }
\$\endgroup\$
  • \$\begingroup\$ What does your shader look like? \$\endgroup\$ – Jordan Schnur May 13 '16 at 9:26
  • \$\begingroup\$ I edit my post for shaders. \$\endgroup\$ – Karoc May 13 '16 at 9:30
  • \$\begingroup\$ Is the screen completely black? You seem rendering correctly. \$\endgroup\$ – Jordan Schnur May 13 '16 at 9:33
  • \$\begingroup\$ Yes, its completlely in glClearColor "GL11.glClearColor(0, 205 / 255.0f, 255 / 255.0f, 1);" \$\endgroup\$ – Karoc May 13 '16 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.