~vcs-imports/artoolkit/sf-trunk

6 by philip_lamb
Added libARvrml.
1
/*
227 by philip_lamb
Update ARvrml for OpenVRML-0.16.1.
2
	Original version for VRML97-0.8.7 by Hirokazu Kato.
3
 
4
	Updated for OpenVRML-0.14.3 by Raphael Grasset.
5
	-remove vrmlScene since we use reference now.
6
	-remove culling pb
6 by philip_lamb
Added libARvrml.
7
227 by philip_lamb
Update ARvrml for OpenVRML-0.16.1.
8
	Updated for OpenVRML-0.16.1 by Philip Lamb. 2006-11-15.
9
	- add arVrmlBrowser class and Boost dependencies.
6 by philip_lamb
Added libARvrml.
10
*/
11
/*
12
 * 
13
 * This file is part of ARToolKit.
14
 * 
15
 * ARToolKit is free software; you can redistribute it and/or modify
16
 * it under the terms of the GNU General Public License as published by
17
 * the Free Software Foundation; either version 2 of the License, or
18
 * (at your option) any later version.
19
 * 
20
 * ARToolKit is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU General Public License for more details.
24
 * 
25
 * You should have received a copy of the GNU General Public License
26
 * along with ARToolKit; if not, write to the Free Software
27
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
 * 
29
 */
