2012-02-08 83 views
3

我是新來的opengGL和藉口如果問題太幼稚了。我正在通過一些tuturials尋求幫助學習這一點。我有一個小程序,我在GLSurfaceView上繪製紋理。在OpenGL Android中繪製紋理從0,0座標

我想從0,0座標開始按順序繪製多個紋理(位圖圖像​​)。另外我需要在屏幕上的圖像有一些差距。請幫忙。我是否需要更改頂點緩衝區的位置?請建議。

這是我的渲染器代碼。

public class GlRenderer implements Renderer{ 

    private Square square; // square1 
    private Context context; 

    /** Constructor to set the handed over context */ 
    public GlRenderer(Context context) { 
     this.context = context; 

     // initialise the square 
     this.square = new Square(); 
    } 

    public void onDrawFrame(GL10 gl) { 
     // clear Screen and Depth Buffer 
     gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 

     // Reset the Modelview Matrix 
     gl.glLoadIdentity(); 

     // Drawing 
     gl.glTranslatef(0.0f, 0.0f, -5.0f);  // move 5 units INTO the screen 
     square.draw(gl);      // Draw the triangle 
     //gl.glTranslatef(0.0f, 0.0f, 0.0f);  // move 5 units INTO the screen 
     //square2.draw(gl);      // Draw the triangle 
    } 

    public void onSurfaceChanged(GL10 gl, int width, int height) { 
     if(height == 0) {      //Prevent A Divide By Zero By 
      height = 1;       //Making Height Equal One 
     } 

     gl.glViewport(0, 0, width, height);  //Reset The Current Viewport 
     gl.glMatrixMode(GL10.GL_TEXTURE); //Select The Projection Matrix 
     gl.glLoadIdentity();     //Reset The Projection Matrix 

     //Calculate The Aspect Ratio Of The Window 
     GLU.gluPerspective(gl, 45.0f, (float)width/(float)height, 0.1f, 100.0f); 

     gl.glMatrixMode(GL10.GL_MODELVIEW);  //Select The Modelview Matrix 
     gl.glLoadIdentity();     //Reset The Modelview Matrix 
    } 

    public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
     // Load the texture for the square 
     square.loadGLTexture(gl, this.context); 

     gl.glEnable(GL10.GL_TEXTURE_2D);   //Enable Texture Mapping (NEW) 
     gl.glShadeModel(GL10.GL_SMOOTH);   //Enable Smooth Shading 
     gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //Black Background 
     gl.glClearDepthf(1.0f);      //Depth Buffer Setup 
     gl.glEnable(GL10.GL_DEPTH_TEST);   //Enables Depth Testing 
     gl.glDepthFunc(GL10.GL_LEQUAL);    //The Type Of Depth Testing To Do 

     //Really Nice Perspective Calculations 
     gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); 

    } 
} 

下面是方形碼

public class Square { 
    private FloatBuffer vertexBuffer; // buffer holding the vertices 
    private float vertices[] = { 
      -1.0f, -.25f, 0.0f, // V1 - bottom left 
      -1.0f, .25f, 0.0f,  // V2 - top left 
      1.0f, -.25f, 0.0f,  // V3 - bottom right 
      1.0f, .25f, 0.0f  // V4 - top right 
    }; 

    private FloatBuffer textureBuffer; // buffer holding the texture coordinates 
    private float texture[] = {   
      // Mapping coordinates for the vertices 
      0.0f, 1.0f,  // top left  (V2) 
      0.0f, 0.0f,  // bottom left (V1) 
      1.0f, 1.0f,  // top right (V4) 
      1.0f, 0.0f  // bottom right (V3) 
    }; 

    /** The texture pointer */ 
    private int[] textures = new int[1]; 

    public Square() { 
     // a float has 4 bytes so we allocate for each coordinate 4 bytes 
     ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4); 
     byteBuffer.order(ByteOrder.nativeOrder()); 

     // allocates the memory from the byte buffer 
     vertexBuffer = byteBuffer.asFloatBuffer(); 

     // fill the vertexBuffer with the vertices 
     vertexBuffer.put(vertices); 

     // set the cursor position to the beginning of the buffer 
     vertexBuffer.position(0); 

