J2ME/Lições/M3G/Criando objetos primitivos

Origem: Wikilivros, livros abertos por um mundo aberto.
< J2ME‎ | Lições‎ | M3G

Além de poder carregar objetos externos você também pode criar objetos primitivos usando os recursos do próprio M3G.

Criando uma câmera[editar | editar código-fonte]

Apesar do arquivo .m3g ter a facilidade de se poder criar a própria câmera, ela pode apresentar alguns tipos de dificuldade em relação ao aspecto de tela de cada celular, que pode deixar o gráfico exibido no celular "esticado" tanto horizontalmente quanto verticalmente. Para resolver isso é recomendável criar uma própria câmera definindo os aspectos de cada celular.

Para fazer isso crie um arquivo .m3g em algum editor externo, mas lembre-se de NÃO colocar uma câmera no arquivo, já que essa nós vamos criar manualmente. Com o arquivo criado vamos carrega-lo normalmente como fizemos anteriomente, adicionar no objeto World, e renderiza-lo normalmente.

 import javax.io.*;
 import javax.microedition.lcdui.*;
 import javax.microedition.m3g.*;
 import javax.microedition.game.*;
 
 public class ClasseMeuGameCanvas extends GameCanvas implements Runnable {
     Thread meuThread;
     Graphics meuGrafico = this.getGraphics();
     Graphics3D meuGrafico3D = Graphics3D.getInstance();
     Object3D[] meuObjeto3D;
     World meuMundo;
     
     public ClasseMeuGameCanvas(){
         super(false);
         meuThread = new Thread(this);
         meuThread.start();
     }
     public void run(){
         try{
             meuObjeto3D = Loader.load("/Meu Objeto 3D.m3g");
             meuMundo = (World) meuObjeto3D[0];
         }catch(Exception minhaExcessao){
             minhaExcessao.printStackTrance();
         }
         
         boolean gameOver = false;
         while(gameOver == false){
             try {
                 meuGrafico3D.bindTarget(meuGrafico);
                 meuGrafico3D.render(meuMundo);
                 meuThread.sleep(50);
             } catch (Exception minhaExcecao) {
                 minhaExcecao.printStackTrance)();
             } finally {
                 meuGrafico3D.releaseTarget();
                 flushGraphics();
             }
         }
     }
 }

Agora, como vamos manipular a câmera manualmente vamos criar um objeto do tipo Camera.

 import javax.io.*;
 import javax.microedition.lcdui.*;
 import javax.microedition.m3g.*;
 import javax.microedition.game.*;
 
 public class ClasseMeuGameCanvas extends GameCanvas implements Runnable {
     Thread meuThread;
     Graphics meuGrafico = this.getGraphics();
     Graphics3D meuGrafico3D = Graphics3D.getInstance();
     Object3D[] meuObjeto3D;
     World meuMundo;
     Camera minhaCamera;
     
     public ClasseMeuGameCanvas(){
         super(false);
         meuThread = new Thread(this);
         meuThread.start();
     }
     public void run(){
         try{
             meuObjeto3D = Loader.load("/Meu Objeto 3D.m3g");
             meuMundo = (World) meuObjeto3D[0];
         }catch(Exception minhaExcessao){
             minhaExcessao.printStackTrance();
         }
         
         boolean gameOver = false;
         while(gameOver == false){
             try {
                 meuGrafico3D.bindTarget(meuGrafico);
                 meuGrafico3D.render(meuMundo);
                 meuThread.sleep(50);
             } catch (Exception minhaExcecao) {
                 minhaExcecao.printStackTrance)();
             } finally {
                 meuGrafico3D.releaseTarget();
                 flushGraphics();
             }
         }
     }
 }

