Пример #1
0
void EntityFootStepFX::renderParticle(Tessellator tessellator, float f, float f1, float f2, float f3, float f4, float f5) 
{
        float f6 = ((float)field_27018_a + f) / (float)field_27020_o;
        f6 *= f6;
        float f7 = 2.0F - f6 * 2.0F;
        if(f7 > 1.0F)
        {
            f7 = 1.0F;
        }
        f7 *= 0.2;
        GL11.glDisable(2896 /*GL_LIGHTING*/);
        float f8 = 0.125;
        float f9 = (float)(posX - interpPosX);
        float f10 = (float)(posY - interpPosY);
        float f11 = (float)(posZ - interpPosZ);
        float f12 = worldObj.getLightBrightness(MathHelper.floor_double(posX), MathHelper.floor_double(posY), MathHelper.floor_double(posZ));
        field_27019_p.bindTexture(field_27019_p.getTexture("/misc/footprint.png"));
        GL11.glEnable(3042 /*GL_BLEND*/);
        GL11.glBlendFunc(770, 771);
        tessellator.startDrawingQuads();
        tessellator.setColorRGBA_F(f12, f12, f12, f7);
        tessellator.addVertexWithUV(f9 - f8, f10, f11 + f8, 0.0D, 1.0D);
        tessellator.addVertexWithUV(f9 + f8, f10, f11 + f8, 1.0D, 1.0D);
        tessellator.addVertexWithUV(f9 + f8, f10, f11 - f8, 1.0D, 0.0D);
        tessellator.addVertexWithUV(f9 - f8, f10, f11 - f8, 0.0D, 0.0D);
        tessellator.draw();
        GL11.glDisable(3042 /*GL_BLEND*/);
        GL11.glEnable(2896 /*GL_LIGHTING*/);
}
Пример #2
0
 void CALLBACK cb_end_data(void *data)
 {
     Tessellator *tess = static_cast<Tessellator *>(data);
     tess->finalize_primitive();
 }
