• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps openGl application falls

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:
public class Objeto3D extends Object {
	
	//Buffers do objeto
	private ShortBuffer _indexBuffer; //indices com a ordem que deve ser desenhado
	 private FloatBuffer _vertexBuffer; //vertices
	 private FloatBuffer _normaxBuffer; //normais
	 //private FloatBuffer _corexBuffer; //cores
	 
	 private short[] _indicesArray;
	 private int _nrOfVertices = 4;
	 public String str_indices = "4,0,1,2,3,4,2,4,1";
	 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";
	 public String str_normais = "-0.000000,0.000000,1.000000,-0.000000,0.000000,1.000000,-0.000000,0.000000,1.000000";
	 public String str_cores = "0,1,0,0,1,0,0,1,0";
	 
	 public float[] coordV;
	 public float[] coordN;
	 public float[] coordC;
	
	public Objeto3D()
	{
		ajustarForma(str_indices, str_vertices, str_normais);
	}
	
	 private void decodificarVertices()
	 {
		 String coordIndices[] = str_indices.split(",");
		 String coordenadas[] = str_vertices.split(",");
		 String coordNormais[] = str_normais.split(",");
		 //String coordCores[] = str_cores.split(",");
		 
		 coordV = new float[coordenadas.length];
		 coordN = new float[coordNormais.length];
		 coordC = new float[coordenadas.length];
		 _indicesArray = new short[coordIndices.length];
		 
		 for(short i=0;i<coordenadas.length;i++)
		 {
			 coordV[i] = Float.valueOf(coordenadas[i]);
			// coordC[i] = Float.valueOf(coordCores[i]);
		 }
		 
		 for(short i=0;i<coordIndices.length;i++)
			 _indicesArray[i] = Short.valueOf(coordIndices[i]);
		 
		 for(short i=0;i < coordNormais.length;i++)
			 coordN[i] = Float.valueOf(coordNormais[i]);
		 
		 _nrOfVertices = coordenadas.length;
	 }
	  
	 private void initShape() {
	     // float has 4 bytes
	     ByteBuffer vbb = ByteBuffer.allocateDirect(_nrOfVertices * 3 * 4);
	     vbb.order(ByteOrder.nativeOrder());
	     _vertexBuffer = vbb.asFloatBuffer();
	     
	     //float has 4 bytes;
	     ByteBuffer nbb = ByteBuffer.allocateDirect(_nrOfVertices * 3 * 4);
	     nbb.order(ByteOrder.nativeOrder());
	     _normaxBuffer = nbb.asFloatBuffer();
	     
	     /*/ float has 4 bytes
	     ByteBuffer cbb = ByteBuffer.allocateDirect(_nrOfVertices * 3 * 4);
	     cbb.order(ByteOrder.nativeOrder());
	     _corexBuffer = cbb.asFloatBuffer();*/
	  
	     // short has 2 bytes
	     ByteBuffer ibb = ByteBuffer.allocateDirect(_indicesArray.length * 2);
	     ibb.order(ByteOrder.nativeOrder());
	     _indexBuffer = ibb.asShortBuffer();
	     
	     _vertexBuffer.put(coordV);
	     _normaxBuffer.put(coordN);
	     //_corexBuffer.put(coordC);
	     _indexBuffer.put(_indicesArray);
	  
	     _vertexBuffer.position(0);
	     _normaxBuffer.position(0);
	     //_corexBuffer.position(0);
	     _indexBuffer.position(0);
	 }
	 
	 public void desenharObjeto(GL10 gl)
	 {
		// Counter-clockwise winding.
		 gl.glFrontFace(GL10.GL_CCW); // OpenGL docs
		 // Enable face culling.
		 gl.glEnable(GL10.GL_CULL_FACE); // OpenGL docs
		 // What faces to remove with the face culling.
		 gl.glCullFace(GL10.GL_BACK); // OpenGL docs
		 // Enabled the vertices buffer for writing and to be used during rendering.
		 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);// OpenGL docs.
		 
		 // Define a cor do elemento a ser desenhado
		 //gl.glColor4f(0.0f, 0.5f, 0f, 0.5f);
		 //gl.glColorPointer(3, GL10.GL_FLOAT, 0, _corexBuffer);
		 
		 float white[] = new float[] { 1f, 1f, 1f, 1.0f};
		 float red[] = new float[] { 1f, 0f, 0f, 1.0f};
		 
		 gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, red, 0);
         gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, white, 0);
	        
		 // Define os vertices que desejam desenhar
		 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
	        
		 //Define as normais
		 //gl.glNormalPointer(GL10.GL_FLOAT, 0, _normaxBuffer);
	     
		 // Desenha os vertices
		 gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
		 
		// Disable the vertices buffer.
		 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); // OpenGL doc
		 
		 // Disable face culling.
		 gl.glDisable(GL10.GL_CULL_FACE); // OpenGL docs
	 }
	 
	 public void ajustarForma(String indices, String vertices, String normais)
	 {
		 str_indices = indices;
		 str_vertices = vertices;
		 str_normais = normais;
		 decodificarVertices();
		 initShape();
	 }

}

Its my first time posting at forum list. Sorry about any mistake and since now thanks guys
 
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:
public class Renderizador3D implements GLSurfaceView.Renderer {
	
	public Objeto3D cenaAtual = null;

	@Override
	public void onDrawFrame(GL10 gl) {
		//Limpa a tela com a cor
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
	    
		//Reseta as informa
 
Upvote 0
Try this:

Code:
for(short i=0;i<coordenadas.length;i++)
		 {
			 coordV[i] = Float.parseFloat(coordenadas[i]);
			// coordC[i] = Float.parseFloat(coordCores[i]);
		 }
		 
		 for(short i=0;i<coordIndices.length;i++)
			 _indicesArray[i] = Short.parseShort(coordIndices[i]);
		 
		 for(short i=0;i < coordNormais.length;i++)
			 coordN[i] = Float.parseFloat(coordNormais[i]);
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones