开发者

How do I convert window coordinates to 2-D OpenGL ES coordinates?

I am developing an iPhone game using OpenGL ES. I want to move the player using touch coordinates using code like the following:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   CGPoint point=[[touches anyObject]locationInView:self.view];
   [eaglView render:point.x];
}// end 

then rendering within my OpenGL ES view using the following code:

- (void) render:(float )delta
{

    if(initialized==0) {
        [self initOpenGL];
    }

    static const GLfloat squareVertices[] = {
        -0.5f, -0.33f,
        0.5f, -0.33f,
        -0.5f,  0.33f,
        0.5f,  0.33f,
    };

    static const GLubyte squareColors[] = {
        255, 255,   0, 255,
        0,   255, 255, 255,
        0,     0,   0,   0,
        255,   0, 255, 255,
    };

    float transY = delta;

    // This application only creates a single context which is already set current at this point.
    // This call is redundant, but needed if dealing with multiple contexts.
    [EAGLContext setCurrentContext:context];

    // This application only creates a single default framebuffer which is already bound at this point.
    // This call is redundant, but needed if dealing with multiple framebuffers.
    //glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
    glViewport(0, 0, backingWidth, backingHeight);

    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    // Use shader program
    //glUseProgram(program);

    // Update uniform value
    //glUniform1f(uniforms[UNIFORM_TRANSLATE], (GLfloat)transY);
    //transY += 0.0开发者_JAVA百科75f; 

    glVertexPointer(2, GL_FLOAT, 0, squareVertices); 
    glEnableClientState(GL_VERTEX_ARRAY);
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
    glTranslatef(0.0f, (GLfloat)(sinf(transY)/2.0f), 0.0f);
    //transY += 0.0001f;

    glEnableClientState(GL_COLOR_ARRAY);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);
    // Update attribute values

    // Validate program before drawing. This is a good check, but only really necessary in a debug build.
    // DEBUG macro must be defined in your debug configurations if that's not already the case.
    // Draw

    // This application only creates a single color renderbuffer which is already bound at this point.
    // This call is redundant, but needed if dealing with multiple renderbuffers.
    // glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES,renderBuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

I can't translate the object using window coordinates, so I have to convert it into normalized device coordinates. How would I go about converting from window coordinates to ones that I could use for translating my OpenGL ES model?


To convert from window to openGL coordinates you just need to flip the y axis (could be x depending on your orientation). If your device is in portrait mode you use the following code:

CGPoint touch_point = [[touches anyObject] locationInView:self.view];
touch_point = CGPointMake(touch_point.x, 480-touch_point.y); //Or you can put touch_point.y = 480-touch_point.y
[eaglview renderTouchAtPoint:touch_point];

Now if your device is in landscape you do the same except for the x axis:

CGPoint touch_point = [[touches anyObject] locationInView:self.view];
touch_point = CGPointMake(480-touch_point.x, touch_point.y);
[eaglview renderTouchAtPoint:touch_point];

This has worked for me, hope it works for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