2011-03-04 3 views
1

OpenGL ES를 사용하여 iPhone 게임을 개발 중입니다. 나는 터치과 같은 코드를 사용하여 좌표를 사용하여 플레이어를 이동하려면 다음어떻게하면 2-D OpenGL ES 좌표로 창 좌표를 변환합니까?

- (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.075f; 

    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]; 
} 

나는 창을 사용하여 객체를 변환 할 수 없습니다 :

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

는 다음 코드를 사용하여 내 OpenGL을 ES보기 내에서 렌더링 좌표를 정규화 된 장치 좌표로 변환해야합니다. OpenGL ES 모델을 번역 할 때 사용할 수있는 좌표로 윈도우 좌표를 변환하는 방법은 무엇입니까?

답변

2

창을 OpenGL 좌표로 변환하려면 y 축을 뒤집기 만하면됩니다 (방향에 따라 x가 될 수 있음). 장치가 세로 모드에있는 경우 다음과 같은 코드를 사용 : 장치가 풍경 경우

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]; 

지금 당신을 제외하고 동일한 작업을 수행을 x 축 :

이 나를 위해 일했다
CGPoint touch_point = [[touches anyObject] locationInView:self.view]; 
touch_point = CGPointMake(480-touch_point.x, touch_point.y); 
[eaglview renderTouchAtPoint:touch_point]; 

, 그것이 당신을 위해 일하기를 바랍니다.

관련 문제