简体   繁体   中英

Android OpenGL ES - Draw 3D then 2D - where is the fault

I found different Stackoverflow-Questons, but I don't see what I'm doing wrong in my code, because the 2D "_floorhelper" texture doesn't show up on the screen. I would like to used it as a HUD element:

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

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

After the Answer of Stefan Hanke I found the solution. I've defined the vertices in the mesh wrong. So I always saw just the side of the mesh....

Thanks to Stefan Hanke.

//* Solution Code

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

可能是因为您没有定义网格的顶点的法线。

Looks like the matrix setup is incorrect. The meshes vertices all have y=0 . With no ModelView matrix whatsoever, you will look at the front edge of the whole mesh. If you remove the second matrix setup from the code -- as you did in your comment to Vincents post -- , the ModelView will be a concoction of the previous gl* calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM