sglTexture2D *tex = new sglTexture2D; tex->setImage(image_ptr, GL_ALPHA, GL_UNSIGNED_BYTE, IMAGE_SIZE_X, IMAGE_SIZE_Y, false); tex->setInternalFormat(GL_ALPHA); tex->setMagFilter(GL_NEAREST); tex->setMinFilter(GL_NEAREST); sglTexEnv *tev = new sglTexEnv(); tev->setMode(sglTexEnv::MODULATE); vector<sglStatelet*> state; state.push_back(tex); state.push_back(tev); state.push_back(new sglAlphaTest(true, GL_GREATER, 0.0f));This state can be associated with an sglDrawable (geoset) with the following call:
geoset->setState(0, state, tex_coords);where tex_coords could be a pointer to an array of sglVec2f's that correspond to the texture coordinates needed by the sglDrawable for the texture contained in state. The tex_coords could also be a reference to an sglTexCoords object that abstracts the dimension and thus could contain 1D, 2D or 3D texcoords, or it could be a reference to a vector of sglTexCoords objects which contains one sglTexCoords for each stage of the multitexture pipeline if multitexturing is being used. Note that there is also a sglGeoSet::setTexCoordList() function that takes any one of these three types as the parameter and is a convenience functions to set the texcoords for the state associated with mask zero. These must be used after the setState(..) function to set/replace texture coordinates, otherwise the setState(..) call will replace them (note the setState(..) that takes no texture coordinate parameter will remove the texture coordinates because it assume a NULL parameter).
geoset->setState(0x1, state_without_texture);More than one state with texture can also be assigned to a single sglDrawable:
geoset->setState(0x2, state_with_tex2, tex_coords2);In this example the texture requires different texture coords and a new set is passed in with the call to setState, but you can share the same texcoords if you like:
geoset->setState(0x4, state_with_tex3, tex_coords);Note that if one of the statelets is an sglTexGen that enables automatic texture coordinate generation (using SPHERE_MAP mode, for example), then texture coodinates do not need to be specified:
geoset->setState(0x8, state_with_texgen, NULL);In each of these examples a unique state mask as been used, so that the resulting geoset will have 5 different states (including the default state with mask 0).
if (cull_state_mask && geoset->state_mask[i]) return geoset->state[i];Therefore, the first one to evaluate to true is the one that will be applied for this sglDrawable.
If cull's state mask is 0, or none of the states' masks cause the above
test to evaluate to true, then sglDrawable's default state is selected.
The default state is the one with a state mask of 0. Every sglDrawable
is constructed with this state, which is empty (no sglStatelets) to begin
with. The user can change the default state of an sglDrawable, by
calling sglDrawable::setState(0, ....); with a different vector of
For the default values, SGL maintains a "full state" specifications for default statelet values which is a vector containing one of each type of sglStatelet. This is "built" at the beginning of sglScene::cull(). The override values, are initialized from a list of override statelets that the user passes into cull function (it is not a full state). As the cull traversal proceeds, these lists can be modified when sglDefaultStateNodes and sglOverrideStateNodes are encountered. Whatever are in these lists at the time a geoset is selected for draw will affect what the complete state for that geoset is. The priority is given as follows:
Therefore, associated with every sglDrawable that is selected for drawing,
a "complete" (or full) state definition is associated (as a vector of
sglStatelets). It is this definition that is used to sort the geometry.
The first two items are fixed and cannot be changed, sort order in the third item can be defined by the user, and is specified via the sglTraversalState::setSortOrder() method. The default sort order is to sort on transforms only.
Geometry with transparency enabled are sorted back to front and rendered last. Non-transparent geometry with the occlusion test enabled are sorted front to back and rendered second to last. All other geometry is sorted via the user defined sort order and rendered first.
To disable state sorting, call setSortOrder with an empty vector.
vector<sglStatelet::StateEnum> order; trav_state.setSortOrder(order);For all other sort orderings, pass a vector of StateEnums containing the desired order. For example, to sort first by lighting on/off, second by texture, and third by material:
vector<sglStatelet::StateEnum> order; order.push_back(sglStatelet::LIGHTING); order.push_back(sglStatelet::TEXTURE); order.push_back(sglStatelet::MATERIAL); trav_state.setSortOrder(order);Note, there is no "best" sort order. The best sort order is dependent on the graphics hardware/driver and the contents of your scene. See the testTraversal program for examples of two scene graphs that work best with completely different sort orders.
Also note that some sorts are more expensive than others. Obviously, the more attributes that you sort on the more time it is going to take. You should also be aware that statelets that can take on an unlimited number of values (e.g. material, texture, etc.) have a worst case O(n*logn) sort time. Statelets with a fixed number of states (e.g., any boolean statelets, polygon style, etc.) a worst case O(n) sort time.