Issues extending AndEngine to render 3D models

For a book I'm writing on AndEngine game development I'm attempting to develop a sample AndEngine application showing how to import and render 3D models. For the importing I use the Assimp library which I successfully compiled for Android and am using via a small NDK-based wrapper in C++.

The model I'm importing is a basic teapot model exported from 3DS Max 2013 using its OBJ exporter. In the Java debugger the resulting vertex, normal and uv array data looks fine. The issue I am having is with actually displaying the model in the AndEngine application. The OpenGL ES 2.0-related code is sprinkled throughout the AndEngine code, making it hard to see what does what exactly and in what order. At this point I alternately see nothing of the model being rendered, or just the following output: 精灵顶部茶壶的模糊轮廓。

To display the model I extended a number of AndEngine classes. First I created a ModelData class which just holds the data arrays. This is passed to a Model class which like Sprite derives from Shape. In its current, messy, Sprite-derived fashion it looks like this:

public class Model extends Shape {
public static final int VERTEX_INDEX_X = 0;
public static final int VERTEX_INDEX_Y = Model.VERTEX_INDEX_X + 1;
public static final int COLOR_INDEX = Model.VERTEX_INDEX_Y + 1;
public static final int TEXTURECOORDINATES_INDEX_U = Model.COLOR_INDEX + 1;
public static final int TEXTURECOORDINATES_INDEX_V = Model.TEXTURECOORDINATES_INDEX_U + 1;

public static final int VERTEX_SIZE = 2 + 1 + 2;
public static final int VERTICES_PER_SPRITE = 4;
public static final int SPRITE_SIZE = Model.VERTEX_SIZE * Model.VERTICES_PER_SPRITE;

public static final int POSITION_ATTRIBUTE_ID = 0;
public static final int NORMAL_ATTRIBUTE_ID = 1;
public static final int TEXTURE_COORDINATE_ATTRIBUTE_ID = 2;

public static final VertexBufferObjectAttributes VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT = 
        new VertexBufferObjectAttributesBuilder(3)
        .add(POSITION_ATTRIBUTE_ID, "Position", 3, GLES20.GL_FLOAT, false)
        .add(NORMAL_ATTRIBUTE_ID, "Normal", 3, GLES20.GL_FLOAT, true)
        .add(TEXTURE_COORDINATE_ATTRIBUTE_ID, "Texture_Coordinate", 2,
                GLES20.GL_FLOAT, false).build();


protected final ModelData mModelData;
protected final ITextureRegion mTextureRegion;
protected final HighPerformanceModelVertexBufferObject mModelVertexBufferObject;
//private int mMVPMatrixHandle;
private float[] mVMatrix = new float[16];
private float[] mProjMatrix = new float[16];
//private float[] mMVPMatrix = new float[16];
private float[] mInterleavedArray;
//private FloatBuffer interleavedBuffer;


// --- CONSTRUCTOR ---
public Model(final float pX, final float pY, final ITextureRegion pTextureRegion, 
        ModelData pModelData, VertexBufferObjectManager pVertexBufferObjectManager) {
    super(pX, pY, ModelShaderProgram.getInstance());
    mTextureRegion = pTextureRegion;
    mInterleavedArray = pModelData.getInterleavedArray();
    mModelVertexBufferObject = new HighPerformanceModelVertexBufferObject(
            pVertexBufferObjectManager, mInterleavedArray, DrawType.STATIC, 
            true, Model.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT);
    mModelData = pModelData;

    //mMVPMatrixHandle = GLES20.glGetUniformLocation(
        //  ((ModelShaderProgram) mShaderProgram).getProgramId(), "uMVPMatrix");
}


// --- GET VERTEX BUFFER OBJECT ---
@Override
public IVertexBufferObject getVertexBufferObject() {
    return mModelVertexBufferObject;
}


// --- ON SURFACE CHANGED ---
public void onSurfaceChanged(GLState pGLState, int pWidth, int pHeight) {
    // onSurfaceChanged
    float ratio = (float) pWidth / pHeight;
    // create a projection matrix from device screen geometry
    Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 37);

    // Create a camera view matrix
    Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    /*int[] buffers = new int[2];
    GLES20.glGenBuffers(2, buffers, 0);
    int rectVerts = buffers[0];
    //int rectInds = buffers[1];
    interleavedBuffer = FloatBuffer.wrap(mInterleavedArray);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, rectVerts);
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, mInterleavedArray.length, 
            interleavedBuffer, GLES20.GL_STATIC_DRAW);*/

    /*GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, rectInds);
    GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, 
            (rectIndices.limit()*4), rectIndices, GLES20.GL_STATIC_DRAW);*/
}


// --- PREDRAW ---
@Override
protected void preDraw(GLState pGLState, Camera pCamera) {
    super.preDraw(pGLState, pCamera);

    //this.mTextureRegion.getTexture().bind(pGLState); // TODO
    this.mModelVertexBufferObject.bind(pGLState, this.mShaderProgram);
}


// --- DRAW ---
@Override
protected void draw(GLState pGLState, Camera pCamera) {
    // Combine the projection and camera view matrices
    //Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);

    // Apply the combined projection and camera view transformations
    //GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);

    // Draw the model
    //GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount); // TODO
    this.mModelVertexBufferObject.draw(GLES20.GL_TRIANGLES, 
            mInterleavedArray.length);
}


