#include <simgear/misc/texcoord.hxx>
#include <simgear/scene/material/mat.hxx>
#include <simgear/scene/material/matlib.hxx>
+#include <simgear/scene/tgdb/leaf.hxx>
+#include <simgear/scene/tgdb/pt_lights.hxx>
#include <Main/globals.hxx>
#include <Main/fg_props.hxx>
#include <Time/light.hxx>
-#include "pt_lights.hxx"
+
#include "obj.hxx"
SG_USING_STD(string);
}
-static void gen_random_surface_points( ssgLeaf *leaf, ssgVertexArray *lights,
- double factor ) {
- int tris = leaf->getNumTriangles();
- if ( tris > 0 ) {
- short int n1, n2, n3;
- float *p1, *p2, *p3;
- sgVec3 result;
-
- // generate a repeatable random seed
- p1 = leaf->getVertex( 0 );
- unsigned int seed = (unsigned int)(fabs(p1[0]*100));
- sg_srandom( seed );
-
- for ( int i = 0; i < tris; ++i ) {
- leaf->getTriangle( i, &n1, &n2, &n3 );
- p1 = leaf->getVertex(n1);
- p2 = leaf->getVertex(n2);
- p3 = leaf->getVertex(n3);
- double area = sgTriArea( p1, p2, p3 );
- double num = area / factor;
-
- // generate a light point for each unit of area
- while ( num > 1.0 ) {
- random_pt_inside_tri( result, p1, p2, p3 );
- lights->add( result );
- num -= 1.0;
- }
- // for partial units of area, use a zombie door method to
- // create the proper random chance of a light being created
- // for this triangle
- if ( num > 0.0 ) {
- if ( sg_random() <= num ) {
- // a zombie made it through our door
- random_pt_inside_tri( result, p1, p2, p3 );
- lights->add( result );
- }
- }
- }
- }
-}
-
-
/**
* User data for populating leaves when they come in range.
*/
// Scenery loaders.
////////////////////////////////////////////////////////////////////////
-ssgLeaf *gen_leaf( const string& path,
- const GLenum ty,
- SGMaterialLib *matlib, const string& material,
- const point_list& nodes, const point_list& normals,
- const point_list& texcoords,
- const int_list& node_index,
- const int_list& normal_index,
- const int_list& tex_index,
- const bool calc_lights, ssgVertexArray *lights )
-{
- double tex_width = 1000.0, tex_height = 1000.0;
- ssgSimpleState *state = NULL;
- float coverage = -1;
-
- SGMaterial *mat = matlib->find( material );
- if ( mat == NULL ) {
- // see if this is an on the fly texture
- string file = path;
- string::size_type pos = file.rfind( "/" );
- file = file.substr( 0, pos );
- // cout << "current file = " << file << endl;
- file += "/";
- file += material;
- // cout << "current file = " << file << endl;
- if ( ! matlib->add_item( file ) ) {
- SG_LOG( SG_TERRAIN, SG_ALERT,
- "Ack! unknown usemtl name = " << material
- << " in " << path );
- } else {
- // locate our newly created material
- mat = matlib->find( material );
- if ( mat == NULL ) {
- SG_LOG( SG_TERRAIN, SG_ALERT,
- "Ack! bad on the fly material create = "
- << material << " in " << path );
- }
- }
- }
-
- if ( mat != NULL ) {
- // set the texture width and height values for this
- // material
- tex_width = mat->get_xsize();
- tex_height = mat->get_ysize();
- state = mat->get_state();
- coverage = mat->get_light_coverage();
- // cout << "(w) = " << tex_width << " (h) = "
- // << tex_width << endl;
- } else {
- coverage = -1;
- }
-
- sgVec2 tmp2;
- sgVec3 tmp3;
- sgVec4 tmp4;
- int i;
-
- // vertices
- int size = node_index.size();
- if ( size < 1 ) {
- SG_LOG( SG_TERRAIN, SG_ALERT, "Woh! node list size < 1" );
- exit(-1);
- }
- ssgVertexArray *vl = new ssgVertexArray( size );
- Point3D node;
- for ( i = 0; i < size; ++i ) {
- node = nodes[ node_index[i] ];
- sgSetVec3( tmp3, node[0], node[1], node[2] );
- vl -> add( tmp3 );
- }
-
- // normals
- Point3D normal;
- ssgNormalArray *nl = new ssgNormalArray( size );
- if ( normal_index.size() ) {
- // object file specifies normal indices (i.e. normal indices
- // aren't 'implied'
- for ( i = 0; i < size; ++i ) {
- normal = normals[ normal_index[i] ];
- sgSetVec3( tmp3, normal[0], normal[1], normal[2] );
- nl -> add( tmp3 );
- }
- } else {
- // use implied normal indices. normal index = vertex index.
- for ( i = 0; i < size; ++i ) {
- normal = normals[ node_index[i] ];
- sgSetVec3( tmp3, normal[0], normal[1], normal[2] );
- nl -> add( tmp3 );
- }
- }
-
- // colors
- ssgColourArray *cl = new ssgColourArray( 1 );
- sgSetVec4( tmp4, 1.0, 1.0, 1.0, 1.0 );
- cl->add( tmp4 );
-
- // texture coordinates
- size = tex_index.size();
- Point3D texcoord;
- ssgTexCoordArray *tl = new ssgTexCoordArray( size );
- if ( size == 1 ) {
- texcoord = texcoords[ tex_index[0] ];
- sgSetVec2( tmp2, texcoord[0], texcoord[1] );
- sgSetVec2( tmp2, texcoord[0], texcoord[1] );
- if ( tex_width > 0 ) {
- tmp2[0] *= (1000.0 / tex_width);
- }
- if ( tex_height > 0 ) {
- tmp2[1] *= (1000.0 / tex_height);
- }
- tl -> add( tmp2 );
- } else if ( size > 1 ) {
- for ( i = 0; i < size; ++i ) {
- texcoord = texcoords[ tex_index[i] ];
- sgSetVec2( tmp2, texcoord[0], texcoord[1] );
- if ( tex_width > 0 ) {
- tmp2[0] *= (1000.0 / tex_width);
- }
- if ( tex_height > 0 ) {
- tmp2[1] *= (1000.0 / tex_height);
- }
- tl -> add( tmp2 );
- }
- }
-
- ssgLeaf *leaf = new ssgVtxTable ( ty, vl, nl, tl, cl );
-
- // lookup the state record
-
- leaf->setState( state );
-
- if ( calc_lights ) {
- if ( coverage > 0.0 ) {
- if ( coverage < 10000.0 ) {
- SG_LOG(SG_INPUT, SG_ALERT, "Light coverage is "
- << coverage << ", pushing up to 10000");
- coverage = 10000;
- }
- gen_random_surface_points(leaf, lights, coverage);
- }
- }
-
- return leaf;
-}
-
-
// Load an Binary obj file
bool fgBinObjLoad( const string& path, const bool is_base,
Point3D *center,
} else {
material = pt_materials[i];
tex_index.clear();
- ssgLeaf *leaf = gen_leaf( path, GL_POINTS, matlib, material,
- nodes, normals, texcoords,
- pts_v[i], pts_n[i], tex_index,
- false, ground_lights );
+ ssgLeaf *leaf = sgMakeLeaf( path, GL_POINTS, matlib, material,
+ nodes, normals, texcoords,
+ pts_v[i], pts_n[i], tex_index,
+ false, ground_lights );
geometry->addKid( leaf );
}
}
group_list const& tris_n = obj.get_tris_n();
group_list const& tris_tc = obj.get_tris_tc();
for ( i = 0; i < tris_v.size(); ++i ) {
- ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLES, matlib, tri_materials[i],
- nodes, normals, texcoords,
- tris_v[i], tris_n[i], tris_tc[i],
- is_base, ground_lights );
+ ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLES, matlib,
+ tri_materials[i],
+ nodes, normals, texcoords,
+ tris_v[i], tris_n[i], tris_tc[i],
+ is_base, ground_lights );
if ( use_random_objects ) {
SGMaterial *mat = matlib->find( tri_materials[i] );
group_list const& strips_n = obj.get_strips_n();
group_list const& strips_tc = obj.get_strips_tc();
for ( i = 0; i < strips_v.size(); ++i ) {
- ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLE_STRIP,
- matlib, strip_materials[i],
- nodes, normals, texcoords,
- strips_v[i], strips_n[i], strips_tc[i],
- is_base, ground_lights );
+ ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLE_STRIP,
+ matlib, strip_materials[i],
+ nodes, normals, texcoords,
+ strips_v[i], strips_n[i], strips_tc[i],
+ is_base, ground_lights );
if ( use_random_objects ) {
SGMaterial *mat = matlib->find( strip_materials[i] );
group_list const& fans_n = obj.get_fans_n();
group_list const& fans_tc = obj.get_fans_tc();
for ( i = 0; i < fans_v.size(); ++i ) {
- ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLE_FAN,
- matlib, fan_materials[i],
- nodes, normals, texcoords,
- fans_v[i], fans_n[i], fans_tc[i],
- is_base, ground_lights );
+ ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLE_FAN,
+ matlib, fan_materials[i],
+ nodes, normals, texcoords,
+ fans_v[i], fans_n[i], fans_tc[i],
+ is_base, ground_lights );
if ( use_random_objects ) {
SGMaterial *mat = matlib->find( fan_materials[i] );
if ( mat == NULL ) {