iPhone OpenGL模板是作弊吗?

发布于 2024-09-04 11:12:48 字数 516 浏览 4 评论 0原文

XCode 的 OpenGL 模板似乎在解决这个“拉伸”视口问题,我在过去 3 个小时里一直试图理解这一问题。

在iphone“OpenGL ES应用程序”模板中,屏幕上上下弹跳的彩色正方形根本不是真正的正方形!

ES1Renderer.m(也是 ES2 文件)

static const GLfloat squareVertices[] = {
    -0.5f,  -0.33f,
     0.5f,  -0.33f,
    -0.5f,   0.33f,
     0.5f,   0.33f,
};

但由于非方形视口的拉伸/挤压效果,它在设备/模拟器上看起来是方形的。我尝试通过摆弄 glFrustumf() 来修复它,但这似乎并没有改变纵横比。

当我以 1:1 的宽度:高度提供 glViewport() 时,我能够得到看起来不错(未拉伸)的东西。但这似乎不是答案,因为它偏移了视口位置。

纠正这种拉伸的正确方法是什么?为什么 XCode 不这样做?

XCode's OpenGL template seems to be cheating to solve this "stretched" viewport problem I've been trying to understand for the last 3 hours.

In the iphone "OpenGL ES Application" template, the colorful square that bounces up and down on the screen is not really a square at all!

ES1Renderer.m (the ES2 file as well)

static const GLfloat squareVertices[] = {
    -0.5f,  -0.33f,
     0.5f,  -0.33f,
    -0.5f,   0.33f,
     0.5f,   0.33f,
};

But it comes out looking square on the device/simulator due to the stretching/squashing effect of a non-square viewport. I tried to fix it by fiddling with glFrustumf() but that doesn't seem to change the aspect ratio.

I was able to get things looking good (not-stretched) when I fed glViewport() with a 1:1 widht:height.. But this doesn't seem like the answer because it offsets the viewport placement.

What's the right way to correct for this stretching and why doesn't XCode do it that way?

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

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

发布评论

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

评论(1

怀念你的温柔 2024-09-11 11:12:48

我认为答案是:

XCode 是这样作弊的,以避免我们对设置视锥体所需的额外代码感到困惑。

这是我用来设置的代码,它修复了我遇到的挤压/拉伸问题:

const GLfloat zNear = 0.0001, zFar = 1000.0, fieldOfView = 45.0; 
GLfloat size;

size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);

aspectRatio = (GLfloat) backingWidth / backingHeight;


glFrustumf(-size,//left
           size,//right
           (GLfloat)-size / aspectRatio,//bottom 
           (GLfloat)size / aspectRatio,//top 
           zNear,//zNear 
           zFar);//zFar     

glViewport(0, 0, backingWidth, backingHeight); 

希望对某人有帮助。

I think the answer is:

XCode is cheating like this to avoid confusing us with the extra code required to setup a frustum.

Here's the code I'm using to set one up, and it fixed the squashing/stretching I was getting:

const GLfloat zNear = 0.0001, zFar = 1000.0, fieldOfView = 45.0; 
GLfloat size;

size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);

aspectRatio = (GLfloat) backingWidth / backingHeight;


glFrustumf(-size,//left
           size,//right
           (GLfloat)-size / aspectRatio,//bottom 
           (GLfloat)size / aspectRatio,//top 
           zNear,//zNear 
           zFar);//zFar     

glViewport(0, 0, backingWidth, backingHeight); 

Hope that helps someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文