/
OpenGLUtils.java
263 lines (223 loc) · 7.79 KB
/
OpenGLUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package com.tid.opengljava;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.opengl.GLUtils;
import android.util.Log;
public class OpenGLUtils {
public static FloatBuffer allocateFloatBuffer(int capacity){
ByteBuffer vbb = ByteBuffer.allocateDirect(capacity);
vbb.order(ByteOrder.nativeOrder());
return vbb.asFloatBuffer();
}
public static IntBuffer allocateInttBuffer(int capacity){
ByteBuffer vbb = ByteBuffer.allocateDirect(capacity);
vbb.order(ByteOrder.nativeOrder());
return vbb.asIntBuffer();
}
public static ShortBuffer allocateShortBuffer(int capacity){
ByteBuffer vbb = ByteBuffer.allocateDirect(capacity);
vbb.order(ByteOrder.nativeOrder());
return vbb.asShortBuffer();
}
public static void addVertex3f(FloatBuffer buffer,float x,float y,float z){
buffer.put(x);
buffer.put(y);
buffer.put(z);
}
public static void addIndex(ShortBuffer buffer,int index1,int index2,int index3){
buffer.put((short) index1);
buffer.put((short) index2);
buffer.put((short) index3);
}
public static void addCoord2f(FloatBuffer buffer,float x,float y){
buffer.put(x);
buffer.put(y);
}
public static void addColorf(FloatBuffer buffer,float r,float g,float b,float a){
buffer.put(r);
buffer.put(g);
buffer.put(b);
buffer.put(a);
}
public static FloatBuffer toFloatBufferPositionZero(float[] values) {
ByteBuffer vbb = ByteBuffer.allocateDirect(values.length*4);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer buffer=vbb.asFloatBuffer();
buffer.put(values);
buffer.position(0);
return buffer;
}
public static ShortBuffer toShortBuffer(short[] values) {
ByteBuffer vbb = ByteBuffer.allocateDirect(values.length*2);
vbb.order(ByteOrder.nativeOrder());
ShortBuffer buffer=vbb.asShortBuffer();
buffer.put(values);
buffer.position(0);
return buffer;
}
public static Bitmap loadBitmap(Context mContext,int id){
Options opt=new Options();
Bitmap bitmap= BitmapFactory.decodeResource(mContext.getResources(), id,opt);
System.out.println(bitmap.getConfig());
if(!sizeCheck(bitmap)){
throw new RuntimeException("width or height not 2x size,it make invalid error on OpenGL:"+id);
}
return bitmap;
}
private static boolean sizeCheck(Bitmap bitmap){
boolean ret=true;
int t=2;
int w=bitmap.getWidth();
while(w!=t){
if(w%t==1){
Log.e("glutils w=",w+","+t);
return false;
}
t*=2;
if(t>w){
return false;
}
}
t=2;
int h=bitmap.getHeight();
while(h!=t){
if(h%t==1){
Log.e("glutils h=",h+","+t);
return false;
}
t*=2;
if(t>h){
return false;
}
}
return ret;
}
/**
* this is for resized
* GLU.gluOrtho2D (gl,-1f, 1.0f, -1f, 1.0f);
* @param x
* @param y
* @param screenWidth
* @param screenHeight
* @return
*/
public static float[] toOpenGLCordinate(float x,float y,int screenWidth,int screenHeight){
float sx=((float)x/screenWidth)*2-1.0f;
float sy=((float)y/screenHeight)*2-1.0f;
sy*=-1;
return new float[]{sx,sy};
}
/*
* y should *=-1;
*/
public static float toOpenGLCordinate(float x,int screenWidth){
Log.i("myapp","x="+x+","+screenWidth);
float sx=((float)x/screenWidth)*2-1.0f;
return sx;
}
public static float realToVirtialValue(int x,int real,float virtial){
return virtial/real*x;
}
public static int virtualToRealvalue(float x,int real,float virtial){
//using DecimalFormat make gc
return (int) ((float)x/(virtial/real));
}
private static FloatBuffer mTextureBuffer;
private static FloatBuffer mFVertexBuffer;
private static ShortBuffer mIndexBuffer;
public static FloatBuffer getBoxTriangleTextureBuffer(){
if(mTextureBuffer==null){
mTextureBuffer=OpenGLUtils.allocateFloatBuffer(4*6*2);
OpenGLUtils.addCoord2f(mTextureBuffer,0.0f, 1.0f);
OpenGLUtils.addCoord2f(mTextureBuffer,0.0f, 0.0f);
OpenGLUtils.addCoord2f(mTextureBuffer,1.0f, 1.0f);
OpenGLUtils.addCoord2f(mTextureBuffer,1.0f, 1.0f);
OpenGLUtils.addCoord2f(mTextureBuffer,1.0f, 0.0f);
OpenGLUtils.addCoord2f(mTextureBuffer,0.0f, 0.0f);
mTextureBuffer.position(0);
}
return mTextureBuffer;
}
public static FloatBuffer getBoxTriangleFlipVerticalTextureBuffer(){
if(mTextureBuffer==null){
mTextureBuffer=OpenGLUtils.allocateFloatBuffer(4*6*2);
OpenGLUtils.addCoord2f(mTextureBuffer,0.0f, 0.0f);
OpenGLUtils.addCoord2f(mTextureBuffer,0.0f, 1.0f);
OpenGLUtils.addCoord2f(mTextureBuffer,1.0f, 0.0f);
OpenGLUtils.addCoord2f(mTextureBuffer,1.0f, 0.0f);
OpenGLUtils.addCoord2f(mTextureBuffer,1.0f, 1.0f);
OpenGLUtils.addCoord2f(mTextureBuffer,0.0f, 1.0f);
mTextureBuffer.position(0);
}
return mTextureBuffer;
}
public static FloatBuffer getBoxTextureBuffer(){
if(mTextureBuffer==null){
mTextureBuffer=OpenGLUtils.allocateFloatBuffer(4*4*2);
OpenGLUtils.addCoord2f(mTextureBuffer,0.0f, 0.0f);
OpenGLUtils.addCoord2f(mTextureBuffer,0.0f, 1.0f);
OpenGLUtils.addCoord2f(mTextureBuffer,1.0f, 0.0f);
OpenGLUtils.addCoord2f(mTextureBuffer,1.0f, 1.0f);
mTextureBuffer.position(0);
}
return mTextureBuffer;
}
public static ShortBuffer getBoxIndexBuffer(){
if(mIndexBuffer==null){
mIndexBuffer=OpenGLUtils.allocateShortBuffer(6*2);
mIndexBuffer.put((short)0);
mIndexBuffer.put((short)1);
mIndexBuffer.put((short)2);
mIndexBuffer.put((short)2);
mIndexBuffer.put((short)3);
mIndexBuffer.put((short)1);
mIndexBuffer.position(0);
}
return mIndexBuffer;
}
public static FloatBuffer getBoxVertexBuffer(){
if(mFVertexBuffer==null){
mFVertexBuffer=OpenGLUtils.allocateFloatBuffer(4*4*3);
OpenGLUtils.addVertex3f(mFVertexBuffer, -1,-1f,0);
OpenGLUtils.addVertex3f(mFVertexBuffer, -1,1f,0);
OpenGLUtils.addVertex3f(mFVertexBuffer, 1,-1f,0);
OpenGLUtils.addVertex3f(mFVertexBuffer, 1,1f,0);
mFVertexBuffer.position(0);
}
return mFVertexBuffer;
}
public static FloatBuffer getBoxTriangleVertexBuffer(){
if(mFVertexBuffer==null){
mFVertexBuffer=OpenGLUtils.allocateFloatBuffer(4*6*3);
OpenGLUtils.addVertex3f(mFVertexBuffer, -1,-1f,0);
OpenGLUtils.addVertex3f(mFVertexBuffer, -1,1f,0);
OpenGLUtils.addVertex3f(mFVertexBuffer, 1,-1f,0);
OpenGLUtils.addVertex3f(mFVertexBuffer, 1,-1f,0);
OpenGLUtils.addVertex3f(mFVertexBuffer, 1,1f,0);
OpenGLUtils.addVertex3f(mFVertexBuffer, -1,1f,0);
mFVertexBuffer.position(0);
}
return mFVertexBuffer;
}
//TODO その他のパラメターをサポート
/*
* bitmapは自動で回収されます。
*/
public static void bindTextureImage(GL10 gl,final int id,final Bitmap bitmap){
gl.glBindTexture(GL10.GL_TEXTURE_2D, id);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
}
}