2

好的,首先我完全同意“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);
    }

}

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

在此处输入图像描述

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

另外,如果它有帮助,我的所有代码都在以下地址:https ://github.com/gemurdock/OpenGlTest/tree/alpha.0.1.0

4

1 回答 1

2

首先,这是一个(严重变形的)立方体,而不是正方形。除非你正面看它,否则它永远不会看起来像一个“好正方形”:)

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

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

于 2013-08-25T21:58:46.450 回答