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
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include <iostream>
00048 #include <osg/Node>
00049 #include <osg/Geode>
00050 #include <osg/Geometry>
00051 #include <osg/Texture2D>
00052 #include <osg/TexEnv>
00053 #include <osg/TexGen>
00054 #include <osg/Depth>
00055 #include <osg/StateSet>
00056 #include <osg/ClearNode>
00057 #include <osg/Transform>
00058 #include <osg/MatrixTransform>
00059 #include <osg/Light>
00060 #include <osg/LightSource>
00061 #include <osg/ShapeDrawable>
00062
00063 #include <osgUtil/CullVisitor>
00064
00065 #include <osgDB/Registry>
00066 #include <osgDB/ReadFile>
00067
00068 #include <osgGA/AnimationPathManipulator>
00069
00070 #include "rendertotexturecallback.h"
00071 #include "base.h"
00072 #include "primitive.h"
00073
00074 using namespace osg;
00075
00076 namespace lpzrobots {
00077
00078
00079 Group* Base::makeScene(){
00080
00081 root = new Group;
00082
00083
00084
00085
00086 ClearNode* clearNode = new ClearNode;
00087
00088
00089 osg::Transform* transform = new osg::Transform;
00090
00091
00092 clearNode->addChild(transform);
00093
00094 root->addChild(clearNode);
00095
00096 Group* group = new Group;
00097
00098 root->addChild(group);
00099
00100
00101
00102
00103
00104
00105 transform->setCullingActive(false);
00106
00107
00108 transform->addChild(makeSky());
00109 transform->addChild(makeGround());
00110
00111 LightSource* lightSource = makeLights(root->getOrCreateStateSet());
00112 transform->addChild(lightSource);
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 return group;
00128 }
00129
00130 Node* Base::makeSky() {
00131
00132 int i, j;
00133 float lev[] = {-5.0, -1.0, 2.0, 12.0, 30.0, 60.0, 90.0 };
00134 float cc[][4] =
00135 {
00136 { 0.0, 0.0, 0.15 },
00137 { 0.0, 0.0, 0.15 },
00138 { 0.4, 0.4, 0.7 },
00139 { 0.2, 0.2, 0.6 },
00140 { 0.1, 0.1, 0.6 },
00141 { 0.1, 0.1, 0.6 },
00142 { 0.1, 0.1, 0.6 },
00143 };
00144 float x, y, z;
00145 float alpha, theta;
00146 float radius = 1000.0f;
00147 int nlev = sizeof( lev )/sizeof(float);
00148
00149 Geometry *geom = new Geometry;
00150
00151 Vec3Array& coords = *(new Vec3Array(19*nlev));
00152 Vec4Array& colors = *(new Vec4Array(19*nlev));
00153 Vec2Array& tcoords = *(new Vec2Array(19*nlev));
00154
00155
00156 int ci = 0;
00157
00158 for( i = 0; i < nlev; i++ )
00159 {
00160 for( j = 0; j <= 18; j++ )
00161 {
00162 alpha = osg::DegreesToRadians(lev[i]);
00163 theta = osg::DegreesToRadians((float)(j*20));
00164
00165 x = radius * cosf( alpha ) * cosf( theta );
00166 y = radius * cosf( alpha ) * -sinf( theta );
00167 z = radius * sinf( alpha );
00168
00169 coords[ci][0] = x;
00170 coords[ci][1] = y;
00171 coords[ci][2] = z;
00172
00173 colors[ci][0] = cc[i][0];
00174 colors[ci][1] = cc[i][1];
00175 colors[ci][2] = cc[i][2];
00176 colors[ci][3] = 1.0;
00177
00178 tcoords[ci][0] = (float)j/18.0;
00179 tcoords[ci][1] = (float)i/(float)(nlev-1);
00180
00181 ci++;
00182 }
00183
00184
00185 }
00186
00187 for( i = 0; i < nlev-1; i++ )
00188 {
00189 DrawElementsUShort* drawElements = new DrawElementsUShort(PrimitiveSet::TRIANGLE_STRIP);
00190 drawElements->reserve(38);
00191
00192 for( j = 0; j <= 18; j++ )
00193 {
00194 drawElements->push_back((i+1)*19+j);
00195 drawElements->push_back((i+0)*19+j);
00196 }
00197
00198 geom->addPrimitiveSet(drawElements);
00199 }
00200
00201 geom->setVertexArray( &coords );
00202 geom->setTexCoordArray( 0, &tcoords );
00203
00204 geom->setColorArray( &colors );
00205 geom->setColorBinding( Geometry::BIND_PER_VERTEX );
00206
00207
00208 Texture2D *tex = new Texture2D;
00209 tex->setImage(osgDB::readImageFile("Images/white.rgb"));
00210
00211 StateSet *dstate = new StateSet;
00212
00213 dstate->setTextureAttributeAndModes(0, tex, StateAttribute::OFF );
00214 dstate->setTextureAttribute(0, new TexEnv );
00215 dstate->setMode( GL_LIGHTING, StateAttribute::OFF );
00216 dstate->setMode( GL_CULL_FACE, StateAttribute::ON );
00217
00218
00219
00220 osg::Depth* depth = new osg::Depth;
00221 depth->setFunction(osg::Depth::ALWAYS);
00222 depth->setRange(1.0,1.0);
00223 dstate->setAttributeAndModes(depth,StateAttribute::ON );
00224
00225 dstate->setRenderBinDetails(-2,"RenderBin");
00226
00227 geom->setStateSet( dstate );
00228
00229 Geode *geode = new Geode;
00230 geode->addDrawable( geom );
00231
00232 geode->setName( "Sky" );
00233
00234 return geode;
00235 }
00236
00237 Node* Base::makeGround(){
00238 int i, c;
00239 float theta;
00240 float ir = 1000.0f;
00241
00242 Vec3Array *coords = new Vec3Array(19);
00243 Vec2Array *tcoords = new Vec2Array(19);
00244 Vec4Array *colors = new Vec4Array(1);
00245
00246 (*colors)[0].set(1.0f,1.0f,1.0f,1.0f);
00247
00248 c = 0;
00249 (*coords)[c].set(0.0f,0.0f,0.0f);
00250 (*tcoords)[c].set(0.0f,0.0f);
00251
00252 for( i = 0; i <= 18; i++ )
00253 {
00254 theta = osg::DegreesToRadians((float)i * 20.0);
00255
00256 (*coords)[c].set(ir * cosf( theta ), ir * sinf( theta ), 0.0f);
00257 (*tcoords)[c].set((*coords)[c][0],(*coords)[c][1]);
00258
00259 c++;
00260 }
00261
00262 Geometry *geom = new Geometry;
00263
00264 geom->setVertexArray( coords );
00265
00266 geom->setTexCoordArray( 0, tcoords );
00267
00268 geom->setColorArray( colors );
00269 geom->setColorBinding( Geometry::BIND_OVERALL );
00270
00271 geom->addPrimitiveSet( new DrawArrays(PrimitiveSet::TRIANGLE_FAN,0,19) );
00272
00273 Texture2D *tex = new Texture2D;
00274
00275 tex->setImage(osgDB::readImageFile("Images/ground.rgb"));
00276 tex->setWrap( Texture2D::WRAP_S, Texture2D::REPEAT );
00277 tex->setWrap( Texture2D::WRAP_T, Texture2D::REPEAT );
00278
00279 StateSet *dstate = new StateSet;
00280 dstate->setMode( GL_LIGHTING, StateAttribute::OFF );
00281 dstate->setTextureAttributeAndModes(0, tex, StateAttribute::ON );
00282
00283 dstate->setTextureAttribute(0, new TexEnv );
00284
00285
00286
00287
00288
00289
00290
00291 dstate->setRenderBinDetails(-1,"RenderBin");
00292
00293
00294 geom->setStateSet( dstate );
00295
00296 Geode *geode = new Geode;
00297 geode->addDrawable( geom );
00298 geode->setName( "Ground" );
00299
00300
00301 ground = dCreatePlane ( odeHandle.space , 0 , 0 , 1 , 0 );
00302
00303 return geode;
00304 }
00305
00306
00307 LightSource* Base::makeLights(StateSet* stateset)
00308 {
00309
00310 Light* light_0 = new Light;
00311 light_0->setLightNum(0);
00312 light_0->setPosition(Vec4(10.0, 0, 20.0, 1.0f));
00313 light_0->setDirection(Vec3(-0.5, 0, -1.0));
00314 light_0->setAmbient(Vec4(0.6f, 0.6f, 0.6f, 1.0f));
00315 light_0->setDiffuse(Vec4(1.0f, 1.0f, 1.0f, 1.0f));
00316
00317
00318
00319 LightSource* light_source_0 = new LightSource;
00320 light_source_0->setLight(light_0);
00321 light_source_0->setLocalStateSetModes(StateAttribute::ON);
00322
00323 light_source_0->setStateSetModes(*stateset, StateAttribute::ON);
00324
00325 return light_source_0;
00326 }
00327
00328
00329
00330
00331 bool MoveEarthySkyWithEyePointTransform::computeLocalToWorldMatrix(osg::Matrix& matrix,osg::NodeVisitor* nv) const
00332 {
00333 osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
00334 if (cv)
00335 {
00336 osg::Vec3 eyePointLocal = cv->getEyeLocal();
00337 matrix.preMult(osg::Matrix::translate(eyePointLocal.x(),eyePointLocal.y(),0.0f));
00338 }
00339 return true;
00340 }
00341
00342 bool MoveEarthySkyWithEyePointTransform::computeWorldToLocalMatrix(osg::Matrix& matrix,osg::NodeVisitor* nv) const
00343 {
00344 std::cout<<"computing transform"<<std::endl;
00345
00346 osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
00347 if (cv)
00348 {
00349 osg::Vec3 eyePointLocal = cv->getEyeLocal();
00350 matrix.postMult(osg::Matrix::translate(-eyePointLocal.x(),-eyePointLocal.y(),0.0f));
00351 }
00352 return true;
00353 }
00354
00355
00356 }
00357
00358
00359