OSDN Git Service

Theta用にOpenGLのビューを用意したけど、無効のままにする。
[gokigen/A01c.git] / wear / src / main / java / jp / sfjp / gokigen / a01c / liveview / glview / GokigenGLUtilities.kt
1 package jp.sfjp.gokigen.a01c.liveview.glview
2
3 import android.content.Context
4 import android.graphics.Bitmap
5 import android.graphics.BitmapFactory
6 import android.opengl.GLUtils
7 import java.io.IOException
8 import java.io.InputStream
9 import javax.microedition.khronos.opengles.GL10
10
11 class GokigenGLUtilities(private val mContext: Context)
12 {
13
14     /**
15      * テクスチャの準備
16      *
17      *
18      */
19     fun prepareTexture(gl: GL10?, resourceId: Int): Int
20     {
21         try
22         {
23             /*
24              * Create our texture. This has to be done each time the
25              * surface is created.
26              */
27             val textures = IntArray(1)
28             gl?.glGenTextures(1, textures, 0)
29             gl?.glBindTexture(GL10.GL_TEXTURE_2D, textures[0])
30             gl?.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST.toFloat())
31             gl?.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR.toFloat())
32             gl?.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE.toFloat())
33             gl?.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE.toFloat())
34             gl?.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE.toFloat())
35
36             /////////////////////////////////////////////////////////////////////
37             val inputStream: InputStream = mContext.resources.openRawResource(resourceId)
38             val bitmap: Bitmap
39             bitmap = try
40             {
41                 BitmapFactory.decodeStream(inputStream)
42             }
43             finally
44             {
45                 try
46                 {
47                     inputStream.close()
48                 }
49                 catch (e: IOException)
50                 {
51                     e.printStackTrace()
52                 }
53             }
54             try
55             {
56                 GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0)
57                 bitmap.recycle()
58             }
59             catch (ex: Exception)
60             {
61                 ex.printStackTrace()
62             }
63             return textures[0]
64         }
65         catch (e : Exception)
66         {
67             e.printStackTrace()
68             return -1
69         }
70     }
71 }