Agora dentro do construtor vamos inicializar a câmera.

 import javax.io.*;
 import javax.microedition.lcdui.*;
 import javax.microedition.m3g.*;
 import javax.microedition.game.*;
 
 public class ClasseMeuGameCanvas extends GameCanvas implements Runnable {
     Thread meuThread;
     Graphics meuGrafico = this.getGraphics();
     Graphics3D meuGrafico3D = Graphics3D.getInstance();
     Object3D[] meuObjeto3D;
     World meuMundo;
     Camera minhaCamera;
     
     public ClasseMeuGameCanvas(){
         super(false);
         meuThread = new Thread(this);
         meuThread.start();
     }
     public void run(){
         try{
             meuObjeto3D = Loader.load("/Meu Objeto 3D.m3g");
             meuMundo = (World) meuObjeto3D[0];
             minhaCamera = new Camera();
         }catch(Exception minhaExcessao){
             minhaExcessao.printStackTrance();
         }
         
         boolean gameOver = false;
         while(gameOver == false){
             try {
                 meuGrafico3D.bindTarget(meuGrafico);
                 meuGrafico3D.render(meuMundo);
                 meuThread.sleep(50);
             } catch (Exception minhaExcecao) {
                 minhaExcecao.printStackTrance)();
             } finally {
                 meuGrafico3D.releaseTarget();
                 flushGraphics();
             }
         }
     }
 }

Agora vamos usar o método setPerspective() para ajustar os atributos da câmera, o primeiro atributo será o ângulo de visão em graus (60 é a angulação padrão), o próximo campo será o aspecto da tela que será a largura da tela dividida pela autura, o terceiro campo é a distância em que a cena começará a ser renderizada (deve ser obrigatoriamente maior que zero), e por último é a distância máxima da renderização (o horizonte).

 import javax.io.*;
 import javax.microedition.lcdui.*;
 import javax.microedition.m3g.*;
 import javax.microedition.game.*;
 
 public class ClasseMeuGameCanvas extends GameCanvas implements Runnable {
     Thread meuThread;
     Graphics meuGrafico = this.getGraphics();
     Graphics3D meuGrafico3D = Graphics3D.getInstance();
     Object3D[] meuObjeto3D;
     World meuMundo;
     Camera minhaCamera;
     
     public ClasseMeuGameCanvas(){
         super(false);
         meuThread = new Thread(this);
         meuThread.start();
     }
     public void run(){
         try{
             meuObjeto3D = Loader.load("/Meu Objeto 3D.m3g");
             minhaCamera = new Camera();
             minhaCamera.setPerspective(60.0f, (float)getWidth()/(float)getHeight(), 1.0f, 100.0f);
             meuMundo = (World) meuObjeto3D[0];
         }catch(Exception minhaExcessao){
             minhaExcessao.printStackTrance();
         }
         
         boolean gameOver = false;
         while(gameOver == false){
             try {
                 meuGrafico3D.bindTarget(meuGrafico);
                 meuGrafico3D.render(meuMundo);
                 meuThread.sleep(50);
             } catch (Exception minhaExcecao) {
                 minhaExcecao.printStackTrance)();
             } finally {
                 meuGrafico3D.releaseTarget();
                 flushGraphics();
             }
         }
     }
 }

Agora vamos adicionar o nosso objeto do tipo Camera para o mundo 3D, para isso vamos usar o método addChild() e entrar a nossa câmera como parâmetro.

 import javax.io.*;
 import javax.microedition.lcdui.*;
 import javax.microedition.m3g.*;
 import javax.microedition.game.*;
 
 public class ClasseMeuGameCanvas extends GameCanvas implements Runnable {
     Thread meuThread;
     Graphics meuGrafico = this.getGraphics();
     Graphics3D meuGrafico3D = Graphics3D.getInstance();
     Object3D[] meuObjeto3D;
     World meuMundo;
     Camera minhaCamera;
     
     public ClasseMeuGameCanvas() throws Exception {
         super(false);
         meuThread = new Thread(this);
         meuThread.start();
     }
     public void run(){
         try{
             meuObjeto3D = Loader.load("/Meu Objeto 3D.m3g");
             minhaCamera = new Camera();
             minhaCamera.setPerspective(60.0f, (float)getWidth()/(float)getHeight(), 1.0f, 100.0f);
             meuMundo = (World) meuObjeto3D[0];
             meuMundo.addChild(minhaCamera);
         }catch(Exception minhaExcessao){
             minhaExcessao.printStackTrance();
         }
         
         boolean gameOver = false;
         while(gameOver == false){
             try {
                 meuGrafico3D.bindTarget(meuGrafico);
                 meuGrafico3D.render(meuMundo);
                 meuThread.sleep(50);
             } catch (Exception minhaExcecao) {
                 minhaExcecao.printStackTrance)();
             } finally {
                 meuGrafico3D.releaseTarget();
                 flushGraphics();
             }
         }
     }
 }

