2011-03-23 34 views
0

我正在關注在insanitydesign.com上找到的教程集,這是NeHe OpenGL上的一個Android端口。我遇到很多問題,理解爲什麼教程版本在任何時候都能正確呈現,而我的修改版本隨機呈現完全不正確。偶爾,程序加載時問題很明顯,但有時我必須在屏幕出現之前旋轉屏幕。你可以下載一個包含screengrabs的zip文件,我已經將屏幕旋轉了12次from filedropper。正如你所看到的,三角形通常呈現,但有時看起來完全扭曲和污跡。如果你很好奇,你可以撥打Here is a link to my entire Eclipse projectAndroid OpenGL ES ...爲什麼教程總是呈現正確,而我的版本是隨機的?

我懷疑問題出在我的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

你應該

glDrawArrays(GL10.GL_TRIANGLES, ...) 

我認爲,你畫一個簡單的三角形,而不是四,那就是麻煩的原因更換

glDrawArrays(GL10.GL_TRIANGLE_STRIP, ...) 

問候

+0

謝謝,你可以詳細說明*爲什麼* TRIANGLE_STRIP導致一個問題一點?如果我想製作非三角形形狀,該怎麼辦? – scriptocalypse 2011-03-25 22:29:45

+0

哦!道歉!看起來這個錯誤不是來自GL_TRIANGLE_STRIP:它是你的原始代碼中的其他地方。 – loloof64 2011-03-26 09:55:39

+0

看看你對glDrawArrays()(在Shape類的draw()方法內)的調用:你用最後一個參數告訴你有九個(mVertices.length)點,而你只用FloatBuffer定義三個點! !所以,你應該已經傳遞了vertices.length/3(因爲你給每個頂點3個coords)。試試這個,它總是對我有用:)道歉。如果你需要建立一個正方形,給這個時間4個頂點放在你的緩衝區裏,並且小心它們的順序! – loloof64 2011-03-26 10:03:28

相關問題