2012-04-01 9 views
0

2Dの "_floorhelper"テクスチャが表示されないため、私は別のStackoverflow-Questonsを見つけましたが、私のコードで何が間違っているのか分かりません画面上に表示されます。私は、HUD要素としてそれを使用したいと思います:Android OpenGL ES - 3Dを描画してから2D - どこに障害があるのですか

Trying to use Ortho for drawing 2D

https://stackoverflow.com/questions/9406753/android-opengl-gluortho2d-keep-original-shape-of-an-object

Android - Draw 3D then 2D with openGL ES

これは私の設定です:私が見つけたステファン・ハンケの回答後

public void gameSetup(GameActivity activity, GL10 gl) {    
     _floorhelper = new Mesh(gl, 4, false, true, false); 
     _floorhelper.texCoord(1, 1); 
     _floorhelper.vertex(-1, 0, 1); 
     _floorhelper.texCoord(1, 0); 
     _floorhelper.vertex(1, 0, 1); 
     _floorhelper.texCoord(0, 0); 
     _floorhelper.vertex(1, 0, -1); 
     _floorhelper.texCoord(0, 1); 
     _floorhelper.vertex(-1, 0, -1); 

    try { 
     InputStream is = getResources().openRawResource(getResources().getIdentifier("levels", "raw", getPackageName())); 
     levelBitmap = BitmapFactory.decodeStream(is); 
     levelTexture = new Texture(gl, levelBitmap, TextureFilter.Linear, TextureFilter.Linear, TextureWrap.ClampToEdge, TextureWrap.ClampToEdge); 
    } 
    catch(Exception ex){ 
     System.out.print(ex.getMessage()); 
    } 


    setTractFloor(gl); 

    float[] lightColor = { 1, 1, 1, 1 }; 
    float[] ambientLightColor = { 0.0f, 0.0f, 0.0f, 1 }; 
    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, ambientLightColor, 0); 
    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightColor, 0); 
    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, lightColor, 0); 
} 
public void gameLoop(GameActivity activity, GL10 gl) { 

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 
    gl.glViewport(0, 0, activity.getViewportWidth(), activity.getViewportHeight()); 

    gl.glEnable(GL10.GL_DEPTH_TEST); 
    gl.glEnable(GL10.GL_CULL_FACE); 

    gl.glMatrixMode(GL10.GL_PROJECTION); 
    gl.glLoadIdentity(); 
    float aspectRatio = (float) activity.getViewportWidth()/activity.getViewportHeight(); 
    GLU.gluPerspective(gl, 67, aspectRatio, 1, 100); 

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

    GLU.gluLookAt(gl, _scaleFactor, 5.0f, _scaleFactor, 0.0f, 0.0f, 0.0f, 0, 1, 0);    

    gl.glEnable(GL10.GL_LIGHTING); 
    gl.glEnable(GL10.GL_LIGHT0); 
    float[] direction = { 1.5f, 0.5f, 0, 0 }; 
    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, direction, 0); 
    gl.glEnable(GL10.GL_COLOR_MATERIAL); 

    // rotation 
    gl.glRotatef(135, 0, 1, 0); 

    gl.glEnable(GL10.GL_LINE_SMOOTH); 
    gl.glBlendFunc(GL10.GL_SRC_ALPHA_SATURATE, GL10.GL_ONE); 
    gl.glHint(GL10.GL_LINE_SMOOTH_HINT, GL10.GL_NICEST); // no visible diff 
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); 
    gl.glColor4f(1, 1, 1, 1); 

    // translation 
    gl.glTranslatef(-_oldTouchY, 0, _oldTouchX); 

    // render 
    currentTractFloor.render(); 

    // Draw 2D ontop of 3D - floorhelper 
    gl.glMatrixMode(GL10.GL_PROJECTION); 
    gl.glPushMatrix(); 
    gl.glLoadIdentity(); 

    GLU.gluOrtho2D(gl, 0.0f, (float) activity.getViewportWidth(), 0.0f, (float)activity.getViewportHeight()); 

    gl.glMatrixMode(GL10.GL_MODELVIEW); 

    gl.glPushMatrix(); 
    gl.glLoadIdentity(); 

    gl.glDisable(GL10.GL_DEPTH_TEST); 

    gl.glDisable(GL10.GL_COLOR_MATERIAL); 
    gl.glEnable(GL10.GL_TEXTURE_2D); 
    gl.glEnable(GL10.GL_BLEND); 
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); 

    levelTexture.bind(); 
    _floorhelper.render(PrimitiveType.TriangleFan); 

    gl.glEnable(GL10.GL_DEPTH_TEST); 
    gl.glPopMatrix(); 
    gl.glMatrixMode(GL10.GL_PROJECTION);   
    gl.glPopMatrix(); 
} 

ソリューション。 メッシュ内の頂点を間違って定義しました。だから私はいつもメッシュのちょうど側面を見た....

ステファンハンケに感謝します。

// *ソリューションコード