Por último agora que adicionamos a câmera ao mundo 3D, vamos dizer ao mundo 3D que queremos que essa câmera seja a câmera ativa, através do método setActiveCamera() entrando como parâmetro o nome da câmera.

 import javax.io.*;
 import javax.microedition.lcdui.*;
 import javax.microedition.m3g.*;
 import javax.microedition.game.*;
 
 public class ClasseMeuGameCanvas extends GameCanvas implements Runnable {
     Thread meuThread;
     Graphics meuGrafico = this.getGraphics();
     Graphics3D meuGrafico3D = Graphics3D.getInstance();
     Object3D[] meuObjeto3D;
     World meuMundo;
     Camera minhaCamera;
     
     public ClasseMeuGameCanvas() throws Exception {
         super(false);
         meuThread = new Thread(this);
         meuThread.start();
     }
     public void run(){
         try{
             meuObjeto3D = Loader.load("/Meu Objeto 3D.m3g");
             minhaCamera = new Camera();
             minhaCamera.setPerspective(60.0f, (float)getWidth()/(float)getHeight(), 1.0f, 100.0f);
             meuMundo = (World) meuObjeto3D[0];
             meuMundo.addChild(minhaCamera);
             meuMundo.setActiveCamera(minhaCamera);
         }catch(Exception minhaExcessao){
             minhaExcessao.printStackTrance();
         }
         
         boolean gameOver = false;
         while(gameOver == false){
             try {
                 meuGrafico3D.bindTarget(meuGrafico);
                 meuGrafico3D.render(meuMundo);
                 meuThread.sleep(50);
             } catch (Exception minhaExcecao) {
                 minhaExcecao.printStackTrance)();
             } finally {
                 meuGrafico3D.releaseTarget();
                 flushGraphics();
             }
         }
     }
 }

Criando um triângulo[editar | editar código-fonte]

Vamos começar criando o mais simples dos polígonos, o triângulo, primeiramente na nossa classe vamos colocar o que aprendemos anteriormente.

 import javax.microedition.lcdui.*;
 import javax.microedition.m3g.*;
 import javax.microedition.game.*;
 
 public class ClasseMeuGameCanvas extends GameCanvas implements Runnable {
     Thread meuThread;
     Graphics meuGrafico = this.getGraphics();
     Graphics3D meuGrafico3D = Graphics3D.getInstance();
     Object3D[] meuObjeto3D;
     World meuMundo;
     Camera minhaCamera;
     
     public ClasseMeuGameCanvas(){
         super(false);
         meuThread = new Thread(this);
         meuThread.start();
     }
     public void run(){
         minhaCamera = new Camera();
         minhaCamera.setPerspective(60.0f, (float)getWidth()/(float)getHeight(), 1.0f, 100.0f);
         meuMundo.addChild(minhaCamera);
         meuMundo.setActiveCamera(minhaCamera);
         
         boolean gameOver = false;
         while(gameOver == false){
             try {
                 meuGrafico3D.bindTarget(meuGrafico);
                 meuGrafico3D.render(meuMundo);
                 meuThread.sleep(50);
             } catch (Exception minhaExcecao) {
                 minhaExcecao.printStackTrance)();
             } finally {
                 meuGrafico3D.releaseTarget();
                 flushGraphics();
             }
         }
     }
 }

VertexArray[editar | editar código-fonte]

O objeto do tipo VertexArray (array de vértices) é o tipo de objeto genérico que pode guardar coisas como a posição dos vértices, normais, texturas, etc...

Vértices[editar | editar código-fonte]

Primeiramente vamos criar um array do tipo short que irá armazenar os vértices X, Y e Z (nessa ordem) do nosso triângulo.

 import javax.microedition.lcdui.*;
 import javax.microedition.m3g.*;
 import javax.microedition.game.*;
 
 public class ClasseMeuGameCanvas extends GameCanvas implements Runnable {
     Thread meuThread;
     Graphics meuGrafico = this.getGraphics();
     Graphics3D meuGrafico3D = Graphics3D.getInstance();
     Object3D[] meuObjeto3D;
     World meuMundo;
     Camera minhaCamera;
     
     public ClasseMeuGameCanvas(){
         super(false);
         meuThread = new Thread(this);
         meuThread.start();
     }
     public void run(){
         minhaCamera = new Camera();
         minhaCamera.setPerspective(60.0f, (float)getWidth()/(float)getHeight(), 1.0f, 100.0f);
         meuMundo.addChild(minhaCamera);
         meuMundo.setActiveCamera(minhaCamera);
         
         short[] vetorVertices = {-1, 0, -1,      -1, 0, 1,      1, 0, -1}
         
         boolean gameOver = false;
         while(gameOver == false){
             try {
                 meuGrafico3D.bindTarget(meuGrafico);
                 meuGrafico3D.render(meuMundo);
                 meuThread.sleep(50);
             } catch (Exception minhaExcecao) {
                 minhaExcecao.printStackTrance)();
             } finally {
                 meuGrafico3D.releaseTarget();
                 flushGraphics();
             }
         }
     }
 }

