渲染一个正方形

时间:2010-07-27 12:54:23

标签: android opengl-es

以下是给我三角形而不是正方形,任何人都知道为什么??

    //Defining as...
        addcube(2,-2, 1, -1, 10);
        addcube(float lx, float hx, float ly, float hy, floathz){
            final float array1[] = new float[] {
            //Front face
            lx, ly, hz,
            lx, hy, hz,
            hx, hy, hz,
            hx, ly, hz
    };
 }

    //Drawing as...
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;

我尝试添加gl.glDisable(GL10.GL_CULL_FACE);,但只是将剩余的方块放在前台。我已经包括了绘制方法......

public void onDrawFrame(GL10 gl) {
    onDrawFrameCounter++;
    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    bindCameraTexture(gl);
    System.out.println(Global.bearing);
    float bear = Global.bearing;
    gl.glLoadIdentity();
    gl.glNormal3f(0,0,1);
    System.out.println("ARRAY!: " + GLCamTest.array.length);
    p = 0;
    gl.glRotatef(bear, 1, 0, 0);

    int e = 0;  
    for(int q=0; q < Global.cubes;q++){
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    }
}

public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);

    float ratio = (float) width / height;
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glFrustumf(-ratio, ratio, -1, 1, 1, 20);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
    gl.glClearColor(0, 0, 0, 0);
    gl.glDisable(GL10.GL_CULL_FACE);
    gl.glShadeModel(GL10.GL_SMOOTH);
    gl.glEnable(GL10.GL_DEPTH_TEST);
    cubeBuff = makeFloatBuffer(GLCamTest.array);
    texBuff = makeFloatBuffer(camTexCoords);                
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, cubeBuff);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuff);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}

3 个答案:

答案 0 :(得分:2)

尝试使用'之字形顺序'中的点 即

xl,yl
xl,yh
xh,yl  // this is hypotenuse of first triangle
xh,yh  // this is endpoint of last triangle, so previous line is hypotenuse
你可能会得到某种双三角形的破碎形状。

你看,正方形是由三角形组成的,它们是按照点的顺序绘制的!

答案 1 :(得分:0)

没有测试过,但我想你应该通过GL_QUAD_STRIP而不是GL_TRIANGLE_STRIP:

gl.glDrawArrays(GL10.GL_QUAD_STRIP, e, 4);

答案 2 :(得分:0)

在你的抽签调用

之间尝试e = e + 3到位e = e + 4