How to draw a cube using openGL?

Asked

Viewed 690 times

1

How to draw a cube using Opengl on Android?

  • Have you made any attempt? What were the results of the attempts you made?

1 answer

2


Cubo.java
package com.teste;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;

public class Cubo {
   private FloatBuffer vertexBuffer;
   private int numFaces = 6;

   private float[][] colors = {  // Array para armazenar cores dos 6 lados
      {1.0f, 0.5f, 0.0f, 1.0f},  // 0. laranja
      {1.0f, 0.0f, 1.0f, 1.0f},  // 1. violeta
      {0.0f, 1.0f, 0.0f, 1.0f},  // 2. verde
      {0.0f, 0.0f, 1.0f, 1.0f},  // 3. azul
      {1.0f, 0.0f, 0.0f, 1.0f},  // 4. vermelho
      {1.0f, 1.0f, 0.0f, 1.0f}   // 5. amarelo
   };

   private float[] vertices = {  // Vertices dos 6 lados
      // FRENTE
      -1.0f, -1.0f,  1.0f,  // 0. left-bottom-front
       1.0f, -1.0f,  1.0f,  // 1. right-bottom-front
      -1.0f,  1.0f,  1.0f,  // 2. left-top-front
       1.0f,  1.0f,  1.0f,  // 3. right-top-front
      // ATRÁS
       1.0f, -1.0f, -1.0f,  // 6. right-bottom-back
      -1.0f, -1.0f, -1.0f,  // 4. left-bottom-back
       1.0f,  1.0f, -1.0f,  // 7. right-top-back
      -1.0f,  1.0f, -1.0f,  // 5. left-top-back
      // ESQUERDA
      -1.0f, -1.0f, -1.0f,  // 4. left-bottom-back
      -1.0f, -1.0f,  1.0f,  // 0. left-bottom-front 
      -1.0f,  1.0f, -1.0f,  // 5. left-top-back
      -1.0f,  1.0f,  1.0f,  // 2. left-top-front
      // DIREITA
       1.0f, -1.0f,  1.0f,  // 1. right-bottom-front
       1.0f, -1.0f, -1.0f,  // 6. right-bottom-back
       1.0f,  1.0f,  1.0f,  // 3. right-top-front
       1.0f,  1.0f, -1.0f,  // 7. right-top-back
      // EM CIMA
      -1.0f,  1.0f,  1.0f,  // 2. left-top-front
       1.0f,  1.0f,  1.0f,  // 3. right-top-front
      -1.0f,  1.0f, -1.0f,  // 5. left-top-back
       1.0f,  1.0f, -1.0f,  // 7. right-top-back
      // EM BAIXO
      -1.0f, -1.0f, -1.0f,  // 4. left-bottom-back
       1.0f, -1.0f, -1.0f,  // 6. right-bottom-back
      -1.0f, -1.0f,  1.0f,  // 0. left-bottom-front
       1.0f, -1.0f,  1.0f   // 1. right-bottom-front
   };

   // Construtor - para configurar o buffer
   public Cubo() {
      ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
      vbb.order(ByteOrder.nativeOrder()); // Use native byte order
      vertexBuffer = vbb.asFloatBuffer(); // Converte Byte para Float
      vertexBuffer.put(vertices);         // Copia os dados para o buffer
      vertexBuffer.position(0);          
   }

   // Desenha a forma
   public void desenha(GL10 gl) {
      gl.glFrontFace(GL10.GL_CCW);   
      gl.glEnable(GL10.GL_CULL_FACE); 
      gl.glCullFace(GL10.GL_BACK); 

      gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
      gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

      // Renderiza todos os lados
      for (int face = 0; face < numFaces; face++) {
         // Set the color for each of the faces
         gl.glColor4f(colors[face][0], colors[face][1], colors[face][2], colors[face][3]);
         // Draw the primitive from the vertex-array directly
         gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, face*4, 4);
      }
      gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
      gl.glDisable(GL10.GL_CULL_FACE);
   }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.