使用gluOrtho2D时出现空白屏幕

时间:2011-04-28 06:12:07

标签: android opengl-es

首先,对不起,如果我的英语不好:(

我目前正在尝试在Android中制作游戏,并使用this link开始。 当我了解到我们可以使用gluOrtho2D绘制并行2D(使用透视图的教程)时,我切换了我的代码。但它只显示空白屏幕。

这是我的代码:

public class GLRenderer implements Renderer{

private Triangle triangle;
private Square square;
private Context context;

/** Constructor for GL
 * @return */
public GLRenderer(Context context) {
    this.context = context;

    this.triangle = new Triangle();
    this.square = new Square();
}

@Override
public void onDrawFrame(GL10 gl) {
    // TODO Auto-generated method stub
    // clear Screen and Depth Buffer
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    // Reset the Modelview Matrix
    gl.glLoadIdentity();

    // Drawing
    //gl.glTranslatef(0.0f, 0.0f, -5.0f);     // move 5 units INTO the screen
                                           // is the same as moving the camera 5 units away
    square.Draw(gl);                      // Draw the triangle

}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    // TODO Auto-generated method stub
    if(height == 0) {                       //Prevent A Divide By Zero By
         height = 1;                         //Making Height Equal One
    }



    gl.glViewport(0, 0, width, height);     //Reset The Current Viewport
    gl.glMatrixMode(GL10.GL_PROJECTION);    //Select The Projection Matrix
    gl.glLoadIdentity();                    //Reset The Projection Matrix

    //Calculate The Aspect Ratio Of The Window
    //GLU.gluPerspective(gl, 1.0f, (float)width / (float)height, -1.0f, 100.0f);
    GLU.gluOrtho2D(gl, 0.0f, (float)width, 0.0f, (float)height);

    gl.glMatrixMode(GL10.GL_MODELVIEW);     //Select The Modelview Matrix
    gl.glLoadIdentity();         
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    // TODO Auto-generated method stub
    // Load the texture for the square
    square.LoadGLTexture(gl, this.context, R.drawable.menu_main);

    gl.glEnable(GL10.GL_TEXTURE_2D);            //Enable Texture Mapping ( NEW )
    gl.glShadeModel(GL10.GL_SMOOTH);            //Enable Smooth Shading
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);    //Black Background
    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

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

    //Really Nice Perspective Calculations
    //gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}

}

任何帮助?

2 个答案:

答案 0 :(得分:3)

public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);
    gl.glOrthof(0, 320f, 480f, 0, 0, 1);
}

public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    /*code*/ 
}

给出三角形和正方形的x坐标,范围为0到320,y坐标为0到480

答案 1 :(得分:0)

如果glLoadIdentity()拨打onDrawFrame()并且glLoadIdentity()中的onSurfaceChanged()的最后一次呼叫被删除,则初始程序代码将正常运行。