开发者

Problems with changing the Orthogonal Projection Matrix

I was experimenting with opengles, and made some simple squares moving around the screen:

    //inside my renderScreen() method, called from onDrawFrame()
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    if (gameObjects != null) {
        // iterate and draw objects
        for (GameObject obj : gameObjects) {
            if (obj != null) {
                gl.glLoadIdentity();
                gl.glTranslatef(obj.posX, obj.posY, 0.0f);
                obj.doDraw(gl);
            }

Just simple, draws objects to the screen (still working). Now I wanted to like drag around the screen by translating the projection matrix based on touch input. I added this code above my previous code (before drawing the objects)

//change projection matrix
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glTranslatef(-cameraX, -cameraY, 0.0f); //cameraX and cameraY set in updateGame()  

updateGame is just gets those from MyPanel.getDiffX() where DiffX is just how much your finger moved:

// Inside MyPanel Class
public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                startingX = event.getX();
                startingY = event.getY();
                MyRenderer.addObject(new GameSquare(event.getX(), v
                        .getHeight() - event.getY())); // Y is inverted!
            }
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                diffX = (event.getX() - startingX) / 100;
                //diffY = (event.getX() - startingY) / 1000;
                Log.d("diffX" ,Float.toString(diffX));
            }
            return true;
        }

Now if I use the code to translate the projection Matrix, I don't get to see any objects on my screen anymore (just the glClear color). Does anyone know what's going on开发者_如何学C ? Am I using the matrices wrong ?


What happens when you set cameraX and cameraY to 0? By changing the projection matrix, are you sure you're not overwriting some previously set values?

Aside from that, you should be translating by -cameraX and -cameraY. Think about it, if you want your camera to be at position (10, 10), then anything at (10, 10) should be at the origin after transforming by the camera, the logical translation would be (-10, -10). You're not translating the camera in the world, but you're translating the world around the camera.

EDIT: let me suggest an alternative method of doing this, that doesn't change the projection matrix.

//inside my renderScreen() method, called from onDrawFrame()
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glTranslate(-cameraX, -cameraY, 0); //Apply the camera here
if (gameObjects != null){
    // iterate and draw objects
    for (GameObject obj : gameObjects) {
        if (obj != null) {
            gl.glPushMatrix(); //Note this line here, we 'push' a matrix onto the top
            gl.glTranslatef(obj.posX, obj.posY, 0.0f);
            obj.doDraw(gl);
            gl.glPopMatrix(); //Here we remove our modified matrix to get the original one
        }
}


I always learnt that you should use projection matrix for perspective, not the camera (compare OpenGL FAQ). So I would set the projection (perspective) with GLU once and not change it anymore. In your draw routine you can set the camera (translate) before every draw or use glPushMatrix and glPopMatrix to safe and restore the camera.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