1

我尝试画一个正方形..但是当我运行代码时,我看到一个三角形,而不是正方形... :))这里有什么问题???

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

3 回答 3

3

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

或使用GL_TRIANGLE_STRIP.

于 2012-09-13T23:31:14.643 回答
1

Left top point seem to be wrong, should be -0.5, 0.5, 0.0 and I also agree with genpfault that think you should use GL_TRIANGLE_STRIP

于 2012-09-13T23:52:03.937 回答
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) {
    }
}
于 2012-09-17T20:47:37.380 回答