2012-02-08 13 views
3

私はopengGLを初めて使用しています。質問があまりにも素朴であれば申し訳ありません。私はこれを学ぶための助けを求めるためにいくつかの修道院を通っています。私はGLSurfaceViewでテクスチャを描画しているところに小さなプログラムを書いています。0,0座標からOpenGL Androidのテクスチャを描画します

テクスチャ(ビットマップイメージ)を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座標を変更し、テクスチャ描画後に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)の間に四角形を描いています。テクスチャが正しくマッピングされているようです。しかし、
のglTexParameterf(GL_TEXTURE_2D、GL_TEXTURE_WRAP_S、GL_REPEATまたはCLAMP)の場合は、apiを使って遊ぶ必要があります。
glTexParameterf(GL_TEXTURE_2D、GL_TEXTURE_WRAP_T、GL_REPEAT);

glTranslatefで実験すると、オブジェクトも(テクスチャとともに)移動します。

パターンを使ってシャツを描いているとします。パターン座標はシャツを基準にしています。テクスチャapisを使用して、パターンをシャツに配置します。 translatef呼び出しはワールド座標系を移動しますが、パターンは変更されません。

+0

ありがとうございました。私は、このアプローチを使用してスクリーンに画像を描画するこの例を取っ​​た。私は画面の実際の座標を描きたい。ほとんどの例では、0,0の座標を中心として画面の中央に画像を描画する方法を示しています。 – Rikki

+0

描画座標を0,0の位置、つまり画面の上部から始める方法を私に理解させてもらえますか? pleaeはコードを使って参照を提供しています。私は共有しているので、簡単に理解できます – Rikki

+0

ピクセル描画関数を使用している場合は、glPixelZoomなどのapisを使用します。負の値を使用して方向を変更することができます。ただし、モデル変換を使用する場合は、glRotate APIを使用してください。翻訳と回転の組み合わせを使用します。 –

0

ピクセル描画関数(画面バッファに直接描画する場合)を使用する場合は、glPixelZoomなどのapisを使用します。あなたはglRotateのAPIを使用するモデル変換を使用しようとしている場合は、方向

を変更するには負の値を使用することができます。翻訳と回転の組み合わせを使用します。

関連する問題