opengl glTranslatef strange behavior

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

opengl glTranslatef strange behavior

Postby eyurdakul » Wed Mar 17, 2010 2:43 pm

Hi,

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.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. //this is the perspective i create when the render is created
  2. //the perspective of the object is more distorted comparing orthof
  3. gl.glFrustumf(-size, size, -size / ratio, size / ratio, -0.01f, 100.0f);
  4.  
Parsed in 0.029 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);

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. gl.glTranslatef(0f, 0f, 1.0f);
  2.  
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:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. //in onDrawFrame
  2. gl.glTranslatef(0f, 0f, zval);
  3. zval -= 0.0001f;
  4.  
Parsed in 0.033 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),

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class ArRenderer implements GLSurfaceView.Renderer {
  2.         private static final String TAG = ArRenderer.class.getSimpleName();
  3.        
  4.         private ShortBuffer indexBuffer;
  5.         private FloatBuffer vertexBuffer;
  6.         private FloatBuffer colorBuffer;
  7.        
  8.         private float xAngle = 0;
  9.         private float yAngle = 0;
  10.         private float zAngle = 0;
  11.        
  12.         public float xAxis  = 0;
  13.         public float yAxis  = 0;
  14.         public float zAxis  = 0;
  15.        
  16.        
  17.         private float width  = 320f;
  18.         private float height = 480f;
  19.        
  20.         @Override
  21.         public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  22.                 initTriangle();
  23.                
  24.                 gl.glMatrixMode(GL10.GL_PROJECTION);
  25.                 float ratio = width / height;
  26.                
  27.                 float size = .01f * (float) Math.tan(Math.toRadians(45.0) / 2);
  28.                
  29.                 //orthographic projection
  30.                 //gl.glOrthof(-1, 1, -1 / ratio, 1 / ratio, 0.01f, 100.0f);
  31.                 //perspective
  32.                 gl.glFrustumf(-size, size, -size / ratio, size / ratio, -0.01f, 100.0f);
  33.                                
  34.                 gl.glViewport(0, 0, (int) width, (int) height);
  35.                 gl.glMatrixMode(GL10.GL_MODELVIEW);
  36.                 gl.glEnable(GL10.GL_DEPTH_TEST);
  37.                 gl.glDisable(GL10.GL_DITHER);
  38.                
  39.                 // define the color we want to be displayed as the "clipping wall"
  40.                 gl.glClearColor(0, 0, 0, 0);
  41.                
  42.                 //differentiate between visible and non visible sides
  43.                 gl.glEnable(GL10.GL_CULL_FACE);
  44.                
  45.                 //the face in the front is the counter-clockwise face
  46.                 gl.glFrontFace(GL10.GL_CW);
  47.                
  48.                 //which face should not be drawn
  49.                 gl.glCullFace(GL10.GL_BACK);
  50.                
  51.                 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  52.                 gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
  53.         }
  54.         @Override
  55.         public void onSurfaceChanged(GL10 gl, int w, int h) {
  56.                 width = w;
  57.                 height = h;
  58.                 gl.glViewport(0, 0, w, h);
  59.         }
  60.        
  61.         @Override
  62.         public void onDrawFrame(GL10 gl) {
  63.                 //set fixed rotation angle
  64.                 gl.glLoadIdentity();
  65.                
  66.                 // clear the color buffer to show the ClearColor we called above...
  67.                 gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  68.                
  69.                 // define the vertices we want to draw
  70.                 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
  71.                 gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
  72.                
  73.                 //translate the perspective
  74.                 //gl.glTranslatef(0f, 0f, 1.0f);
  75.                
  76.                 //set the rotation angle (if any)
  77.                 gl.glRotatef(zAngle, 0, 0, 1);
  78.                 gl.glRotatef(xAngle, 0, 1, 0);
  79.                 gl.glRotatef(yAngle, 1, 0, 0);
  80.                
  81.                 //zAxis += 0.001f;
  82.                 // finally draw the vertices
  83.                 //GL_LINE_STRIP = wireframe
  84.                 //GL_TRIANGLES = solid
  85.                 gl.glDrawElements(GL10.GL_TRIANGLES, 60, GL10.GL_UNSIGNED_SHORT, indexBuffer);
  86.         }
  87.        
  88.         private void initTriangle() {
  89.                 float u = 0.525731f;
  90.                 float v = 0.850651f;
  91.                
  92.                 //vertices
  93.                 float[] coords = {
  94.                         0f, -u,  v,     //0
  95.                          v, 0f,  u,     //1
  96.                          v, 0f, -u,     //2
  97.                         -v, 0f, -u,     //3
  98.                         -v, 0f,  u,     //4
  99.                         -u,  v, 0f,     //5
  100.                          u,  v, 0f,     //6
  101.                          u, -v, 0f,     //7
  102.                         -u, -v, 0f,     //8
  103.                         0f, -u, -v,     //9
  104.                         0f,  u, -v,     //10
  105.                         0f,  u,  v,     //11
  106.                 };
  107.                
  108.                 float[] colors = {
  109.                         1f, 0f, 0f, 1f,         //red           0      
  110.                         1f, 1f, 0f, 1f,         //yellow        1      
  111.                         1f, 0f, 1f, 1f,         //pink          2      
  112.                         0f, 1f, 0f, 1f,         //green         3
  113.                         0f, 1f, 1f, 1f,         //l.blue        4
  114.                         0f, 0f, 1f, 1f,         //blue          5
  115.                         1f, 1f, 1f, 1f,         //white         6
  116.                         1f, 1f, 0f, 1f,         //yellow        7
  117.                         1f, 1f, 1f, 1f,         //white         8
  118.                         1f, 0f, 0f, 1f,         //red           9
  119.                         1f, 1f, 0f, 1f,         //yellow        10
  120.                         0f, 0f, 1f, 1f,         //blue          11
  121.                 };
  122.                
  123.                 //faces - clockwise
  124.                 short[] indices = new short[] {
  125.                          1,  2,  6,     //
  126.                          1,  7,  2,     //
  127.                          3,  4,  5,     //
  128.                          4,  3,  8,     //
  129.                          6,  5, 11,     //
  130.                          5,  6, 10,     //
  131.                          9, 10,  2,     //
  132.                         10,  9,  3,     //
  133.                          7,  8,  9,     //
  134.                          8,  7,  0,     //
  135.                         11,  0,  1,     //
  136.                          0, 11,  4,     //
  137.                          6,  2, 10,     //
  138.                          1,  6, 11,     //
  139.                          3,  5, 10,     //
  140.                          5,  4, 11,     //
  141.                          2,  7,  9,     //
  142.                          7,  1,  0,     //
  143.                          3,  9,  8,     //error? (based on color assignment)
  144.                          4,  8,  0,     //
  145.                 };
  146.  
  147.                 // float has 4 bytes, coordinate * 4 bytes
  148.                 ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4);
  149.                 vbb.order(ByteOrder.nativeOrder());
  150.                 vertexBuffer = vbb.asFloatBuffer();
  151.  
  152.                 // short has 2 bytes, indices * 2 bytes
  153.                 ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
  154.                 ibb.order(ByteOrder.nativeOrder());
  155.                 indexBuffer = ibb.asShortBuffer();
  156.  
  157.                 // float has 4 bytes, colors (RGBA) * 4 bytes
  158.                 ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
  159.                 cbb.order(ByteOrder.nativeOrder());
  160.                 colorBuffer = cbb.asFloatBuffer();
  161.  
  162.                 vertexBuffer.put(coords);
  163.                 indexBuffer.put(indices);
  164.                 colorBuffer.put(colors);
  165.        
  166.                 vertexBuffer.position(0);
  167.                 indexBuffer.position(0);
  168.                 colorBuffer.position(0);
  169.         }
  170.         public void setXAngle(float a) {
  171.                 xAngle = a;
  172.         }
  173.        
  174.         public void setYAngle(float a) {
  175.                 yAngle = a;
  176.         }
  177.        
  178.         public void setZAngle(float a) {
  179.                 zAngle = a;
  180.         }
  181. }
  182.  
Parsed in 0.057 seconds, using GeSHi 1.0.8.4


thanks in advance.
eyurdakul
Junior Developer
Junior Developer
 
Posts: 13
Joined: Thu Mar 04, 2010 1:19 pm

Top

Postby eyurdakul » Thu Mar 18, 2010 8:30 pm

i will answer my question again. I don't know if it is mentioned here but i've solved the perspective problems by changing the sequence of the transitions and adding glScalef after glTranslatef. Here is my code;

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. gl.glRotatef(xAngle, 0, 1, 0);
  3.  
  4. gl.glRotatef(yAngle, 1, 0, 0);
  5.  
  6.        
  7.  
  8. _scale = 1-zAxis;
  9.  
  10. gl.glTranslatef(xAxis, yAxis, zAxis);
  11.  
  12. gl.glScalef(_scale, _scale, _scale);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


I tought, glTranslatef would do them all for me but now it's solved.
eyurdakul
Junior Developer
Junior Developer
 
Posts: 13
Joined: Thu Mar 04, 2010 1:19 pm

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests