为什么这不是一个好看的广场?

时间:2013-08-25 20:40:20

标签: java android opengl-es opengl-es-2.0

好的,首先我完全同意“TheNewBoston”教程有时候非常错误。然而,这就是我在Android上尝试在OpenGL中创建一个立方体时所遵循的。

无论如何,我不能说我在OpenGL上找到了很棒的材料。我读过OpenGL Superbible ......没关系。

所以这是我的代码。

GLRenderer.java

package android.gem.opengltest;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import android.os.SystemClock;

public class GLRenderer implements Renderer {

    private GLCube cube;

    public GLRenderer() {
        cube = new GLCube();
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        // TODO Auto-generated method stub
        gl.glDisable(GL10.GL_DITHER);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT | GL10.GL_STENCIL_BUFFER_BIT);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        GLU.gluLookAt(gl, 0, 0, -5, 0, 0, 0, 0, 2, 0);

        long time = SystemClock.uptimeMillis() % 4000L;
        float angle = .09f * ((int) time);

        gl.glRotatef(angle, 1, 1.5f, 2);

        cube.draw(gl);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        // TODO Auto-generated method stub
        float ratio = (float) width / height;

        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 25);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
        // TODO Auto-generated method stub

        // START OF PERFORMANCE //
        gl.glDisable(GL10.GL_DITHER);
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
        // END OF PERFORMANCE //

        gl.glClearColor(.8f, 0, .2f, 1f);  // Red, Green, Blue, Alpha (0 - 1)
        gl.glClearDepthf(1f);
    }

}

GLCube.java

package android.gem.opengltest;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

public class GLCube {

    private float verticies[] = {
            1, 1, -1, // point 0 - topFrontRight
            1, -1, -1, // point 1 - bottomFrontRight
            -1, -1, -1, // point 2 - bottomFrontLeft
            -1, 1, -1, // point 3 - frontTopLeft
            1, 1, 1, // point 4 - topBackRight
            1, -1, 1, // point 5 - bottomBackRight
            -1, -1, 1, // point 6 - bottomBackLeft
            -1, 1, 1 // point 7 - frontBackLeft
    };

    private float rgbaValues[] = {
             1, 1, 0, 1,
             .25f, 0, .85f, 1,
             0, 1, 1, 1,
             1, 1, 0, 1,
             .25f, 0, .85f, 1,
             0, 1, 1, 1,
             .5f, .5f, .2f, 1,
             .3f, .3f, .3f, 1
    };

    private FloatBuffer vertBuff, colorBuff;

    private short[] pIndex = {  // Indicies
            3, 4, 0,  0, 4, 1,  3, 0, 1,
            3, 7, 4,  7, 6, 4,  7, 3, 6,
            3, 1, 2,  1, 6, 2,  6, 3, 2,
            1, 4, 5,  5, 6, 1,  6, 5, 4
    };

    private ShortBuffer pBuffer;

    public GLCube() {
        ByteBuffer bBuff = ByteBuffer.allocateDirect(this.verticies.length * 4);
        bBuff.order(ByteOrder.nativeOrder());
        vertBuff = bBuff.asFloatBuffer();
        vertBuff.put(this.verticies);
        vertBuff.position(0);

        ByteBuffer pbBuff = ByteBuffer.allocateDirect(this.pIndex.length * 2);
        pbBuff.order(ByteOrder.nativeOrder());
        pBuffer = pbBuff.asShortBuffer();
        pBuffer.put(pIndex);
        pBuffer.position(0);

        ByteBuffer cBuff = ByteBuffer.allocateDirect(this.rgbaValues.length * 4);
        cBuff.order(ByteOrder.nativeOrder());
        colorBuff = cBuff.asFloatBuffer();
        colorBuff.put(rgbaValues);
        colorBuff.position(0);
    }

    public void draw(GL10 gl) {
        gl.glFrontFace(GL10.GL_CW); // Clock Wise
        gl.glEnable(GL10.GL_CULL_FACE);  // Removes back end of cube
        gl.glCullFace(GL10.GL_BACK);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, this.vertBuff);
        gl.glColorPointer(4, GL10.GL_FLOAT, 0, this.colorBuff);
        gl.glDrawElements(GL10.GL_TRIANGLES, this.pIndex.length, GL10.GL_UNSIGNED_SHORT, this.pBuffer);

        gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisable(GL10.GL_CULL_FACE);
    }

}

我得到一个看起来像这样的立方体......

enter image description here

我做错了什么?我该怎么做才能让它看起来像一个真正的立方体?

此外,如果它有助于我的所有代码位于以下地址:https://github.com/gemurdock/OpenGlTest/tree/alpha.0.1.0

1 个答案:

答案 0 :(得分:2)

首先,这是一个(可怕的变形)立方体,不是一个正方形。除非你面对面看,它永远不会看起来像一个好的广场" :)

您的多边形面部剔除似乎正在剔除立方体面的错误一面。它看起来仍然像一个立方体,只是一个内部立方体。

这可能是因为您已经覆盖了OpenGL的默认正面缠绕方向(GL_CCW)。