Sabemos que o VertexArray pode guardar coisas como a posição dos vértices, normais, texturas, etc... no nosso caso iremos criar um objeto para armazenar a posição dos vértices. Como parâmetros do construtor iremos entrar:

  1. O número de vértices do VertexArray (como faremos um triângulo então teremos 3 vértices)
  2. O número de componentes por vértice (nesse caso são 3, os eixos X, Y e Z).
  3. O tamanho em bytes de cada componente (nesse caso 2, pois um short ocupa 2 bytes). OBS: O tamanho máximo é 2.
 import javax.microedition.lcdui.*;
 import javax.microedition.m3g.*;
 import javax.microedition.game.*;
 
 public class ClasseMeuGameCanvas extends GameCanvas implements Runnable {
     Thread meuThread;
     Graphics meuGrafico = this.getGraphics();
     Graphics3D meuGrafico3D = Graphics3D.getInstance();
     Object3D[] meuObjeto3D;
     World meuMundo;
     Camera minhaCamera;
     
     public ClasseMeuGameCanvas(){
         super(false);
         meuThread = new Thread(this);
         meuThread.start();
     }
     public void run(){
         minhaCamera = new Camera();
         minhaCamera.setPerspective(60.0f, (float)getWidth()/(float)getHeight(), 1.0f, 100.0f);
         meuMundo.addChild(minhaCamera);
         meuMundo.setActiveCamera(minhaCamera);
         
         short[] vetorVertices = {-1, 0, -1,      -1, 0, 1,      1, 0, -1}
         VertexArray arrayDePosicoes = new VertexArray(3, 3, 2);
         
         boolean gameOver = false;
         while(gameOver == false){
             try {
                 meuGrafico3D.bindTarget(meuGrafico);
                 meuGrafico3D.render(meuMundo);
                 meuThread.sleep(50);
             } catch (Exception minhaExcecao) {
                 minhaExcecao.printStackTrance)();
             } finally {
                 meuGrafico3D.releaseTarget();
                 flushGraphics();
             }
         }
     }
 }

Criamos o array dos vértices e criamos também o objeto do tipo VertexArray. Agora iremos colocar o array vetorVertices que criamos dentro do objeto VertexArray para isso iremos usar o método set() onde iremos entrar 3 parâmetros:

  1. O primeiro vértice do vetor vertices (nesse caso e quase sempre será zero)
  2. O número de vértices do VertexArray (como faremos um triângulo então teremos 3 vértices).
  3. O vetor de vértices (o que criamos short[] vertices).
 import javax.microedition.lcdui.*;
 import javax.microedition.m3g.*;
 import javax.microedition.game.*;
 
 public class ClasseMeuGameCanvas extends GameCanvas implements Runnable {
     Thread meuThread;
     Graphics meuGrafico = this.getGraphics();
     Graphics3D meuGrafico3D = Graphics3D.getInstance();
     Object3D[] meuObjeto3D;
     World meuMundo;
     Camera minhaCamera;
     
     public ClasseMeuGameCanvas(){
         super(false);
         meuThread = new Thread(this);
         meuThread.start();
     }
     public void run(){
         minhaCamera = new Camera();
         minhaCamera.setPerspective(60.0f, (float)getWidth()/(float)getHeight(), 1.0f, 100.0f);
         meuMundo.addChild(minhaCamera);
         meuMundo.setActiveCamera(minhaCamera);
         
         short[] vetorVertices = {-1, 0, -1,      -1, 0, 1,      1, 0, -1}
         VertexArray arrayDePosicoes = new VertexArray(3, 3, 2);
         arrayDePosicoes.set(0, 3, vetorVertices);
         
         boolean gameOver = false;
         while(gameOver == false){
             try {
                 meuGrafico3D.bindTarget(meuGrafico);
                 meuGrafico3D.render(meuMundo);
                 meuThread.sleep(50);
             } catch (Exception minhaExcecao) {
                 minhaExcecao.printStackTrance)();
             } finally {
                 meuGrafico3D.releaseTarget();
                 flushGraphics();
             }
         }
     }
 }

Normais[editar | editar código-fonte]

A exemplo do anterior primeiramente criamos um array do tipo short que agora irá armazenar a posição das noemais de cada vértice (127-padrão, 0-sem normal, -127-invertido) nesse caso como fizemos um triângulo plano vamos setar a normal padrão para o eixo Y (virado para cima).

 import javax.microedition.lcdui.*;
 import javax.microedition.m3g.*;
 import javax.microedition.game.*;
 
 public class ClasseMeuGameCanvas extends GameCanvas implements Runnable {
     Thread meuThread;
     Graphics meuGrafico = this.getGraphics();
     Graphics3D meuGrafico3D = Graphics3D.getInstance();
     Object3D[] meuObjeto3D;
     World meuMundo;
     Camera minhaCamera;
     
     public ClasseMeuGameCanvas(){
         super(false);
         meuThread = new Thread(this);
         meuThread.start();
     }
     public void run(){
         minhaCamera = new Camera();
         minhaCamera.setPerspective(60.0f, (float)getWidth()/(float)getHeight(), 1.0f, 100.0f);
         meuMundo.addChild(minhaCamera);
         meuMundo.setActiveCamera(minhaCamera);
         
         short[] vetorVertices = {-1, 0, -1,      -1, 0, 1,       1, 0, -1}
         VertexArray arrayDePosicoes = new VertexArray(3, 3, 2);
         arrayDePosicoes.set(0, 3, vetorVertices);

         short[] vetorNormais = {0, 127, 0,       0, 127, 0,      0, 127, 0}
         
         boolean gameOver = false;
         while(gameOver == false){
             try {
                 meuGrafico3D.bindTarget(meuGrafico);
                 meuGrafico3D.render(meuMundo);
                 meuThread.sleep(50);
             } catch (Exception minhaExcecao) {
                 minhaExcecao.printStackTrance)();
             } finally {
                 meuGrafico3D.releaseTarget();
                 flushGraphics();
             }
         }
     }
 }

Agora vamos criar o objeto VertexArray com os mesmos atributos no construtor (número de vertices do VextexArray, o numero de componentes de cada vértice, o tamanho em bytes de cada componente).

 import javax.microedition.lcdui.*;
 import javax.microedition.m3g.*;
 import javax.microedition.game.*;
 
 public class ClasseMeuGameCanvas extends GameCanvas implements Runnable {
     Thread meuThread;
     Graphics meuGrafico = this.getGraphics();
     Graphics3D meuGrafico3D = Graphics3D.getInstance();
     Object3D[] meuObjeto3D;
     World meuMundo;
     Camera minhaCamera;
     
     public ClasseMeuGameCanvas(){
         super(false);
         meuThread = new Thread(this);
         meuThread.start();
     }
     public void run(){
         minhaCamera = new Camera();
         minhaCamera.setPerspective(60.0f, (float)getWidth()/(float)getHeight(), 1.0f, 100.0f);
         meuMundo.addChild(minhaCamera);
         meuMundo.setActiveCamera(minhaCamera);
         
         short[] vetorVertices = {-1, 0, -1,      -1, 0, 1,       1, 0, -1}
         VertexArray arrayDePosicoes = new VertexArray(3, 3, 2);
         arrayDePosicoes.set(0, 3, vetorVertices);

         short[] vetorNormais = {0, 127, 0,       0, 127, 0,      0, 127, 0}
         VertexArray arrayDeNormais = new VertexArray(3, 3, 2);
         
         boolean gameOver = false;
         while(gameOver == false){
             try {
                 meuGrafico3D.bindTarget(meuGrafico);
                 meuGrafico3D.render(meuMundo);
                 meuThread.sleep(50);
             } catch (Exception minhaExcecao) {
                 minhaExcecao.printStackTrance)();
             } finally {
                 meuGrafico3D.releaseTarget();
                 flushGraphics();
             }
         }
     }
 }

