扭曲OpenGL绘图精灵(图像)的伪3D透视 [英] openGL drawing distorted sprites (images) in psuedo-3d perspective

查看:1039
本文介绍了扭曲OpenGL绘图精灵(图像)的伪3D透视的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以这将是一个谎言来解释。我在做一个很基本的伪3D赛车使用AndEngine游戏为Android(反过来,openGL的 - 我认为)。我不相信使用AndEngine真的有什么关系这一问题,但因为我直接访问OpenGL函数来完成我的画。

Alright so this is going to be a doozy to explain. I'm making a very basic "pseudo-3d" racing game for Android using AndEngine (and in turn, openGL - I think). I don't believe using AndEngine really has anything to do with this problem though, because I'm directly accessing openGL functions to accomplish my drawing.

不管怎么说,我复制pasta'd一些code,允许正常的2D AndEngine有一个3D透视图(教程等可以发现的这里。这工作pretty的好,我也不相信这有很多工作要做,我的问题,但我不完全理解的openGL所以这是一个有点难受,说这里有一个从onLoadEngine(应用程序启动时调用),设置了一个3D透视相机code:

Anyways, I copy-pasta'd some code that allowed the normally 2d AndEngine to have a 3d perspective (tutorial for such can be found here. This works pretty well, and I also don't believe this has much to do with my problem, but I don't fully understand openGL so it's a little hard for me to say. Here's the code from the onLoadEngine (called when app starts) that sets up the camera with a 3d perspective:

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(其中,图中发生),我画了一堆我的图像,像这样:

Then later in the onLoadScene (where the drawing takes place), I draw a bunch of my images like so:

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           
}

其中一期工程pretty的织补以及好,除了一件事!它扭曲了狗屎了我的图像。这些图像是简单的PNG,既符合CAMERA_WIDTH,都在寻找与此类似:

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:

其中有一个体面的直线中间线(TBH我很高兴与他们这样),但你可以看到路的边缘都面临着基本上他们应该面对完全相反的方式。我想为什么不翻呢?我以为我会旋转它们绕X轴180度,而是说只是让他们消失,而是我发现,90度的作品(???)。不管怎么说,这是我得到:

Which has a decently straight middle line (tbh I'd be happy with them this way), but then you can see the edges of the road are all facing basically the exact opposite way they should be facing. I figured why not just flip them? I thought I would have to rotate them 180 degrees around the x axis, but instead that just makes them disappear, and instead I found that 90 degrees works (???). Anyways, Here's what I get:

所以呀。这里就是我的问题所在 - 中间车道分隔被扭曲的废话!标志着马路边上的线排队奇妙,但因为种种原因,它真的与中间线搞乱。我得到的大部分3D背后的数学,但我真的不知道该怎么利用这个......这几乎就像图像被COM pressed,因为它被认为在这样一个尖锐的角度,但我不吨真的知道我到底能解决而不只是使其成为一个自上而下的看法如何? :•

So yeah. Here's where my problem lies - the middle lane divider is distorted as crap!! The lines marking the edge of the road line up wonderfully, but for whatever reason its really messing with that middle line. I get most of the math behind the 3d, but I really don't know what to make of this...it's almost like the image is being compressed because its being viewed at such a sharp angle, but I don't really know how the hell I could solve that without simply making it a top-down view? :S

反正...任何意见或指导意见是值得欢迎的。很抱歉,这是如此漫长而曲折后 - 这使得它很难,当我真的不知道问题出在哪里

Anyways... any ideas or guidance is welcome. Sorry this is such a long and convoluted post - it makes it hard when I really have no idea where the problem lies.

此外 - 这可能是值得一提的,我很少有与OpenGL或3D图形的经验,并在学习很多关于他们的深度甚至不感兴趣。我需要一个创可贴这个问题!

Also - It might be worth noting I have little to no experience with openGL or 3d graphics, and have even less interest in learning much about them in depth. I need a band-aid to this problem!

推荐答案

行,所以我找到了解决办法!原来还有这​​个所谓的小问题透视校正纹理,这基本上是一个需要在OpenGL中被打开,使其不歪斜的图像时,正在绘制的角度来看,他们的标志。

Ok so I found the solution! Turns out there is this little issue called Perspective Correct Texturing, that is basically just a flag that needs to be turned on in OpenGL to make it not skew images when they are being drawn with perspective.

在我的相机初始化code,我说这行的方法setFrustum(GL10 PGL):

Inside my Camera initializing code, I added this line to the method setFrustum(GL10 pGL):

pGL.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);

这是该基本上解决了这个问题。线条直如初现在:)我发现了这一点,在其它计算器帖子里这样的回答居然是不是有什么提问者想要的,但它只是碰巧为我工作:D,导致我的答案线程可以找到<一href="http://stackoverflow.com/questions/5424078/perspective-correction-of-texture-coordinates-in-3d">here.

That this essentially solved the problem. Lines are as straight as ever now :) I found this out on another stackoverflow post where this answer actually wasn't what the asker wanted, but it just so happened to work for me :D The thread that led me to the answer can be found here.

这篇关于扭曲OpenGL绘图精灵(图像)的伪3D透视的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