无法执行正确的openGL ES平移

时间:2014-02-27 16:10:36

标签: 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 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模拟器上测试)并开始拖动再一次。

在上面提到的场景中,模型只是直接跳回到它开始的中间位置,然后从那里恢复平移。我真的想弄明白为什么。

1 个答案:

答案 0 :(得分:0)

您需要在每个新手势开始时将_currentPan初始化为零。