OpenGL Programming

Ubuntu Korea Community Wiki
둘러보기로 이동 검색으로 이동

OpenGL이란 무엇인가?[편집]

  • Open Graphics Library의 약자.
  • 1992년 Silicon Graphics라는 회사에서 만든 2D와 3D 그래픽스와 관련된 API.하지만 그때는 저걸 제대로 지원하는 하드웨어가 집 몇채값
  • 약 250개의 Function Call로 이뤄져 있다.

어떻게 사용할 것인가?[편집]

  • 하드웨어별로 특성을 많이 타는 라이브러리고, 가상머신에서 제대로 도는 꼴을 아직 못 보고 있다.
  • 보통 Unity Engine이나 Cocos2D 등의 상위 레이어 라이브러리들을 사용하는게 낫다.

Raspberry Pi[편집]

  • Raspberry Pi에서 OpenGL을 사용하기는 비교적 쉽다.
  • Application Processor 안에 VideoCore4라는 코어가 내장되어 있다.
  • OpenGL ES 2.0까지 지원한다.
  • Raspbian 2014.1 기반으로 설명한다.
  • 각종 라이브러리는 /opt/vc/src 에 들어가 있다.
  • 일반적으로, /opt/vc의 내용을 홈 디렉토리로 가져와 변경하여 쓰는 방법도 있고, /usr/local에 라이브러리를 집어넣는 방법도 있다.
  • 나도 귀찮고 글 보는 여러분도 귀찮을테니, /opt/vc의 내용을 홈디렉토리에서 적용하는걸로 해보자.
  • 실무에서 사용될 일은 거의 없다고 봐도 된다. 상위 라이브러리들이 있으니..

<source lang="bash"> $ cp -r /opt/vc/src/* ~ </source>

  • hello_pi라는 디렉토리가 생겼다.
  • 라이브러리부터 컴파일해놓자.
  • ~/hello_pi 로 들어간다.

<source lang="bash"> $ make -C libs/ilclient $ make -C libs/vgfont </source>

  • 기본제공되는 Hello 프로그램은 다음과 같다.
  1. hello_audio - audio 예제
  2. hello_dispmanx - 디스플레이 지원 프로그램 예제
  3. hello_encode - 인코딩 예제
  4. hello_fft - 부동소수점과 쓰레드 예제
  5. hello_font - 폰트 출력 예제

파일:Pi font.png

  1. hello_jpeg - jpeg 디코더 예제
  2. hello_teapot - 간단한 3D 예제

파일:Pi teapot.png

  1. hello_triangle - 간단한 텍스쳐 폴리곤 예제

파일:Pi triangle.png

  1. hello_triangle2 - 간단한 폴리곤 예제 2

파일:Pi triangle2.png

  1. hello_video - h264 하드웨어 디코딩 예제

파일:Pi video.png

  1. hello_videocube - h264 하드웨어 디코딩을 텍스쳐로 표현하는 예제

파일:Pi videocube.png

  1. hello_world - 그냥 C 헬로월드
  • 일단 이거 다 돌려보고 소스 한번 훑어보는걸 추천한다.
  • fbgrap으로는 터미널만 입력되고, gnome-screenshot이나 scrot는 x가 뜨지 않으면 작동하지 않는다.
  • 홈의 hello_pi에 screenshot이라는 디렉토리를 만들고 아래의 내용을 넣는다.
  • 파일명은 각각 Makefile과 screenshot.c다.

<source lang="make"> OBJS=screenshot.o BIN=screenshot.bin

include ../Makefile.include </source> <source lang="c"> /* screenshot.c */ // A simple demo using dispmanx to display get screenshot

  1. include <stdio.h>
  2. include <stdlib.h>
  3. include <stdarg.h>
  4. include <assert.h>
  5. include <unistd.h>
  6. include <sys/time.h>
  1. include "bcm_host.h"

int main(void) {

   DISPMANX_DISPLAY_HANDLE_T   display;
   DISPMANX_MODEINFO_T         info;
   DISPMANX_RESOURCE_HANDLE_T  resource;
   VC_IMAGE_TYPE_T type = VC_IMAGE_RGB888;
   VC_IMAGE_TRANSFORM_T   transform = 0;
   VC_RECT_T         rect;
   void                       *image;
   uint32_t                    vc_image_ptr;
   int                   ret;
   uint32_t        screen = 0;
   bcm_host_init();
   printf("Open display[%i]...\n", screen );
   display = vc_dispmanx_display_open( screen );
   ret = vc_dispmanx_display_get_info(display, &info);
   assert(ret == 0);
   printf( "Display is %d x %d\n", info.width, info.height );
   image = calloc( 1, info.width * 3 * info.height );
   assert(image);
   resource = vc_dispmanx_resource_create( type,
                                                 info.width,
                                                 info.height,
                                                 &vc_image_ptr );
   vc_dispmanx_snapshot(display, resource, transform);
   vc_dispmanx_rect_set(&rect, 0, 0, info.width, info.height);
   vc_dispmanx_resource_read_data(resource, &rect, image, info.width*3); 
   FILE *fp = fopen("out.ppm", "wb");
   fprintf(fp, "P6\n%d %d\n255\n", info.width, info.height);
   fwrite(image, info.width*3*info.height, 1, fp);
   fclose(fp);
   ret = vc_dispmanx_resource_delete( resource );
   assert( ret == 0 );
   ret = vc_dispmanx_display_close(display );
   assert( ret == 0 );
   return 0;

} </source>

  • make하면 된다.
  • 스크린샷 프로그램이다.