     byteBuffer = ByteBuffer.allocateDirect(texture.length * 4); 
     byteBuffer.order(ByteOrder.nativeOrder()); 
     textureBuffer = byteBuffer.asFloatBuffer(); 
     textureBuffer.put(texture); 
     textureBuffer.position(0); 
    } 

    /** 
    * Load the texture for the square 
    * @param gl 
    * @param context 
    */ 
    public void loadGLTexture(GL10 gl, Context context) { 
     // loading texture 
     Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), 
       R.drawable.progressbar_bg); 

     // generate one texture pointer 
     gl.glGenTextures(1, textures, 0); 
     // ...and bind it to our array 
     gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); 

     // create nearest filtered texture 
     gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST); 
     gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); 

     //Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE 
     //  gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT); 
     //  gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT); 

     // Use Android GLUtils to specify a two-dimensional texture image from our bitmap 
     GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); 

     // Clean up 
     bitmap.recycle(); 
    } 


    /** The draw method for the square with the GL context */ 
    public void draw(GL10 gl) { 
     // bind the previously generated texture 
     gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); 

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

     // 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.glTranslatef(0.0f,0.0f,-5.0f);操縱屏幕上紋理的座標。但仍然無法獲得實際的圖像大小,並將紋理圖像定位在從0,0座標開始的期望位置上。

回答

1

我正確地使用glTranslate()來解決問題。現在我明白,translate會將表面的最後一個轉換值作爲其當前位置。我已經做了修改,現在使用固定的x和z座標,y在每次紋理繪製後都會改變。我做了以下內容:

 float x = -1.0f; 
    float y = 1.5f; 
    float z = -5.0f; 

    // Drawing 
    gl.glTranslatef(x, y, z); 
    square1.draw(gl);      // Draw the square 
    gl.glTranslatef(0.0f, -0.50f, 0.0f); 
    square2.draw(gl);      // Draw the square 
    gl.glTranslatef(0.0f, -0.50f, 0.0f); 
    square2.draw(gl);      // Draw the square 
    gl.glTranslatef(0.0f, -0.50f, 0.0f); 
    square2.draw(gl);      // Draw the square 
0

我覺得這是

的圖形對象和繪製位圖(像素操作)之間的混淆從你的要求「我要畫一些按順序紋理(位圖圖像​​),從0,0座標開始。我還需要在屏幕上的圖像有一定的空白,請幫助,我需要改變頂點緩衝區的位置「。 它更喜歡你應該繪製位圖。參考api glBitmap,glDrawPixels等等

對於紋理。您應用的概念/方法是正確的。您正在繪製(-1,-1)和(1,1)之間的矩形。紋理映射似乎是正確的。然而,你需要與api玩
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT或CLAMP);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
(PS-PLease適當地使用基於android的apis)

當您試驗glTranslatef時,對象也隨着紋理一起移動。

想象一下,你正在繪製襯衫,有圖案。圖案座標是相對於襯衫的。使用紋理apis將圖案放在襯衫上。 translatef調用移動世界座標系,但模式不會改變。

+0

感謝您的回覆。我用這個方法在屏幕上繪製了一個圖像。我想繪製屏幕的實際座標。本例中的大部分內容都告訴您如何以0,0座標爲中心在屏幕中心繪製圖像。 – Rikki 2012-02-08 07:24:52

+0

您能否幫助我理解如何從0,0位置開始,即從屏幕頂部開始繪製座標。 pleae通過我已共享的代碼提供引用,以使其變得易於使用 – Rikki 2012-02-08 08:23:12

+0

如果您正在使用像素繪製函數(在您直接繪製到屏幕緩衝區的位置),請使用apis glPixelZoom。您可以使用負值來改變方向。但是,如果您嘗試使用模型轉換,請使用glRotate API。使用翻譯和旋轉的組合。 – 2012-02-08 09:43:19

0

如果您正在使用像素繪製函數(在您直接繪製到屏幕緩衝區的位置),請使用apis glPixelZoom。您可以使用負值來改變方向

如果您嘗試使用模型轉換,請使用glRotate API。使用翻譯和旋轉的組合。