Android / OpenGL glRotatef在使用GLOrthof时没有任何角度。

时间:2022-09-10 18:05:28

I'm writing a game that is basically 2D, what i need is to have a scene with simple coordinates to emulate something like the Android Canvas, so 0,0 is top left and w/h are bottom right. Up to now i have been able to create the scene and draw to it, everything works properly, translate moves the objects correctly according to w/h coordinates in my 2D plane.

我在写一个基本上是2D的游戏,我需要一个具有简单坐标的场景来模拟Android Canvas, 0 0是左上角w/h是右下角。到目前为止,我已经能够创建场景并绘制到它,一切工作正常,翻译根据我的二维平面的w/h坐标正确移动对象。

Problem is that now i want to "flip" a texture using X axis rotation and i want a 3D effect while flipping, if i use the "rotatef" call the flip occurs but its flat, so there is no perspective at all, is just like scaling the height till it goes to zero so there is no distance perception.

问题是,现在我想“翻转”纹理使用X轴旋转和我想要一个3 d效果翻转时,如果我使用“rotatef”调用另发生但其平坦,所以没有角度,就像缩放高度直到趋于零距离所以没有知觉。

I currently create the ViewPort in this way:

我目前用这种方式创建ViewPort:

gl.glViewport(0, 0, w, h);
gl.glMatrixMode(GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0, w, h, 0, -1f, 100f);
gl.glEnable(GL_TEXTURE_2D);

This will create my desired viewport with 0/0 at top left and w/h at bottom right filling the screen. Then i load my texture from a Bitmap.

这将创建我想要的viewport, 0/0在左上角,w/h在右下角填充屏幕。然后我从位图加载纹理。

gl.glBindTexture(GL10.GL_TEXTURE_2D, id);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

And finally i draw the texture:

最后我画出了纹理:

gl.glMatrixMode(GL_MODELVIEW);
gl.glLoadIdentity();
gl.glPushMatrix();
// Translate to starting position first in 0,0,w,h coordinates
gl.glTranslatef(mXOffset, mYOffset, 0f);
// Rotate 45 degrees on X axis
gl.glRotatef(45, 1.0f, 0.0f, 0.0f)
// Draw
gl.glEnableClientState(GL_VERTEX_ARRAY);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glVertexPointer(2, GL_FLOAT, 0, sVertexBuffer);
gl.glEnableClientState(GL_TEXTURE_COORD_ARRAY);
gl.glBindTexture(GL10.GL_TEXTURE_2D, id);
gl.glTexCoordPointer(2, GL_FLOAT, 0, mTextureBuffer);
gl.glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
gl.glDisableClientState(GL_VERTEX_ARRAY);
gl.glDisableClientState(GL_TEXTURE_COORD_ARRAY);
gl.glPopMatrix();

Finally my vertex buffers are:

最后我的顶点缓冲是:

float vertices[] = {
        0f, 0f,  // 0, Top Left
        0f, 1f,  // 1, Bottom Left
        1f, 0f,  // 3, Top Right
        1f, 1f,  // 2, Bottom Right
};
float texturePoints[] = {
        0f, 0f,
        0f, v,
        u, 0,
        u, v,
};

Is this caused by the Ortho call? I have tried to obtain a similar result with GLU but there is no way i can replicate this easy coordinate system and i am hardly able to draw something.

这是由直呼引起的吗?我曾尝试用GLU获得类似的结果,但我无法复制这个简单的坐标系,我也很难画出什么。

1 个解决方案

#1


0  

An orthographic projection will not give you perspective since it just projects the vertices in a straight line towards the camera. Instead of gl.glOrthof you should use GLU.gluPerspective, if it is possible to do this only while flipping the texture.

正射投影不会给你透视,因为它只是把顶点投影到相机的直线上。你应该使用GLU而不是gl.glOrthof。如果可能的话,只能在翻转纹理时进行。

http://developer.android.com/reference/android/opengl/GLU.html

http://developer.android.com/reference/android/opengl/GLU.html

#1


0  

An orthographic projection will not give you perspective since it just projects the vertices in a straight line towards the camera. Instead of gl.glOrthof you should use GLU.gluPerspective, if it is possible to do this only while flipping the texture.

正射投影不会给你透视,因为它只是把顶点投影到相机的直线上。你应该使用GLU而不是gl.glOrthof。如果可能的话,只能在翻转纹理时进行。

http://developer.android.com/reference/android/opengl/GLU.html

http://developer.android.com/reference/android/opengl/GLU.html