Ios 无法执行正确的openGL ES平移
我正在尝试创建openGL ES对象平移,并使用以下方法来实现: 首先,渲染方法:Ios 无法执行正确的openGL ES平移,ios,opengl-es,Ios,Opengl Es,我正在尝试创建openGL ES对象平移,并使用以下方法来实现: 首先,渲染方法: - (void)render:(CADisplayLink*)displayLink { glClearColor(0, 0, 0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); CC3GLMatrix *projection = [CC3GLMatrix ma
- (void)render:(CADisplayLink*)displayLink {
glClearColor(0, 0, 0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
CC3GLMatrix *projection = [CC3GLMatrix matrix];
float h = 4.0f * self.frame.size.height / self.frame.size.width;
[projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:100];
glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);
// This is where the panning is being done
[modelView populateFromTranslation:_currentPan];
_currentRotation += displayLink.duration * 50;
[modelView rotateBy:CC3VectorMake(0, 0, 0)];
glUniformMatrix4fv(_modelViewUniform, 1, 0, modelView.glMatrix);
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE,
sizeof(Vertex), 0);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (GLvoid*) (sizeof(float)* 3));
glVertexAttribPointer(_texCoordSlot, 2, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (GLvoid*) (sizeof(float) * 7));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _floorTexture);
glUniform1i(_textureUniform, 0);
glDrawElements(GL_TRIANGLES, numberOfFaces*3, GL_UNSIGNED_SHORT, 0);
[_context presentRenderbuffer:GL_RENDERBUFFER];
}
以及翻译功能:
- (void) translateXAxis:(float)x andY:(float)y
{
float dx = translationEnd.x + (x-translationStart.x);
float dy = translationEnd.y - (y-translationStart.y);
translationEnd.x = dx;
translationEnd.y = dy;
translationStart.x = x;
translationStart.y = y;
_currentPan = CC3VectorMake(-translationEnd.x*depthFactor, -translationEnd.y*depthFactor, depthFactor);
}
一切正常,对象按应有的方式移动,除了我将对象拖动到屏幕上的某个位置,将鼠标光标放置在当前在iOS模拟器上测试它的不同位置,然后再次开始拖动
在上面提到的场景中,模型只是跳回到它开始的中间位置,然后从那里继续平移。我真的在想原因。你需要在每个新动作开始时将“当前平移”初始化为零。是的,我也考虑过,但它只是总结平移,也就是说,它不会再跳到开始,但是平移中的每个阶段都是前一个平移的总和,这使得模型在很大程度上跳跃。当你开始手势时,是否初始化平移开始?它应该重新初始化为零。是的,我已经。。。它被初始化为零。问题是,一旦translateXAxis:andY:再次被调用,x和y值初始化为零,然后再次开始平移,从这些新的x和y值开始,这就是导致跳跃的原因,仍然试图解决这个问题…如果你不重新初始化_currentPan,那么我不明白为什么它会跳回零。据我从你的代码中所知,当前pan是应用的pan。它会跳回到零的唯一原因是如果_currentPan跳回到零。也许记录下_currentPan,看看它在哪里被设置为零?嗯,事实证明你是对的,但我也是:我的计算是正确的,但我真的必须在每一个新开始的手势上初始化_currentPan,所以如果你能重新编辑你的e答案,我会很高兴。谢谢你,伙计!