00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <iostream>
00032 #include <math.h>
00033 #ifdef __APPLE__
00034 # include <GLUT/glut.h>
00035 #else
00036 # include <GL/glut.h>
00037 #endif
00038 #include "arViewer.h"
00039
00040 using namespace openvrml;
00041
00042 arVrmlBrowser::arVrmlBrowser(): openvrml::browser(std::cout, std::cerr)
00043 {
00044 }
00045
00046 std::auto_ptr<openvrml::resource_istream>
00047 arVrmlBrowser::do_get_resource(const std::string & uri)
00048 {
00049 using std::auto_ptr;
00050 using std::invalid_argument;
00051 using std::string;
00052 using openvrml::resource_istream;
00053
00054 class file_resource_istream : public resource_istream {
00055 std::string url_;
00056 std::filebuf buf_;
00057
00058 public:
00059 explicit file_resource_istream(const std::string & path): resource_istream(&this->buf_)
00060 {
00061 if (!this->buf_.open(path.c_str(), ios_base::in | ios_base::binary)) {
00062 this->setstate(ios_base::badbit);
00063 }
00064 }
00065
00066 void url(const std::string & str) throw (std::bad_alloc)
00067 {
00068 this->url_ = str;
00069 }
00070
00071 private:
00072 virtual const std::string do_url() const throw ()
00073 {
00074 return this->url_;
00075 }
00076
00077 virtual const std::string do_type() const throw ()
00078 {
00079
00080
00081
00082
00083
00084 using std::find;
00085 using std::string;
00086 using boost::algorithm::iequals;
00087 using boost::next;
00088 string media_type = "application/octet-stream";
00089 const string::const_reverse_iterator dot_pos =
00090 find(this->url_.rbegin(), this->url_.rend(), '.');
00091 if (dot_pos == this->url_.rend()
00092 || next(dot_pos.base()) == this->url_.end()) {
00093 return media_type;
00094 }
00095 const string::const_iterator hash_pos =
00096 find(next(dot_pos.base()), this->url_.end(), '#');
00097 const string ext(dot_pos.base(), hash_pos);
00098 if (iequals(ext, "wrl")) {
00099 media_type = openvrml::vrml_media_type;
00100 } else if (iequals(ext, "x3dv")) {
00101 media_type = openvrml::x3d_vrml_media_type;
00102 } else if (iequals(ext, "png")) {
00103 media_type = "image/png";
00104 } else if (iequals(ext, "jpg") || iequals(ext, "jpeg")) {
00105 media_type = "image/jpeg";
00106 }
00107 return media_type;
00108 }
00109
00110 virtual bool do_data_available() const throw ()
00111 {
00112 return !!(*this);
00113 }
00114 };
00115
00116 const string scheme = uri.substr(0, uri.find_first_of(':'));
00117 if (scheme != "file") {
00118 throw invalid_argument('\"' + scheme + "\" URI scheme not supported");
00119 }
00120
00121
00122
00123
00124 static const string::size_type authority_start_index = 7;
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 string::size_type path_start_index =
00135 # ifdef _WIN32
00136 uri.find_first_of('/', authority_start_index) + 1;
00137 # else
00138 uri.find_first_of('/', authority_start_index);
00139 # endif
00140 string path = uri.substr(path_start_index);
00141
00142 auto_ptr<resource_istream> in(new file_resource_istream(path));
00143 static_cast<file_resource_istream *>(in.get())->url(uri);
00144
00145 return in;
00146 }
00147
00148
00149 arVrmlViewer::arVrmlViewer()
00150 {
00151 internal_light = true;
00152
00153 translation[0] = 0.0;
00154 translation[1] = 0.0;
00155 translation[2] = 0.0;
00156
00157 rotation[0] = 0.0;
00158 rotation[1] = 0.0;
00159 rotation[2] = 0.0;
00160 rotation[3] = 0.0;
00161
00162 scale[0] = 1.0;
00163 scale[1] = 1.0;
00164 scale[2] = 1.0;
00165 }
00166
00167 arVrmlViewer::~arVrmlViewer() throw()
00168 {
00169
00170 }
00171
00172 void arVrmlViewer::timerUpdate()
00173 {
00174 this->update(0.0);
00175 }
00176
00177
00178
00179 void arVrmlViewer::setInternalLight(bool flag)
00180 {
00181 internal_light = flag;
00182 }
00183
00184 void arVrmlViewer::redraw()
00185 {
00186
00187 glMatrixMode(GL_MODELVIEW);
00188 glPushMatrix();
00189 glTranslated( translation[0], translation[1], translation[2] );
00190 if (rotation[0] != 0.0) {
00191 glRotated(rotation[0], rotation[1], rotation[2], rotation[3]);
00192 }
00193 glScaled(scale[0], scale[1], scale[2]);
00194
00195 #if USE_STENCIL_SHAPE
00196 glEnable(GL_STENCIL_TEST);
00197 glStencilFunc(GL_ALWAYS, 1, 1);
00198 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
00199 #endif
00200 glEnable(GL_DEPTH_TEST);
00201 glDepthFunc(GL_LEQUAL);
00202 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00203 glDisable(GL_FOG);
00204
00205 glEnable(GL_CULL_FACE);
00206 glFrontFace(GL_CCW);
00207 glCullFace(GL_BACK);
00208
00209 if (internal_light) {
00210 if (lit) glEnable(GL_LIGHTING);
00211 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
00212 glEnable(GL_NORMALIZE);
00213 glDisable(GL_COLOR_MATERIAL);
00214 glDisable(GL_BLEND);
00215 glShadeModel(GL_SMOOTH);
00216
00217 for (int i = 0; i < max_lights; ++i) {
00218 light_info_[i].type = light_unused;
00219 GLenum light = (GLenum) (GL_LIGHT0 + i);
00220 glDisable(light);
00221 }
00222 }
00223
00224 objects = 0;
00225 nested_objects = 0;
00226 sensitive = 0;
00227
00228 browser()->render();
00229
00230 if (internal_light) {
00231 if (lit) glDisable(GL_LIGHTING);
00232 }
00233 glDisable(GL_BLEND);
00234 glDisable(GL_CULL_FACE);
00235
00236
00237 glMatrixMode(GL_MODELVIEW);
00238 glPopMatrix();
00239 }
00240
00241 void arVrmlViewer::post_redraw()
00242 {
00243
00244 }
00245 void arVrmlViewer::set_cursor(cursor_style c)
00246 {
00247
00248 }
00249
00250 void arVrmlViewer::swap_buffers()
00251 {
00252
00253 }
00254
00255 void arVrmlViewer::set_timer(double t)
00256 {
00257
00258 }
00259
00260
00261 void arVrmlViewer::set_viewpoint(const openvrml::vec3f & position,
00262 const openvrml::rotation & orientation,
00263 float fieldOfView,
00264 float avatarSize,
00265 float visibilityLimit)
00266 {
00267
00268 }
00269
00270 viewer::object_t arVrmlViewer::insert_background(const std::vector<float> & groundAngle,
00271 const std::vector<color> & groundColor,
00272 const std::vector<float> & skyAngle,
00273 const std::vector<color> & skyColor,
00274 const image & front,
00275 const image & back,
00276 const image & left,
00277 const image & right,
00278 const image & top,
00279 const image & bottom)
00280 {
00281 return 0;
00282 }
00283
00284 viewer::object_t arVrmlViewer::insert_dir_light(float ambientIntensity,
00285 float intensity,
00286 const openvrml::color & color,
00287 const openvrml::vec3f & direction)
00288 {
00289 if (internal_light) return gl::viewer::insert_dir_light(ambientIntensity, intensity, color, direction);
00290 return 0;
00291 }
00292
00293 viewer::object_t arVrmlViewer::insert_point_light(float ambientIntensity,
00294 const openvrml::vec3f & attenuation,
00295 const openvrml::color & color,
00296 float intensity,
00297 const openvrml::vec3f & location,
00298 float radius)
00299 {
00300 if (internal_light) return gl::viewer::insert_point_light(ambientIntensity, attenuation, color, intensity, location, radius);
00301
00302 return 0;
00303 }
00304
00305
00306 viewer::object_t arVrmlViewer::insert_spot_light(float ambientIntensity,
00307 const openvrml::vec3f & attenuation,
00308 float beamWidth,
00309 const openvrml::color & color,
00310 float cutOffAngle,
00311 const openvrml::vec3f & direction,
00312 float intensity,
00313 const openvrml::vec3f & location,
00314 float radius)
00315 {
00316 if (internal_light) return gl::viewer::insert_spot_light(ambientIntensity, attenuation, beamWidth, color,cutOffAngle, direction, intensity, location, radius);
00317 return 0;
00318 }
00319
00320 bounding_volume::intersection
00321 arVrmlViewer::intersect_view_volume(const bounding_volume & bvolume) const
00322 {
00323
00324
00325
00326
00327 return bounding_volume::inside;
00328
00329 }