好吧,这将是一个很难解释的事情。我正在使用 AndEngine(我认为是 openGL)为 Android 制作一个非常基本的“伪 3d”赛车游戏。我不相信使用 AndEngine 真的与这个问题有任何关系,因为我直接访问 openGL 函数来完成我的绘图。
无论如何,我复制了一些代码,允许正常的 2d AndEngine 具有 3d 视角(可以找到这样的教程here http://www.andengine.org/forums/tutorials/3d-perspective-tutorial-t1790.html。这工作得很好,我也不相信这与我的问题有太大关系,但我不完全理解 openGL,所以这对我来说有点难说。以下是 onLoadEngine 中的代码(在应用程序启动时调用),用于设置具有 3D 视角的相机:
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT) {
//other methods...
private void setFrustum(GL10 pGL) {
// set field of view to 60 degrees
float fov_degrees = 60;
float fov_radians = fov_degrees / 180 * (float)Math.PI;
// set aspect ratio and distance of the screen
float aspect = this.getWidth() / (this.getHeight());
float camZ = this.getHeight()/2 / (float)Math.tan(fov_radians/2);
// set projection
GLHelper.setProjectionIdentityMatrix(pGL);
GLU.gluPerspective(pGL, fov_degrees, aspect, camZ/10, camZ*10);
// set view
GLU.gluLookAt(pGL, 0,120f, camZ, 0, 0, 0f, 0, 1, 0); // move camera back+up
pGL.glScalef(1,-1,1); // reverse y-axis
pGL.glTranslatef(-CAMERA_WIDTH/2,-CAMERA_HEIGHT/2,0); // origin at top left
}
};
然后在 onLoadScene (绘图发生的地方)中,我画了一堆图像,如下所示:
for (int n=0;n<=100;n++) {
final int k = n;
final Sprite line = new Sprite(0, 0,CAMERA_WIDTH,16f, [AndEngine texture holding road img]) {
@Override
protected void applyTranslation(GL10 pGL) {
pGL.glTranslatef(this.mX, 120f, 15f*k); //16*k causes a sliver of a space between each segment
pGL.glRotatef(90f, 1, 0, 0); //**MAY BE MY ISSUE**
}
};
scene.attachChild(line); //actually draws the image to the screen
}
Which works pretty darn well as well, except for one thing! It distorts the shit out of my images. The images are simple pngs, both matching the CAMERA_WIDTH, and both looking similar to this:
And when I draw it without the rotate line, I get this:
它有一条相当笔直的中线(说实话,我对这种方式很满意),但是你可以看到道路的边缘基本上都面向与它们应该面向的方向完全相反的方向。我想为什么不把它们翻转过来呢?我以为我必须将它们绕 x 轴旋转 180 度,但这只会让它们消失,相反我发现 90 度有效(???)。无论如何,这就是我得到的:
嗯是的。这就是我的问题所在 - 中间车道分隔线被扭曲得一塌糊涂!标记道路边缘的线排列得非常好,但无论出于何种原因,它确实扰乱了中间线。我掌握了 3d 背后的大部分数学知识,但我真的不知道该怎么做……这几乎就像图像被压缩一样,因为它是以如此锐利的角度观看的,但我真的不知道我到底如何才能解决这个问题而不简单地将其设为自上而下的视图? :S
无论如何......欢迎任何想法或指导。抱歉,这是一篇又长又复杂的文章——当我真的不知道问题出在哪里时,这让我变得很困难。
另外 - 可能值得注意的是,我对 openGL 或 3d 图形几乎没有经验,并且对深入了解它们更没有兴趣。我需要创可贴来解决这个问题!