openGl application falls


Last Updated:

  1. niksonleonard

    niksonleonard New Member This Topic's Starter

    Joined:
    Nov 26, 2010
    Messages:
    2
    Likes Received:
    0
    Hello friends!

    I would like to ask you a little help.
    Im trying to develop an application using openGl but when I run it the it closes.
    All that Im trying to do is get vertex values from a string and them draw it using openGl.

    Code (Text):
    1. public class Objeto3D extends Object {
    2.    
    3.     //Buffers do objeto
    4.     private ShortBuffer _indexBuffer; //indices com a ordem que deve ser desenhado
    5.      private FloatBuffer _vertexBuffer; //vertices
    6.      private FloatBuffer _normaxBuffer; //normais
    7.      //private FloatBuffer _corexBuffer; //cores
    8.      
    9.      private short[] _indicesArray;
    10.      private int _nrOfVertices = 4;
    11.      public String str_indices = "4,0,1,2,3,4,2,4,1";
    12.      public String str_vertices = "-1.000000,1.000000,0.000000,-1.000000,-1.000000,0.000000,1.000000,-1.000000,0.000000,1.000000,1.000000,0.000000,0.200000,0.100000,0.000000";
    13.      public String str_normais = "-0.000000,0.000000,1.000000,-0.000000,0.000000,1.000000,-0.000000,0.000000,1.000000";
    14.      public String str_cores = "0,1,0,0,1,0,0,1,0";
    15.      
    16.      public float[] coordV;
    17.      public float[] coordN;
    18.      public float[] coordC;
    19.    
    20.     public Objeto3D()
    21.     {
    22.         ajustarForma(str_indices, str_vertices, str_normais);
    23.     }
    24.    
    25.      private void decodificarVertices()
    26.      {
    27.          String coordIndices[] = str_indices.split(",");
    28.          String coordenadas[] = str_vertices.split(",");
    29.          String coordNormais[] = str_normais.split(",");
    30.          //String coordCores[] = str_cores.split(",");
    31.          
    32.          coordV = new float[coordenadas.length];
    33.          coordN = new float[coordNormais.length];
    34.          coordC = new float[coordenadas.length];
    35.          _indicesArray = new short[coordIndices.length];
    36.          
    37.          for(short i=0;i<coordenadas.length;i++)
    38.          {
    39.              coordV[i] = Float.valueOf(coordenadas[i]);
    40.             // coordC[i] = Float.valueOf(coordCores[i]);
    41.          }
    42.          
    43.          for(short i=0;i<coordIndices.length;i++)
    44.              _indicesArray[i] = Short.valueOf(coordIndices[i]);
    45.          
    46.          for(short i=0;i < coordNormais.length;i++)
    47.              coordN[i] = Float.valueOf(coordNormais[i]);
    48.          
    49.          _nrOfVertices = coordenadas.length;
    50.      }
    51.      
    52.      private void initShape() {
    53.          // float has 4 bytes
    54.          ByteBuffer vbb = ByteBuffer.allocateDirect(_nrOfVertices * 3 * 4);
    55.          vbb.order(ByteOrder.nativeOrder());
    56.          _vertexBuffer = vbb.asFloatBuffer();
    57.          
    58.          //float has 4 bytes;
    59.          ByteBuffer nbb = ByteBuffer.allocateDirect(_nrOfVertices * 3 * 4);
    60.          nbb.order(ByteOrder.nativeOrder());
    61.          _normaxBuffer = nbb.asFloatBuffer();
    62.          
    63.          /*/ float has 4 bytes
    64.          ByteBuffer cbb = ByteBuffer.allocateDirect(_nrOfVertices * 3 * 4);
    65.          cbb.order(ByteOrder.nativeOrder());
    66.          _corexBuffer = cbb.asFloatBuffer();*/
    67.      
    68.          // short has 2 bytes
    69.          ByteBuffer ibb = ByteBuffer.allocateDirect(_indicesArray.length * 2);
    70.          ibb.order(ByteOrder.nativeOrder());
    71.          _indexBuffer = ibb.asShortBuffer();
    72.          
    73.          _vertexBuffer.put(coordV);
    74.          _normaxBuffer.put(coordN);
    75.          //_corexBuffer.put(coordC);
    76.          _indexBuffer.put(_indicesArray);
    77.      
    78.          _vertexBuffer.position(0);
    79.          _normaxBuffer.position(0);
    80.          //_corexBuffer.position(0);
    81.          _indexBuffer.position(0);
    82.      }
    83.      
    84.      public void desenharObjeto(GL10 gl)
    85.      {
    86.         // Counter-clockwise winding.
    87.          gl.glFrontFace(GL10.GL_CCW); // OpenGL docs
    88.          // Enable face culling.
    89.          gl.glEnable(GL10.GL_CULL_FACE); // OpenGL docs
    90.          // What faces to remove with the face culling.
    91.          gl.glCullFace(GL10.GL_BACK); // OpenGL docs
    92.          // Enabled the vertices buffer for writing and to be used during rendering.
    93.          gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);// OpenGL docs.
    94.          
    95.          // Define a cor do elemento a ser desenhado
    96.          //gl.glColor4f(0.0f, 0.5f, 0f, 0.5f);
    97.          //gl.glColorPointer(3, GL10.GL_FLOAT, 0, _corexBuffer);
    98.          
    99.          float white[] = new float[] { 1f, 1f, 1f, 1.0f};
    100.          float red[] = new float[] { 1f, 0f, 0f, 1.0f};
    101.          
    102.          gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, red, 0);
    103.          gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, white, 0);
    104.            
    105.          // Define os vertices que desejam desenhar
    106.          gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
    107.            
    108.          //Define as normais
    109.          //gl.glNormalPointer(GL10.GL_FLOAT, 0, _normaxBuffer);
    110.          
    111.          // Desenha os vertices
    112.          gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
    113.          
    114.         // Disable the vertices buffer.
    115.          gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); // OpenGL doc
    116.          
    117.          // Disable face culling.
    118.          gl.glDisable(GL10.GL_CULL_FACE); // OpenGL docs
    119.      }
    120.      
    121.      public void ajustarForma(String indices, String vertices, String normais)
    122.      {
    123.          str_indices = indices;
    124.          str_vertices = vertices;
    125.          str_normais = normais;
    126.          decodificarVertices();
    127.          initShape();
    128.      }
    129.  
    130. }
    131.  
    Its my first time posting at forum list. Sorry about any mistake and since now thanks guys
     

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    Well, this is only the code for your 3d object. You also need to code an Activity and a Renderer class.
     
  3. niksonleonard

    niksonleonard New Member This Topic's Starter

    Joined:
    Nov 26, 2010
    Messages:
    2
    Likes Received:
    0
    Hello jonbonazza, thanks for reply!

    On actualy, I alreary have a Activity and render class, and its working fine. I made a test setting floats values on a float array to draw a quad and worked pretty well. My problem is when I need to get values from a string and more than 3 vertices.
    I will put my classe here anyway, they are very simple.

    Code (Text):
    1. public class Renderizador3D implements GLSurfaceView.Renderer {
    2.    
    3.     public Objeto3D cenaAtual = null;
    4.  
    5.     @Override
    6.     public void onDrawFrame(GL10 gl) {
    7.         //Limpa a tela com a cor
    8.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    9.        
    10.         //Reseta as informa
     
  4. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    Try this:

    Code (Text):
    1.  
    2. for(short i=0;i<coordenadas.length;i++)
    3.          {
    4.              coordV[i] = Float.parseFloat(coordenadas[i]);
    5.             // coordC[i] = Float.parseFloat(coordCores[i]);
    6.          }
    7.          
    8.          for(short i=0;i<coordIndices.length;i++)
    9.              _indicesArray[i] = Short.parseShort(coordIndices[i]);
    10.          
    11.          for(short i=0;i < coordNormais.length;i++)
    12.              coordN[i] = Float.parseFloat(coordNormais[i]);
    13.  
     

Share This Page

Loading...