#ifndef
_H_SPACEFIELD#define
_H_SPACEFIELD#include
"ext_includes.h"#include
"util.h"class
SpaceField{
public
:SpaceField();
virtual ~SpaceField(); void PutIntoScene(osg::Group * scene); void UpdatePosition(osg::Vec3 position,osg::Vec3 speed);protected
:osg::MatrixTransform * mTransformShip;
osg::MatrixTransform * mTransformField;
osg::Group * mDescent;
osg::Group * mNode;
osg::Group * mScene;
int NumberOfVertex; int NumberOfVertexInBox; int Radius; int Frontier; int Frontier2;osg::Vec3 Camera;
osg::Vec3 Center;
};
#endif
#include
"SpaceField.h"
SpaceField::SpaceField() :
mNode(0),
mScene(0),
mTransformShip(0),
mTransformField(0)
{
NumberOfVertexInBox=256;
NumberOfVertex=NumberOfVertexInBox*(4*4*4);
Radius=10000.0f;
Frontier=Radius*0.25f;
Center=osg::Vec3(0,0,0);
Camera=osg::Vec3(0,0,0);
}
SpaceField::~SpaceField()
{
}
void
SpaceField::PutIntoScene(osg::Group * scene){
osg::Geometry* geometry =
new osg::Geometry; // set up verticesosg::Vec3Array* vertices =
new osg::Vec3Array(NumberOfVertex);geometry->setVertexArray(vertices);
for (int i=0;i<NumberOfVertexInBox;i++){
float x1=random(0,1)*Frontier; float x2=random(0,1)*Frontier; float x3=random(0,1)*Frontier;(*vertices)[i].set(x1,x2,x3);
}
for (int i=0;i<4;i++) for (int j=0;j<4;j++) for (int k=0;k<4;k++){
if (i==0 && j==0 && k==0) continue; float xo=Frontier*i; float yo=Frontier*j; float zo=Frontier*k; int idx2=i*16*NumberOfVertexInBox+j*4*NumberOfVertexInBox+k*NumberOfVertexInBox; for (int idx=0;idx<NumberOfVertexInBox;idx++){
(*vertices)[idx2+idx].set((*vertices)[idx].x()+xo,
(*vertices)[idx].y()+yo,
(*vertices)[idx].z()+zo);
}
}
for (int i=0;i<NumberOfVertex;i++){
(*vertices)[i].set(((*vertices)[i].x()-2.*Frontier),
((*vertices)[i].y()-2.*Frontier),
((*vertices)[i].z()-2.*Frontier));
}
// set up colours
osg::Vec4Array* colours =
new osg::Vec4Array(NumberOfVertex);geometry->setColorArray(colours);
geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
for (int i=0;i<NumberOfVertex;i++)(*colours)[i].set(1.0f,1.0f,1.0f,1.0f);
// set up the points for the stars.
osg::DrawElementsUShort* points =
new osg::DrawElementsUShort(GL_POINTS,NumberOfVertex);geometry->addPrimitiveSet(points);
for(int i=0;i<NumberOfVertex;++i){
(*points)[i] = i;
}
geometry->setUseDisplayList(
false); // geometry->setDrawCallback(new DrawCallback);osg::Geode* geode =
new osg::Geode;geode->addDrawable(geometry);
geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
osg::Group* group =
new osg::Group;group->addChild(geode);
mNode=group;
mDescent=
new osg::Group();mTransformShip=
new osg::MatrixTransform();mDescent->addChild(mTransformShip);
mScene=scene;
mScene->addChild(mDescent);
mTransformField=
new osg::MatrixTransform();// mTransformShip->addChild(mTransformField);
osg::ClipNode *cn=
new osg::ClipNode();cn->createClipBox(osg::BoundingBox(-2500,-2500,-2500,2500,2500,2500));
// cn->addClipPlane(new osg::ClipPlane(0,osg::Vec4d(2500,0,0,1)));
mTransformShip->addChild(cn);
cn->addChild(mTransformField);
mTransformField->addChild(mNode);
}
void
SpaceField::UpdatePosition(osg::Vec3 position,osg::Vec3 speed){
osg::Vec3 star=position;
star*=100.;
double x=star.x(); double y=star.y(); double z=star.z();x=fmod(x,Frontier);
y=fmod(y,Frontier);
z=fmod(z,Frontier);
star=osg::Vec3(x,y,z);
star*=-1.;
osg::Matrix matrix=mTransformShip->getMatrix();
matrix.setTrans ( position.x(), position.y(),position.z());
mTransformShip->setMatrix(matrix);
osg::Matrix matrix2=mTransformField->getMatrix();
matrix2.setTrans ( star.x(), star.y(),star.z());
mTransformField->setMatrix(matrix2);
}
Yes It is working great!Clipplanes aranged into box are working great!!! Thanks guys!I aranged nodes into hierarhy, adjusted some transformation and cliping and voila, the random star field now function as infinite one.I was wondering, how many vertex to you think graphic card should display without problems (nvidia 6600gt). Is this cliping process fast ( less than 10% of stars are actually visible)Secondly, i was wondering how to make those stars resolution independent and transparent.
2006/11/2, Robert Osfield <[EMAIL PROTECTED]>:On 11/2/06, Ivan Bolčina < [EMAIL PROTECTED]> wrote:
> Hmmm, there seems to be ClipNode. Can I clip my node with it?
Well try it, osgclip cow.osg
_______________________________________________
osg-users mailing list
[email protected]
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/
_______________________________________________ osg-users mailing list [email protected] http://openscenegraph.net/mailman/listinfo/osg-users http://www.openscenegraph.org/
