Skip to content
Search

OpenGL ES

lastmod 2021-05-08created 2014-03-1711 edits

OpenGL ES (OpenGL for Embedded Systems) is a subset of OpenGL, the 3D computer graphics API defined by the Khronos Group, and is an API for embedded devices such as mobile phones and PDAs.

OpenGL ES 1.1 & 2.0

There are two main versions of OpenGL ES.

OpenGL ES 1.1 is compatible with OpenGL 1.5 and provides an API for fixed function hardware. Version 1.1 drops primitives commonly used in traditional OpenGL that hurt code size and performance, such as glBegin/glEnd, color index mode, and GL_QUAD/GL_POLYGON, and instead uses APIs such as Vertex Array and Vertex Buffer Object (VBO), while faithfully providing features such as full transform, lighting, multitexturing, fixed point, and floating point.

OpenGL ES 2.0 is compatible with OpenGL 2.0 and provides an API for programmable hardware using shaders. It removes the fixed function pipeline and uses vertex shaders and fragment shaders, and for this it supports a shading language called GLSL ES.

Because OpenGL ES 1.1 and 2.0 are not compatible with each other, code written for 1.1 must be rewritten to run on 2.0, and the same goes for running 2.0 code on 1.1.

Implementing gluPerspective()

For perspective projection, the gluPerspective function from OpenGL is missing; only the glFrustumf function exists. So you can implement it as follows.

//------------------------------------------------------------------------------
// gluPerspective 함수 구현
//
// opengles 에는 gluPerspective 함수가 없고, glFrustumf 함수만 있다.
// gluPerspective 함수가 더 직관적이고, 사용하기 편하므로,
// 
// iFovy : 시야의 각도. (0.0 ~ 180.0)
//         이 값이 크면 넓게 보는것임 (오브젝트들이 작게 보임)
//         이 값이 작으면 좁게 보는것임 (오브젝트들이 크게 보임.
//                                                   각도 밖의 오브젝트는 시야에서 벗어나서 안보임)
// iAspect : 시야의 종횡비 (w / h)
// iNear : 시점에서 관측공간 (=절두체) 앞부분의 거리 (앞쪽 투상평면까지의 거리)
// iFar : 시점에서 관측공간 (=절두체) 뒷부분의 거리 (뒤쪽 투상평면까지의 거리)
//------------------------------------------------------------------------------
- (void)setPerspectiveWithFovy:(GLfloat)iFovy withAspect:(GLfloat)iAspect withNear:(GLfloat)iNear withFar:(GLfloat)iFar
{
  // 시야의 종횡비
  if(iAspect == 0.0) iAspect = self.bounds.size.width / self.bounds.size.height;

  GLfloat iLeftRight = iNear * tanf(DEGREES_TO_RADIANS(iFovy) / 2.0);
  GLfloat iTopBottom = iLeftRight / iAspect;
  glFrustumf(-iLeftRight, iLeftRight, -iTopBottom, iTopBottom, iNear, iFar);
  glViewport(0, 0, self.bounds.size.width, self.bounds.size.height);
}

How to set the resolution

Resolution-related settings made in onSurfaceChanged() can be done as follows.

void onSurfaceChanged(int width, int height)
{
    glMatrixMode(GL_PROJECTION); // 이 명령뒤에 나오는 행렬과 관련된 것은 투영행렬에 영향을 줍니다.
    glLoadIdentity(); // 행렬을 초기화시킵니다.

    glOrthof(0.0f, 480, 800, 0.0f, 1.0f, -1.0f); // 우리는 2D를 만들것이므로 직교투영으로 화면을 지정합니다. 개발자가 원하는 영역을 지정해줍니다.

    glMatrixMode(GL_MODELVIEW); // 이 명령뒤에 나오는 행렬과 관련된 것은 모델뷰행렬에 영향을 줍니다.
    glLoadIdentity(); // 초기화.

    glViewport(0, 0, width, height); // 실제 기기에 보여질 화면영역을 지정해줍니다.
}

What matters here are glOrthof() and glViewport(). First, set the working area size you want in glOrthof(). "I'll base everything on a 480800 screen." Then just pass 480, 800 as above. If you pass the actual device resolution to glViewport(), you'll see images drawn in the right place at every resolution. On a 1280720 device, it stretches 480800 to fit the 1280720 resolution, and on a smaller one it shrinks it when drawing.

See also

Favorite site

References


  1. IOS_OpenGL_ES_Tutorial_13_-_Perspective_projection.pdf ↩

  2. Egl_opengles2_tutorials.pdf ↩

author Zer0 sitemap