Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

Get excited for the Samsung Galaxy S5! Find everything you need and discuss it in our Galaxy S5 Forum!

test: Reply
 
LinkBack Thread Tools
Old November 26th, 2010, 09:34 PM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Nov 2010
Posts: 2
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

niksonleonard is offline  
Last edited by niksonleonard; November 26th, 2010 at 09:43 PM.
Reply With Quote
sponsored links
Old November 27th, 2010, 03:02 PM   #2 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,936
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 478 Times in 292 Posts
Default

Well, this is only the code for your 3d object. You also need to code an Activity and a Renderer class.
jonbonazza is offline  
Reply With Quote
Old November 28th, 2010, 07:02 AM   #3 (permalink)
New Member
Thread Author (OP)
 
Join Date: Nov 2010
Posts: 2
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

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çoes da matriz
        gl.glLoadIdentity();
        
        //Translada cena
        gl.glTranslatef(0, 0, -14);
        
        //Iluminação
        gl.glEnable(GL10.GL_LIGHTING);
        gl.glEnable(GL10.GL_LIGHT0);
        
        desenharCenaAtual(gl);
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// Ajusta a porta de exibição (view) para o atual tamanho.
		gl.glViewport(0, 0, width, height);
		
		// Seleciona a matriz de projeção
		gl.glMatrixMode(GL10.GL_PROJECTION);
		
		// Reseta matriz de projeção
		gl.glLoadIdentity();
		
		// Calcula o aspect ratio da janela
		GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);
		
		// Seleciona a matriz de modelo de exibição
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		
		// Reseta a matriz de modelo de exibição
		gl.glLoadIdentity();
		
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		
		//Objeto3D teste = new Objeto3D();
		//adicionarObjeto3d(teste);
		
		// Cor de fundo da tela e tipo de sombreamento
		gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    	gl.glShadeModel(GL10.GL_SMOOTH);
    	
    	//Iluminação
    	 iluminacao(gl);
    	 
    	//Ajuste de buffer de profundidade
    	gl.glClearDepthf(1.0f);
	
    	// Habilita profundidade.
    	gl.glEnable(GL10.GL_DEPTH_TEST);
		
    	// Tipo de teste de profundidade.
	    gl.glDepthFunc(GL10.GL_LEQUAL);
	    
	    // Calculo de perspectiva.
	    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
		
	}
	
	public void iluminacao(GL10 gl)
	{
		if(true)
    	{
			//Caracteristicas da iluminação
			float lightAmbient[] = new float[] { 0.2f, 0.3f, 0.6f, 1.0f };
			float lightSpecular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
		    float[] lightPosition = {0.f, 0.f, 15.f, 1.f};
		
	    	gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient,     0);
		    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, lightSpecular, 0);
		    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPosition, 0);
    	}
	}
	
	public void desenharCenaAtual(GL10 gl)
	{
		if(cenaAtual!=null)
			cenaAtual.desenharObjeto(gl);
		
		/*for(Objeto3D obj: cenaAtual)
			obj.desenharObjeto(gl);*/
	}
}
Code:
public class inicialActivity extends Activity {
	
	//Saida de texto na tela
	public TextView txtSaida;
	public Renderizador3D _renderizador;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        txtSaida = new TextView(this);
        txtSaida.setText("Virtuomons iniciado");
        
        //Cria um layout para essa atividade
        LinearLayout viewInicial = new LinearLayout(this);
        viewInicial.addView(txtSaida);
        
        //Edereço do arquivo vtm a ser carregado
        String enderecoUrl = "http://cim2.com.br/virtuomons/virtuomon.vtm";
         
        _renderizador = new Renderizador3D();
        _renderizador.cenaAtual = new Objeto3D();
        
        GLSurfaceView view = new GLSurfaceView(this);
   		view.setRenderer(_renderizador);
   		setContentView(view);
    }
}
My idea is to get vertices information from a file. When I put simple values, like two triangules to build a quad face it works but when I use a shape with 5 vertices it doesnt happens.
This is what I have.
Thanks again jonbonazza.
niksonleonard is offline  
Reply With Quote
Old November 28th, 2010, 09:19 AM   #4 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,936
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 478 Times in 292 Posts
Default

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]);
jonbonazza is offline  
Reply With Quote
Reply
Tags
opengl, stopped working


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 01:45 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.