0

首先,我为我愚蠢的英语能力道歉。

我在 Google 的 Android 开发者示例中学习了 openGL ES。

我测试了代码,它工作正常。

但是,我想制作自己的库,所以我修改了代码,并分发了它。

(像这样,Shader.java、ColorManager.java、ObjectManager.java ... Rectangle.java。)

我的渲染器绘制背景颜色,但它不绘制我的图形。

这是我的 GLSurfaceView

 public class MenuView extends GraphicView{

    EnhancedRenderer mRenderer;

    public MenuView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        mRenderer = new EnhancedRenderer();
        setEGLContextClientVersion(2);
        setRenderer(mRenderer);
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }
    public MenuView(Context context, EnhancedRenderer mRenderer){
        super(context, mRenderer);
        setRenderer(mRenderer);
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e){
        return true;
    }
}

GraphicView只需扩展GLSurfaceView并添加一个自定义方法。

这是我的渲染器源代码

public class EnhancedRenderer extends GraphicRenderer{

protected final float[] mMVPMatrix = new float[16];
protected final float[] mProjMatrix = new float[16];
protected final float[] mVMatrix = new float[16];
protected final float[] mRotationMatrix = new float[16];

private Rectangle rect1;
private Rectangle 

rect2;
    private Triangle tri;
    //    private Circle circle;
    public EnhancedRenderer(){
        super();

    }
    public EnhancedRenderer(Context context){
    //  super(context);
    }
    public void setBackgroundColor(short r,  short g, short b, float Alpha){
        float[] Color = ColorManager.custom_COLOR(r, g, b, Alpha);

        GLES20.glClearColor(Color[0], Color[1], Color[2], Color[3]);
    }
    @Override
    public void onSurfaceCreated(GL10 unused, EGLConfig config){
        setBackgroundColor((short)111,(short)0,(short)111,1000.0f);

        rect1 = new Rectangle();
        rect1.init_Figure();

        rect2 = new Rectangle();
        rect2.init_Figure();

        tri = new Triangle();
        tri.init_Figure();
    }
    @Override
    public void onDrawFrame(GL10 unused){
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

        Matrix.setLookAtM(mVMatrix, 0, 
                0.0f, 0.0f, -0.3f, 
                0.0f, 0.0f, 0.0f, 
                0.0f, 1.0f, 0.0f);
        Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
        rect1.draw(mMVPMatrix);

        Matrix.setLookAtM(mVMatrix, 0, 
                0.0f, 0.0f, -0.3f, 
                0.0f, 0.0f, 0.0f, 
                0.0f, 1.0f, 0.0f);
        Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
        tri.draw(mMVPMatrix);
    }
    @Override
    public void onSurfaceChanged(GL10 unused, int width, int height){
        GLES20.glViewport(0, 0, width, height);
        float ratio = (float) width / height;
        // this projection matrix is applied to object coordinates
        // in the onDrawFrame() method
        Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
    }
}

-GraphicRenderer只是实现GLSurfaceView.Renderer

这是'init_Figure()'方法

 public boolean init_Figure(){

        //only once Initialize
    if(isProgramSetUp){return isProgramSetUp;}
        //initialize the vertex and drawList buffer
       vertexBuffer = BufferManager.makeFloatBuffer(coords);
       drawListBuffer = BufferManager.makeShortBuffer(drawOrder);

        //make Shader
       vertexShader = Shader.get_VertexShader();
       fragmentShader = Shader.get_FragmentShader();

        //add Shader to Program
       mProgram = Shader.makeProgram(vertexShader, fragmentShader);

       //mProgram = Shader.makeProgram();

       isProgramSetUp = true;
       return isProgramSetUp;

    }

这是 draw() 方法

public void draw(float[] mMVPMatrix){

    //Add program to OpenGL environment
     Log.d("draw()-","draw");
    GLES20.glUseProgram(mProgram);
    Log.d("draw()-","draw -mProgram-"+mProgram);
    // get handle to vertex shader's vPosition member
    mPosition_Handle = GLES20.glGetAttribLocation(mProgram, "vPosition");
    Log.d("draw()-","draw -mPosition Handle-"+mPosition_Handle);
    // Enable a handle to the triangle vertices
    GLES20.glEnableVertexAttribArray(mPosition_Handle);
    // Prepare the triangle coordinate data
    GLES20.glVertexAttribPointer(mPosition_Handle, COORDS_PER_VERTEX,
                                 GLES20.GL_FLOAT, false,
                                 vertex_Stribe, vertexBuffer);
    // get handle to fragment shader's vColor member
    mColor_Handle = GLES20.glGetUniformLocation(mProgram, "vColor");
    Log.d("draw()-","draw -mColor Handle-"+mColor_Handle);
    // Set color for drawing the Square
    GLES20.glUniform4fv(mColor_Handle, 1, color, 0);
    Log.d("draw()-", color[0]+", "+color[1]+", "+color[2]+" length- "+color.length);
    // get handle to shape's transformation matrix
    mMVPMatrix_Handle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    GraphicRenderer.checkGlError("glGetUniformLocation");
    Log.d("draw()-","draw -mMVPMatrix_Handle-"+mMVPMatrix_Handle);
    // Apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mMVPMatrix_Handle, 1, false, mMVPMatrix, 0);
    GraphicRenderer.checkGlError("glUniformMatrix4fv");
    // Draw the square 
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length,
                          GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
    Log.d("draw()-", drawOrder[0]+", "+drawOrder[1]+", "+drawOrder[2]+" length- "+drawOrder.length);
    // Disable vertex array
    GLES20.glDisableVertexAttribArray(mPosition_Handle);

}

当我检查 logcat 时,图的颜色、坐标、drawOrder、mProgram 设置正确......

4

1 回答 1

0

我解决了问题

我改变了这部分......

Matrix.setLookAtM(mVMatrix, 0, 0.0f, 0.0f, -0.3f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

像这样

Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

于 2013-07-08T02:08:29.260 回答