Hi , I want to create a simple 2D-sprite as a rectangle(4 vertices) with some static texture on it from the source code.
Is it possible with CS? I use CS 1.2
with "from the code" i mean without having some factory-file or world file
What would be the few lines to create the mentioned 2D-sprite?
I got some code from net that should do this:
csRef<iSprite2DState> Billboard::createSprite2DState(iMeshObject* sprite, iMaterialWrapper* matwrap)
{
csRef<iSprite2DState> state;
state = SCF_QUERY_INTERFACE(sprite, iSprite2DState);
assert(state.IsValid());
// create a 1x1 square polygon. this creates a diamond. the vertices need to be moved a bit.
state->CreateRegularVertices (4, false);
// move the vertices.
csColoredVertices& v = state->GetVertices();
// csColoredVertices is defined in sprite2d.h as an array of csSprite2DVertex's. csSprite2dVertex is
// defined in sprite2d.h as a struct containing a csVector2 named pos, and two floats named u and v.
// the polygon vertices are moved to create a square (-.5,-.5), (-.5,.5), (.5,.5), (.5,-.5) with the texxture mapped apropriately. (and right-side up).
v[0].pos.x = -0.5; v[0].u = 0;
v[0].pos.y = -0.5; v[0].v = 1;
v[1].pos.x = -0.5; v[1].u = 0;
v[1].pos.y = 0.5; v[1].v = 0;
v[2].pos.x = 0.5; v[2].u = 1;
v[2].pos.y = 0.5; v[2].v = 0;
v[3].pos.x = 0.5; v[3].u = 1;
v[3].pos.y = -0.5; v[3].v = 1;
state->SetMaterialWrapper(matwrap);
return state;
}
csRef<iMeshObject> Billboard::createSprite(iObjectRegistry* object_reg)
{
assert(object_reg);
csRef<iMeshObjectType> type;
type = CS_QUERY_REGISTRY(object_reg, iMeshObjectType);
assert(type.IsValid());
csRef<iMeshObjectFactory> factory = type->NewFactory();
assert(factory.IsValid());
return factory->NewInstance();
}
csRef<iMeshWrapper> Billboard::createMeshWrapper(iMeshObject* sprite, iEngine* engine, const char* name, iSector* sector)
{
csRef<iMeshWrapper> meshwrapper = engine->CreateMeshWrapper(sprite, name, sector);
assert(meshwrapper.IsValid());
LOG("terangreal:Billboard", 4, "setting billboard render priority to " << engine->GetRenderPriority("alpha") << " (for alpha)");
meshwrapper->SetRenderPriority (engine->GetRenderPriority ("alpha"));
meshwrapper->SetZBufMode (CS_ZBUF_TEST);
return meshwrapper;
}
void Billboard::resizeSprite(iSprite2DState* state, double x, double y)
{
csColoredVertices& v = state->GetVertices();
// the polygon vertices are moved to create a square (-.5,-.5), (-.5,.5), (.5,.5), (.5,-.5)
// with the texxture mapped apropriately. (and right-side up).
v[0].pos.x = -(x/2); v[0].u = 0;
v[0].pos.y = -(y/2); v[0].v = 1;
v[1].pos.x = -(x/2); v[1].u = 0;
v[1].pos.y = (y/2); v[1].v = 0;
v[2].pos.x = (x/2); v[2].u = 1;
v[2].pos.y = (y/2); v[2].v = 0;
v[3].pos.x = (x/2); v[3].u = 1;
v[3].pos.y = -(y/2); v[3].v = 1;
}
void Billboard::loadObject()
{
if(alreadyLoaded) return;
try
{
spriteMesh = createSprite(object_reg);
assert(spriteMesh.IsValid());
cs_sprite2d_state = createSprite2DState(spriteMesh, material);
assert(cs_sprite2d_state.IsValid());
meshwrapper = createMeshWrapper(spriteMesh, engine, getURL().getString().c_str(), sector);
assert(meshwrapper.IsValid());
cs_sprite2d_state->SetMixMode(CS_FX_ALPHA);
transformgroup->GetChildren()->Add(meshwrapper);
Object3D::loadObject();
}
catch(Vobject::NoSuchObjectError& e)
{
LOG("S3VR::Billboard", 1, "Warning: recieved NoSuchObjectError exception in loadObject(). (" << e.what() << ")");
}
catch(bad_cast)
{
LOG("S3VR::Billboard", 1, "Warning: recieved bad_cast exception in loadObject().");
}
catch(exception)
{
LOG("S3VR::Billboard", 1, "Warning: received unknown exception in loadObject().");
}
}
Maybe this woks for older versions of CS
But it doesnt work for mine.
- In the CS1.2 the methode state->GetVertices() defined in sprite2d.h has changed in its return value ( in former versions it returned a reference to array for vertices , now its a pointer
-csRef<iSprite2DState> state->SetMaterialWrapper(matwrap) there is no SetMaterialWrapper()- methode anymore for csRef<iSprite2DState> object
Sorry for these newbe questions but can someone help me to create a simple 2D-sprite out of the code with 4 vertices and some tuxture?
Thanx a lot