2 * PROJECT: NyARToolkit JOGL utilities.
\r
3 * --------------------------------------------------------------------------------
\r
4 * This work is based on the original ARToolKit developed by
\r
7 * HITLab, University of Washington, Seattle
\r
8 * http://www.hitl.washington.edu/artoolkit/
\r
10 * The NyARToolkit is Java edition ARToolKit class library.
\r
11 * Copyright (C)2008-2009 Ryo Iizuka
\r
13 * This program is free software; you can redistribute it and/or
\r
14 * modify it under the terms of the GNU Lesser General Public License
\r
15 * as published by the Free Software Foundation; either version 3
\r
16 * of the License, or (at your option) any later version.
\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 Lesser General Public License for more details
\r
23 * You should have received a copy of the GNU Lesser General Public
\r
24 * License along with this program. If not, see <http://www.gnu.org/licenses/>.
\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
31 package jp.nyatla.nyartoolkit.jogl.utils;
\r
34 import javax.media.opengl.GL;
\r
35 import javax.media.opengl.glu.GLU;
\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
43 * NyARToolkit用のJOGL支援関数群
\r
45 public class NyARGLUtil
\r
47 private javax.media.opengl.GL _gl;
\r
49 private javax.media.opengl.glu.GLU _glu;
\r
51 public NyARGLUtil(javax.media.opengl.GL i_gl)
\r
54 this._glu = new GLU();
\r
58 * GLNyARRaster_RGBをバックグラウンドに書き出す。
\r
62 public void drawBackGround(GLNyARRaster_RGB i_raster, double i_zoom)
\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
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
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
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
83 gl.glMatrixMode(GL.GL_PROJECTION);
\r
85 gl.glLoadIdentity();
\r
86 _glu.gluOrtho2D(0.0,rsize.w, 0.0,rsize.h);
\r
87 gl.glMatrixMode(GL.GL_MODELVIEW);
\r
89 gl.glLoadIdentity();
\r
90 arglDispImageStateful(i_raster, i_zoom);
\r
92 // Restore previous projection, camera position, and GL state.
\r
93 gl.glMatrixMode(GL.GL_PROJECTION);
\r
95 gl.glMatrixMode(GL.GL_MODELVIEW);
\r
97 if (depthTestSave) {
\r
98 gl.glEnable(GL.GL_DEPTH_TEST); // Restore enabled state of depth test.
\r
100 if (lightingSave) {
\r
101 gl.glEnable(GL.GL_LIGHTING); // Restore enabled state of lighting.
\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
110 * arglDispImageStateful関数モドキ
\r
114 private void arglDispImageStateful(GLNyARRaster_RGB i_raster, double zoom)
\r
116 javax.media.opengl.GL gl_ = this._gl;
\r
117 final NyARIntSize rsize = i_raster.getSize();
\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
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
134 public void setScaleFactor(double i_new_value)
\r
136 this.view_scale_factor = i_new_value;
\r
139 public void setViewDistanceMin(double i_new_value)
\r
141 this.view_distance_min = i_new_value;
\r
144 public void setViewDistanceMax(double i_new_value)
\r
146 this.view_distance_max = i_new_value;
\r
150 * void arglCameraFrustumRH(const ARParam *cparam, const double focalmin, const double focalmax, GLdouble m_projection[16])
\r
152 * NyARParamからOpenGLのProjectionを作成します。
\r
154 * @param o_gl_projection
\r
155 * double[16]を指定して下さい。
\r
157 public void toCameraFrustumRH(NyARParam i_arparam,double[] o_gl_projection)
\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
164 final NyARIntSize size=i_arparam.getScreenSize();
\r
165 final int width = size.w;
\r
166 final int height = size.h;
\r
168 i_arparam.getPerspectiveProjectionMatrix().decompMat(icpara_mat, trans_mat);
\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
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
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
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
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
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
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
215 * NyARTransMatResultをOpenGLの行列へ変換します。
\r
216 * @param i_ny_result
\r
217 * @param o_gl_result
\r
218 * @throws NyARException
\r
220 public void toCameraViewRH(NyARTransMatResult i_ny_result, double[] o_gl_result) throws NyARException
\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