OSDN Git Service

f26c2f96c9223abc1d52fa887e5afa9bf268e128
[nyartoolkit-and/nyartoolkit-and.git] / 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 version ARToolkit class library.\r
11  * Copyright (C)2008 R.Iizuka\r
12  *\r
13  * This program is free software; you can redistribute it and/or\r
14  * modify it under the terms of the GNU General Public License\r
15  * as published by the Free Software Foundation; either version 2\r
16  * of the License, or (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 framework; if not, write to the Free Software\r
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
26  * \r
27  * For further information please contact.\r
28  *      http://nyatla.jp/nyatoolkit/\r
29  *      <airmail(at)ebony.plala.or.jp>\r
30  * \r
31  */\r
32 package jp.nyatla.nyartoolkit.jogl.utils;\r
33 \r
34 import java.nio.ByteBuffer;\r
35 import java.nio.IntBuffer;\r
36 \r
37 import javax.media.opengl.GL;\r
38 import javax.media.opengl.glu.GLU;\r
39 \r
40 import jp.nyatla.nyartoolkit.core.types.*;\r
41 /**\r
42  * NyARToolkit用のJOGL支援関数群\r
43  */\r
44 public class NyARGLUtil\r
45 {\r
46         private javax.media.opengl.GL _gl;\r
47 \r
48         private javax.media.opengl.glu.GLU _glu;\r
49 \r
50         public NyARGLUtil(javax.media.opengl.GL i_gl)\r
51         {\r
52                 this._gl = i_gl;\r
53                 this._glu = new GLU();\r
54         }\r
55 \r
56         /**\r
57          * GLNyARRaster_RGBをバックグラウンドに書き出す。\r
58          * @param image\r
59          * @param zoom\r
60          */\r
61         public void drawBackGround(GLNyARRaster_RGB i_raster, double i_zoom)\r
62         {\r
63                 IntBuffer texEnvModeSave = IntBuffer.allocate(1);\r
64                 boolean lightingSave;\r
65                 boolean depthTestSave;\r
66                 javax.media.opengl.GL gl = this._gl;\r
67                 final NyARIntSize rsize=i_raster.getSize();\r
68 \r
69                 // Prepare an orthographic projection, set camera position for 2D drawing, and save GL state.\r
70                 gl.glGetTexEnviv(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, texEnvModeSave); // Save GL texture environment mode.\r
71                 if (texEnvModeSave.array()[0] != GL.GL_REPLACE) {\r
72                         gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);\r
73                 }\r
74                 lightingSave = gl.glIsEnabled(GL.GL_LIGHTING); // Save enabled state of lighting.\r
75                 if (lightingSave == true) {\r
76                         gl.glDisable(GL.GL_LIGHTING);\r
77                 }\r
78                 depthTestSave = gl.glIsEnabled(GL.GL_DEPTH_TEST); // Save enabled state of depth test.\r
79                 if (depthTestSave == true) {\r
80                         gl.glDisable(GL.GL_DEPTH_TEST);\r
81                 }\r
82                 gl.glMatrixMode(GL.GL_PROJECTION);\r
83                 gl.glPushMatrix();\r
84                 gl.glLoadIdentity();\r
85                 _glu.gluOrtho2D(0.0,rsize.w, 0.0,rsize.h);\r
86                 gl.glMatrixMode(GL.GL_MODELVIEW);\r
87                 gl.glPushMatrix();\r
88                 gl.glLoadIdentity();\r
89                 arglDispImageStateful(i_raster, i_zoom);\r
90 \r
91                 // Restore previous projection, camera position, and GL state.\r
92                 gl.glMatrixMode(GL.GL_PROJECTION);\r
93                 gl.glPopMatrix();\r
94                 gl.glMatrixMode(GL.GL_MODELVIEW);\r
95                 gl.glPopMatrix();\r
96                 if (depthTestSave) {\r
97                         gl.glEnable(GL.GL_DEPTH_TEST); // Restore enabled state of depth test.\r
98                 }\r
99                 if (lightingSave) {\r
100                         gl.glEnable(GL.GL_LIGHTING); // Restore enabled state of lighting.\r
101                 }\r
102                 if (texEnvModeSave.get(0) != GL.GL_REPLACE) {\r
103                         gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, texEnvModeSave.get(0)); // Restore GL texture environment mode.\r
104                 }\r
105                 gl.glEnd();\r
106         }\r
107 \r
108         /**\r
109          * arglDispImageStateful関数モドキ\r
110          * @param image\r
111          * @param zoom\r
112          */\r
113         private void arglDispImageStateful(GLNyARRaster_RGB i_raster, double zoom)\r
114         {\r
115                 javax.media.opengl.GL gl_ = this._gl;\r
116                 final NyARIntSize rsize = i_raster.getSize();\r
117                 float zoomf;\r
118                 IntBuffer params = IntBuffer.allocate(4);\r
119                 zoomf = (float) zoom;\r
120                 gl_.glDisable(GL.GL_TEXTURE_2D);\r
121                 gl_.glGetIntegerv(GL.GL_VIEWPORT, params);\r
122                 gl_.glPixelZoom(zoomf * ((float) (params.get(2)) / (float) rsize.w), -zoomf * ((float) (params.get(3)) / (float) rsize.h));\r
123                 gl_.glWindowPos2f(0.0f, (float) rsize.h);\r
124                 gl_.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);\r
125                 ByteBuffer buf = ByteBuffer.wrap(i_raster.getGLRgbArray());\r
126                 gl_.glDrawPixels(rsize.w,rsize.h, i_raster.getGLPixelFlag(), GL.GL_UNSIGNED_BYTE, buf);\r
127         }\r
128 }\r