2011-03-23 2 views
0

OpenGL에 대한 NeHe 튜토리얼의 Android 포트 인 insanitydesign.com에있는 자습서 세트를 따르고 있습니다. 튜토리얼 버전이 올바르게 수정 된 것처럼 보이는 이유는 무엇입니까? 변경된 버전 임의로이 완전히 잘못 렌더링됩니다. 경우에 따라 프로그램이로드 될 때 문제가 즉시 나타나는 경우도 있지만 화면이 나타나기 전에 화면을 회전해야 할 때도 있습니다. from filedropper 12 번 화면을 회전시킨 screengrabs가 포함 된 zip 파일을 다운로드 할 수 있습니다. 보시다시피 삼각형은 대개 렌더링되지만 때때로 왜곡되고 얼룩진 것처럼 보입니다. 호기심이 있다면 Here is a link to my entire Eclipse project.Android 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

+0

오! 사과! GL_TRIANGLE_STRIP에서 오류가 발생하지 않은 것으로 보입니다 : 원래 코드의 다른 위치에있었습니다. – loloof64

+0

glDrawArrays() (Shape 클래스의 draw() 메서드 내에서) 호출을 살펴보십시오. 마지막 매개 변수로 NINE (mVertices.length) 점을 가지고있는 반면 FloatBuffer를 사용하면 세 점만 정의 할 수 있습니다. ! 따라서 정점 당 3 개의 좌표를 부여하기 때문에 정사각형을 길이/3 대신 전달해야합니다. 이걸 해봐, 나에게 항상 효과가있어. 사과 해. 사각형을 만들어야하는 경우 버퍼에 4 개의 꼭지점을 지정하고 순서를 조심하십시오! – loloof64

관련 문제