I've been programming for years, but I have very little experience with 3D
graphics. I thought OpenGL would be the most efficient in this project.
It's coming along alright, I am still studying the API and documentation.
But I have encountered a problem using multiple classes. For instance, here
I try to use a separate class for the player model. (Yes, currently it's
just a cube.)
Renderer:
Code:
package com.braindrool.bla;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
public class BlaRenderer implements Renderer {
int nrOfVertices;
float A;
Player player = new Player();
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// TODO Auto-generated method stub
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glFrontFace(GL10.GL_CCW);
gl.glCullFace(GL10.GL_BACK);
gl.glClearColor(0.3f, 0.8f, 0.9f, 1);
// initTriangle();
player.initPlayer();
}
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
gl.glLoadIdentity();
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
// gl.glRotatef(A * 2, 1f, 0f, 0f);
// gl.glRotatef(A, 0f, 1f, 0f);
//
// gl.glColor4f(0.5f, 0, 0, 1);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, nrOfVertices,
GL10.GL_UNSIGNED_SHORT, indexBuffer);
// A++;
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
gl.glViewport(0, 0, width, height);
}
private FloatBuffer vertexBuffer;
private ShortBuffer indexBuffer;
private FloatBuffer colorBuffer;
private void initTriangle() {
float[] coords = { 0, 0.5f, 0, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f,
0.5f,
0, 0, -0.5f };
nrOfVertices = coords.length;
ByteBuffer vbb = ByteBuffer.allocateDirect(nrOfVertices * 3 *
4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
ByteBuffer ibb = ByteBuffer.allocateDirect(nrOfVertices * 2);
ibb.order(ByteOrder.nativeOrder());
indexBuffer = ibb.asShortBuffer();
ByteBuffer cbb = ByteBuffer.allocateDirect(4 * nrOfVertices *
4);
cbb.order(ByteOrder.nativeOrder());
colorBuffer = cbb.asFloatBuffer();
float[] colors = { 1f, 0f, 0f, 1f, // point 1
0f, 1f, 0f, 1f, // point 2
0f, 0f, 1f, 1f, // point 3
};
short[] indices = new short[] {
// indices
0, 1, 2, 0, 2, 3, 0, 3, 1, 3, 1, 2
};
vertexBuffer.put(coords);
indexBuffer.put(indices);
colorBuffer.put(colors);
vertexBuffer.position(0);
indexBuffer.position(0);
colorBuffer.position(0);
}
}
And now the player class.
Code:
package com.braindrool.bla;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class Player extends BlaRenderer{
private FloatBuffer vertexBuffer;
private ShortBuffer indexBuffer;
private FloatBuffer colorBuffer;
private int nrOfVertices;
float A;
public void initPlayer() {
float[] coords = {
-0.5f, 0.5f, 0, // P0
-0.5f, 0, 0, // P1
0.5f, 0, 0, // P2
0.5f, 0.5f, 0, // P3
0.5f, 0.5f, 0.5f, // P4
-0.5f, 0.5f, 0.5f, // P5
-0.5f, 0, 0.5f, // P6
0.5f, 0, 0.5f // P7
};
nrOfVertices = coords.length;
ByteBuffer vbb = ByteBuffer.allocateDirect(nrOfVertices * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
ByteBuffer ibb = ByteBuffer.allocate(nrOfVertices * 2);
ibb.order(ByteOrder.nativeOrder());
indexBuffer = ibb.asShortBuffer();
ByteBuffer cbb = ByteBuffer.allocate(12 * 4);
ibb.order(ByteOrder.nativeOrder());
colorBuffer = cbb.asFloatBuffer();
short[] indices = {
0, 1, 2, 3, 0, 2, 0, 6, 1, 0, 5, 6, 5, 7, 6, 5, 4, 7, 4, 3, 7,
3, 2, 7
};
float[] colors = { 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1 };
vertexBuffer.put(coords);
indexBuffer.put(indices);
colorBuffer.put(colors);
vertexBuffer.position(0);
indexBuffer.position(0);
colorBuffer.position(0);
}
}
It's a bit sloppy but meh.
The error:
Code:
Player player = new Player()
&
Code:
public class Player extends BlaRenderer{
Help appreciated!
~Braindrool
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en