[SOLVED]A 3D arrow: strange things

Problems with Canvas, OpenGL, etc...

[SOLVED]A 3D arrow: strange things

Postby Vermouth » Tue Feb 22, 2011 11:02 pm

Hello!

I'm trying to create a 3D arrow with openGL (with java). But I've visual problems and I don't find solutions. For details, it's for a studie's project (cool RA project on android :D ).

One things: I'm a noob regarding to openGL programming, probably THE problem :mrgreen:

I've an 3D arrow builded with 2D arrow and borders between both face. I only make border around the triangle of the top of the arrow.
All polygone I put in my arrays are present but they aren't always drawn depending on the arraw rotation (did with touch event).

So, in order that you understand me, I numbered the 3 edge of the triangle of the top of the arrow:
Image
And now, here are 3 different positions of my arraw:
*pointing to the top and to the back of the device
Image
Egde 3 is not visible.

*same orientation but reversed lengthwise (180 degrees rotation)
Image
Egde 3 is still not visible and the blue face is partialy translucent.

*pointing to the top and to the front of the device (I insiste, it's not the same than the last):
Image
Edges 1 and 2 aren't visible and edge 3 is partialy translucent.

Firstly, the yellow face have something like a visibility priority. The blue face is translucent where it needed to let the yellow face visible. What is the problem ?

Secondly, borders of the arrow are sometime visible or not while I of course want that them always visible. What is the problem again ?

Here is the code used to draw my arrow:
ArrowRenderer:
Code: Select all
package vermouth.gl.test;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView;

public class ArrowRenderer  implements GLSurfaceView.Renderer {

   private GLArrow glArrow;
   private Arrow arrow;
   private float Xangle = 0f;
   private float Yangle = 0f;
   private float Zangle = 0f;
   
   public void setXA(float f){
      Xangle += f;
   }
   
   public void setYA(float f){
      Yangle += f;
   }
   
   public void setZA(float f){
      Zangle = f;
   }
   
   public ArrowRenderer(){
      glArrow = new GLArrow();
      arrow = new Arrow(0);
   }

   @Override
   public void onDrawFrame(GL10 gl) {
      // TODO Auto-generated method stub
      // define the color we want to be displayed as the "clipping wall"
        gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
       
       
       
        // clear the color buffer to show the ClearColor we called above...
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
       
        gl.glLoadIdentity();
       
        gl.glRotatef(Xangle, 0, 1, 0);
        gl.glRotatef(Yangle, 1, 0, 0);
        gl.glRotatef(Zangle, 0, 0, 1);
       
       
      glArrow.draw(gl);
   }

   @Override
   public void onSurfaceChanged(GL10 gl, int width, int height) {
      // TODO Auto-generated method stub
      gl.glViewport(0, 0, width, height);
      //gl.glFrustumf(-10, 10, -10, 10, 10, 10);
   }

   @Override
   public void onSurfaceCreated(GL10 gl, EGLConfig config) {
      // TODO Auto-generated method stub
      //gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
      //gl.glEnable(GL10.GL_CULL_FACE);
      //gl.glFrontFace(GL10.GL_CW);
      //gl.glCullFace(GL10.GL_BACK);
   }
   
   
   
   /* public void onDrawFrame(GL10 gl) {
         /*
          * Usually, the first thing one might want to do is to clear
          * the screen. The most efficient way of doing this is to use
          * glClear().
          * /

         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

         /*
          * Now we're ready to draw some 3D objects
          * /

         gl.glMatrixMode(GL10.GL_MODELVIEW);
         gl.glLoadIdentity();
         //gl.glTranslatef(0, 0, -3.0f);
         gl.glRotatef(Xangle, 0, 1, 0);
         gl.glRotatef(Yangle, 1, 0, 0);

         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
         gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

         glArrow.draw(gl);
     }

     public void onSurfaceChanged(GL10 gl, int width, int height) {
          gl.glViewport(0, 0, width, height);

          /*
           * Set our projection matrix. This doesn't have to be done
           * each time we draw, but usually a new projection needs to
           * be set when the viewport is resized.
           * /

          float ratio = (float) width / height;
          gl.glMatrixMode(GL10.GL_PROJECTION);
          gl.glLoadIdentity();
          gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
     }

     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
         /*
          * By default, OpenGL enables features that improve quality
          * but reduce performance. One might want to tweak that
          * especially on software renderer.
          * /
         gl.glDisable(GL10.GL_DITHER);

         /*
          * Some one-time OpenGL initialization can be made here
          * probably based on features of this particular context
          * /
          gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
                  GL10.GL_FASTEST);


          gl.glClearColor(0.5f,0.5f,0.5f,1);
          gl.glEnable(GL10.GL_CULL_FACE);
          gl.glShadeModel(GL10.GL_SMOOTH);
          gl.glEnable(GL10.GL_DEPTH_TEST);
     }*/

}

GLArrow:
Code: Select all
package vermouth.gl.test;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

public class GLArrow {

   private ArrowFace topFace;
   private ArrowFace bottomFace;
   private ArrowBorder border;
   
   public GLArrow(){
      topFace = new ArrowFace(0.1f);
      bottomFace = new ArrowFace(-0.1f);
      border = new ArrowBorder(0.1f);
   }
   
   public void draw(GL10 gl)
    {
      gl.glColor4f(0f, 0f, 0f, 1f);
      border.draw(gl);
      
      gl.glColor4f(0.5f, 1f, 1f, 1f);
      topFace.draw(gl);
      gl.glColor4f(1f, 1f, 0.5f, 1f);
      bottomFace.draw(gl);
    }
   
   public class ArrowFace{
      
      private FloatBuffer vertexBuffer;
      private ByteBuffer  indexBuffer;
      
      public ArrowFace(float z){
         ByteBuffer vbb = ByteBuffer.allocateDirect(27*4);
           vbb.order(ByteOrder.nativeOrder());
           vertexBuffer = vbb.asFloatBuffer();
           vertexBuffer.put(new float[]{
                 0f, 0.5f, z,//Pointe de la flêche
                   -0.5f, 0f, z,//Pointe gauche
                   0.5f, 0f, z, //Pointe droite
                  
                   -0.25f, 0f, z,
                   0.25f, 0f, z,
                   -0.25f, -0.5f, z,
                  
                   0.25f, -0f, z,
                   0.25f, -0.5f, z,
                   -0.25f, -0.5f, z});
           vertexBuffer.position(0);
          
           indexBuffer = ByteBuffer.allocateDirect(9);
           indexBuffer.put(new byte[]{0, 1 ,2, 3, 4, 5, 6, 7, 8});
           indexBuffer.position(0);
      }
      
      public void draw(GL10 gl)
       {
         gl.glFrontFace(gl.GL_CW);
           gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertexBuffer);
           gl.glDrawElements(gl.GL_TRIANGLES, 9, gl.GL_UNSIGNED_BYTE, indexBuffer);
       }
   }
   
   public class ArrowBorder{
      
      private FloatBuffer vertexBuffer;
      private ByteBuffer  indexBuffer;
      
      public ArrowBorder(float z){
         ByteBuffer vbb = ByteBuffer.allocateDirect(54*4);
           vbb.order(ByteOrder.nativeOrder());
           vertexBuffer = vbb.asFloatBuffer();
           vertexBuffer.put(new float[]{
                 0f, 0.5f, z,//Pointe de la flêche
                   -0.5f, 0f, z,//Pointe gauche
                   0.5f, 0f, z, //Pointe droite
                  
                   -0.25f, 0f, z,
                   0.25f, 0f, z,
                   -0.25f, -0.5f, z,
                  
                   0.25f, -0f, z,
                   0.25f, -0.5f, z,
                   -0.25f, -0.5f, z,
                  
                 0f, 0.5f, -z,//Pointe de la flêche
                   -0.5f, 0f, -z,//Pointe gauche
                   0.5f, 0f, -z, //Pointe droite
                  
                   -0.25f, 0f, -z,
                   0.25f, 0f, -z,
                   -0.25f, -0.5f, -z,
                  
                   0.25f, -0f, -z,
                   0.25f, -0.5f, -z,
                   -0.25f, -0.5f, -z});
           vertexBuffer.position(0);
          
           indexBuffer = ByteBuffer.allocateDirect(18);
           //0, 1 ,2, 3, 4, 5, 6, 7, 8
           //9 10 11 12 13 14 15 16 17
           indexBuffer.put(new byte[]{
                 0, 1, 9,  1, 9, 10,
                 0, 2, 9,  2, 9, 11,
                 1, 2, 10,  2, 10, 11,
                 
                 //3, 12, 4,  4, 12, 13,
                 
           });
           indexBuffer.position(0);
      }
      
      public void draw(GL10 gl)
       {
         gl.glFrontFace(gl.GL_CW);
           gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertexBuffer);
           gl.glDrawElements(gl.GL_TRIANGLES, 18, gl.GL_UNSIGNED_BYTE, indexBuffer);
       }
   }
}