E por fim, chamar novamente o método set() com os mesmos atributos (índice do primeiro vértice, o numero de vértice do VertexArray, o vetor de vértices).

 import javax.microedition.lcdui.*;
 import javax.microedition.m3g.*;
 import javax.microedition.game.*;
 
 public class ClasseMeuGameCanvas extends GameCanvas implements Runnable {
     Thread meuThread;
     Graphics meuGrafico = this.getGraphics();
     Graphics3D meuGrafico3D = Graphics3D.getInstance();
     Object3D[] meuObjeto3D;
     World meuMundo;
     Camera minhaCamera;
     
     public ClasseMeuGameCanvas(){
         super(false);
         meuThread = new Thread(this);
         meuThread.start();
     }
     public void run(){
         minhaCamera = new Camera();
         minhaCamera.setPerspective(60.0f, (float)getWidth()/(float)getHeight(), 1.0f, 100.0f);
         meuMundo.addChild(minhaCamera);
         meuMundo.setActiveCamera(minhaCamera);
         
         short[] vetorVertices = {-1, 0, -1,      -1, 0, 1,       1, 0, -1}
         VertexArray arrayDePosicoes = new VertexArray(3, 3, 2);
         arrayDePosicoes.set(0, 3, vetorVertices);

         short[] vetorNormais = {0, 127, 0,       0, 127, 0,      0, 127, 0}
         VertexArray arrayDeNormais = new VertexArray(3, 3, 2);
         arrayDeNormais.set(0, 3, vetorNormais);
         
         boolean gameOver = false;
         while(gameOver == false){
             try {
                 meuGrafico3D.bindTarget(meuGrafico);
                 meuGrafico3D.render(meuMundo);
                 meuThread.sleep(50);
             } catch (Exception minhaExcecao) {
                 minhaExcecao.printStackTrance)();
             } finally {
                 meuGrafico3D.releaseTarget();
                 flushGraphics();
             }
         }
     }
 }

Textura*[editar | editar código-fonte]

Por último vamos agora criar um vetor para as texturas.

 import javax.microedition.lcdui.*;
 import javax.microedition.m3g.*;
 import javax.microedition.game.*;
 
 public class ClasseMeuGameCanvas extends GameCanvas implements Runnable {
     Thread meuThread;
     Graphics meuGrafico = this.getGraphics();
     Graphics3D meuGrafico3D = Graphics3D.getInstance();
     Object3D[] meuObjeto3D;
     World meuMundo;
     Camera minhaCamera;
     
     public ClasseMeuGameCanvas(){
         super(false);
         meuThread = new Thread(this);
         meuThread.start();
     }
     public void run(){
         minhaCamera = new Camera();
         minhaCamera.setPerspective(60.0f, (float)getWidth()/(float)getHeight(), 1.0f, 100.0f);
         meuMundo.addChild(minhaCamera);
         meuMundo.setActiveCamera(minhaCamera);
         
         short[] vetorVertices = {-1, 0, -1,      -1, 0, 1,       1, 0, -1}
         VertexArray arrayDePosicoes = new VertexArray(3, 3, 2);
         arrayDePosicoes.set(0, 3, vetorVertices);

         short[] vetorNormais = {0, 127, 0,       0, 127, 0,      0, 127, 0}
         VertexArray arrayDeNormais = new VertexArray(3, 3, 2);
         arrayDeNormais.set(0, 3, vetorNormais);

         short[] vetorTexturas = {1, 0,       0, 0,       1, 1}
         
         boolean gameOver = false;
         while(gameOver == false){
             try {
                 meuGrafico3D.bindTarget(meuGrafico);
                 meuGrafico3D.render(meuMundo);
                 meuThread.sleep(50);
             } catch (Exception minhaExcecao) {
                 minhaExcecao.printStackTrance)();
             } finally {
                 meuGrafico3D.releaseTarget();
                 flushGraphics();
             }
         }
     }
 }