ref<Geometry> Extrusion::extrude()
{
  if (silhouette().empty())
  {
    Log::error("Extrusion::extrude(): no silhouette defined.\n");
    return NULL;
  }
  if (positionPath().empty())
  {
    Log::error("Extrusion::extrude() needs at least a non empty positionPath().\n");
    return NULL;
  }
  if (!scalingPath().empty() && scalingPath().size() != positionPath().size()-2)
  {
    Log::error("Extrusion::extrude(): scalingPath() must have the same number of control points as positionPath().\n");
    return NULL;
  }
  if (!rotationPath().empty() && rotationPath().size() != positionPath().size()-2)
  {
    Log::error("Extrusion::extrude(): rotationPath() must have the same number of control points as positionPath().\n");
    return NULL;
  }
  if (!colorPath().empty() && colorPath().size() != positionPath().size()-2)
  {
    Log::error("Extrusion::extrude(): colorPath() must have the same number of control points as positionPath().\n");
    return NULL;
  }

  ref<Geometry> geom = new Geometry;

  size_t segments = positionPath().size()-2;

  std::vector<fvec3> verts;
  verts.resize( silhouette().size() * segments );

  vl::fmat4 m = fmat4::getRotation(fvec3(0,1,0),positionPath()[1]-positionPath()[0]);

  // initialize silhouette on the x/z plane
  std::vector<vl::fvec3> projected_sil;
  projected_sil.resize(silhouette().size());
  for(unsigned i=0; i<silhouette().size(); ++i)
  {
    projected_sil[i] = m * vl::fvec3(silhouette()[i].x(),0,silhouette()[i].y()) + positionPath()[0];
  }

  // initialize plane normals from 1 to n-1 (end points are excluded)
  std::vector<fvec3> plane_normals;
  plane_normals.resize(positionPath().size());
  for(unsigned i=1; i<plane_normals.size()-1; ++i)
  {
    fvec3 p0 = positionPath()[i-1] - positionPath()[i];
    fvec3 p1 = positionPath()[i+1] - positionPath()[i];
    p0.normalize();
    p1.normalize();
    plane_normals[i] = (p1-p0).normalize();
  }

  for(unsigned i=1; i<positionPath().size()-1; ++i)
  {
    for(int j=0; j<(int)silhouette().size(); ++j)
    {
      fvec3 V = (positionPath()[i] - positionPath()[i-1]).normalize();
      const fvec3& P = projected_sil[j];
      const fvec3& orig = positionPath()[i];
      const fvec3& N = plane_normals [i];
      float d = dot(N,orig);
      float t = dot(V,N) ? (d-dot(P,N))/dot(V,N) : 0 /*error*/;
      // project current projected_sil on next plane along p0->p1 vector
      verts.at(j+silhouette().size()*(i-1)) = projected_sil[j] = P + V*t;
    }
  }

  // rotation
  if(!rotationPath().empty())
  {
    for(unsigned i=1; i<positionPath().size()-1; ++i)
    {
      fvec3 r = (positionPath()[i+1] - positionPath()[i]).normalize();
      fmat4 mat = vl::fmat4::getRotation(rotationPath()[i-1],r);
      fvec3 c;
      for(int j=0; j<(int)silhouette().size(); ++j)
        c += verts.at(j+silhouette().size()*(i-1));
      c /= (float)silhouette().size();
      for(int j=0; j<(int)silhouette().size(); ++j)
        verts.at(j+silhouette().size()*(i-1)) = (mat*(verts.at(j+silhouette().size()*(i-1))-c))+c;
    }
  }

  // scaling
  if(!scalingPath().empty())
  {
    for(unsigned i=1; i<positionPath().size()-1; ++i)
    {
      float s = scalingPath()[i-1];
      fvec3 c;
      for(int j=0; j<(int)silhouette().size(); ++j)
        c += verts.at(j+silhouette().size()*(i-1));
      c /= (float)silhouette().size();
      for(int j=0; j<(int)silhouette().size(); ++j)
        verts.at(j+silhouette().size()*(i-1)) = (s*(verts.at(j+silhouette().size()*(i-1))-c))+c;
    }
  }

  int prof_count = silhouetteMode() == SilhouetteClosed ? (int)silhouette().size() : (int)silhouette().size()-1;
  ref<DrawElementsUInt> de = new DrawElementsUInt(PT_QUADS);
  geom->drawCalls()->push_back(de.get());
  de->indexBuffer()->resize(4 * prof_count * (segments-1));
  for(size_t iseg=0; iseg<segments-1; ++iseg)
  {
    for(int iquad=0; iquad<prof_count; ++iquad)
    {
      de->indexBuffer()->at(iquad*4+iseg*4*prof_count + 3) = (iseg + 0) * (GLuint)silhouette().size() + iquad;
      de->indexBuffer()->at(iquad*4+iseg*4*prof_count + 2) = (iseg + 0) * (GLuint)silhouette().size() + (iquad+1)%silhouette().size();
      de->indexBuffer()->at(iquad*4+iseg*4*prof_count + 1) = (iseg + 1) * (GLuint)silhouette().size() + (iquad+1)%silhouette().size();
      de->indexBuffer()->at(iquad*4+iseg*4*prof_count + 0) = (iseg + 1) * (GLuint)silhouette().size() + iquad;
    }
  }

  // bottom/top caps

  size_t tess_bottom_count = 0;
  size_t tess_top_count    = 0;

  if(fillBottom())
  {
    size_t start = verts.size();
    Tessellator tessellator;
    tessellator.contours().push_back((int)silhouette().size());
    for(unsigned i=0; i<silhouette().size(); ++i)
      tessellator.contourVerts().push_back((dvec3)verts[i]);
    tessellator.setWindingRule(vl::TW_TESS_WINDING_NONZERO);
    tessellator.tessellate();
    for(unsigned i=0; i<tessellator.tessellatedTris().size(); ++i)
      verts.push_back(tessellator.tessellatedTris()[i]);
    if (tessellator.tessellatedTris().size())
      geom->drawCalls()->push_back( new DrawArrays(PT_TRIANGLES, start, tessellator.tessellatedTris().size()) );
    tess_bottom_count = tessellator.tessellatedTris().size();
  }
  if(fillTop())
  {
    size_t start = verts.size();
    Tessellator tessellator;
    tessellator.contours().push_back(silhouette().size());
    for(unsigned i=0; i<silhouette().size(); ++i)
      tessellator.contourVerts().push_back((dvec3)verts[verts.size()-i-1-tess_bottom_count]);
    tessellator.setWindingRule(vl::TW_TESS_WINDING_NONZERO);
    tessellator.tessellate();
    for(unsigned i=0; i<tessellator.tessellatedTris().size(); ++i)
      verts.push_back(tessellator.tessellatedTris()[i]);
    if (tessellator.tessellatedTris().size())
      geom->drawCalls()->push_back( new DrawArrays(PT_TRIANGLES, start, tessellator.tessellatedTris().size()) );
    tess_top_count = tessellator.tessellatedTris().size();
  }

  ref<ArrayFloat3> vert_array = new ArrayFloat3;
  geom->setVertexArray( vert_array.get() );
  vert_array->initFrom(verts);

  if (!colorPath().empty())
  {
    ref<ArrayFloat4> col_array = new ArrayFloat4;
    geom->setColorArray(col_array.get());
    col_array->resize(geom->vertexArray()->size());
    int offs = 0;
    for(size_t iseg=0; iseg<segments; ++iseg)
    {
      for(unsigned j=0; j<silhouette().size(); ++j, ++offs)
        col_array->at(offs) = colorPath()[iseg];
    }
    if (fillBottom())
    {
      for(unsigned j=0; j<tess_bottom_count; ++j, ++offs)
        col_array->at(offs) = colorPath()[0];
    }
    if (fillTop())
    {
      for(unsigned j=0; j<tess_top_count; ++j, ++offs)
        col_array->at(offs) = colorPath().back();
    }
  }

  if (!smooth())
    geom->convertDrawCallToDrawArrays();

  geom->computeNormals();

  return geom;
}
Пример #4
0
void Converter::build_scene_graph(Object &obj)
{
    // generate layer structure
    typedef std::map<int, osg::ref_ptr<osg::Group> > Layer_group_map;
    Layer_group_map lymap;

    OSG_DEBUG << "DEBUG INFO: lwosg::Converter: creating layer structure\n";

    // create a flat layer structure, no parenting since it's handled in scene files
    for (Object::Layer_map::const_iterator i=obj.layers().begin(); i!=obj.layers().end(); ++i) {
        const Layer &layer = i->second;
        osg::ref_ptr<osg::Group> new_group = new osg::Group;
        lymap[layer.number()] = new_group.get();
        if (layer.get_layer_chunk()) {
            new_group->setName(layer.get_layer_chunk()->name);
            if (layer.get_layer_chunk()->flags & 1) {
                new_group->setNodeMask(0);
            }
        } else {
            new_group->setName("Default_layer");
        }
        root_->addChild(new_group.get());
    }

    for (Object::Layer_map::iterator li=obj.layers().begin(); li!=obj.layers().end(); ++li) {
        Layer &layer = li->second;

        osg::Group *layer_group = lymap[layer.number()].get();

        OSG_DEBUG << "DEBUG INFO: lwosg::Converter: processing layer '" << layer_group->getName() << "'\n";

        for (Layer::Unit_list::iterator j=layer.units().begin(); j!=layer.units().end(); ++j) {

            OSG_DEBUG << "DEBUG INFO: lwosg::Converter: \tcreating primitives\n";

            int tess_success = 0;
            int tess_fail = 0;

            typedef std::map<const Surface *, GeometryBin> GeometryBin_map;
            GeometryBin_map bins;

            typedef std::map<const Surface *, Unit::Index_list> Remapping_map;
            Remapping_map remappings;

            // compute remapping map for default surface
            j->compute_vertex_remapping(0, remappings[0]);

            // compute remapping maps for other surfaces
            for (Object::Surface_map::const_iterator h=obj.surfaces().begin(); h!=obj.surfaces().end(); ++h) {
                j->compute_vertex_remapping(&h->second, remappings[&h->second]);
            }

            // create primitive sets, taking into account remapping maps
            for (unsigned k=0; k<j->polygons().size(); ++k) {
                const Polygon &poly = j->polygons()[k];
                GeometryBin &bin = bins[poly.get_surface()];
                const Unit::Index_list &remapping = remappings[poly.get_surface()];

                if (poly.indices().size() == 1) {
                    bin.deui_points->push_back(remapping[poly.indices()[0]]);
                }
                if (poly.indices().size() == 2) {
                    bin.deui_lines->push_back(remapping[poly.indices()[0]]);
                    bin.deui_lines->push_back(remapping[poly.indices()[1]]);
                }
                if (poly.indices().size() == 3) {
                    bin.deui_triangles->push_back(remapping[poly.indices()[0]]);
                    bin.deui_triangles->push_back(remapping[poly.indices()[1]]);
                    bin.deui_triangles->push_back(remapping[poly.indices()[2]]);
                }
                if (poly.indices().size() >= 4) {
                    Tessellator tess;
                    if (tess.tessellate(poly, j->points(), bin.deui_triangles.get(), &remapping)) {
                        ++tess_success;
                    } else {
                        ++tess_fail;
                    }
                }
            }

            if (tess_success > 0) {
                OSG_DEBUG << "DEBUG INFO: lwosg::Converter:   " << tess_success << " polygons have been tessellated correctly\n";
            }

            if (tess_fail > 0) {
                OSG_WARN << "Warning: lwosg::Converter:   could not tessellate " << tess_fail << " polygons correctly. This is probably due to self-intersecting polygons being used, try to Triple them in Lightwave and restart the conversion" << std::endl;
            }

            // create normal array
            osg::ref_ptr<osg::Vec3Array> normals = j->normals()->asVec3Array(j->points()->size());

            // create first geode
            osg::ref_ptr<osg::Geode> geode = new osg::Geode;

            for (GeometryBin_map::iterator i=bins.begin(); i!=bins.end(); ++i) {
                const Surface *surface = i->first;
                GeometryBin &bin = i->second;

                const Unit::Index_list &remapping = remappings[surface];

                // clean up points and normals according to remapping map
                OSG_DEBUG << "DEBUG INFO: lwosg::Converter: \tcleaning up redundant vertices and vertex attributes for surface '" << (surface ? surface->get_name() : std::string("anonymous")) << "'\n";
                osg::ref_ptr<osg::Vec3Array> new_points = new osg::Vec3Array;
                osg::ref_ptr<osg::Vec3Array> new_normals = new osg::Vec3Array;
                for (unsigned pi=0; pi<j->points()->size(); ++pi) {
                    if (remapping[pi] != -1) {
                        new_points->push_back((*j->points())[pi]);
                        new_normals->push_back((*normals)[pi]);
                    }
                }

                OSG_DEBUG << "DEBUG INFO: lwosg::Converter: \tcreating geometry for surface '" << (surface ? surface->get_name() : std::string("anonymous")) << "'\n";

                osg::ref_ptr<osg::Geometry> geo = new osg::Geometry;
                geo->setVertexArray(new_points.get());
                geo->setNormalArray(new_normals.get(), osg::Array::BIND_PER_VERTEX);

                bool group_used = false;

                if (surface) {
                    if (!options_.combine_geodes)
                    {
                        geode->setName(surface->get_name());
                    }

                    // apply surface parameters and texture/color maps according to remapping map
                    osg::ref_ptr<VertexMap_map> rm_texture_maps = j->texture_maps()->remap(remapping);
                    osg::ref_ptr<VertexMap_map> rm_rgb_maps = j->rgb_maps()->remap(remapping);
                    osg::ref_ptr<VertexMap_map> rm_rgba_maps = j->rgba_maps()->remap(remapping);
                    osg::Group *sgrp = surface->apply(geo.get(),
                        rm_texture_maps.get(),
                        rm_rgb_maps.get(),
                        rm_rgba_maps.get(),
                        options_.max_tex_units,
                        options_.use_osgfx,
                        options_.force_arb_compression,
                        options_.texturemap_bindings,
                        db_options_.get());
                    if (sgrp)
                    {
                        group_used = true;
                        osg::ref_ptr<osg::Geode> grp_geode = new osg::Geode;
                        grp_geode->setName(surface->get_name());
                        grp_geode->addDrawable(geo.get());
                        sgrp->addChild(grp_geode.get());
                        layer_group->addChild(sgrp);
                    }
                }

                if (!group_used)
                {
                    geode->addDrawable(geo.get());
                    if (geode->getNumParents() == 0)
                    {
                        layer_group->addChild(geode.get());
                    }
                }

                if (!options_.combine_geodes)
                {
                    geode = new osg::Geode;
                }

                // add primitive sets to geometry
                if (!bin.deui_points->empty()) geo->addPrimitiveSet(bin.deui_points.get());
                if (!bin.deui_lines->empty()) geo->addPrimitiveSet(bin.deui_lines.get());
                if (!bin.deui_triangles->empty()) geo->addPrimitiveSet(bin.deui_triangles.get());
            }
        }

        osg::ref_ptr<osg::CullFace> cf = new osg::CullFace;
        cf->setMode(osg::CullFace::BACK);
        root_->getOrCreateStateSet()->setAttributeAndModes(cf.get());

        if (options_.apply_light_model) {
            osg::ref_ptr<osg::LightModel> lm = new osg::LightModel;
            lm->setTwoSided(true);
            lm->setColorControl(osg::LightModel::SEPARATE_SPECULAR_COLOR);
            lm->setAmbientIntensity(osg::Vec4(0, 0, 0, 0));
            lm->setLocalViewer(true);
            root_->getOrCreateStateSet()->setAttributeAndModes(lm.get());
        }
    }
}
Пример #5
0
void Extruder::extrude(){
	if (silhouette().empty())
	{
		Log::error("Extrusion::extrude(): no silhouette defined.\n");
		this->errFlag = true;
		return ;
	}
	if (positionPath().empty())
	{
		Log::error("Extrusion::extrude() needs at least a non empty positionPath().\n");
		this->errFlag = true;
		return ;
	}
	if (!scalingPath().empty() && scalingPath().size() != positionPath().size()-2)
	{
		Log::error("Extrusion::extrude(): scalingPath() must have the same number of control points as positionPath().\n");
		this->errFlag = true;
		return ;
	}
	if (!rotationPath().empty() && rotationPath().size() != positionPath().size()-2)
	{
		Log::error("Extrusion::extrude(): rotationPath() must have the same number of control points as positionPath().\n");
		this->errFlag = true;
		return ;
	}
	if (!colorPath().empty() && colorPath().size() != positionPath().size()-2)
	{
		Log::error("Extrusion::extrude(): colorPath() must have the same number of control points as positionPath().\n");
		this->errFlag = true;
		return ;
	}

	size_t segments = positionPath().size()-2;
	currPos = vertices.size();

	vertices.resize( currPos + silhouette().size() * segments );

	vl::fmat4 m = fmat4::getRotation(fvec3(0,1,0),positionPath()[1]-positionPath()[0]);

	// initialize silhouette on the x/z plane
	std::vector<vl::fvec3> projected_sil;
	projected_sil.resize(silhouette().size());
	for(unsigned i=0; i<silhouette().size(); ++i)
	{
		projected_sil[i] = m * vl::fvec3(silhouette()[i].x(),0,silhouette()[i].y()) + positionPath()[0];
	}

	// initialize plane normals from 1 to n-1 (end points are excluded)
	std::vector<fvec3> plane_normals;
	plane_normals.resize(positionPath().size());
	for(unsigned i=1; i<plane_normals.size()-1; ++i)
	{
		fvec3 p0 = positionPath()[i-1] - positionPath()[i];
		fvec3 p1 = positionPath()[i+1] - positionPath()[i];
		p0.normalize();
		p1.normalize();
		plane_normals[i] = (p1-p0).normalize();
	}

	for(unsigned i=1; i<positionPath().size()-1; ++i)
	{
		for(int j=0; j<(int)silhouette().size(); ++j)
		{
		  fvec3 V = (positionPath()[i] - positionPath()[i-1]).normalize();
		  const fvec3& P = projected_sil[j];
		  const fvec3& orig = positionPath()[i];
		  const fvec3& N = plane_normals [i];
		  float d = dot(N,orig);
		  float t = dot(V,N) ? (d-dot(P,N))/dot(V,N) : 0;
		  // project current projected_sil on next plane along p0->p1 vector
		  vertices.at(currPos+j+silhouette().size()*(i-1)) = projected_sil[j] = P + V*t;
		}
	}


	// rotation
	if(!rotationPath().empty())
	{
		for(unsigned i=1; i<positionPath().size()-1; ++i)
		{
			fvec3 r = (positionPath()[i+1] - positionPath()[i]).normalize();
			fmat4 mat = vl::fmat4::getRotation(rotationPath()[i-1],r);
			fvec3 c;
			for(int j=0; j<(int)silhouette().size(); ++j)
			c += vertices.at(currPos+j+silhouette().size()*(i-1));
			c /= (float)silhouette().size();
			for(int j=0; j<(int)silhouette().size(); ++j)
			vertices.at(currPos+j+silhouette().size()*(i-1)) = (mat*(vertices.at(currPos+j+silhouette().size()*(i-1))-c))+c;
		}
	}

	// scaling
	if(!scalingPath().empty())
	{
		for(unsigned i=1; i<positionPath().size()-1; ++i)
		{
			float s = scalingPath()[i-1];
			fvec3 c;
			for(int j=0; j<(int)silhouette().size(); ++j)
			c += vertices.at(currPos+j+silhouette().size()*(i-1));
			c /= (float)silhouette().size();
			for(int j=0; j<(int)silhouette().size(); ++j)
			vertices.at(currPos+j+silhouette().size()*(i-1)) = (s*(vertices.at(currPos+j+silhouette().size()*(i-1))-c))+c;
		}
	}

	int prof_count = silhouetteMode() == SilhouetteClosed ? (int)silhouette().size() : (int)silhouette().size()-1;
	currDE = de->indexBuffer()->size();
	de->indexBuffer()->resize(currDE + 4 * prof_count * (segments-1));
	for(size_t iseg=0; iseg<segments-1; ++iseg)
	{
		for(int iquad=0; iquad<prof_count; ++iquad)
		{
			de->indexBuffer()->at(currDE + iquad*4+iseg*4*prof_count + 3) = currPos +  (iseg + 0) * (GLuint)silhouette().size() + iquad;
			de->indexBuffer()->at(currDE + iquad*4+iseg*4*prof_count + 2) = currPos +(iseg + 0) * (GLuint)silhouette().size() + (iquad+1)%silhouette().size();
			de->indexBuffer()->at(currDE + iquad*4+iseg*4*prof_count + 1) = currPos +(iseg + 1) * (GLuint)silhouette().size() + (iquad+1)%silhouette().size();
			de->indexBuffer()->at(currDE + iquad*4+iseg*4*prof_count + 0) = currPos +(iseg + 1) * (GLuint)silhouette().size() + iquad;
		}
	}

	// bottom/top caps

	size_t tess_bottom_count = 0;
	size_t tess_top_count    = 0;

	if(fillBottom())
	{
		size_t start = vertices.size();
		Tessellator tessellator;
		tessellator.contours().push_back((int)silhouette().size());
		for(unsigned i=0; i<silhouette().size(); ++i){
			tessellator.contourVerts().push_back((dvec3)vertices[currPos+i]);
		}
		tessellator.setWindingRule(vl::TW_TESS_WINDING_NONZERO);
		tessellator.tessellate();
		for(unsigned i=0; i<tessellator.tessellatedTris().size(); ++i){
			vertices.push_back(tessellator.tessellatedTris()[i]);
		}
		if (tessellator.tessellatedTris().size()){
			geom->drawCalls()->push_back( new DrawArrays(PT_TRIANGLES, start, tessellator.tessellatedTris().size()) );
		}tess_bottom_count = tessellator.tessellatedTris().size();
	}
	if(fillTop())
	{
		size_t start = vertices.size();
		Tessellator tessellator;
		tessellator.contours().push_back(silhouette().size());
		for(unsigned i=0; i<silhouette().size(); ++i){
			tessellator.contourVerts().push_back((dvec3)vertices[vertices.size()-i-1-tess_bottom_count]);
		}
		tessellator.setWindingRule(vl::TW_TESS_WINDING_NONZERO);
		tessellator.tessellate();
		for(unsigned i=0; i<tessellator.tessellatedTris().size(); ++i){
			vertices.push_back(tessellator.tessellatedTris()[i]);
		}
		if (tessellator.tessellatedTris().size()){
			geom->drawCalls()->push_back( new DrawArrays(PT_TRIANGLES, start, tessellator.tessellatedTris().size()) );
		}
		tess_top_count = tessellator.tessellatedTris().size();
	}

}