30
31
#include <iostream>
32
#include <math.h>
33
#ifdef __APPLE__
34
#  include <GLUT/glut.h>
35
#else
36
#  include <GL/glut.h>
37
#endif
38
#include "arViewer.h"
39
40
using namespace openvrml;
41
227 by philip_lamb
Update ARvrml for OpenVRML-0.16.1.
42
arVrmlBrowser::arVrmlBrowser(): openvrml::browser(std::cout, std::cerr)
43
{
44
}
45
46
std::auto_ptr<openvrml::resource_istream>
47
arVrmlBrowser::do_get_resource(const std::string & uri)
48
{
49
	using std::auto_ptr;
50
	using std::invalid_argument;
51
	using std::string;
52
	using openvrml::resource_istream;
53
	
54
	class file_resource_istream : public resource_istream {
55
		std::string url_;
56
		std::filebuf buf_;
57
		
58
	public:
59
		explicit file_resource_istream(const std::string & path): resource_istream(&this->buf_)
60
		{
262 by philip_lamb
Provide linkage to OpenVRML-0.14.3 for Visual Studio .NET 2003.
61
			if (!this->buf_.open(path.c_str(), ios_base::in | ios_base::binary)) {
62
				this->setstate(ios_base::badbit);
227 by philip_lamb
Update ARvrml for OpenVRML-0.16.1.
63
			}
64
		}
65
			
66
		void url(const std::string & str) throw (std::bad_alloc)
67
		{
68
			this->url_ = str;
69
		}
70
			
71
	private:
72
		virtual const std::string do_url() const throw ()
73
		{
74
			return this->url_;
75
		}
76
			
77
		virtual const std::string do_type() const throw ()
78
		{
79
			//
80
			// A real application should use OS facilities for this.  This
262 by philip_lamb
Provide linkage to OpenVRML-0.14.3 for Visual Studio .NET 2003.
81
            // is a crude hack because sdl-viewer uses std::filebuf in
82
            // order to remain simple and portable.
227 by philip_lamb
Update ARvrml for OpenVRML-0.16.1.
83
			//
84
			using std::find;
85
			using std::string;
86
			using boost::algorithm::iequals;
87
			using boost::next;
88
			string media_type = "application/octet-stream";
89
			const string::const_reverse_iterator dot_pos =
90
				find(this->url_.rbegin(), this->url_.rend(), '.');
91
			if (dot_pos == this->url_.rend()
92
				|| next(dot_pos.base()) == this->url_.end()) {
93
				return media_type;
94
			}
95
			const string::const_iterator hash_pos =
96
				find(next(dot_pos.base()), this->url_.end(), '#');
97
			const string ext(dot_pos.base(), hash_pos);
98
			if (iequals(ext, "wrl")) {
262 by philip_lamb
Provide linkage to OpenVRML-0.14.3 for Visual Studio .NET 2003.
99
                media_type = openvrml::vrml_media_type;
227 by philip_lamb
Update ARvrml for OpenVRML-0.16.1.
100
			} else if (iequals(ext, "x3dv")) {
262 by philip_lamb
Provide linkage to OpenVRML-0.14.3 for Visual Studio .NET 2003.
101
                media_type = openvrml::x3d_vrml_media_type;
227 by philip_lamb
Update ARvrml for OpenVRML-0.16.1.
102
			} else if (iequals(ext, "png")) {
103
				media_type = "image/png";
104
			} else if (iequals(ext, "jpg") || iequals(ext, "jpeg")) {
105
				media_type = "image/jpeg";
106
			}
107
			return media_type;
108
		}
109
			
110
		virtual bool do_data_available() const throw ()
111
		{
112
			return !!(*this);
113
		}
114
	}; // class file_resource_istream
115
	
116
	const string scheme = uri.substr(0, uri.find_first_of(':'));
117
	if (scheme != "file") {
118
		throw invalid_argument('\"' + scheme + "\" URI scheme not supported");
119
	}
120
	//
121
	// file://
122
	//        ^
123
	// 01234567
262 by philip_lamb
Provide linkage to OpenVRML-0.14.3 for Visual Studio .NET 2003.
124
	static const string::size_type authority_start_index = 7;
125
126
	//
127
	// On Windows we want to start at the drive letter, which is after the
128
	// first slash in the path.
129
	//
130
	// We ignore the content of the authority; a smarter implementation
131
	// should confirm that it is localhost, the machine name, or zero
132
	// length.
133
	//
134
	string::size_type path_start_index =
135
# ifdef _WIN32
136
		uri.find_first_of('/', authority_start_index) + 1;
137
# else
138
		uri.find_first_of('/', authority_start_index);
139
# endif
140
	string path = uri.substr(path_start_index);
227 by philip_lamb
Update ARvrml for OpenVRML-0.16.1.
141
	
142
	auto_ptr<resource_istream> in(new file_resource_istream(path));
143
	static_cast<file_resource_istream *>(in.get())->url(uri);
144
	
145
	return in;
146
}
147
148
262 by philip_lamb
Provide linkage to OpenVRML-0.14.3 for Visual Studio .NET 2003.
149
arVrmlViewer::arVrmlViewer()
6 by philip_lamb
Added libARvrml.
150
{
151
    internal_light = true;
152
153
    translation[0] = 0.0;
154
    translation[1] = 0.0;
155
    translation[2] = 0.0;
156
157
    rotation[0] = 0.0;
158
    rotation[1] = 0.0;
159
    rotation[2] = 0.0;
160
    rotation[3] = 0.0;
161
162
    scale[0] = 1.0;
163
    scale[1] = 1.0;
164
    scale[2] = 1.0;
165
}
166
227 by philip_lamb
Update ARvrml for OpenVRML-0.16.1.
167
arVrmlViewer::~arVrmlViewer() throw()
6 by philip_lamb
Added libARvrml.
168
{
169
  
170
}
171
172
void arVrmlViewer::timerUpdate()
173
{ 
227 by philip_lamb
Update ARvrml for OpenVRML-0.16.1.
174
	this->update(0.0);
6 by philip_lamb
Added libARvrml.
175
}
176
177
178
179
void arVrmlViewer::setInternalLight(bool flag)
180
{
181
    internal_light = flag;
182
}
183
184
void arVrmlViewer::redraw()
185
{
227 by philip_lamb
Update ARvrml for OpenVRML-0.16.1.
186
	//double start = browser::current_time();
6 by philip_lamb
Added libARvrml.
187
    glMatrixMode(GL_MODELVIEW);
188
    glPushMatrix();
189
    glTranslated( translation[0], translation[1], translation[2] );
168 by philip_lamb
Trivial formatting pickiness.
190
    if (rotation[0] != 0.0) {
191
        glRotated(rotation[0], rotation[1], rotation[2], rotation[3]);
6 by philip_lamb
Added libARvrml.
192
    }
168 by philip_lamb
Trivial formatting pickiness.
193
    glScaled(scale[0], scale[1], scale[2]);
194
	
6 by philip_lamb
Added libARvrml.
195
#if USE_STENCIL_SHAPE
196
    glEnable(GL_STENCIL_TEST);
197
    glStencilFunc(GL_ALWAYS, 1, 1);
198
    glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
199
#endif
200
    glEnable(GL_DEPTH_TEST);
201
    glDepthFunc(GL_LEQUAL);
202
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
168 by philip_lamb
Trivial formatting pickiness.
203
    glDisable(GL_FOG);          // this is a global attribute
237 by philip_lamb
Fix up vrml renderer (backgrounds were still showing).
204
    //glDisable(GL_TEXTURE_2D);
168 by philip_lamb
Trivial formatting pickiness.
205
    glEnable(GL_CULL_FACE);
206
    glFrontFace(GL_CCW);
207
    glCullFace(GL_BACK);
208
	
209
	if (internal_light) {
210
		if (lit) glEnable(GL_LIGHTING);
211
		glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
212
		glEnable(GL_NORMALIZE);
213
		glDisable(GL_COLOR_MATERIAL);
214
		glDisable(GL_BLEND);
215
		glShadeModel(GL_SMOOTH);
216
		
217
		for (int i = 0; i < max_lights; ++i) {
218
			light_info_[i].type = light_unused;
219
			GLenum light = (GLenum) (GL_LIGHT0 + i);
220
			glDisable(light);
221
		}
222
	}
223
	
6 by philip_lamb
Added libARvrml.
224
    objects = 0;
225
    nested_objects = 0;
226
    sensitive = 0;
168 by philip_lamb
Trivial formatting pickiness.
227
	
227 by philip_lamb
Update ARvrml for OpenVRML-0.16.1.
228
    browser()->render();
168 by philip_lamb
Trivial formatting pickiness.
229
	
230
	if (internal_light) {
231
		if (lit) glDisable(GL_LIGHTING);
232
	}
6 by philip_lamb
Added libARvrml.
233
    glDisable(GL_BLEND);
168 by philip_lamb
Trivial formatting pickiness.
234
    glDisable(GL_CULL_FACE);
237 by philip_lamb
Fix up vrml renderer (backgrounds were still showing).
235
    //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
168 by philip_lamb
Trivial formatting pickiness.
236
	
6 by philip_lamb
Added libARvrml.
237
    glMatrixMode(GL_MODELVIEW);
238
    glPopMatrix();
239
}
240
241
void  arVrmlViewer::post_redraw()
242
{
243
244
}
245
void  arVrmlViewer::set_cursor(cursor_style c)
246
{
247
248
}
249
250
void  arVrmlViewer::swap_buffers()
251
{
252
253
}
254
255
void  arVrmlViewer::set_timer(double t)
256
{
257
258
}
259
260
261
void arVrmlViewer::set_viewpoint(const openvrml::vec3f & position,
262
				 const openvrml::rotation & orientation,
263
				 float fieldOfView,
264
				 float avatarSize,
265
				 float visibilityLimit)
