2012-09-13 29 views
0

我試圖畫一個正方形..但是當我運行代碼時,看到一個tringle,而不是一個正方形...... :))這裏有什麼問題?繪製一個方形開放式gles

public class MyGL20Renderer implements GLSurfaceView.Renderer { 

    private FloatBuffer square1; 

    private void initShapes(){ 

     float square1Coords[]={ 
       -0.5f, -0.5f, 0.0f, // 0. left-bottom 
       0.5f, -0.5f, 0.0f, // 1. right-bottom 
       0.0f, 0.0f, 0.0f, // 2. left-top 
       0.5f, 0.5f, 0.0f // 3. right-top 
     }; 

     // initialize vertex Buffer for square 
     ByteBuffer vbb4 = ByteBuffer.allocateDirect(

     // (# of coordinate values * 4 bytes per float) 
     square1Coords.length * 4); 
     vbb4.order(ByteOrder.nativeOrder()); 
     square1 = vbb4.asFloatBuffer(); 
     square1.put(square1Coords); 
     square1.position(0); 

    } 

。 。 。 。

public void onDrawFrame(GL10 gl) { 

    // Redraw background color 
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 

    // Draw the square 
    gl.glColor4f(0.0f, 0.0f, 1.0f, 0.0f); //blue 
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, square1); 
    gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 4); 
} 

回答

3

您在指定GL_TRIANGLES,但只有四個頂點。試試六。

或使用GL_TRIANGLE_STRIP

+0

我試過..沒有解決問題.. – futuristixa

1

左頂部點似乎是錯誤的,應該是-0.5, 0.5, 0.0,我也同意genpfault是認爲你應該使用GL_TRIANGLE_STRIP

+0

和ı試過他們..它沒有再次解決問題... :((ı仍然看到屏幕上的三角... – futuristixa

+0

@futuristixa沒有你修正square1Coords中的座標錯字?第3行需要在數組中有'-0.5,0.5,0.0'? – epatel

+0

ı試過了.. didn解決.. – futuristixa

0

確定..我發現了一個方法來解決這個問題,我想..

//this is our Square class 
public class Square { 

    private FloatBuffer vertexBuffer; // buffer holding the vertices 
    private float vertices[] = { 
       -0.3f, -0.3f, 0.0f, // 0. left-bottom 
       0.3f, -0.3f, 0.0f, // 1. right-bottom 
       -0.3f, 0.3f, 0.0f, // 2. left-top 
       0.3f, 0.3f, 0.0f // 3. right-top 
    }; 

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

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

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

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

    } 


    /** The draw method for the square with the GL context */ 
    public void draw(javax.microedition.khronos.opengles.GL10 gl) { 

     gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 

     // set the colour for the square 
     gl.glColor4f(1.0f, 0.0f, 0.0f, 0.0f); //red 

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

     // 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); 
    } 
} 


// and this is our renderer class 
public class MyRenderer implements GLSurfaceView.Renderer { 
    // the square to be drawn 
    private Square  square; 

public MyRenderer() { 

     this.square = new Square(); 

    @Override 
    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);  

     // Draw the square 
     square.draw(gl); 

    } 

    @Override 
    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_PROJECTION); //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 
    } 

    @Override 
    public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
    } 
}