ホームに戻る
 NDK テクスチャ(透過を含む)

// 四角形の2×2のテクスチャ(透過含む)を貼るサンプル

#include <jni.h>
#include <errno.h>

#include <EGL/egl.h>
#include <GLES/gl.h>

#include <android_native_app_glue.h>

struct engine{
  struct android_app* app;

  int32_t x; // タッチしたX座標を記録
  int32_t y; // タッチしたY座標を記録

  int animating;
  EGLDisplay display;
  EGLSurface surface;
  EGLContext context;
  int32_t width;
  int32_t height;
};

// 四角形の座標
GLfloat quadBuf[] ={
  -1.0,  1.0, 0.0,
  -1.0, -1.0, 0.0,
   1.0, -1.0, 0.0,
   1.0,  1.0, 0.0
};

GLuint textures;

unsigned char texBuf[2][2][4] = {
  0xff, 0x00, 0x00, 0xff,
  0x00, 0x00, 0xff, 0xff,
  0x00, 0x00, 0xff, 0xff,
  0x00, 0x00, 0xff, 0x00
};

float quadTexture[2 * 4] = {
  0.0f, 0.0f,
  0.0f, 1.0f,
  1.0f, 1.0f,
  1.0f, 0.0f
};

void initTexture(void){
  glGenTextures(1, &textures);
  glBindTexture(GL_TEXTURE_2D, textures);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, texBuf);
}

static int engine_init_display(struct engine* engine){
  EGLint attribs[] = {
    EGL_SURFACE_TYPE,
    EGL_WINDOW_BIT,
    EGL_BLUE_SIZE, 8,
    EGL_GREEN_SIZE, 8,
    EGL_RED_SIZE, 8,
    EGL_NONE
  };

  EGLint w, h, format;
  EGLint numConfigs;
  EGLConfig config;
  EGLSurface surface;
  EGLContext context;

  EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);

  eglInitialize(display, 0, 0);
  eglChooseConfig(display, attribs, &config, 1, &numConfigs);
  eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);

  ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format);
  surface = eglCreateWindowSurface(display, config, engine->app->window, NULL);
  context = eglCreateContext(display, config, NULL, NULL);

  if(eglMakeCurrent(display, surface, surface, context) == EGL_FALSE){
    return -1;
  }

  eglQuerySurface(display, surface, EGL_WIDTH, &w);
  eglQuerySurface(display, surface, EGL_HEIGHT, &h);

  engine->display = display;
  engine->context = context;
  engine->surface = surface;
  engine->width = w;
  engine->height = h;

  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);

  glViewport(0, 0, w, h);

  glMatrixMode(GL_PROJECTION);

  glLoadIdentity();

  glFrustumf(-1.33, 1.33, -1.0, 1.0, 1.0, 5.0);

  glMatrixMode(GL_MODELVIEW);

  glEnable(GL_BLEND);

  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  initTexture();

  glEnableClientState(GL_VERTEX_ARRAY);
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);

  return 0;
}

static void engine_draw_frame(struct engine* engine){
  if(engine->display == NULL){
    return;
  }

  glClearColor(0.0, 0.0, 0.0, 1);
  glClear(GL_COLOR_BUFFER_BIT);

  glEnable(GL_TEXTURE_2D);
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D, textures);

  glLoadIdentity();

  glTranslatef(0.0, 0.0, -3.0);

  glRotatef(360.0 * ((float)engine->x) / engine->width, 0.0, 0.0, 1.0);

  glPushMatrix();

  glVertexPointer(3, GL_FLOAT, 0, quadBuf);
  glTexCoordPointer(2, GL_FLOAT, 0, quadTexture);
  glDrawArrays(GL_TRIANGLE_FAN, 0, 4); // GL_QUADS は使えないので注意

  glPopMatrix();

  eglSwapBuffers(engine->display, engine->surface);
}

static void engine_term_display(struct engine* engine){
  if(engine->display != EGL_NO_DISPLAY){
    eglMakeCurrent(engine->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
    if(engine->context != EGL_NO_CONTEXT){
      eglDestroyContext(engine->display, engine->context);
    }
    if(engine->surface != EGL_NO_SURFACE){
      eglDestroySurface(engine->display, engine->surface);
    }
    eglTerminate(engine->display);
  }
  engine->animating = 0;
  engine->display = EGL_NO_DISPLAY;
  engine->context = EGL_NO_CONTEXT;
  engine->surface = EGL_NO_SURFACE;
}

// 入力に対する処理
static int32_t engine_handle_input(struct android_app* app, AInputEvent* event) {
  struct engine* engine = (struct engine*)app->userData;
  if(AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION){
    engine->animating = 1;
    engine->x = AMotionEvent_getX(event, 0);
    engine->y = AMotionEvent_getY(event, 0);
    return 1;
  }
  return 0;
}

// 入力以外のイベントに対する処理
static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
  struct engine* engine = (struct engine*)app->userData;
  switch(cmd){
    case APP_CMD_INIT_WINDOW:
      if(engine->app->window != NULL){
        engine_init_display(engine);
        engine_draw_frame(engine);
      }
      break;
    case APP_CMD_TERM_WINDOW:
      engine_term_display(engine);
      break;
    default:
      break;
  }
}

void android_main(struct android_app* state){
  struct engine engine;

  app_dummy();

  memset(&engine, 0, sizeof(engine));
  state->userData = &engine;
  state->onAppCmd = engine_handle_cmd;
  state->onInputEvent = engine_handle_input;
  engine.app = state;

  while(1){
    int ident;
    int events;
    struct android_poll_source* source;

    while((ident=ALooper_pollAll(engine.animating ? 0 : -1, NULL, &events, (void**)&source)) >= 0){
      if(source != NULL){
        source->process(state, source);
      }

      if(state->destroyRequested != 0){
        engine_term_display(&engine);
        return;
      }
    }

    if(engine.animating){
      engine_draw_frame(&engine);
    }
  }
}

inserted by FC2 system