gpt4 book ai didi

ios - 旋转时 OpenGL View 消失

转载 作者:行者123 更新时间:2023-12-02 08:55:31 27 4
gpt4 key购买 nike

我正在使用 OpenGL 2.0 绘制一个矩形。最初,视口(viewport)是这样的,我从上面看,我可以看到我预期的矩形。然后我开始绕 x 轴旋转这个矩形。当旋转角度等于-90度(如果向另一个方向旋转则为+90度)时,矩形消失。当我旋转超过 90 度/-90 度时,我期望看到矩形的底面,但 View 消失了。当上表面即将显示时,它会重新出现,总旋转角度为 -270 度(或 +270 度)。

如何确保我可以一直看到矩形(旋转时上表面和下表面都必须可见)?

这是相关的代码:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
if ([touches count] == 1) {
CGPoint currLoc = [touch locationInView:self];
CGPoint lastLoc = [touch previousLocationInView:self];
CGPoint diff = CGPointMake(lastLoc.x - currLoc.x, lastLoc.y - currLoc.y);

rotX = -1 * GLKMathDegreesToRadians(diff.y / 2.0);
rotY = -1 * GLKMathDegreesToRadians(diff.x / 2.0);

totalRotationX += ((rotX * 180.0f)/3.141592f);

NSLog(@"rotX: %f, rotY: %f, totalRotationX: %f", rotX, rotY, totalRotationX);

//rotate around x axis
GLKVector3 xAxis = GLKMatrix4MultiplyVector3(GLKMatrix4Invert(_rotMatrix, &isInvertible), GLKVector3Make(1, 0, 0));
_rotMatrix = GLKMatrix4Rotate(_rotMatrix, rotX, xAxis.v[0], 0, 0);
}
}


-(void)update{
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0, 0, -6.0f);
modelViewMatrix = GLKMatrix4Multiply(modelViewMatrix, _rotMatrix);
self.effect.transform.modelviewMatrix = modelViewMatrix;

float aspect = fabsf(self.bounds.size.width / self.bounds.size.height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0, 10.0f);
self.effect.transform.projectionMatrix = projectionMatrix;
}

- (void)setupGL {

NSLog(@"setupGL");
isInvertible = YES;
totalRotationX = 0;
[EAGLContext setCurrentContext:self.context];
glEnable(GL_CULL_FACE);

self.effect = [[GLKBaseEffect alloc] init];

// New lines
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);

// Old stuff
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

glGenBuffers(1, &_indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);

glViewport(0, 0, self.frame.size.width, self.frame.size.height);

// New lines (were previously in draw)
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color));

_rotMatrix = GLKMatrix4Identity;

// New line
glBindVertexArrayOES(0);

initialized = 1;
}

我是 OpenGL 的新手,我正在使用 GLKit 和 OpenGL 2.0

谢谢。

最佳答案

导致 OpenGL 无法渲染的原因有很多。在这种情况下,它是背面剔除(请参阅对该问题的评论)。背面剔除非常有用,因为它可以忽略背对相机的三角形并节省一些光栅化/片段处理时间。由于许多网格/对象都是防水的,并且您永远不想看到内部,因此实际上需要两侧着色的情况并不常见。此功能从定义三角形的前/后开始。这是按照给定顶点的顺序(有时称为缠绕方向)完成的。 glFrontFace 选择定义向前的顺时针/逆时针方向,glCullFace 选择剔除前面或后面(我想有些人可能会认为同时拥有这两个方向没有多大意义),最后启用/禁用:

glEnable(GL_CULL_FACE); //discards triangles facing away from the camera
glDisable(GL_CULL_FACE); //default, two-sided rendering

我检查几何图形不可见的其他一些内容包括...

  • 几何颜色与背景颜色相同吗?在这里选择非黑/白背景会很方便。
  • 几何图形是否实际在查看体积内绘制。放入一个简单的对象(立即模式有帮助),也许可以使用身份投影/模型 View 来排除它们。
  • 观看音量是否正确。近/远平面距离太远(导致深度冲突)或近平面 0.0f 是常见问题。此外,当切换到投影矩阵时,在 Z=0 平面上绘制的任何内容都将不再可见。
  • 是否启用混合并且一切都是透明的。
  • 深度缓冲区是否未被清除并导致后续帧被丢弃。
  • 在固定管道渲染中,从前一帧继承的 glTranslate/glRotate 变换会导致对象射向远处。始终在显示函数的顶部保留 glLoadIdentity
  • 渲染循环结构是否正确 - 清除/绘制/交换缓冲区
  • 当然还有更多 - 几何着色器不输出任何内容,顶点着色器将顶点转换到同一位置(因此它们都是退化的),片段着色器在不应该调用时调用丢弃,VBO 绑定(bind)/索引问题等。总帐错误是必须的,但不能捕获所有错误。

关于ios - 旋转时 OpenGL View 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19615207/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com