// --- POST-DRAW ---
@Override
protected void postDraw(GLState pGLState, Camera pCamera) {
    this.mModelVertexBufferObject.unbind(pGLState, this.mShaderProgram);

    super.postDraw(pGLState, pCamera);
}


// --- ON UPDATE VERTICES ---
@Override
protected void onUpdateVertices() {
    this.mModelVertexBufferObject.onUpdateVertices(this);
}


// --- ON UPDATE COLOR ---
@Override
protected void onUpdateColor() {
    this.mModelVertexBufferObject.onUpdateColor(this);
}


// --- ON UPDATE TEXTURE COORDINATES ---
protected void onUpdateTextureCoordinates() {
    this.mModelVertexBufferObject.onUpdateTextureCoordinates(this);
}
}

The HighPerformanceModelVertexBufferObject class looks as follows:

public class HighPerformanceModelVertexBufferObject extends
    HighPerformanceVertexBufferObject {
ModelShaderProgram mModelShaderProgram;

public HighPerformanceModelVertexBufferObject(
        VertexBufferObjectManager pVertexBufferObjectManager,
        final float[] pBufferData, DrawType pDrawType, boolean pAutoDispose,
        VertexBufferObjectAttributes pVertexBufferObjectAttributes) {
    super(pVertexBufferObjectManager, pBufferData, pDrawType, pAutoDispose,
            pVertexBufferObjectAttributes);
}


// --- SET VERTEX DATA ---
/*public void setVertexData(float[] va) {
    mBufferData = va;
}*/


@Override
public void bind(final GLState pGLState, final ShaderProgram pShaderProgram) {
    mModelShaderProgram = (ModelShaderProgram) pShaderProgram;
    super.bind(pGLState, pShaderProgram);
}


@Override
public void draw(final int pPrimitiveType, final int pCount) {
    GLES20.glDrawArrays(pPrimitiveType, 0, pCount);
    //GLES20.glDrawElements(pPrimitiveType, pCount, GLES20.GL_UNSIGNED_SHORT, mByteBuffer);
}


public void onUpdateColor(final Model pModel) {
    //
}


public void onUpdateVertices(final Model pModel) {
    //
}


public void onUpdateTextureCoordinates(final Model pModel) {
    //
}
}

Finally the ModelShaderProgram class, containing the vertex and fragment shaders:

public class ModelShaderProgram extends ShaderProgram {
private static ModelShaderProgram INSTANCE;

public static final String VERTEXSHADER =
        "attribute vec3 positionAttrib; n" +
        "attribute vec3 normalAttrib; n" +
        "attribute vec2 uvAttrib; n" +

        // This matrix member variable provides a hook to manipulate
        // the coordinates of objects that use this vertex shader.
        "uniform mat4 uMVPMatrix;   n" +
        "void main() {n" +         
        // The matrix must be included as part of gl_Position
        // Note that the uMVPMatrix factor *must be first* in order
        // for the matrix multiplication product to be correct.
        "gl_Position = uMVPMatrix * vec4(positionAttrib, 1.0); n" +
        "}";

public static final String FRAGMENTSHADER =
        "void main() {n" +
        " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); n" +
        "}";

public static int sUniformModelViewPositionMatrixLocation = -1;
public static int sUniformTexture0Location = -1;


private ModelShaderProgram() {
    super(ModelShaderProgram.VERTEXSHADER, ModelShaderProgram.FRAGMENTSHADER);
}

public static ModelShaderProgram getInstance() {
    if (ModelShaderProgram.INSTANCE == null) {
        ModelShaderProgram.INSTANCE = new ModelShaderProgram();
    }

    return ModelShaderProgram.INSTANCE;
}


@Override
protected void link(final GLState pGLState) throws ShaderProgramLinkException {
    GLES20.glBindAttribLocation(this.mProgramID, 0, "positionAttrib");
    GLES20.glBindAttribLocation(this.mProgramID, 1, "normalAttrib");
    GLES20.glBindAttribLocation(this.mProgramID, 2, "uvAttrib");

    super.link(pGLState);

    ModelShaderProgram.sUniformModelViewPositionMatrixLocation = 
            this.getUniformLocation("uMVPMatrix");
    //ModelShaderProgram.sUniformTexture0Location = 
        //  this.getUniformLocation(ShaderProgramConstants.UNIFORM_TEXTURE_0);
}

@Override
public void bind(final GLState pGLState, final VertexBufferObjectAttributes pVertexBufferObjectAttributes) {
    super.bind(pGLState, pVertexBufferObjectAttributes);

    GLES20.glUniformMatrix4fv(ModelShaderProgram.sUniformModelViewPositionMatrixLocation, 1, false, pGLState.getModelViewProjectionGLMatrix(), 0);
    //GLES20.glUniform1i(ModelShaderProgram.sUniformTexture0Location, 0);
}
}

As you can see in the fragment shader, it should colour any fragments red, but this obviously doesn't happen. The weird outline drawing is also very curious. Is there something I missed in integrating this code? Am I trying to override something in AndEngine's code which I should leave alone?

Thank you in advance for any hints/advice. Feel free to ask for more information and the like.

Update A colleague pointed out that the model I am using is indexed. Switching to indexed drawing using glDrawElements instead of glDrawArray.

链接地址: http://www.djcxy.com/p/68962.html

上一篇: Libgdx Android游戏绘图形状(位图与SVG)

下一篇: 扩展AndEngine渲染3D模型的问题