public void gameSetup(GameActivity activity, GL10 gl) { 
    _floorhelper = new Mesh(gl, 4, false, true, false); 
    _floorhelper.texCoord(0, 1); 
    _floorhelper.vertex(50, 50, 0); 
    _floorhelper.texCoord(1, 1); 
    _floorhelper.vertex(1000, 50, 0); 
    _floorhelper.texCoord(1, 0); 
    _floorhelper.vertex(1000, 1000, 0); 
    _floorhelper.texCoord(0, 0); 
    _floorhelper.vertex(50, 1000, 0); 

    try { 
     InputStream is = getResources().openRawResource(getResources().getIdentifier("levels", "raw", getPackageName())); 
     levelBitmap = BitmapFactory.decodeStream(is); 
     levelTexture = new Texture(gl, levelBitmap, TextureFilter.Linear, TextureFilter.Linear, TextureWrap.ClampToEdge, TextureWrap.ClampToEdge); 
    } 
    catch(Exception ex){ 
     System.out.print(ex.getMessage()); 
    } 

    setTractFloor(gl); 

    float[] lightColor = { 1, 1, 1, 1 }; 
    float[] ambientLightColor = { 0.0f, 0.0f, 0.0f, 1 }; 
    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, ambientLightColor, 0); 
    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightColor, 0); 
    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, lightColor, 0); 
} 

public void gameLoop(GameActivity activity, GL10 gl) { 

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 
    gl.glViewport(0, 0, activity.getViewportWidth(), activity.getViewportHeight()); 
    gl.glClearColor(0.18f, 0.68f, 1.0f, 1.0f); 

    gl.glEnable(GL10.GL_DEPTH_TEST); 
    gl.glEnable(GL10.GL_CULL_FACE);  

    setPerspective(activity, gl); 

    GLU.gluLookAt(gl, getScaleFactor(), 5.0f, getScaleFactor(), 0.0f, 0.0f, 0.0f, 0, 1, 0);  

    gl.glEnable(GL10.GL_LIGHTING); 
    gl.glEnable(GL10.GL_LIGHT0); 
    float[] direction = { 1.5f, 0.5f, 0, 0 }; 
    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, direction, 0); 
    gl.glEnable(GL10.GL_COLOR_MATERIAL); 

    // rotation 
    gl.glRotatef(135, 0, 1, 0); 

    gl.glEnable(GL10.GL_LINE_SMOOTH); 
    gl.glBlendFunc(GL10.GL_SRC_ALPHA_SATURATE, GL10.GL_ONE); 
    gl.glHint(GL10.GL_LINE_SMOOTH_HINT, GL10.GL_NICEST); // no visible diff 
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); 
    gl.glColor4f(1, 1, 1, 1); 


    // translation 
    gl.glTranslatef(-_oldTouchY, 0, _oldTouchX); 


    // render 
    currentTractFloor.render(); 

    // levels 
    setOrtho2D(activity, gl); 

    gl.glEnable(GL10.GL_TEXTURE_2D); 
    gl.glEnable(GL10.GL_BLEND); 
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); 

    levelTexture.bind();   
    _floorhelper.render(PrimitiveType.TriangleFan); 

    gl.glPopMatrix(); 
    gl.glMatrixMode(GL10.GL_PROJECTION);   
    gl.glPopMatrix(); 

} 

private void setPerspective(GameActivity activity, GL10 gl) { 
    gl.glMatrixMode(GL10.GL_PROJECTION); 
    gl.glLoadIdentity(); 
    float aspectRatio = (float) activity.getViewportWidth()/activity.getViewportHeight(); 
    GLU.gluPerspective(gl, 67, aspectRatio, 1, 100); 
    gl.glMatrixMode(GL10.GL_MODELVIEW); 
    gl.glLoadIdentity(); 
} 

private void setOrtho2D(GameActivity activity, GL10 gl) { 
    gl.glMatrixMode(GL10.GL_PROJECTION); 
    gl.glPushMatrix(); 
    gl.glLoadIdentity(); 
    // set ortho view 
    gl.glOrthof(0.0f,(float) activity.getViewportWidth(), 0.0f, (float)activity.getViewportHeight(), -1.0f, 1.0f); 
    gl.glMatrixMode(GL10.GL_MODELVIEW); 
    gl.glPushMatrix(); 
    gl.glLoadIdentity(); 
} 
+0

何が問題なのか説明してください。 – Vincent

+0

あなたの応答に感謝Vincent。 2D "_フロアヘルパー"テクスチャが画面に表示されません。 –

+0

テクスチャではなく、メッシュです。代わりに何が表示されますか?それは黒いのですか、それとも現れませんか? – Vincent

答えて

0

マトリックス設定が正しくないように見えます。メッシュの頂点はすべてy=0です。 ModelViewマトリックスがない場合は、メッシュ全体の前端を見ます。 Vincents投稿へのあなたのコメントで行ったように、のコードから2番目のマトリックス設定を削除した場合、は前のgl*呼び出しの組み合わせになります。

+0

ご協力ありがとうございます。メッシュの頂点は定義されていませんでした。元の質問に答えを加えました。 –

0

それはあなたのメッシュの頂点の法線を定義していないことが考えられます。

+0

次のコード行を削除すると、通常の3Dオブジェクトとして表示されます。だからこのビューでは法線は大丈夫です。しかし、Orthoで違っていることは分かりません。 gl.glMatrixMode(GL10.GL_PROJECTION); gl.glPushMatrix(); gl.glLoadIdentity(); GLU.gluOrtho2D(gl、0.0f、(float)activity.getViewportWidth()、0.0f、(float)activity.getViewportHeight()); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glPushMatrix(); gl.glLoadIdentity(); –

+0

法線を追加しようとしましたが、まだ現れません。 私は、私が意図的に有効にすることはできないオプションかもしれないと思うが、私はどちらが可能かは分からない。 –

+0

ご協力ありがとうございます。しかし、私はホールメッシュを間違った方法で定義しました。 –

関連する問題