我如何获得opengl对象iphone的中心位置?

发布于 2024-11-17 03:50:38 字数 2449 浏览 2 评论 0原文

在我的项目中..我随机移动opengl对象..如果我单击该opengl对象..我需要获取该对象的中心位置..这是我的代码..

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
 if (objShowing == YES) {

     NSLog(@"touch count %d",[touches count]);
     UITouch *touchA,*touchB;
     CGPoint pointA, pointB;

        if ([touches count] == 1)
        {
            touchA = [[touches allObjects] objectAtIndex:0];

            pointA = [touchA locationInView:self];
            pointB = [touchA previousLocationInView:self];

            float yDistance = pointA.y - pointB.y;

            float xDistance = pointA.x - pointB.x;

            GLfloat totalRotation = sqrt(xDistance*xDistance + yDistance*yDistance);

            CATransform3D temporaryMatrix = CATransform3DRotate(currentCalculatedMatrix, totalRotation * M_PI / 180.0, 
                                                                ((xDistance/totalRotation) * currentCalculatedMatrix.m11 + (yDistance/totalRotation) * currentCalculatedMatrix.m12),
                                                                ((xDistance/totalRotation) * currentCalculatedMatrix.m21 + (yDistance/totalRotation) * currentCalculatedMatrix.m22),
                                                                ((xDistance/totalRotation) * currentCalculatedMatrix.m31 + (yDistance/totalRotation) * currentCalculatedMatrix.m32));
            if ((temporaryMatrix.m11 >= -50.0) && (temporaryMatrix.m11 <= 50.0))
                currentCalculatedMatrix = temporaryMatrix;
            else
            {
            }

            [self convert3DTransform:&currentCalculatedMatrix toMatrix:matrix];

            [self adjustView];
        }
}
}



-(void)adjustView{
    NSLog(@"adjusting View");
    [EAGLContext setCurrentContext:context];

    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_NORMALIZE);

    glLoadIdentity();
    glScalef(_scale, _scale, 1.0f);
    glRotatef(-90, 0.0f, 0.0f, 1.0f);
    glRotatef(180, 0, 1, 0);

    glClearColor(0.0f, 0.0f, 0.0f, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

    //Finally load matrix
    glMultMatrixf(matrix);

    glRotatef(0, 0, 0, 1);
    glRotatef(-90, 1, 0, 0);


        drawTeapot(codesiz);


    glPopMatrix();
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];


}

In my project.. I moving the opengl object randomly.. if i click on that opengl object.. i need to get the center position of that object.. Here is my Code..

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
 if (objShowing == YES) {

     NSLog(@"touch count %d",[touches count]);
     UITouch *touchA,*touchB;
     CGPoint pointA, pointB;

        if ([touches count] == 1)
        {
            touchA = [[touches allObjects] objectAtIndex:0];

            pointA = [touchA locationInView:self];
            pointB = [touchA previousLocationInView:self];

            float yDistance = pointA.y - pointB.y;

            float xDistance = pointA.x - pointB.x;

            GLfloat totalRotation = sqrt(xDistance*xDistance + yDistance*yDistance);

            CATransform3D temporaryMatrix = CATransform3DRotate(currentCalculatedMatrix, totalRotation * M_PI / 180.0, 
                                                                ((xDistance/totalRotation) * currentCalculatedMatrix.m11 + (yDistance/totalRotation) * currentCalculatedMatrix.m12),
                                                                ((xDistance/totalRotation) * currentCalculatedMatrix.m21 + (yDistance/totalRotation) * currentCalculatedMatrix.m22),
                                                                ((xDistance/totalRotation) * currentCalculatedMatrix.m31 + (yDistance/totalRotation) * currentCalculatedMatrix.m32));
            if ((temporaryMatrix.m11 >= -50.0) && (temporaryMatrix.m11 <= 50.0))
                currentCalculatedMatrix = temporaryMatrix;
            else
            {
            }

            [self convert3DTransform:¤tCalculatedMatrix toMatrix:matrix];

            [self adjustView];
        }
}
}



-(void)adjustView{
    NSLog(@"adjusting View");
    [EAGLContext setCurrentContext:context];

    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_NORMALIZE);

    glLoadIdentity();
    glScalef(_scale, _scale, 1.0f);
    glRotatef(-90, 0.0f, 0.0f, 1.0f);
    glRotatef(180, 0, 1, 0);

    glClearColor(0.0f, 0.0f, 0.0f, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

    //Finally load matrix
    glMultMatrixf(matrix);

    glRotatef(0, 0, 0, 1);
    glRotatef(-90, 1, 0, 0);


        drawTeapot(codesiz);


    glPopMatrix();
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];


}

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

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

发布评论

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

评论(1

泅渡 2024-11-24 03:50:38

OpenGL没有“持久性”。事物被吸引,事物被遗忘。基本上OpenGL只是用来在称为“帧缓冲区”的纸上绘图的笔。仅此而已。因此,您所要求的内容对于 OpenGL 本身来说是不可能的。

您需要做的是跟踪您自己在一个单独的结构中绘制的所有内容,并在您维护的这个结构上进行此类测试和查询,就像您所询问的那样。

OpenGL has no "persistency". Things are drawn, things are forgotten. Basically OpenGL is just the pens used to draw on some paper called "framebuffer". It's nothing more. Because of that what you ask is not possible with OpenGL itself.

What you need to do is keep track of all the things you draw in a separate structure yourself and do such tests and queries, like the one you're asking about, on this structure YOU maintain.

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