~vcs-imports/artoolkit/sf-trunk

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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
	Original version for VRML97-0.8.7 by Hirokazu Kato.
 
	Updated for OpenVRML-0.14.3 by Raphael Grasset.
	-remove vrmlScene since we use reference now.
	-remove culling pb

	Updated for OpenVRML-0.16.1 by Philip Lamb. 2006-11-15.
	- add arVrmlBrowser class and Boost dependencies.
*/
/*
 * 
 * This file is part of ARToolKit.
 * 
 * ARToolKit is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * ARToolKit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ARToolKit; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 */

#include <iostream>
#include <math.h>
#ifdef __APPLE__
#  include <GLUT/glut.h>
#else
#  include <GL/glut.h>
#endif
#include "arViewer.h"

using namespace openvrml;

arVrmlBrowser::arVrmlBrowser(): openvrml::browser(std::cout, std::cerr)
{
}

std::auto_ptr<openvrml::resource_istream>
arVrmlBrowser::do_get_resource(const std::string & uri)
{
	using std::auto_ptr;
	using std::invalid_argument;
	using std::string;
	using openvrml::resource_istream;
	
	class file_resource_istream : public resource_istream {
		std::string url_;
		std::filebuf buf_;
		
	public:
		explicit file_resource_istream(const std::string & path): resource_istream(&this->buf_)
		{
			if (!this->buf_.open(path.c_str(), ios_base::in | ios_base::binary)) {
				this->setstate(ios_base::badbit);
			}
		}
			
		void url(const std::string & str) throw (std::bad_alloc)
		{
			this->url_ = str;
		}
			
	private:
		virtual const std::string do_url() const throw ()
		{
			return this->url_;
		}
			
		virtual const std::string do_type() const throw ()
		{
			//
			// A real application should use OS facilities for this.  This
            // is a crude hack because sdl-viewer uses std::filebuf in
            // order to remain simple and portable.
			//
			using std::find;
			using std::string;
			using boost::algorithm::iequals;
			using boost::next;
			string media_type = "application/octet-stream";
			const string::const_reverse_iterator dot_pos =
				find(this->url_.rbegin(), this->url_.rend(), '.');
			if (dot_pos == this->url_.rend()
				|| next(dot_pos.base()) == this->url_.end()) {
				return media_type;
			}
			const string::const_iterator hash_pos =
				find(next(dot_pos.base()), this->url_.end(), '#');
			const string ext(dot_pos.base(), hash_pos);
			if (iequals(ext, "wrl")) {
                media_type = openvrml::vrml_media_type;
			} else if (iequals(ext, "x3dv")) {
                media_type = openvrml::x3d_vrml_media_type;
			} else if (iequals(ext, "png")) {
				media_type = "image/png";
			} else if (iequals(ext, "jpg") || iequals(ext, "jpeg")) {
				media_type = "image/jpeg";
			}
			return media_type;
		}
			
		virtual bool do_data_available() const throw ()
		{
			return !!(*this);
		}
	}; // class file_resource_istream
	
	const string scheme = uri.substr(0, uri.find_first_of(':'));
	if (scheme != "file") {
		throw invalid_argument('\"' + scheme + "\" URI scheme not supported");
	}
	//
	// file://
	//        ^
	// 01234567
	static const string::size_type authority_start_index = 7;

	//
	// On Windows we want to start at the drive letter, which is after the
	// first slash in the path.
	//
	// We ignore the content of the authority; a smarter implementation
	// should confirm that it is localhost, the machine name, or zero
	// length.
	//
	string::size_type path_start_index =
# ifdef _WIN32
		uri.find_first_of('/', authority_start_index) + 1;
# else
		uri.find_first_of('/', authority_start_index);
# endif
	string path = uri.substr(path_start_index);
	
	auto_ptr<resource_istream> in(new file_resource_istream(path));
	static_cast<file_resource_istream *>(in.get())->url(uri);
	
	return in;
}


arVrmlViewer::arVrmlViewer()
{
    internal_light = true;

    translation[0] = 0.0;
    translation[1] = 0.0;
    translation[2] = 0.0;

    rotation[0] = 0.0;
    rotation[1] = 0.0;
    rotation[2] = 0.0;
    rotation[3] = 0.0;

    scale[0] = 1.0;
    scale[1] = 1.0;
    scale[2] = 1.0;
}