Vamos também criar o objeto do tipo VextexArray utilizando os mesmos atributos anteriores (número de vertices do VextexArray, o numero de componentes de cada vértice, o tamanho em bytes de cada componente).

 import javax.microedition.lcdui.*;
 import javax.microedition.m3g.*;
 import javax.microedition.game.*;
 
 public class ClasseMeuGameCanvas extends GameCanvas implements Runnable {
     Thread meuThread;
     Graphics meuGrafico = this.getGraphics();
     Graphics3D meuGrafico3D = Graphics3D.getInstance();
     Object3D[] meuObjeto3D;
     World meuMundo;
     Camera minhaCamera;
     
     public ClasseMeuGameCanvas(){
         super(false);
         meuThread = new Thread(this);
         meuThread.start();
     }
     public void run(){
         minhaCamera = new Camera();
         minhaCamera.setPerspective(60.0f, (float)getWidth()/(float)getHeight(), 1.0f, 100.0f);
         meuMundo.addChild(minhaCamera);
         meuMundo.setActiveCamera(minhaCamera);
         
         short[] vetorVertices = {-1, 0, -1,      -1, 0, 1,       1, 0, -1}
         VertexArray arrayDePosicoes = new VertexArray(3, 3, 2);
         arrayDePosicoes.set(0, 3, vetorVertices);

         short[] vetorNormais = {0, 127, 0,       0, 127, 0,      0, 127, 0}
         VertexArray arrayDeNormais = new VertexArray(3, 3, 2);
         arrayDeNormais.set(0, 3, vetorNormais);

         short[] vetorTexturas = {1, 0,       0, 0,       1, 1}
         VertexArray arrayDeTexturas = new VertexArray(2, 2, 2);

         boolean gameOver = false;
         while(gameOver == false){
             try {
                 meuGrafico3D.bindTarget(meuGrafico);
                 meuGrafico3D.render(meuMundo);
                 meuThread.sleep(50);
             } catch (Exception minhaExcecao) {
                 minhaExcecao.printStackTrance)();
             } finally {
                 meuGrafico3D.releaseTarget();
                 flushGraphics();
             }
         }
     }
 }

E por fim chamamos o método set() com os mesmos atributos (índice do primeiro vértice, o numero de vértice do VertexArray, o vetor de vértices).

 import javax.microedition.lcdui.*;
 import javax.microedition.m3g.*;
 import javax.microedition.game.*;
 
 public class ClasseMeuGameCanvas extends GameCanvas implements Runnable {
     Thread meuThread;
     Graphics meuGrafico = this.getGraphics();
     Graphics3D meuGrafico3D = Graphics3D.getInstance();
     Object3D[] meuObjeto3D;
     World meuMundo;
     Camera minhaCamera;
     
     public ClasseMeuGameCanvas(){
         super(false);
         meuThread = new Thread(this);
         meuThread.start();
     }
     public void run(){
         minhaCamera = new Camera();
         minhaCamera.setPerspective(60.0f, (float)getWidth()/(float)getHeight(), 1.0f, 100.0f);
         meuMundo.addChild(minhaCamera);
         meuMundo.setActiveCamera(minhaCamera);
         
         short[] vetorVertices = {-1, 0, -1,      -1, 0, 1,       1, 0, -1}
         VertexArray arrayDePosicoes = new VertexArray(3, 3, 2);
         arrayDePosicoes.set(0, 3, vetorVertices);

         short[] vetorNormais = {0, 127, 0,       0, 127, 0,      0, 127, 0}
         VertexArray arrayDeNormais = new VertexArray(3, 3, 2);
         arrayDeNormais.set(0, 3, vetorNormais);

         short[] vetorTexturas = {1, 0,       0, 0,       1, 1}
         VertexArray arrayDeTexturas = new VertexArray(2, 2, 2);
         arrayDeTexturas.set(0, 2, vetorTexturas);

         boolean gameOver = false;
         while(gameOver == false){
             try {
                 meuGrafico3D.bindTarget(meuGrafico);
                 meuGrafico3D.render(meuMundo);
                 meuThread.sleep(50);
             } catch (Exception minhaExcecao) {
                 minhaExcecao.printStackTrance)();
             } finally {
                 meuGrafico3D.releaseTarget();
                 flushGraphics();
             }
         }
     }
 }

Vertex buffer*[editar | editar código-fonte]

