Interesting/iPhone | Posted by hyena0 2010. 1. 19. 22:00

[iPhone] OpenGL 프로그래밍..2


  

  [iPhone] OpenGL 프로그래밍..2


  오랜만에 아이폰 포스팅을 하게 되었습니다.

  그간 작업하던 것을 마무리 하느라 어쩔 수 없었군요.

  이전 포스팅에서 참고하고 있던 simon maurice 의 블로그는 계정이

  만료되었는지 더이상 접근하기 어렵습니다.

  참고할 만한 사이트가 없어진게 좀 아쉬운 부분이지요.

  이번 포스팅에서는 GLGravity 프로젝트를 기준으로 설명해 보겠습니다.

  이 프로젝트 파일은 구글 개발자 사이트에서 다운로드 받을 수 있습니다.

  GLGravityAppDelegate.h
  GLGravityAppDelegate.m
  GLGravityView.h
  GLGravityView.m

  네 개의 클래스로 구성되는 이 프로젝트는 가속도 센서 값을 받아서 
 
  주전자 모양의 3차원 그래픽을 아이폰을 회전할때마다 중력반대방향으로

  세워놓는 구성을 가집니다. 물론 시뮬레이터일 경우는 빙글빙글 회전만 합니다.



  GLGravityAppDelegate 클래스는 센서값을 주기적으로 가져오도록 설정되어 있고,

  GLGravityView 클래스에서 다루려고 하는 3차원 도형을 그리게 됩니다.

  기존에 다루고 있는 tutorial 에서는 대부분 정육면체를 보여주기 때문에 이해가 가지 않는

  부분도 존재합니다. 물론 정육면체부터 이해하고 넘어가는게 순서겠지요.

  어쨌건, 해당 파일을 보면 다음의 메소드를 가집니다.

  - (id)initWithCoder:(NSCoder*)coder 
  - (void)setupView
  - (void)drawView
  - (void)layoutSubviews
 - (BOOL)createFramebuffer
 - (void)destroyFramebuffer 
  - (NSInteger) animationFrameInterval
 - (void) setAnimationFrameInterval:(NSInteger)frameInterval
 - (void) startAnimation
 - (void)stopAnimation
 - (void)dealloc

  이 메소드들 중에서 다뤄볼 부분은 drawView 입니다. 경우에 따라서는 다른 메소드를 수정할 수 있겠지만

  현재 3차원 도형을 다루는 부분을 볼 것이므로 이 메소드만 보겠습니다.

  해당 메소드를 그대로 옮겨보면 아래와 같습니다.

// Updates the OpenGL view

- (void)drawView

{

// Make sure that you are drawing to the current context

[EAGLContext setCurrentContext:context];

glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

GLfloat matrix[4][4], length;

//Make sure we have a big enough acceleration vector

length = sqrtf(accel[0] * accel[0] + accel[1] * accel[1] + accel[2] * accel[2]);

//Setup model view matrix

glLoadIdentity();

glTranslatef(0.0, -0.1, -1.0);

glScalef(kTeapotScale, kTeapotScale, kTeapotScale);

if(length >= 0.1)

{

//Clear matrix to be used to rotate from the current referential to one based on the gravity vector

bzero(matrix, sizeof(matrix));

matrix[3][3] = 1.0;

//Setup first matrix column as gravity vector

matrix[0][0] = accel[0] / length;

matrix[0][1] = accel[1] / length;

matrix[0][2] = accel[2] / length;

//Setup second matrix column as an arbitrary vector in the plane perpendicular to the gravity vector {Gx, Gy, Gz} defined by by the equation "Gx * x + Gy * y + Gz * z = 0" in which we arbitrarily set x=0 and y=1

matrix[1][0] = 0.0;

matrix[1][1] = 1.0;

matrix[1][2] = -accel[1] / accel[2];

length = sqrtf(matrix[1][0] * matrix[1][0] + matrix[1][1] * matrix[1][1] + matrix[1][2] * matrix[1][2]);

matrix[1][0] /= length;

matrix[1][1] /= length;

matrix[1][2] /= length;

//Setup third matrix column as the cross product of the first two

matrix[2][0] = matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1];

matrix[2][1] = matrix[1][0] * matrix[0][2] - matrix[1][2] * matrix[0][0];

matrix[2][2] = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];

//Finally load matrix

glMultMatrixf((GLfloat*)matrix);

// Rotate a bit more so that its where we want it.

glRotatef(90.0, 0.0, 0.0, 1.0);

}

// If we're in the simulator we'd like to do something more interesting than just sit there

// But if we're on a device, we want to just let the accelerometer do the work for us without a fallback.

#if TARGET_IPHONE_SIMULATOR

else

{

static GLfloat spinX = 0.0, spinY = 0.0;

glRotatef(spinX, 0.0, 0.0, 1.0);

glRotatef(spinY, 0.0, 1.0, 0.0);

glRotatef(90.0, 1.0, 0.0, 0.0);

spinX += 1.0;

spinY += 0.25;

}

#endif

// Draw teapot. The new_teapot_indicies array is an RLE (run-length encoded) version of the teapot_indices array in teapot.h

for(int i = 0; i < num_teapot_indices; i += new_teapot_indicies[i] + 1)

{

glDrawElements(GL_TRIANGLE_STRIP, new_teapot_indicies[i], GL_UNSIGNED_SHORT, &new_teapot_indicies[i+1]);

}

 

glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);

[context presentRenderbuffer:GL_RENDERBUFFER_OES];

}


  주석에서 설명이 되어있으므로 주요한 설명은 안해도 될 것 같습니다.


  여기서 3차원 도형을 그리기 위한 주요한 내용만 다시 보겠습니다.


  단순히 도형만 그린다면 아래와 같이 고쳐볼 수 있습니다.


- (void)drawView

{

        [EAGLContext setCurrentContext:context];

glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


//Setup model view matrix

glLoadIdentity();

glTranslatef(0.0, -0.1, -1.0);

glScalef(kTeapotScale, kTeapotScale, kTeapotScale);


// Draw teapot. The new_teapot_indicies array is an RLE (run-length encoded) version of the teapot_indices array in teapot.h

for(int i = 0; i < num_teapot_indices; i += new_teapot_indicies[i] + 1)

{

glDrawElements(GL_TRIANGLE_STRIP, new_teapot_indicies[i], GL_UNSIGNED_SHORT, &new_teapot_indicies[i+1]);

}

 

glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);

[context presentRenderbuffer:GL_RENDERBUFFER_OES];

}


  위에서 glDrawElements 함수가 도형정보를 그리는 부분입니다. 


  주석에서처럼 teapot.h 파일에 num_teapot_indices, new_teapot_indices[i] 가 정의되어 있지요.


  이 부분이 어떻게 정의되는 지는 다음 포스트에서 봅시다.



 [Next] OpenGL 프로그래밍..3