2011-09-14 5 views
2

OpenGL을 처음 사용하고 Android 용 증강 현실 애플리케이션을 개발 중입니다.사각형에 텍스처로 텍스트를 그리는 데 아무 것도 표시되지 않습니다.

지금까지 카메라에 수직 인 흰색 정사각형을 그려 "관심 지점"이되는 방향으로 사용자를 안내했습니다.

이제 사각형에 텍스트를 표시하려고합니다.

나는 많은 것을 읽었으며 텍스트로 텍스처를 만드는 것이 가장 직접적이고 쉬운 접근법 인 것처럼 보이므로 관심있는 포인트의 데이터를 얻은 후 바로 텍스쳐를 생성합니다. 그들의 사각형. 이를 위해 저는 비트 맵을 사용합니다.

몇 가지 코드를 살펴 보겠습니다.

public void onDrawFrame(GL10 gl) { 

     // Get sensors matrix 
       ... 


     //Clear Screen And Depth Buffer 
     gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 

     // Load remapped matrix 
     gl.glMatrixMode(GL10.GL_MODELVIEW); 



     // List of Points of interest (modified when some data is downloaded) 
     synchronized (poiList) { 

      if(mNewData){ // True if new data has been dowloaded) 
       if(textures != null) // Delete old textures 
        gl.glDeleteTextures(textures.length, textures, 0); 

       textures = loadGLTexture(gl, soapPoiList.getPoiList()); 
       mNewData = false; 
      } 

      int i = 0; 
      // Iterate the list 
      for (PointOfInterest c : poiList) { 

         gl.glLoadIdentity(); 
       gl.glLoadMatrixf(remappedRotationMatrix, 0); 

       // Get bearing 
          ... 

       // Place polygon in the right place 
       gl.glRotatef(-bearing, 0, 0, 1); 
       gl.glTranslatef(0, ICONS_SIZE_RATIO, 0); 

         // Actually draws the polygon with text 
       c.draw(gl, textures[i]); 

       i++; 
      } 
     } 
    } 

loadGLTextures은 다음과 같습니다 : 내 onDrawFrame 방법 내에서,이 같은 것을 할 그것은 기본적으로 관심의 각 포인트에 대한 비트 맵을 생성

protected int[] loadGLTexture(GL10 gl, List<PointOfInterest> l){ 
    int res[] = new int[l.size()]; 
    gl.glGenTextures(res.length, res, 0); 
    int i = 0; 

    for(PointOfInterest p : l) { 
     Bitmap bitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.RGB_565); 
     bitmap.eraseColor(Color.BLACK); 

     Canvas canvas = new Canvas(bitmap); 

     Paint textPaint = new Paint(); 
     textPaint.setTextSize(35); 
     textPaint.setFakeBoldText(true); 
     textPaint.setAntiAlias(true); 
     textPaint.setARGB(255, 255, 255, 255); 
     // Draw the text centered 
     canvas.drawText(Float.toString(p.getDinstanceToOrigin()) + " m.", 10,35, textPaint); 

     // Bind the texture to our array 
     gl.glBindTexture(GL10.GL_TEXTURE_2D, res[i]); 

     // Create Nearest Filtered Texture 
     gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); 
     gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); 

     gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE); 
     gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE); 

     GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); 

     bitmap.recycle(); 

     i++; 
    } 
    return res; 
} 

을하고있는 텍스처를 생성합니다. 나는 그것이 성공하지 herehere을 가르쳐으로 텍스처를 매핑하는 시도

public class PointOfInterest { 

     // MEMBERS ---------------------------------------------------------------- 
      .... 
      .... 

     // OpenGL necessary variables 
     /** The buffer holding the vertices */ 
     private FloatBuffer vertexBuffer; 

     /** The initial vertex definition */ 
     private float vertices[] = { 
            -1.0f, 0.0f, -1.0f, //Bottom Left V1 
            -1.0f, 0.0f, 1.0f, //Top Left  V2 
            1.0f, 0.0f, -1.0f, //Bottom Right V3 
            1.0f, 0.0f, 1.0f, //Top Right  V4 
            }; 