Criamos os arrays dos vértices, das normais e das texturas, agora precisamos agrupar esses arrays em um único buffer, por isso agora iremos criar um objeto do tipo VertexBuffer.


 import javax.microedition.lcdui.*;
 import javax.microedition.m3g.*;
 import javax.microedition.game.*;
 
 public class ClasseMeuGameCanvas extends GameCanvas implements Runnable {
     Thread meuThread;
     Graphics meuGrafico = this.getGraphics();
     Graphics3D meuGrafico3D = Graphics3D.getInstance();
     Object3D[] meuObjeto3D;
     World meuMundo;
     Camera minhaCamera;
     
     public ClasseMeuGameCanvas(){
         super(false);
         meuThread = new Thread(this);
         meuThread.start();
     }
     public void run(){
         minhaCamera = new Camera();
         minhaCamera.setPerspective(60.0f, (float)getWidth()/(float)getHeight(), 1.0f, 100.0f);
         meuMundo.addChild(minhaCamera);
         meuMundo.setActiveCamera(minhaCamera);
         
         short[] vetorVertices = {-1, 0, -1,      -1, 0, 1,       1, 0, -1}
         VertexArray arrayDePosicoes = new VertexArray(3, 3, 2);
         arrayDePosicoes.set(0, 3, vetorVertices);

         short[] vetorNormais = {0, 127, 0,       0, 127, 0,      0, 127, 0}
         VertexArray arrayDeNormais = new VertexArray(3, 3, 2);
         arrayDeNormais.set(0, 3, vetorNormais);

         short[] vetorTexturas = {1, 0,       0, 0,       1, 1}
         VertexArray arrayDeTexturas = new VertexArray(2, 2, 2);
         arrayDeTexturas.set(0, 2, vetorTexturas);
         
         VertexBuffer meuVertexBuffer = new VertexBuffer();
         
         boolean gameOver = false;
         while(gameOver == false){
             try {
                 meuGrafico3D.bindTarget(meuGrafico);
                 meuGrafico3D.render(meuMundo);
                 meuThread.sleep(50);
             } catch (Exception minhaExcecao) {
                 minhaExcecao.printStackTrance)();
             } finally {
                 meuGrafico3D.releaseTarget();
                 flushGraphics();
             }
         }
     }
 }

Agora para colocar o array de posições que criamos anteriormente vamos usar o método setPositions() da classe VertexBuffer, como parâmetros entraremos o objeto VertexArray, a escala (tamanho) do objeto e por fim o parâmetro null.

 import javax.microedition.lcdui.*;
 import javax.microedition.m3g.*;
 import javax.microedition.game.*;
 
 public class ClasseMeuGameCanvas extends GameCanvas implements Runnable {
     Thread meuThread;
     Graphics meuGrafico = this.getGraphics();
     Graphics3D meuGrafico3D = Graphics3D.getInstance();
     Object3D[] meuObjeto3D;
     World meuMundo;
     Camera minhaCamera;
     
     public ClasseMeuGameCanvas(){
         super(false);
         meuThread = new Thread(this);
         meuThread.start();
     }
     public void run(){
         minhaCamera = new Camera();
         minhaCamera.setPerspective(60.0f, (float)getWidth()/(float)getHeight(), 1.0f, 100.0f);
         meuMundo.addChild(minhaCamera);
         meuMundo.setActiveCamera(minhaCamera);
         
         short[] vetorVertices = {-1, 0, -1,      -1, 0, 1,       1, 0, -1}
         VertexArray arrayDePosicoes = new VertexArray(3, 3, 2);
         arrayDePosicoes.set(0, 3, vetorVertices);

         short[] vetorNormais = {0, 127, 0,       0, 127, 0,      0, 127, 0}
         VertexArray arrayDeNormais = new VertexArray(3, 3, 2);
         arrayDeNormais.set(0, 3, vetorNormais);

         short[] vetorTexturas = {1, 0,       0, 0,       1, 1}
         VertexArray arrayDeTexturas = new VertexArray(2, 2, 2);
         arrayDeTexturas.set(0, 2, vetorTexturas);
         
         VertexBuffer meuVertexBuffer = new VertexBuffer();
         meuVertexBuffer.setPositions(arrayDePosicoes, 1.0f, null);
         
         boolean gameOver = false;
         while(gameOver == false){
             try {
                 meuGrafico3D.bindTarget(meuGrafico);
                 meuGrafico3D.render(meuMundo);
                 meuThread.sleep(50);
             } catch (Exception minhaExcecao) {
                 minhaExcecao.printStackTrance)();
             } finally {
                 meuGrafico3D.releaseTarget();
                 flushGraphics();
             }
         }
     }
 }