2012-02-22 51 views
0

我目前正在嘗試在Android中製作遊戲。 我有這段代碼,我試圖在屏幕的一個角落顯示廣場。 我試過使用GLU.gluOrtho2D,但每次使用它時我都會看到空白屏幕。 有人能告訴我如何將屏幕中心的廣場移動到其中一個角落嗎?Android OpenGL在屏幕角落顯示對象

這是我的代碼:

public class OpenGLRenderer implements Renderer { 

private TexturedSquare element; 
private float angle; 

public OpenGLRenderer(Context context) { 
    // Initialize things 
    int[] bilder = {R.drawable.first}; 
    element = new TexturedSquare(context, 1f, 1, bilder); 
    angle = 0.5f; 
} 

public void onSurfaceCreated(GL10 gl, EGLConfig config) { 

    // Set the background color to black (rgba) 
    gl.glClearColor(1f, 1f, 1f, 1f); 
    // Enable Smooth Shading, default not really needed 
    gl.glShadeModel(GL10.GL_SMOOTH);// OpenGL docs 
    // Depth buffer setup. 
    gl.glClearDepthf(1.0f); 
    // Enables depth testing. 
    gl.glEnable(GL10.GL_DEPTH_TEST); 
    // The type of depth testing to do. 
    gl.glDepthFunc(GL10.GL_LEQUAL); 
    // Really nice perspective calculations. 
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, 
         GL10.GL_NICEST); 
    // NEW 
    element.loadTexture(gl);    // Load images into textures (NEW) 
    gl.glEnable(GL10.GL_TEXTURE_2D); 
} 


public void onDrawFrame(GL10 gl) { 

    // Clears the screen and depth buffer. 
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 
    // Replace the current matrix with the identity matrix 
    gl.glLoadIdentity(); 
    // Translates 4 units into the screen. 
    gl.glTranslatef(0f, 0f, -4f); 
    // What is to be drawn on the screen 
    //gl.glRotatef(-angle*20f, 0f, 0f, 1f); // Rotate the element around 3 axes 
    //gl.glRotatef(angle, 0f, 1f, 0f); 
    //gl.glRotatef(angle, 0f, 0f, 1f); 
    element.draw(gl); 
    angle++; 
} 


public void onSurfaceChanged(GL10 gl, int width, int height) { 

    // Sets the current view port to the new size. 
    gl.glViewport(0, 0, width, height); 
    // Select the projection matrix 
    gl.glMatrixMode(GL10.GL_PROJECTION); 
    // Reset the projection matrix 
    gl.glLoadIdentity();// OpenGL docs. 
    // Calculate the aspect ratio of the window 
    GLU.gluPerspective(gl, 45.0f, 
           (float) width/(float) height, 
           0.1f, 100.0f); 
    // Select the modelview matrix 
    gl.glMatrixMode(GL10.GL_MODELVIEW); 
    // Reset the modelview matrix 
    gl.glLoadIdentity(); 
} 
} 

這是抽籤法方類使用:

public void draw(GL10 gl) { 
    gl.glFrontFace(GL10.GL_CCW); 

    gl.glFrontFace(GL10.GL_CCW); // OpenGL docs 
    // Enable face culling. 
    gl.glEnable(GL10.GL_CULL_FACE); // OpenGL docs 
    // What faces to remove with the face culling. 
    gl.glCullFace(GL10.GL_BACK); // OpenGL docs 

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); 
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuffer); 

    gl.glPushMatrix(); 
    gl.glTranslatef(0f, 0f, cubeHalfSize); 
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[0]); 
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); 
    gl.glPopMatrix(); 

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 
} 

任何幫助嗎?

回答

0

請看gl.glTranslatef(0f,0f,cubeHalfSize);

如果cubeHalfSize爲立方體不會顯示的正值。

你已經有一個「的glTranslatef」在你OpenGLRenderer類,所以嘗試在多類註釋掉「的glTranslatef」:

gl.glPushMatrix(); 
    gl.glTranslatef(0f, 0f, cubeHalfSize); 
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[0]); 
// gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); 
    gl.glPopMatrix();