Android OpenGL ES ...为什么教程总是正确呈现,而我的版本随机没有?

时间:2011-03-23 22:35:01

标签: java android opengl-es glsurfaceview

我正在关注在insanitydesign.com找到的教程集,这是关于OpenGL的NeHe教程的Android端口。我理解为什么教程版本在任何时候都能正确呈现,而我修改后的版本随机呈现完全错误,我遇到了很多问题。偶尔,当程序加载时问题立即显现,但有时我必须在屏幕出现之前旋转它。您可以下载包含screengrabs的zip文件,我将屏幕旋转了12次from filedropper。正如您所看到的,三角形通常会呈现,但有时会出现完全扭曲和污迹。 Here is a link to my entire Eclipse project如果你很好奇。

我怀疑问题出在我的Shape.draw()方法中,但我无法确定。

我相信这是相关的代码:

GLRenderer.java

    private Shape shape;

public GLRenderer(){
    Log.i(this.toString(),"ctor");
    shape = Shape.makeTriangle();
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    speedPerSecond = SystemClock.uptimeMillis();
    Log.d(this.toString(), ".onSurfaceCreated");

    gl.glShadeModel(GL10.GL_SMOOTH);            



    gl.glClearColor(1.0f, 0.0f, 0.0f, 0.5f);


    gl.glClearDepthf(1.0f);                     //Depth Buffer Setup



    gl.glEnable(GL10.GL_DEPTH_TEST);            //Enables Depth Testing
    gl.glDepthFunc(GL10.GL_LEQUAL);             //The Type Of Depth Testing To Do

    //Really Nice Perspective Calculations
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); 

}

@Override
public void onDrawFrame(GL10 gl) {

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();

    shape.setX(0.0f);
    shape.setY(0.0f);
    shape.setZ(-6.0f);
    shape.draw(gl);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    if(height == 0){
        height = 1;
    }
    gl.glViewport(0, 0, width, height);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();


    GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 50.0f);


    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();


}

Shape.java     包com.smashing.test;

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

import javax.microedition.khronos.opengles.GL10;

import android.util.Log;

public class Shape implements IGLShape {

    public float getX(){
        return x;
    }

    public float getY(){
        return y;
    }

    public float getZ(){
        return z;
    }

    public void setX(float pX){
        x = pX;
    }

    public void setY(float pY){
        y = pY;
    }

    public void setZ(float pZ){
        z = pZ;
    }



    public static Shape makeTriangle(){
        Shape s = new Shape();
                /*
                 *  Is this correct?  I want to create a right triangle on screen like this
                 *  |\
                 *  | \
                 *  |  \
                 *  |   \
                 *  |____\  though with a 90/45/45 degree spread rather than this poor representation.
                 */
        float v[] = {
            0.0f, 1.0f, 0.0f,
            0.0f, 0.0f, 0.0f,
            1.0f, 0.0f, 0.0f
        };
        s.setVertices(v);
        return s;
    }

    public static Shape makeShapeFromVertices(float pVerts[]){
        Shape s = new Shape();
        s.setVertices(pVerts);
        return s;   
    }


    private float x = 0.0f;
    private float y = 0.0f;
    private float z = 0.0f;
    private float[] mVertices;
    private FloatBuffer mVertexBuffer;






    public void setVertices(float pVerts[]){
        mVertices = pVerts;
        ByteBuffer bb = ByteBuffer.allocateDirect(mVertices.length * 4);
        bb.order(ByteOrder.nativeOrder());
        mVertexBuffer = bb.asFloatBuffer();
        mVertexBuffer.put(mVertices);
        mVertexBuffer.position(0);
    }

    public float[] getVertices(){
        return mVertices;
    }

    private Shape(){

    }


    @Override
    public void draw(GL10 gl) {


        /*
         * I believe the problem is here... but I don't understand how or why?
         */
        gl.glTranslatef(x, y, z);
        gl.glFrontFace(GL10.GL_CW);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, mVertices.length);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glTranslatef(-x, -y, -z);

    }



}

编辑:我知道该教程与我的版本略有不同。我试图从中进行推断并进行实验......我对这些差异感到非常惊讶和震惊,并想知道它们存在的原因,以及如何避免我在渲染中发现的这种令人不快的结果方案

1 个答案:

答案 0 :(得分:1)

你应该替换

glDrawArrays(GL10.GL_TRIANGLE_STRIP, ...)

glDrawArrays(GL10.GL_TRIANGLES, ...)

我认为,当你画一个简单的三角形而不是四边形时,这就是问题的原因。

此致