OSDN Git Service

[TAG]2.4.0
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.4.0 / src.utils / jogl / jp / nyatla / nyartoolkit / jogl / utils / NyARGLUtil.java
1 /* \r
2  * PROJECT: NyARToolkit JOGL utilities.\r
3  * --------------------------------------------------------------------------------\r
4  * This work is based on the original ARToolKit developed by\r
5  *   Hirokazu Kato\r
6  *   Mark Billinghurst\r
7  *   HITLab, University of Washington, Seattle\r
8  * http://www.hitl.washington.edu/artoolkit/\r
9  *\r
10  * The NyARToolkit is Java edition ARToolKit class library.\r
11  * Copyright (C)2008-2009 Ryo Iizuka\r
12  *\r
13  * This program is free software: you can redistribute it and/or modify\r
14  * it under the terms of the GNU General Public License as published by\r
15  * the Free Software Foundation, either version 3 of the License, or\r
16  * (at your option) any later version.\r
17  * \r
18  * This program is distributed in the hope that it will be useful,\r
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
21  * GNU General Public License for more details.\r
22  *\r
23  * You should have received a copy of the GNU General Public License\r
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
25  * \r
26  * For further information please contact.\r
27  *      http://nyatla.jp/nyatoolkit/\r
28  *      <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
29  * \r
30  */\r
31 package jp.nyatla.nyartoolkit.jogl.utils;\r
32 \r
33 import java.nio.*;\r
34 import javax.media.opengl.GL;\r
35 import javax.media.opengl.glu.GLU;\r
36 \r
37 import jp.nyatla.nyartoolkit.NyARException;\r
38 import jp.nyatla.nyartoolkit.core.NyARMat;\r
39 import jp.nyatla.nyartoolkit.core.param.NyARParam;\r
40 import jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult;\r
41 import jp.nyatla.nyartoolkit.core.types.*;\r
42 /**\r
43  * NyARToolkit用のJOGL支援関数群\r
44  */\r
45 public class NyARGLUtil\r
46 {\r
47         private javax.media.opengl.GL _gl;\r
48 \r
49         private javax.media.opengl.glu.GLU _glu;\r
50 \r
51         public NyARGLUtil(javax.media.opengl.GL i_gl)\r
52         {\r
53                 this._gl = i_gl;\r
54                 this._glu = new GLU();\r
55         }\r
56 \r
57         /**\r
58          * GLNyARRaster_RGBをバックグラウンドに書き出す。\r
59          * @param image\r
60          * @param zoom\r
61          */\r
62         public void drawBackGround(GLNyARRaster_RGB i_raster, double i_zoom)\r
63         {\r
64                 IntBuffer texEnvModeSave = IntBuffer.allocate(1);\r
65                 boolean lightingSave;\r
66                 boolean depthTestSave;\r
67                 javax.media.opengl.GL gl = this._gl;\r
68                 final NyARIntSize rsize=i_raster.getSize();\r
69 \r
70                 // Prepare an orthographic projection, set camera position for 2D drawing, and save GL state.\r
71                 gl.glGetTexEnviv(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, texEnvModeSave); // Save GL texture environment mode.\r
72                 if (texEnvModeSave.array()[0] != GL.GL_REPLACE) {\r
73                         gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);\r
74                 }\r
75                 lightingSave = gl.glIsEnabled(GL.GL_LIGHTING); // Save enabled state of lighting.\r
76                 if (lightingSave == true) {\r
77                         gl.glDisable(GL.GL_LIGHTING);\r
78                 }\r
79                 depthTestSave = gl.glIsEnabled(GL.GL_DEPTH_TEST); // Save enabled state of depth test.\r
80                 if (depthTestSave == true) {\r
81                         gl.glDisable(GL.GL_DEPTH_TEST);\r
82                 }\r
83                 gl.glMatrixMode(GL.GL_PROJECTION);\r
84                 gl.glPushMatrix();\r
85                 gl.glLoadIdentity();\r
86                 _glu.gluOrtho2D(0.0,rsize.w, 0.0,rsize.h);\r
87                 gl.glMatrixMode(GL.GL_MODELVIEW);\r
88                 gl.glPushMatrix();\r
89                 gl.glLoadIdentity();\r
90                 arglDispImageStateful(i_raster, i_zoom);\r
91 \r
92                 // Restore previous projection, camera position, and GL state.\r
93                 gl.glMatrixMode(GL.GL_PROJECTION);\r
94                 gl.glPopMatrix();\r
95                 gl.glMatrixMode(GL.GL_MODELVIEW);\r
96                 gl.glPopMatrix();\r
97                 if (depthTestSave) {\r
98                         gl.glEnable(GL.GL_DEPTH_TEST); // Restore enabled state of depth test.\r
99                 }\r
100                 if (lightingSave) {\r
101                         gl.glEnable(GL.GL_LIGHTING); // Restore enabled state of lighting.\r
102                 }\r
103                 if (texEnvModeSave.get(0) != GL.GL_REPLACE) {\r
104                         gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, texEnvModeSave.get(0)); // Restore GL texture environment mode.\r
105                 }\r
106                 gl.glEnd();\r
107         }\r
108 \r
109         /**\r
110          * arglDispImageStateful関数モドキ\r
111          * @param image\r
112          * @param zoom\r
113          */\r
114         private void arglDispImageStateful(GLNyARRaster_RGB i_raster, double zoom)\r
115         {\r
116                 javax.media.opengl.GL gl_ = this._gl;\r
117                 final NyARIntSize rsize = i_raster.getSize();\r
118                 float zoomf;\r
119                 IntBuffer params = IntBuffer.allocate(4);\r
120                 zoomf = (float) zoom;\r
121                 gl_.glDisable(GL.GL_TEXTURE_2D);\r
122                 gl_.glGetIntegerv(GL.GL_VIEWPORT, params);\r
123                 gl_.glPixelZoom(zoomf * ((float) (params.get(2)) / (float) rsize.w), -zoomf * ((float) (params.get(3)) / (float) rsize.h));\r
124                 gl_.glWindowPos2f(0.0f, (float) rsize.h);\r
125                 gl_.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);\r
126                 ByteBuffer buf = ByteBuffer.wrap(i_raster.getGLRgbArray());\r
127                 gl_.glDrawPixels(rsize.w,rsize.h, i_raster.getGLPixelFlag(), GL.GL_UNSIGNED_BYTE, buf);\r
128         }\r
129         \r
130         private double view_scale_factor = 0.025;\r
131         private double view_distance_min = 0.1;//#define VIEW_DISTANCE_MIN              0.1                     // Objects closer to the camera than this will not be displayed.\r
132         private double view_distance_max = 100.0;//#define VIEW_DISTANCE_MAX            100.0           // Objects further away from the camera than this will not be displayed.\r
133 \r
134         public void setScaleFactor(double i_new_value)\r
135         {\r
136                 this.view_scale_factor = i_new_value;\r
137         }\r
138 \r
139         public void setViewDistanceMin(double i_new_value)\r
140         {\r
141                 this.view_distance_min = i_new_value;\r
142         }\r
143 \r
144         public void setViewDistanceMax(double i_new_value)\r
145         {\r
146                 this.view_distance_max = i_new_value;\r
147         }\r
148 \r
149         /**\r
150          * void arglCameraFrustumRH(const ARParam *cparam, const double focalmin, const double focalmax, GLdouble m_projection[16])\r
151          * 関数の置き換え\r
152          * NyARParamからOpenGLのProjectionを作成します。\r
153          * @param i_arparam\r
154          * @param o_gl_projection\r
155          * double[16]を指定して下さい。\r
156          */\r
157         public void toCameraFrustumRH(NyARParam i_arparam,double[] o_gl_projection)\r
158         {\r
159                 NyARMat trans_mat = new NyARMat(3, 4);\r
160                 NyARMat icpara_mat = new NyARMat(3, 4);\r
161                 double[][] p = new double[3][3], q = new double[4][4];\r
162                 int i, j;\r
163 \r
164                 final NyARIntSize size=i_arparam.getScreenSize();\r
165                 final int width = size.w;\r
166                 final int height = size.h;\r
167                 \r
168                 i_arparam.getPerspectiveProjectionMatrix().decompMat(icpara_mat, trans_mat);\r
169 \r
170                 double[][] icpara = icpara_mat.getArray();\r
171                 double[][] trans = trans_mat.getArray();\r
172                 for (i = 0; i < 4; i++) {\r
173                         icpara[1][i] = (height - 1) * (icpara[2][i]) - icpara[1][i];\r
174                 }\r
175 \r
176                 for (i = 0; i < 3; i++) {\r
177                         for (j = 0; j < 3; j++) {\r
178                                 p[i][j] = icpara[i][j] / icpara[2][2];\r
179                         }\r
180                 }\r
181                 q[0][0] = (2.0 * p[0][0] / (width - 1));\r
182                 q[0][1] = (2.0 * p[0][1] / (width - 1));\r
183                 q[0][2] = -((2.0 * p[0][2] / (width - 1)) - 1.0);\r
184                 q[0][3] = 0.0;\r
185 \r
186                 q[1][0] = 0.0;\r
187                 q[1][1] = -(2.0 * p[1][1] / (height - 1));\r
188                 q[1][2] = -((2.0 * p[1][2] / (height - 1)) - 1.0);\r
189                 q[1][3] = 0.0;\r
190 \r
191                 q[2][0] = 0.0;\r
192                 q[2][1] = 0.0;\r
193                 q[2][2] = (view_distance_max + view_distance_min) / (view_distance_min - view_distance_max);\r
194                 q[2][3] = 2.0 * view_distance_max * view_distance_min / (view_distance_min - view_distance_max);\r
195 \r
196                 q[3][0] = 0.0;\r
197                 q[3][1] = 0.0;\r
198                 q[3][2] = -1.0;\r
199                 q[3][3] = 0.0;\r
200 \r
201                 for (i = 0; i < 4; i++) { // Row.\r
202                         // First 3 columns of the current row.\r
203                         for (j = 0; j < 3; j++) { // Column.\r
204                                 o_gl_projection[i + j * 4] = q[i][0] * trans[0][j] + q[i][1] * trans[1][j] + q[i][2] * trans[2][j];\r
205                         }\r
206                         // Fourth column of the current row.\r
207                         o_gl_projection[i + 3 * 4] = q[i][0] * trans[0][3] + q[i][1] * trans[1][3] + q[i][2] * trans[2][3] + q[i][3];\r
208                 }\r
209                 return;\r
210         }\r
211         \r
212         \r
213         \r
214         /**\r
215          * NyARTransMatResultをOpenGLの行列へ変換します。\r
216          * @param i_ny_result\r
217          * @param o_gl_result\r
218          * @throws NyARException\r
219          */\r
220         public void toCameraViewRH(NyARTransMatResult i_ny_result, double[] o_gl_result) throws NyARException\r
221         {\r
222                 o_gl_result[0 + 0 * 4] = i_ny_result.m00; \r
223                 o_gl_result[0 + 1 * 4] = i_ny_result.m01;\r
224                 o_gl_result[0 + 2 * 4] = i_ny_result.m02;\r
225                 o_gl_result[0 + 3 * 4] = i_ny_result.m03;\r
226                 o_gl_result[1 + 0 * 4] = -i_ny_result.m10;\r
227                 o_gl_result[1 + 1 * 4] = -i_ny_result.m11;\r
228                 o_gl_result[1 + 2 * 4] = -i_ny_result.m12;\r
229                 o_gl_result[1 + 3 * 4] = -i_ny_result.m13;\r
230                 o_gl_result[2 + 0 * 4] = -i_ny_result.m20;\r
231                 o_gl_result[2 + 1 * 4] = -i_ny_result.m21;\r
232                 o_gl_result[2 + 2 * 4] = -i_ny_result.m22;\r
233                 o_gl_result[2 + 3 * 4] = -i_ny_result.m23;\r
234                 o_gl_result[3 + 0 * 4] = 0.0;\r
235                 o_gl_result[3 + 1 * 4] = 0.0;\r
236                 o_gl_result[3 + 2 * 4] = 0.0;\r
237                 o_gl_result[3 + 3 * 4] = 1.0;\r
238                 if (view_scale_factor != 0.0) {\r
239                         o_gl_result[12] *= view_scale_factor;\r
240                         o_gl_result[13] *= view_scale_factor;\r
241                         o_gl_result[14] *= view_scale_factor;\r
242                 }\r
243                 return;\r
244         }       \r
245         \r
246 }\r