使用 2D iPhone OpenGL ES 1.1 应用程序,如何让深度缓冲区适用于纹理?

发布于 2024-12-17 03:54:48 字数 727 浏览 4 评论 0原文

我正在制作 2D 视频游戏。现在我没有那么多精灵,并且没有深度缓冲区的纹理可以正常工作。但是,当我扩展到多个纹理时,我想使用深度缓冲区,这样我就不必对同一纹理进行多次传递,并且不必根据任何深度约束来组织我的纹理。

当我尝试让深度缓冲区工作时,我只能得到具有正确清晰颜色的空白屏幕。我将在没有深度缓冲区的情况下解释我的工作设置,并列出升级到深度缓冲区时遇到的问题:

  • 现在我的顶点只有位置(x,y)和纹理(x,y)坐标。没有别的了。没有光照,没有法线,没有颜色等。我在这里要做的唯一升级是将 az 坐标添加到我的位置,这是否正确?

  • 现在我正在使用:

glOrthof(-2, 2, -3, 3, -1, 1);

这适用于没有深度缓冲区。但是当我添加深度缓冲区时,我认为我需要更改近处和远处的值。我应该把它们改成什么?

  • 现在对于我的 glTexImage2D() 我正在使用:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

当我添加深度缓冲区时,我是否必须更改这些参数中的任何一个?

  • 在调用 glClearDepthf(); 时,我应该使用在调用 glOrthof() 时使用的近值或远值之一吗?哪一个?

I'm making a 2D videogame. Right now I don't have that many sprites and one texture with no depth buffer works fine. But when I expand to multiple textures I want to use a depth buffer so that I don't have to make multiple passes over the same texture and so that I don't have to organize my textures with respect to any depth constraints.

When I try to get the depth buffer working I can only get a blank screen with the correct clear color. I'm going to explain my working setup without the depth buffer and list questions I have for upgrading to the depth buffer:

  • Right now my vertices only have position(x,y) and texture(x,y) coords. There is nothing else. No lighting, no normals, no color, etc. Is it correct that the only upgrade I have to make here is to add a z coord to my position?

  • Right now I am using:

glOrthof(-2, 2, -3, 3, -1, 1);

this works with no depth buffer. But when I add the depth buffer I think I need to change the near and far values. What should I change them to?

  • Right now for my glTexImage2D() I am using:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

when I add the depth buffer do I have to change any of those arguments?

  • With my call to glClearDepthf();, should I be using one of the near or far values that I use in my call to glOrthof()? which one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

友欢 2024-12-24 03:54:48

由于您使用 2D 和正交,我发现拥有一个坐标与您的分辨率相匹配的视口会有所帮助,因此这将使内容更具可读性:

CGRect rect = self.view.bounds;
if (ORTHO) {
    if (highRes && (retina == 1)) {
        glOrthof(0.0, rect.size.width/2, 0.0 ,  rect.size.height/2, -1, 1000.0);
    } else {
        glOrthof(0.0, rect.size.width, 0.0 ,  rect.size.height, -1, 1000.0);    
    }
    glViewport(0, 0, rect.size.width*retina, rect.size.height*retina);
} 

请注意,即使在视网膜上,我也始终使用 320x480 坐标,这样我就可以使用相同的坐标对于这两种分辨率,0.5 会给我视网膜上的完美像素,但你也可以采用其他方法。

关于深度,我使用 -1 到 1000 深度,因此我可以绘制最多 -1000 Z。

确保正确绑定深度缓冲区,如下所示:

    // Need a depth buffer
    glGenRenderbuffersOES(1, &depthRenderbuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
    glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES,  framebufferWidth, framebufferHeight);
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);

或者您的问题可以像使用后面的深度一样简单相机和灯光或者比你的缓冲区更大,尝试使用 0 到 -1 之间的深度(例如 -0.5),使用我的 glOrthof 你可以达到 -1000;

编辑

glOrthof 中的近距和远距值指定数量(距离),而不是坐标,这在指定深度值时可能会造成混淆。
当您为远参数指定 1000 时,我们实际上是说远剪裁平面距观察者 1000 个单位,与近场相同,不幸的是指定观察者后面的剪裁平面将取负值,这会导致到混乱。

因此,当谈到绘制时间时,我们有一个距前方观看者 1000 个单位(远或进入屏幕)的剪裁平面,当低于观看平面(进入屏幕)时,就坐标 Z 而言,Z 为负,我们实际的绘图世界是在 Z = 1 和 Z = -1000 之间,-1000 是我们可以使用这些参数的最远值。

Since your working with 2D and ortho I find that it helps to have a viewport with coordinates that match your resolution, so this will keep things more readable:

CGRect rect = self.view.bounds;
if (ORTHO) {
    if (highRes && (retina == 1)) {
        glOrthof(0.0, rect.size.width/2, 0.0 ,  rect.size.height/2, -1, 1000.0);
    } else {
        glOrthof(0.0, rect.size.width, 0.0 ,  rect.size.height, -1, 1000.0);    
    }
    glViewport(0, 0, rect.size.width*retina, rect.size.height*retina);
} 

Notice that I always use 320x480 coordinates even on retina, this way I can use the same coordinates for both res, and a .5 will give me pixel perfect on retina, but you can go the other way.

Regarding depth I use a -1 to 1000 depth, so I can draw up to -1000 Z.

Make sure you're binding the depth buffer correctly, something like this:

    // Need a depth buffer
    glGenRenderbuffersOES(1, &depthRenderbuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
    glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES,  framebufferWidth, framebufferHeight);
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);

Or your problem can be as simple as using a depth that's behind your camera and lights or bigger than your buffer, try to use a depth between 0 and -1 (-0.5 for ex.), with my glOrthof you can go up to -1000;

EDIT

Values in glOrthof for near and far specify a quantity (distance), not coordinates, this can be confusing when specifying depth values.
When you specify 1000 for the far parameter, what we are actually saying is the far clipping plane is a 1000 units distant from the viewer, the same with the near field, unfortunately specifying a clipping plane behind the viewer will take negative values, which contributes to the confusion.

So when it comes drawing time we have a clipping plane that's 1000 units from the viewer in front (far or into the screen), in terms of coordinates Z is negative when bellow the viewing plane (into the screen), our actually drawing world is between Z = 1 and Z = -1000, being -1000 the farthest we can go with these parameters.

苍风燃霜 2024-12-24 03:54:48

例如,如果您不打算使用现有的库Cocos2D,那么您将不得不编写一个 自行管理深度缓冲区

  • 管理器根据它们添加到屏幕的顺序
  • 用户自定义 Z 值,以便您可以根据需要交换它们

If you arn't going to use an exisiting library lie Cocos2D for example then you will have to write a manager to manage the Depth buffer yourself based on either

  • Order that they were added to the screen
  • User Customised Z value so you can swap them around as needed
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文