     private FloatBuffer textureBuffer; 
     private float texture[] = { 
            0.0f, 0.0f,  // V1 
            1.0f, 0.0f,  // V3 
            0.0f, 1.0f,  // V2 
            1.0f, 1.0f  // V4 
     }; 

     // CONSTRUCTORS ----------------------------------------------------------- 

     public PointOfInterest(Location originLocation){ 
      currentLocation = originLocation; 

      mPoiLocation = new Location(LocationManager.GPS_PROVIDER); 

      ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4); 
      byteBuf.order(ByteOrder.nativeOrder()); 

      vertexBuffer = byteBuf.asFloatBuffer(); 
      vertexBuffer.put(vertices); 
      vertexBuffer.position(0); 

      byteBuf = ByteBuffer.allocateDirect(texture.length * 4); 
      byteBuf.order(ByteOrder.nativeOrder()); 

      textureBuffer = byteBuf.asFloatBuffer(); 
      textureBuffer.put(texture); 
      textureBuffer.position(0); 
     } 

     // PUBLIC METHODS --------------------------------------------------------- 

     public void draw(GL10 gl, int textureId){ 
      // Bind the previously generated texture 
      gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId); 

      // Point to our buffers 
      gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
      gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 

      // set the colour for the square 
      //gl.glColor4f(0.0f, 0.1f, 0.0f, 0.5f); 

      //Set the face rotation 
      gl.glFrontFace(GL10.GL_CW); 

      //Point to our vertex buffer 
      gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); 
      gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer); 

      //Draw the vertices as triangle strip 
      gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length/3); 

      //Disable the client state before leaving 
      gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
      gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 
     } 

     .... 
     .... 
} 

:이이 클래스에 도시 된 바와 같이 텍스처는 흰색 사각형 나중에 이상 적용됩니다. 나는 정말로 편지에 사각형을 그려 넣기 위해 무엇을해야할지 모르며, 정말로 여기에서 잃어버린 ... 어쩌면 텍스쳐가 사각형의 다른면에 그려져 있을지도 모릅니다. 텍스처가 생성되지 않을 수도 있습니다. 몰라.

모든 종류의 도움을 주시면 감사하겠습니다.

+0

어딘가에서 코드를 복사 했습니까? OpenGL을 처음 사용하는 사람이라고합니다. 복사 한 경우 아마 그 과정에서 뭔가를 놓쳤을 것입니다. – Ronnie

+0

나는 아주 간단한 예제를 만들었습니다. 텍스트가있는 텍스처가있는 정적 정사각형입니다. 자,이 예제를 어플리케이션에 적용시키고 있습니다. 내가 뭔가를 놓쳤다면 나는 백 번을 체크했다. 그러나 나는 아무것도 본다. – Pedriyoo

답변

2

좋아, 텍스처 매핑을 사용하는 것을 잊어 버렸습니다. GL10 오브젝트를 사용하는 모든 메소드 내에서이를 수행 할 수 있습니다. 나는 내 객체의 draw 메소드 내에서 그것을 선호하므로 다른 객체는 텍스처의 영향을받지 않습니다. 이것은 간단합니다 (그냥 2 줄만 바꿔서 NEW라고 말한 것입니다!) :

public void draw(GL10 gl, int textureId){ 
    gl.glEnable(GL10.GL_TEXTURE_2D); //NEW !!! Enable Texture Mapping 

    // Bind the previously generated texture 
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId); 

    // Point to our buffers 
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 

    // set the colour for the square 
    //gl.glColor4f(0.0f, 0.1f, 0.0f, 0.5f); 

    //Set the face rotation 
    gl.glFrontFace(GL10.GL_CW); 

    //Point to our vertex buffer 
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); 
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer); 

    //Draw the vertices as triangle strip 
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length/3); 

    //Disable the client state before leaving 
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 

    gl.glDisable(GL10.GL_TEXTURE_2D); ////NEW !!! Disable texture mapping 
} 
관련 문제