arVrmlViewer::~arVrmlViewer() throw()
{
  
}

void arVrmlViewer::timerUpdate()
{ 
	this->update(0.0);
}



void arVrmlViewer::setInternalLight(bool flag)
{
    internal_light = flag;
}

void arVrmlViewer::redraw()
{
	//double start = browser::current_time();
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glTranslated( translation[0], translation[1], translation[2] );
    if (rotation[0] != 0.0) {
        glRotated(rotation[0], rotation[1], rotation[2], rotation[3]);
    }
    glScaled(scale[0], scale[1], scale[2]);
	
#if USE_STENCIL_SHAPE
    glEnable(GL_STENCIL_TEST);
    glStencilFunc(GL_ALWAYS, 1, 1);
    glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
#endif
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glDisable(GL_FOG);          // this is a global attribute
    //glDisable(GL_TEXTURE_2D);
    glEnable(GL_CULL_FACE);
    glFrontFace(GL_CCW);
    glCullFace(GL_BACK);
	
	if (internal_light) {
		if (lit) glEnable(GL_LIGHTING);
		glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
		glEnable(GL_NORMALIZE);
		glDisable(GL_COLOR_MATERIAL);
		glDisable(GL_BLEND);
		glShadeModel(GL_SMOOTH);
		
		for (int i = 0; i < max_lights; ++i) {
			light_info_[i].type = light_unused;
			GLenum light = (GLenum) (GL_LIGHT0 + i);
			glDisable(light);
		}
	}
	
    objects = 0;
    nested_objects = 0;
    sensitive = 0;
	
    browser()->render();
	
	if (internal_light) {
		if (lit) glDisable(GL_LIGHTING);
	}
    glDisable(GL_BLEND);
    glDisable(GL_CULL_FACE);
    //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
}

void  arVrmlViewer::post_redraw()
{

}
void  arVrmlViewer::set_cursor(cursor_style c)
{

}

void  arVrmlViewer::swap_buffers()
{

}

void  arVrmlViewer::set_timer(double t)
{

}


void arVrmlViewer::set_viewpoint(const openvrml::vec3f & position,
				 const openvrml::rotation & orientation,
				 float fieldOfView,
				 float avatarSize,
				 float visibilityLimit)
{

}

viewer::object_t arVrmlViewer::insert_background(const std::vector<float> & groundAngle,
												 const std::vector<color> & groundColor,
												 const std::vector<float> & skyAngle,
												 const std::vector<color> & skyColor,
												 const image & front,
												 const image & back,
												 const image & left,
												 const image & right,
												 const image & top,
												 const image & bottom)
{
	return 0;
}

viewer::object_t arVrmlViewer::insert_dir_light(float ambientIntensity,
				  float intensity,
				  const openvrml::color & color,
				  const openvrml::vec3f & direction)
{
    if (internal_light) return gl::viewer::insert_dir_light(ambientIntensity, intensity, color, direction);
    return 0;
}

viewer::object_t arVrmlViewer::insert_point_light(float ambientIntensity,
				    const openvrml::vec3f & attenuation,
				    const openvrml::color & color,
				    float intensity,
				    const openvrml::vec3f & location,
				    float radius)
{
	if (internal_light) return gl::viewer::insert_point_light(ambientIntensity, attenuation, color, intensity, location, radius);

    return 0;
}

 
viewer::object_t arVrmlViewer::insert_spot_light(float ambientIntensity,
				   const openvrml::vec3f & attenuation,
				   float beamWidth,
				   const openvrml::color & color,
				   float cutOffAngle,
				   const openvrml::vec3f & direction,
				   float intensity,
				   const openvrml::vec3f & location,
				   float radius)
{
    if (internal_light) return gl::viewer::insert_spot_light(ambientIntensity, attenuation, beamWidth, color,cutOffAngle, direction, intensity, location, radius);
    return 0;
}

 bounding_volume::intersection
arVrmlViewer::intersect_view_volume(const bounding_volume & bvolume) const
{
  // if( d_cull ) {
  //return  openvrml::viewer::intersect_view_volume(bvolume);
	// }
	// else {
         return bounding_volume::inside;
	//}
}