-1

I am trying to draw a simple cube in a glsurfaceview but i dont get a clean representation. I want to get only drawn a simple cube.

My own renderer

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;

public class MyRenderer implements Renderer
{
Cube cube = new Cube();

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig arg1) 
{
    //set background color
    gl.glClearColor(0.0f, 0.0f, 0.0f, 1f);

    gl.glShadeModel(GL10.GL_SMOOTH);

    // Buffer depth
    gl.glClearDepthf(1.0f);

    gl.glEnable(GL10.GL_DEPTH_TEST);

    gl.glDepthFunc(GL10.GL_LEQUAL);

    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}

@Override
public void onDrawFrame(GL10 gl) 
{
    // Clean the screen and the buffer depth
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    // replace actual matrix with the identity matrix
    gl.glLoadIdentity();

    // Move object 8 units in Z axis
    gl.glTranslatef(0, 0, -8);

    // Draw the cube
    cube.draw(gl);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) 
{
    gl.glViewport(0, 0, width, height);

    // Select proyection matrix
    gl.glMatrixMode(GL10.GL_PROJECTION);

    // Reset proyection matrix
    gl.glLoadIdentity();

    // Calculates the proportion of the window
    GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,100.0f);

    // Model matrix view
    gl.glMatrixMode(GL10.GL_MODELVIEW);

    // Reset model3d matrix
    gl.glLoadIdentity(); 
}
}

My own 3D model

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

public class Cube 
{
private float vertices[] = //(x,y,z)
{ 
1.000000f, -1.000000f, -1.000000f,
1.000000f, -1.000000f, 1.000000f,
-1.000000f, -1.000000f, 1.000000f,
-1.000000f, -1.000000f, -1.000000f,
1.000000f, 1.000000f, -0.999999f,
0.999999f, 1.000000f, 1.000001f,
-1.000000f, 1.000000f, 1.000000f,
-1.000000f, 1.000000f, -1.000000f
};

private short caras[] = //
{
        2, 3, 4,
        8, 7, 6,
        1, 5, 6,
        2, 6, 7,
        7, 8, 4,
        1, 4, 8,
        1, 2, 4,
        5, 8, 6,
        2, 1, 6,
        3, 2, 7,
        3, 7, 4,
        5, 1, 8
};

private float colors[] =
{
    1f, 0f, 0f, 1f
};

private FloatBuffer vertexBuffer;
private FloatBuffer colorBuffer;
private ShortBuffer indexBuffer;

public Cube() 
{
    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    vertexBuffer = vbb.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);

    ByteBuffer ibb = ByteBuffer.allocateDirect(caras.length * 2);
    ibb.order(ByteOrder.nativeOrder());
    indexBuffer = ibb.asShortBuffer();
    indexBuffer.put(caras);
    indexBuffer.position(0);

    ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
    cbb.order(ByteOrder.nativeOrder());
    colorBuffer = cbb.asFloatBuffer();
    colorBuffer.put(colors);
    colorBuffer.position(0);               
}

public void draw(GL10 gl) 
{
    gl.glFrontFace(GL10.GL_CCW);

    gl.glEnable(GL10.GL_CULL_FACE);

    gl.glCullFace(GL10.GL_BACK);

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

    gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

    gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);

    gl.glDrawElements(GL10.GL_TRIANGLES, caras.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

    gl.glDisable(GL10.GL_CULL_FACE);

    gl.glDisableClientState(GL10.GL_COLOR_ARRAY);          
}
}

External image of actual result: http://i60.tinypic.com/v3zuvr.png

genpfault
  • 51,148
  • 11
  • 85
  • 139
unotki
  • 1
  • 1

1 Answers1

0

Two problems:

  • OpenGL indices are 0-based, while this code uses 1-based indices.
  • The color array needs to contain a color for each vertex. Since there are 8 vertices, it needs 8 colors, or 8 * 4 = 32 floats.
Reto Koradi
  • 53,228
  • 8
  • 93
  • 133