i am trying to view a model in the air like augmented reality stuff. I was able to draw the model, and bind it's rotation with aimuth, pitch and roll values so you can actually walk around the sphere and look behind it, under it, really fun. Now i want to translate it's x, y and z coordinates but i am definately doing something wrong.
Using java Syntax Highlighting
- //this is the perspective i create when the render is created
- //the perspective of the object is more distorted comparing orthof
- gl.glFrustumf(-size, size, -size / ratio, size / ratio, -0.01f, 100.0f);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
In onDrawFrame event i set x, y and z angles and try to translate the x, y, and z values with glTranslatef(x, y, z);
Using java Syntax Highlighting
- gl.glTranslatef(0f, 0f, 1.0f);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
if i give a value bigger than 1.6f or smaller than -1.0, the model is not visible. Even i do this:
Using java Syntax Highlighting
- //in onDrawFrame
- gl.glTranslatef(0f, 0f, zval);
- zval -= 0.0001f;
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
the shape get smaller but the perspective is very bad. and at the and it becomes a line and disappears.
another question, if i can translate the new x, y and z coordinates of the model , is the model going to move with the correct perspective or do i have to calculate by myself according to distance?
Hier i will give the full source code of the renderer. It is based on general_bradley's code (20face sphere),
Using java Syntax Highlighting
- public class ArRenderer implements GLSurfaceView.Renderer {
- private static final String TAG = ArRenderer.class.getSimpleName();
- private ShortBuffer indexBuffer;
- private FloatBuffer vertexBuffer;
- private FloatBuffer colorBuffer;
- private float xAngle = 0;
- private float yAngle = 0;
- private float zAngle = 0;
- public float xAxis = 0;
- public float yAxis = 0;
- public float zAxis = 0;
- private float width = 320f;
- private float height = 480f;
- @Override
- public void onSurfaceCreated(GL10 gl, EGLConfig config) {
- initTriangle();
- gl.glMatrixMode(GL10.GL_PROJECTION);
- float ratio = width / height;
- float size = .01f * (float) Math.tan(Math.toRadians(45.0) / 2);
- //orthographic projection
- //gl.glOrthof(-1, 1, -1 / ratio, 1 / ratio, 0.01f, 100.0f);
- //perspective
- gl.glFrustumf(-size, size, -size / ratio, size / ratio, -0.01f, 100.0f);
- gl.glViewport(0, 0, (int) width, (int) height);
- gl.glMatrixMode(GL10.GL_MODELVIEW);
- gl.glEnable(GL10.GL_DEPTH_TEST);
- gl.glDisable(GL10.GL_DITHER);
- // define the color we want to be displayed as the "clipping wall"
- gl.glClearColor(0, 0, 0, 0);
- //differentiate between visible and non visible sides
- gl.glEnable(GL10.GL_CULL_FACE);
- //the face in the front is the counter-clockwise face
- gl.glFrontFace(GL10.GL_CW);
- //which face should not be drawn
- gl.glCullFace(GL10.GL_BACK);
- gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
- gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
- }
- @Override
- public void onSurfaceChanged(GL10 gl, int w, int h) {
- width = w;
- height = h;
- gl.glViewport(0, 0, w, h);
- }
- @Override
- public void onDrawFrame(GL10 gl) {
- //set fixed rotation angle
- gl.glLoadIdentity();
- // clear the color buffer to show the ClearColor we called above...
- gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
- // define the vertices we want to draw
- gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
- gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
- //translate the perspective
- //gl.glTranslatef(0f, 0f, 1.0f);
- //set the rotation angle (if any)
- gl.glRotatef(zAngle, 0, 0, 1);
- gl.glRotatef(xAngle, 0, 1, 0);
- gl.glRotatef(yAngle, 1, 0, 0);
- //zAxis += 0.001f;
- // finally draw the vertices
- //GL_LINE_STRIP = wireframe
- //GL_TRIANGLES = solid
- gl.glDrawElements(GL10.GL_TRIANGLES, 60, GL10.GL_UNSIGNED_SHORT, indexBuffer);
- }
- private void initTriangle() {
- float u = 0.525731f;
- float v = 0.850651f;
- //vertices
- float[] coords = {
- 0f, -u, v, //0
- v, 0f, u, //1
- v, 0f, -u, //2
- -v, 0f, -u, //3
- -v, 0f, u, //4
- -u, v, 0f, //5
- u, v, 0f, //6
- u, -v, 0f, //7
- -u, -v, 0f, //8
- 0f, -u, -v, //9
- 0f, u, -v, //10
- 0f, u, v, //11
- };
- float[] colors = {
- 1f, 0f, 0f, 1f, //red 0
- 1f, 1f, 0f, 1f, //yellow 1
- 1f, 0f, 1f, 1f, //pink 2
- 0f, 1f, 0f, 1f, //green 3
- 0f, 1f, 1f, 1f, //l.blue 4
- 0f, 0f, 1f, 1f, //blue 5
- 1f, 1f, 1f, 1f, //white 6
- 1f, 1f, 0f, 1f, //yellow 7
- 1f, 1f, 1f, 1f, //white 8
- 1f, 0f, 0f, 1f, //red 9
- 1f, 1f, 0f, 1f, //yellow 10
- 0f, 0f, 1f, 1f, //blue 11
- };
- //faces - clockwise
- short[] indices = new short[] {
- 1, 2, 6, //
- 1, 7, 2, //
- 3, 4, 5, //
- 4, 3, 8, //
- 6, 5, 11, //
- 5, 6, 10, //
- 9, 10, 2, //
- 10, 9, 3, //
- 7, 8, 9, //
- 8, 7, 0, //
- 11, 0, 1, //
- 0, 11, 4, //
- 6, 2, 10, //
- 1, 6, 11, //
- 3, 5, 10, //
- 5, 4, 11, //
- 2, 7, 9, //
- 7, 1, 0, //
- 3, 9, 8, //error? (based on color assignment)
- 4, 8, 0, //
- };
- // float has 4 bytes, coordinate * 4 bytes
- ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4);
- vbb.order(ByteOrder.nativeOrder());
- vertexBuffer = vbb.asFloatBuffer();
- // short has 2 bytes, indices * 2 bytes
- ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
- ibb.order(ByteOrder.nativeOrder());
- indexBuffer = ibb.asShortBuffer();
- // float has 4 bytes, colors (RGBA) * 4 bytes
- ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
- cbb.order(ByteOrder.nativeOrder());
- colorBuffer = cbb.asFloatBuffer();
- vertexBuffer.put(coords);
- indexBuffer.put(indices);
- colorBuffer.put(colors);
- vertexBuffer.position(0);
- indexBuffer.position(0);
- colorBuffer.position(0);
- }
- public void setXAngle(float a) {
- xAngle = a;
- }
- public void setYAngle(float a) {
- yAngle = a;
- }
- public void setZAngle(float a) {
- zAngle = a;
- }
- }
Parsed in 0.057 seconds, using GeSHi 1.0.8.4
thanks in advance.


