Ejemplo n.º 1
0
void SceneManager::vist(const NodePtr &node)
{
    if (node->getEntity()->getTransform()->refreshFlags & Transform::RF_LOCAL_TRANSFORM) {
        this->sceneTransformUpdateBucket.push_back(node);
    }

    if (node->isVisible()) {
        auto renderElement = node->getComponent<RenderElement>();
        if (renderElement) {
            auto transform = node->getEntity()->getTransform();
            auto material = renderElement->getMaterial();
            auto mesh = renderElement->getMesh();
            this->renderManger.getRenderQueue().addRenderable(transform->getWorldMatrix(),
                                                              material, mesh->getMeshData(), material->getQueueID());
        } else if (node->getComponent<ui::ElementBatcher>()) {
            auto batcher = node->getComponent<ui::ElementBatcher>();

            batcher->Render();
        }

        for (auto& child : node->getChildren()) {
            this->vist(child);
        }
    }
}
Ejemplo n.º 2
0
foundation::auto_release_ptr<renderer::MeshObject> createMesh(const MObject& mobject)
{
    MStatus stat = MStatus::kSuccess;
    MFnMesh meshFn(mobject, &stat);
    CHECK_MSTATUS(stat);

    MPointArray points;
    MFloatVectorArray normals;
    MFloatArray uArray, vArray;
    MIntArray triPointIds, triNormalIds, triUvIds, triMatIds, perFaceAssignments;
    getMeshData(mobject, points, normals, uArray, vArray, triPointIds, triNormalIds, triUvIds, triMatIds, perFaceAssignments);

    foundation::auto_release_ptr<renderer::MeshObject> mesh(
        renderer::MeshObjectFactory::create(
            makeGoodString(meshFn.fullPathName()).asChar(),
            renderer::ParamArray()));

    for (unsigned int i = 0; i < points.length(); i++)
        mesh->push_vertex(mPointToGVector3(points[i]));

    for (unsigned int i = 0; i < normals.length(); i++)
        mesh->push_vertex_normal(mPointToGVector3(normals[i]));

    for (unsigned int i = 0; i < uArray.length(); i++)
    {
        mesh->push_tex_coords(
            renderer::GVector2(
                static_cast<float>(uArray[i]),
                static_cast<float>(vArray[i])));
    }

    const unsigned int numTris = triPointIds.length() / 3;

    for (unsigned int i = 0; i < numTris; i++)
    {
        const int perFaceShadingGroup = triMatIds[i];
        const int vtxId0 = triPointIds[i * 3 + 0];
        const int vtxId1 = triPointIds[i * 3 + 1];
        const int vtxId2 = triPointIds[i * 3 + 2];
        const int normalId0 = triNormalIds[i * 3 + 0];
        const int normalId1 = triNormalIds[i * 3 + 1];
        const int normalId2 = triNormalIds[i * 3 + 2];
        const int uvId0 = triUvIds[i * 3 + 0];
        const int uvId1 = triUvIds[i * 3 + 1];
        const int uvId2 = triUvIds[i * 3 + 2];
        mesh->push_triangle(
            renderer::Triangle(
                vtxId0, vtxId1, vtxId2,
                normalId0, normalId1, normalId2,
                uvId0, uvId1, uvId2,
                perFaceShadingGroup));
    }

    return mesh;
}
Ejemplo n.º 3
0
int ProjectShell::project()
{
  double dist1= dist(m_direction);
  if (dist1==0)
    {
      std::cerr << " null direction \n";
      return 1;
    }
  // normalize direction
  for (int k=0; k<3; k++)
    {
      m_direction[k]/=dist1;
    }
  int ret = getMeshData();
  if (ret)
    {
      std::cout<< " bad mesh\n" ;
      return 1;
    }
  // we have now the 3D mesh
  // verify orientation of the triangles; is it manifold?
  ret =  checkMeshValidity();
  if (ret)
    {
      std::cout<< " bad orientation\n" ;
      return 1;
    }
  
  ret =  projectIn2D();
  if (ret)
    {
      std::cout<< " cannot project in 2d\n" ;
      return 1;
    }
  ret =  computeIntersections();
  if (ret)
    {
      std::cout<< " error in computing intersections\n" ;
      return 1;
    }
  return 0;
}
Ejemplo n.º 4
0
void defineMesh(Corona::IGeometryGroup *group, const MObject& meshObject)
{
	MStatus stat = MStatus::kSuccess;
	bool hasDisplacement = false;
	Corona::SharedPtr<Corona::Abstract::Map> displacementMap = nullptr;
	float displacementMin = 0.0f;
	float displacementMax = 0.01f;

	MFnMesh meshFn(meshObject, &stat);
	CHECK_MSTATUS(stat);

	MPointArray points;
	MFloatVectorArray normals;
	MFloatArray uArray, vArray;
	MIntArray triPointIds, triNormalIds, triUvIds, triMatIds, perFaceAssignments;
	MObject mo = meshObject;
	getMeshData(mo, points, normals, uArray, vArray, triPointIds, triNormalIds, triUvIds, triMatIds, perFaceAssignments);


	Logging::debug(MString("Translating mesh object ") + meshFn.name().asChar());
	MString meshFullName = makeGoodString(meshFn.fullPathName());

	uint numVertices = points.length();
	uint numNormals = normals.length();
	uint numUvs = uArray.length();

	Corona::TriangleData td;
	Corona::IGeometryGroup* geom = group;

	geom->setMapChannelCount(1);

	uint npts = 0;
	for (uint vtxId = 0; vtxId < numVertices; vtxId++)
	{
		MPoint& p = points[vtxId];
		geom->getVertices().push(Corona::Pos(p.x, p.y, p.z));
	}

	for (uint nId = 0; nId < numNormals; nId++)
	{
		MFloatVector& n = normals[nId];
		geom->getNormals().push(Corona::Dir(n.x, n.y, n.z));
	}

	for (uint tId = 0; tId < numUvs; tId++)
	{
		size_t mcl = geom->getMapCoordIndices().size();
		geom->getMapCoordIndices().push(mcl);
		geom->getMapCoords().push(Corona::Pos(uArray[tId], vArray[tId], 0.0f));
	}

	int numTris = triPointIds.length() / 3;
	for (uint triId = 0; triId < numTris; triId++)
	{
		uint index = triId * 3;
		int perFaceShadingGroup = triMatIds[triId];
		int vtxId0 = triPointIds[index];
		int vtxId1 = triPointIds[index + 1];
		int vtxId2 = triPointIds[index + 2];
		int normalId0 = triNormalIds[index];
		int normalId1 = triNormalIds[index + 1];
		int normalId2 = triNormalIds[index + 2];
		int uvId0 = triUvIds[index];
		int uvId1 = triUvIds[index + 1];
		int uvId2 = triUvIds[index + 2];

		std::auto_ptr<Corona::TriangleData> trip;

		if (hasDisplacement)
		{
			trip = std::auto_ptr<Corona::TriangleData>(new Corona::DisplacedTriangleData);
			Corona::DisplacedTriangleData *dtri = (Corona::DisplacedTriangleData *)trip.get();
			dtri->displacement.map = displacementMap;
			dtri->displacement.waterLevel = -Corona::INFINITY;
			dtri->displacement.min = displacementMin;
			dtri->displacement.max = displacementMax;
		}
		else{
			trip = std::auto_ptr<Corona::TriangleData>(new Corona::TriangleData);
		}
		trip->v.setSegments(1 - 1);
		trip->n.setSegments(1 - 1);

		for (int stepId = 0; stepId < 1; stepId++)
		{
			trip->v[stepId][0] = vtxId0 + numVertices * stepId;
			trip->v[stepId][1] = vtxId1 + numVertices * stepId;
			trip->v[stepId][2] = vtxId2 + numVertices * stepId;
			trip->n[stepId][0] = normalId0 + numNormals * stepId;
			trip->n[stepId][1] = normalId1 + numNormals * stepId;
			trip->n[stepId][2] = normalId2 + numNormals * stepId;
		}

		if (numUvs > 0)
		{
			trip->t[0] = uvId0;
			trip->t[1] = uvId1;
			trip->t[2] = uvId2;
		}
		trip->materialId = perFaceShadingGroup;
		trip->edgeVis[0] = trip->edgeVis[1] = trip->edgeVis[2] = true;
		geom->addPrimitive(*trip);
	}
}
Ejemplo n.º 5
0
void SceneGraphComponent::executeEvent(GameEvent *event)
{
	switch (event->id())
	{
	case GameEvent::E_SET_TRANSFORMATION:
		setTransformation(static_cast<const float*>(event->data()));
		break;
	case GameEvent::E_TRANSFORMATION:
		memcpy(static_cast<float*>(event->data()), m_transformation, sizeof(float) * 16);
		break;
	case GameEvent::E_SET_TRANSLATION:
		{
			Vec3f* translation = static_cast<Vec3f*>(event->data());
			m_transformation[12] = translation->x;
			m_transformation[13] = translation->y;
			m_transformation[14] = translation->z;
			sendTransformation();
		}
		break;
	case GameEvent::E_SET_SCALE:
		setScale(static_cast<Vec3f*>(event->data()));
		break;
	case GameEvent::E_MESH_DATA:
		getMeshData(static_cast<MeshData*>(event->data()));
		break;
	case GameEvent::E_TRANSLATE_LOCAL:
		translateLocal(static_cast<Vec3f*>(event->data()));
		break;
	case GameEvent::E_TRANSLATE_GLOBAL:
		translateGlobal(static_cast<Vec3f*>(event->data()));
		break;
	case GameEvent::E_ROTATE_LOCAL:
		rotate(static_cast<Vec3f*>(event->data()));
		break;
	case GameEvent::E_SET_ROTATION:
		setRotation(static_cast<Vec3f*>(event->data()));
		break;
	case GameEvent::E_ATTACH:
		attach(static_cast<Attach*>(event->data()));
		break;	
	case GameEvent::E_SET_NODE_PARENT:
		setParentNode(static_cast<Attach*>(event->data()));
		break;
	case GameEvent::E_MORPH_TARGET:
		if (m_hordeID > 0)
		{
			h3dSetModelMorpher(m_hordeID, static_cast<MorphTarget*>(event->data())->Name, static_cast<MorphTarget*>(event->data())->Value);
			h3dUpdateModel(m_hordeID, H3DModelUpdateFlags::Geometry );		
		}
		break;
	case GameEvent::E_ACTIVATE_CAM:
		if (m_hordeID > 0 && h3dGetNodeType(m_hordeID) == H3DNodeTypes::Camera)
			SceneGraphManager::instance()->setActiveCam( m_hordeID );
		break;
	case GameEvent::E_SET_ANIM_FRAME:
		{
			if (m_hordeID > 0)
			{
				const SetAnimFrame* const data = static_cast<SetAnimFrame*>(event->data());			
				h3dSetModelAnimParams(m_hordeID, data->Stage, data->Time, data->Weight);	
				h3dUpdateModel(m_hordeID, H3DModelUpdateFlags::Animation | H3DModelUpdateFlags::Geometry );		
			}
		}
		break;
	case GameEvent::E_SET_ENABLED:
		setEnabled(*static_cast<bool*>(event->data()));
		break;
	case GameEvent::E_GET_VISIBILITY:
		{
			bool* visible = static_cast<bool*>(event->data());
			if (visible)
				*visible = getVisibility();
		}
		break;
	case GameEvent::E_GET_PROJECTION_MATRIX:
		{
			float* mat = static_cast<float*>(event->data());
			SceneGraphManager::instance()->getCameraProjectionMatrix(mat);
		}
		break;
	case GameEvent::E_GET_ACTIVE_CAM:
		if (SceneGraphManager::instance()->getActiveCam() == m_hordeID)
		{
			unsigned int* id = static_cast<unsigned int*>(event->data());
			if (id)
				*id = m_owner->worldId();
		}
		break;
	case GameEvent::E_GET_SCENEGRAPH_ID:
		{
			int* id = static_cast<int*>(event->data());
			if (id)
				*id = m_hordeID;
		}
		break;
	}
}
Ejemplo n.º 6
0
void VoxelBlockPersistent::update()
{
	// Get the Block
	VoxelBlock* myBlock = volume->getBlockAbs(position.x, position.y, position.z);

	if (!myBlock->getDataChanged())
		return;
	
	if (myBlock->isPlaceholder())
	{
		myBlock->setDataChanged(false);
		return;
	}

	int myTypeID = myBlock->typeID();
	bool otherTypeID = myBlock->innerVoxelsRelevant();

	// Only update if this block is in the center of a 3x3 group of loaded blocks
	for (int dx = -1; dx < 2; dx++)
	{
		for (int dy = -1; dy < 2; dy++)
		{
			for (int dz = -1; dz < 2; dz++)
			{
				if (volume->pagingMode == PAGINGMODE_OPEN)
				{
					// Don't update boundary blocks in open mode
					if ((position.x + dx - volume->windowXInBlocks < 0) ||
						(position.x + dx - volume->windowXInBlocks >= volume->numBlocksX))
						return;
					if ((position.y + dy - volume->windowYInBlocks < 0) ||
						(position.y + dy - volume->windowYInBlocks >= volume->numBlocksY))
						return;
					if ((position.z + dz - volume->windowZInBlocks < 0) ||
						(position.z + dz - volume->windowZInBlocks >= volume->numBlocksZ))
						return;
				}
				else
				{
					if ((position.x + dx - volume->windowXInBlocks < 0) ||
						(position.x + dx - volume->windowXInBlocks >= volume->numBlocksX))
						continue;
					if ((position.y + dy - volume->windowYInBlocks < 0) ||
						(position.y + dy - volume->windowYInBlocks >= volume->numBlocksY))
						continue;
					if ((position.z + dz - volume->windowZInBlocks < 0) ||
						(position.z + dz - volume->windowZInBlocks >= volume->numBlocksZ))
						continue;
				}

				VoxelBlock* block = volume->getBlockAbs(position.x + dx, position.y + dy, position.z + dz);
				if (block->isPlaceholder())
					return;
				if (!otherTypeID && (block->typeID() != myTypeID))
					otherTypeID = true;
			}
		}
	}

	myBlock->setDataChanged(false);
	
	// Recreate geometry for this block ...
	sceneNode->setPosition(positionVec * volume->scale);
	surfaceRenderable->clearGeometry();

	if (otherTypeID)
	{
		int lastX = BLOCK_SIDE_LENGTH - 1;
		int lastY = BLOCK_SIDE_LENGTH - 1;
		int lastZ = BLOCK_SIDE_LENGTH - 1;

		if (volume->pagingMode == PAGINGMODE_CLOSED)
		{
			if (position.x - volume->windowXInBlocks == volume->numBlocksX - 1)	lastX--;
			if (position.y - volume->windowYInBlocks == volume->numBlocksY - 1) lastY--;
			if (position.z - volume->windowZInBlocks == volume->numBlocksZ - 1) lastZ--;
		}

		// Apply marching cubes
		getMeshData(lastX, lastY, lastZ);
		if (myBlock->innerVoxelsRelevant())
			getMeshDataInner(lastX, lastY, lastZ, myBlock);
	}
	else
		tempVertexDataSize = 0;

	// delete old surface data
	if (surface)
	{
		delete surface;
		surface = NULL;
	}

	if (tempVertexDataSize <= 0)
	{
		myBlock->loadObjects();
		return;
	}

	// Copy data into a SurfacePatch where it will also be accessible for the physics engine and update the renderable
	surface = new SurfacePatch(tempVertexData, tempTextureData, tempVertexNumber, tempVertexDataSize, tempIndexData, tempIndexDataSize, positionVec, volume->scale);
	surfaceRenderable->setGeometry(surface, volume->materialName.c_str());
	surfaceRenderable->createHardwareBuffers();

	sceneNode->needUpdate();

	myBlock->loadObjects();
}