PDA

View Full Version : Cube Code Help



JMoore
09-26-2010, 01:31 PM
So I'm attempting to draw a cube using glDrawArray and I can't figure out why the polygons aren't drawing correctly...my code is below.

package com.android.vortex;

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

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

import android.opengl.GLSurfaceView;

public class VortexRenderer implements GLSurfaceView.Renderer {
private FloatBuffer _vertexBuffer;
private FloatBuffer _colorBuffer;

private int _nrOfVertices = 0;

private float _xAngle;
private float _yAngle;

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glEnable(GL10.GL_CULL_FACE);
gl.glFrontFace(GL10.GL_CCW);

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

initShape();
}

public void onSurfaceChanged(GL10 gl, int w, int h) {
gl.glViewport(0, 0, w, h);
}

public void setXAngle(float angle) {
_xAngle = angle;
}

public float getXAngle() {
return _xAngle;
}

public void setYAngle(float angle) {
_yAngle = angle;
}

public float getYAngle() {
return _yAngle;
}

public void onDrawFrame(GL10 gl) {
// define the color of clipping wall
gl.glClearColor(0f, 0f, 0f, 1.0f);

// reset the rotation matrix
gl.glLoadIdentity();

// clear the color buffer to show the called color
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

// set the rotation
gl.glRotatef(_xAngle, 1f, 0f, 0f);
gl.glRotatef(_yAngle, 0f, 1f, 0f);

gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, _colorBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, _nrOfVertices);
}

private void initShape() {
float[] coords = {
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 1.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f,
};
_nrOfVertices = coords.length;

float[] colors = {
1f, 0f, 0f, 1f, // red
0f, 1f, 0f, 1f, // green
0f, 0f, 1f, 1f, // blue
1f, 1f, 1f, 1f, // white
1f, 0f, 0f, 1f, // red
0f, 1f, 0f, 1f, // green
1f, 0f, 0f, 1f, // red
0f, 1f, 0f, 1f, // green
};

ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4);
vbb.order(ByteOrder.nativeOrder());
_vertexBuffer = vbb.asFloatBuffer();

ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
cbb.order(ByteOrder.nativeOrder());
_colorBuffer = cbb.asFloatBuffer();

_vertexBuffer.put(coords);
_colorBuffer.put(colors);

_vertexBuffer.position(0);
_colorBuffer.position(0);
}
}

Any help would be most appreciated!