266
{
267
268
}
269
270
viewer::object_t arVrmlViewer::insert_background(const std::vector<float> & groundAngle,
237 by philip_lamb
Fix up vrml renderer (backgrounds were still showing).
271
												 const std::vector<color> & groundColor,
272
												 const std::vector<float> & skyAngle,
273
												 const std::vector<color> & skyColor,
274
												 const image & front,
275
												 const image & back,
276
												 const image & left,
277
												 const image & right,
278
												 const image & top,
279
												 const image & bottom)
6 by philip_lamb
Added libARvrml.
280
{
168 by philip_lamb
Trivial formatting pickiness.
281
	return 0;
6 by philip_lamb
Added libARvrml.
282
}
283
284
viewer::object_t arVrmlViewer::insert_dir_light(float ambientIntensity,
285
				  float intensity,
286
				  const openvrml::color & color,
287
				  const openvrml::vec3f & direction)
288
{
168 by philip_lamb
Trivial formatting pickiness.
289
    if (internal_light) return gl::viewer::insert_dir_light(ambientIntensity, intensity, color, direction);
6 by philip_lamb
Added libARvrml.
290
    return 0;
291
}
292
293
viewer::object_t arVrmlViewer::insert_point_light(float ambientIntensity,
294
				    const openvrml::vec3f & attenuation,
295
				    const openvrml::color & color,
296
				    float intensity,
297
				    const openvrml::vec3f & location,
298
				    float radius)
299
{
168 by philip_lamb
Trivial formatting pickiness.
300
	if (internal_light) return gl::viewer::insert_point_light(ambientIntensity, attenuation, color, intensity, location, radius);
6 by philip_lamb
Added libARvrml.
301
302
    return 0;
303
}
304
305
 
306
viewer::object_t arVrmlViewer::insert_spot_light(float ambientIntensity,
307
				   const openvrml::vec3f & attenuation,
308
				   float beamWidth,
309
				   const openvrml::color & color,
310
				   float cutOffAngle,
311
				   const openvrml::vec3f & direction,
312
				   float intensity,
313
				   const openvrml::vec3f & location,
314
				   float radius)
315
{
168 by philip_lamb
Trivial formatting pickiness.
316
    if (internal_light) return gl::viewer::insert_spot_light(ambientIntensity, attenuation, beamWidth, color,cutOffAngle, direction, intensity, location, radius);
6 by philip_lamb
Added libARvrml.
317
    return 0;
318
}
319
320
 bounding_volume::intersection
321
arVrmlViewer::intersect_view_volume(const bounding_volume & bvolume) const
322
{
323
  // if( d_cull ) {
324
  //return  openvrml::viewer::intersect_view_volume(bvolume);
325
	// }
326
	// else {
327
         return bounding_volume::inside;
328
	//}
329
}