发现2个错误:
1 - 为什么我的纹理在旋转后得到奇怪的位置
问题是纹理只有在window_width = 768,window_height = 1004.0 从纵向模式开始时才能正常显示。有了这样的尺寸,它在旋转后可以正常工作。当我设置取决于 window_width = self.frame.size.width 和 window_height = self.frame.size.height 并以横向模式旋转或从它开始时 - 纹理显示的宽度和高度比屏幕尺寸大。用“bounds”替换“frame”并不能解决问题。
我通过在覆盖的设置框架方法中更新上下文来解决它
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
_openglViewer.setWindowSize(frame.size.width, frame.size.height);
[_context renderbufferStorage:GL_RENDERBUFFER
fromDrawable:(CAEAGLLayer *)self.layer];
_openglViewer.setupVBOs();
_openglViewer.compileShaders();
}
2 - 为什么纹理显示没有节省比率
感谢 Trax,我听取了您的建议并重新计算了我的投影矩阵。
void calculateRatio (GLsizei &width, GLsizei &height, GLfloat &w, GLfloat &h)
{
// The bigger side returns 1.0,
// smaller - percentage value of bigger side
GLfloat ratio = (GLfloat)width/(GLfloat)height;
if(ratio < 1.0) {
w = ratio;
h = 1.0;
} else {
w = 1.0;
h = 1.0/ratio;
}
}
我将此方法调用了 2 次:1)用于获取纹理比率 2)用于获取窗口比率。在我得到这个值后,我检查
if(winW > winH) {
w = texW*winH;
h = texH;
} else {
w = texW;
h = texH*winW;
}
现在我有了最终比例。我的任务是 - 以调整到屏幕尺寸的最大可能尺寸渲染纹理。这就是为什么我将这个比率标准化 normalizeSizes(w,h);
void normalizeSizes(GLfloat &w, GLfloat &h)
{
// the smaller size get 1.0 value,
// bigger - times it is bigger than smaller;
bool isWider = false;
GLfloat maxSize;
if(w > h) {
isWider = true;
maxSize = w;
} else {
maxSize = h;
}
if(maxSize != 1.0) {
if(isWider) {
w = 1.0;
h = h/maxSize;
} else {
h = 1.0;
w = w/maxSize ;
}
}
w = 1.0/w;
h = 1.0/h;
}
获得归一化值后,我生成投影矩阵。
projectionMatrix = tmp.genOrthoProjMatrix(-1.0, 1.0, -w, w, -h, h);
//genOrthoProjMatrix(float near ,float far ,float left, float right, float bottom, float top)
// First Column
res.p[0][0] = 2.0 / (right - left);
res.p[1][0] = 0.0;
res.p[2][0] = 0.0;
res.p[3][0] = 0.0;
// Second Column
res.p[0][1] = 0.0;
res.p[1][1] = 2.0 / (top - bottom);
res.p[2][1] = 0.0;
res.p[3][1] = 0.0;
// Third Column
res.p[0][2] = 0.0;
res.p[1][2] = 0.0;
res.p[2][2] = -2.0 / (far - near);
res.p[3][2] = 0.0;
// Fourth Column
res.p[0][3] = -(right + left) / (right - left);
res.p[1][3] = -(top + bottom) / (top - bottom);
res.p[2][3] = -(far + near) / (far - near);
res.p[3][3] = 1;
现在一切正常。感谢帮助!