Thank for helping me :) !

PS: english isn't my native language :roll:, I accept corrections with pleasure!
Last edited by Vermouth on Thu Apr 07, 2011 1:36 pm, edited 1 time in total.
/!\ Bad English Speaker /!\
Tell me if a sentence is not corect :P
Vermouth
Junior Developer
Junior Developer
 
Posts: 15
Joined: Wed Apr 15, 2009 2:01 pm

Top

Re: A 3D arrow: strange things

Postby Orabig » Thu Apr 07, 2011 11:33 am

Should be late to answer this, but...

You should use culling or Z-buffer in order to see and hide these faces correctly.

(Envoie moi un email pour plus d'explications, je crois qu'on est compatriotes :)
Orabig
Freshman
Freshman
 
Posts: 4
Joined: Thu Apr 07, 2011 10:37 am

Re: A 3D arrow: strange things

Postby Vermouth » Thu Apr 07, 2011 1:36 pm

Yesit's a bit late to answer but thank you anyway :) !
Another personne who's working with me has solved the problem. But I don't watched how he solved it (not enough time).

I've forget to add "solved" in the topic's title. I'll do it.

(Et oui je suis un compatriote :lol: ! Je fait des fautes de francophone dans mes phrases :lol: ?)
/!\ Bad English Speaker /!\
Tell me if a sentence is not corect :P
Vermouth
Junior Developer
Junior Developer
 
Posts: 15
Joined: Wed Apr 15, 2009 2:01 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 4 guests