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が、頂点は 4 つしかありません。6つ試してみてください。

または使用しますGL_TRIANGLE_STRIP

于 2012-09-13T23:31:14.643 に答える
1

左上のポイントは間違っているようです。そうあるべきで-0.5, 0.5, 0.0あり、使用すべきだと思うgenpfaultにも同意します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 に答える