First of all, I'm very new to OpenGL, so maybe im missing out some basic concept.
I am trying to make a cube that rotates and change the color with flat lighting. Something like in game "EDGE", the problem comes that when the illumination is presented by triangles instead of by faces.
Thats what i want
https://i.stack.imgur.com/GBM8D.jpg
And thats what i have
https://i.stack.imgur.com/mRj0I.png
https://i.stack.imgur.com/b1Xfx.png
Cube2.java
private FloatBuffer mVertexBuffer;
private FloatBuffer mColorBuffer;
private ByteBuffer mIndexBuffer;
private int colorIndex = 0;
private int colorTimer = 0;
private int colorWait = 5;
private int colorCant = 360;
private double colorJump = 360.0 / (colorCant * 1.0);
private int[] colors = new int[colorCant];
private float vertices[] = {
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f
};
private byte indices[] = {
0, 4, 5, 0, 5, 1,
1, 5, 6, 1, 6, 2,
2, 6, 7, 2, 7, 3,
3, 7, 4, 3, 4, 0,
4, 7, 6, 4, 6, 5,
3, 0, 1, 3, 1, 2
};
public Cube2() {
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
mVertexBuffer = byteBuf.asFloatBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(colors.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
mColorBuffer = byteBuf.asFloatBuffer();
mColorBuffer.position(0);
mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
mIndexBuffer.put(indices);
mIndexBuffer.position(0);
for (int i = 0; i < colors.length; i++) {
colors[i] = Color.HSVToColor(new float[]{(float) (colorJump * i), 1.0f, 1.0f});
}
}
public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CW);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glColor4f((Color.red(colors[colorIndex])) / 255.0f, (Color.green(colors[colorIndex])) / 255.0f, (Color.blue(colors[colorIndex])) / 255.0f, 1.0f);
gl.glDrawElements(GL10.GL_TRIANGLES, 36, GL10.GL_UNSIGNED_BYTE,
mIndexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
colorTimer++;
if (colorTimer < colorWait) {
colorTimer++;
} else {
colorTimer = 0;
if (colorIndex < colors.length - 1) {
colorIndex++;
} else {
colorIndex = 0;
}
}
}
MyGLRenderer.java
private float cubeAngleY = 90.0f;
private float cubeSpeedY = 1.0f;
private float[] lightDiffuse = {1.0f, 1.0f, 1.0f, 1.0f};
private float[] lightPosition = {10.0f, 0.0f, 10.0f, 1.0f};
public MyGLRenderer(Context context) {
cube2 = new Cube2();
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
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);
gl.glShadeModel(GL10.GL_FLAT);
gl.glDisable(GL10.GL_DITHER);
gl.glEnable(GL10.GL_COLOR_MATERIAL);
gl.glEnable(GL10.GL_NORMALIZE);
gl.glEnable(GL10.GL_LIGHTING);
gl.glEnable(GL10.GL_LIGHT0);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuse, 0);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
if (height == 0) height = 1;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(-2.0f, 2.0f, -2.0f, 2.0f, 2.0f, -2.0f);
gl.glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPosition, 0);
gl.glLoadIdentity();
gl.glRotatef(cubeAngleY, 0.0f, 1.0f, 0.0f);
cube2.draw(gl);
cubeAngleY += cubeSpeedY;
}
Can someone help me? code examples would be apreciated, ty so much