2012-04-01 2 views
0

ich verschiedene Stackoverflow-Questons gefunden, aber ich sehe nicht, was ich falsch mache in meinem Code, weil die 2D „_floorhelper“ Textur nicht besagen auf dem Bildschirm. Ich möchte es als ein HUD Element verwendet:Android OpenGL ES - Draw 3D dann 2D -, wo der Fehler ist

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

das ist mein Setup:

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(); 
} 

Nach der Antwort von Stefan Hanke fand ich die Lösung. Ich habe die Scheitelpunkte im Netz falsch definiert. Also immer ich sah nur die Seite des Netzes ....

Dank Stefan Hanke.

// * Lösungscode

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

Bitte erklären, was los ist. – Vincent

+0

Danke für Ihre Antwort Vincent. Die 2D-Textur "_floorhelper" wird nicht auf dem Bildschirm angezeigt. –

+0

Es ist keine Textur, es ist ein Mesh. Was erscheint stattdessen? Ist es schwarz oder es erscheint nicht? – Vincent

Antwort

0

Sieht aus wie die Matrix-Setup nicht korrekt ist. Die Maschenscheitelpunkte haben alle y=0. Mit keiner Matrix, sehen Sie sich die Vorderkante des gesamten Netzes an. Wenn Sie den zweiten Matrix-Setup aus dem Code entfernen - wie Sie in Ihrem Kommentar zu Vincents Post taten -, wird die ModelView ein Gebräu der vorherigen gl* Anrufe sein.

+0

Danke für Ihre Hilfe. Die Maschenscheitel wurden definiert. Ich habe die Lösung der ursprünglichen Frage hinzugefügt. –

0

Es könnte sein, dass Sie nicht die Normalen der Eckpunkte des Mesh-definiert haben.

+0

Wenn ich die folgenden Codezeilen entferne, erscheint es als normales 3D-Objekt. Also in dieser Ansicht sind die Normalen in Ordnung. Aber ich weiß nicht, dass sie in Ortho anders sind? gl.glMatrixMode (GL10.GL_PROJECTION); gl.glPushMatrix(); gl.glLoadIdentity(); GLU.gluOrtho2D (gl, 0.0f, (Gleitkomma) activity.getViewportWidth(), 0.0f, (Gleitkomma) activity.getViewportHeight()); gl.glMatrixMode (GL10.GL_MODELVIEW); gl.glPushMatrix(); gl.glLoadIdentity(); –

+0

Ich habe versucht, Normalen hinzuzufügen, aber es erscheint immer noch nicht. Ich denke, es könnte eine Option sein, die ich nicht absichtlich aktiviert habe, aber ich weiß nicht, welches das sein könnte. –

+0

Danke für Ihre Hilfe. Aber ich habe das Lochgitter falsch herum definiert. –