在开放的gles中画一个正方形

时间:2012-09-13 23:23:00

标签: android opengl-es drawing

ı尝试绘制一个正方形..但是当ı运行代码时,ı看到一个tringle,而不是一个正方形... :))这里有什么问题?

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 个答案:

答案 0 :(得分:2)

您指定GL_TRIANGLES,但只有四个顶点。试试六。

或使用GL_TRIANGLE_STRIP

答案 1 :(得分:1)

左上角似乎是错误的,应该是-0.5, 0.5, 0.0我同意genpfault认为你应该使用GL_TRIANGLE_STRIP

答案 2 :(得分: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) {
    }
}