Hello everyone,
I created a rotating 3-D Cube in OpenGLES 1 and I can't figure out how to get color on it. Hopefully this is an easy fix, I wrote the code I needed to draw color on it, but it doesn't seem to do anything.
Here is the code for the Cube:
public class Square {
private FloatBuffer floatBuffer, colorBuffer;
private float vertices[] = {
1, 1, 1,
1, -1, -1,
-1, -1, -1,
-1, 1, -1,
1, 1, 1,
1, -1, 1,
-1, -1, 1,
-1, 1, 1,
};
private ShortBuffer shortBuffer;
private short index[] = {
3,4,0, 0,4,1, 3,0,1,
3,7,4, 7,6,4, 7,3,6,
3,1,2, 1,6,2, 6,3,2,
1,4,5, 5,6,1, 6,5,1,
};
private float cColors[] = {
0, .5f, .7f, 1,
0, .7f, .5f, 1,
.3f, .7f, 0, 1,
.4f, 0, .2f, 1,
0, .5f, .2f, 1,
.3f, 0, .6f, 1,
0, .4f, .3f, 1,
0, .3f, .7f, 1
};
Square() {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
floatBuffer = byteBuffer.asFloatBuffer();
floatBuffer.put(vertices);
floatBuffer.position(0);
ByteBuffer bBuff = ByteBuffer.allocateDirect(index.length * 2);
bBuff.order(ByteOrder.nativeOrder());
shortBuffer = bBuff.asShortBuffer();
shortBuffer.put(index);
shortBuffer.position(0);
ByteBuffer bColors = ByteBuffer.allocateDirect(cColors.length * 4);
bColors.order(ByteOrder.nativeOrder());
colorBuffer = bColors.asFloatBuffer();
colorBuffer.put(cColors);
colorBuffer.position(0);
}
public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CW);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, floatBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, index.length, GL10.GL_UNSIGNED_SHORT, shortBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
colors(gl);
}
private void colors(GL10 gl) {
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
}
Also here is the code for the Renderer class:
public class ExampleRenderer implements GLSurfaceView.Renderer {
Triangle triangle;
Square square;
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(0f, .3f, .6f, 1f);
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
triangle = new Triangle();
square = new Square();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
float ratio = (float) width/height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 25);
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glDisable(GL10.GL_DITHER);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 0, 3, 0, 0, 0, 0, 2, 0);
long time = SystemClock.uptimeMillis() % 4000L;
float angle = (float) (.090 * ((int) time));
gl.glRotatef(angle, 3, 0, 1);
square.draw(gl);
}
}
Thanks for the help!