Tidying up normal generation code.
This commit is contained in:
		
							
								
								
									
										174
									
								
								PolyVoxCore/source/GradientEstimators.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										174
									
								
								PolyVoxCore/source/GradientEstimators.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,174 @@ | ||||
| #include "GradientEstimators.h" | ||||
| #include "IndexedSurfacePatch.h" | ||||
| #include "RegionGeometry.h" | ||||
| #include "SurfaceVertex.h" | ||||
|  | ||||
| #include "boost/cstdint.hpp" | ||||
|  | ||||
| using namespace boost; | ||||
|  | ||||
| namespace PolyVox | ||||
| { | ||||
| 	POLYVOX_API void computeNormalsForVertices(BlockVolume<boost::uint8_t>* volumeData, RegionGeometry& regGeom, NormalGenerationMethod normalGenerationMethod) | ||||
| 	{ | ||||
| 		std::vector<SurfaceVertex>& vecVertices = regGeom.m_patchSingleMaterial->m_vecVertices; | ||||
| 		std::vector<SurfaceVertex>::iterator iterSurfaceVertex = vecVertices.begin(); | ||||
| 		while(iterSurfaceVertex != vecVertices.end()) | ||||
| 		{ | ||||
| 			const Vector3DFloat& v3dPos = iterSurfaceVertex->getPosition() + static_cast<Vector3DFloat>(regGeom.m_v3dRegionPosition); | ||||
| 			const Vector3DInt32 v3dFloor = static_cast<Vector3DInt32>(v3dPos); | ||||
|  | ||||
| 			BlockVolumeIterator<boost::uint8_t> volIter(*volumeData); | ||||
|  | ||||
| 			//Check all corners are within the volume, allowing a boundary for gradient estimation | ||||
| 			bool lowerCornerInside = volumeData->containsPoint(v3dFloor,1); | ||||
| 			bool upperCornerInside = volumeData->containsPoint(v3dFloor+Vector3DInt32(1,1,1),1); | ||||
|  | ||||
| 			if(lowerCornerInside && upperCornerInside) //If this test fails the vertex will be left as it was | ||||
| 			{ | ||||
| 				Vector3DFloat v3dGradient; //To store the result | ||||
|  | ||||
| 				if(normalGenerationMethod == SOBEL) | ||||
| 				{ | ||||
| 					volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor)); | ||||
| 					const Vector3DFloat gradFloor = computeSobelGradient(volIter); | ||||
| 					if((v3dPos.getX() - v3dFloor.getX()) > 0.001) | ||||
| 					{			 | ||||
| 						volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor+Vector3DInt32(1,0,0))); | ||||
| 					} | ||||
| 					if((v3dPos.getY() - v3dFloor.getY()) > 0.001) | ||||
| 					{			 | ||||
| 						volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor+Vector3DInt32(0,1,0))); | ||||
| 					} | ||||
| 					if((v3dPos.getZ() - v3dFloor.getZ()) > 0.001) | ||||
| 					{			 | ||||
| 						volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor+Vector3DInt32(0,0,1)));					 | ||||
| 					} | ||||
| 					const Vector3DFloat gradCeil = computeSobelGradient(volIter); | ||||
| 					v3dGradient = (gradFloor + gradCeil);				 | ||||
| 				} | ||||
| 				if(normalGenerationMethod == CENTRAL_DIFFERENCE) | ||||
| 				{ | ||||
| 					volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor)); | ||||
| 					const Vector3DFloat gradFloor = computeCentralDifferenceGradient(volIter); | ||||
| 					if((v3dPos.getX() - v3dFloor.getX()) > 0.001) | ||||
| 					{			 | ||||
| 						volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor+Vector3DInt32(1,0,0))); | ||||
| 					} | ||||
| 					if((v3dPos.getY() - v3dFloor.getY()) > 0.001) | ||||
| 					{			 | ||||
| 						volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor+Vector3DInt32(0,1,0))); | ||||
| 					} | ||||
| 					if((v3dPos.getZ() - v3dFloor.getZ()) > 0.001) | ||||
| 					{			 | ||||
| 						volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor+Vector3DInt32(0,0,1)));					 | ||||
| 					} | ||||
| 					const Vector3DFloat gradCeil = computeCentralDifferenceGradient(volIter); | ||||
| 					v3dGradient = (gradFloor + gradCeil); | ||||
| 				} | ||||
| 				if(v3dGradient.lengthSquared() > 0.0001) | ||||
| 				{ | ||||
| 					//If we got a normal of significant length then update it. | ||||
| 					//Otherwise leave it as it was (should be the 'simple' version) | ||||
| 					v3dGradient.normalise(); | ||||
| 					iterSurfaceVertex->setNormal(v3dGradient); | ||||
| 				} | ||||
| 			} //(lowerCornerInside && upperCornerInside) | ||||
| 			++iterSurfaceVertex; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	Vector3DFloat computeNormal(BlockVolume<uint8_t>* volumeData, const Vector3DFloat& position, NormalGenerationMethod normalGenerationMethod) | ||||
| 	{ | ||||
| 		const float posX = position.getX(); | ||||
| 		const float posY = position.getY(); | ||||
| 		const float posZ = position.getZ(); | ||||
|  | ||||
| 		const uint16_t floorX = static_cast<uint16_t>(posX); | ||||
| 		const uint16_t floorY = static_cast<uint16_t>(posY); | ||||
| 		const uint16_t floorZ = static_cast<uint16_t>(posZ); | ||||
|  | ||||
| 		//Check all corners are within the volume, allowing a boundary for gradient estimation | ||||
| 		bool lowerCornerInside = volumeData->containsPoint(Vector3DInt32(floorX, floorY, floorZ),1); | ||||
| 		bool upperCornerInside = volumeData->containsPoint(Vector3DInt32(floorX+1, floorY+1, floorZ+1),1); | ||||
| 		if((!lowerCornerInside) || (!upperCornerInside)) | ||||
| 		{ | ||||
| 			normalGenerationMethod = SIMPLE; | ||||
| 		} | ||||
|  | ||||
| 		Vector3DFloat result; | ||||
|  | ||||
| 		BlockVolumeIterator<boost::uint8_t> volIter(*volumeData); //FIXME - save this somewhere - could be expensive to create? | ||||
|  | ||||
|  | ||||
| 		if(normalGenerationMethod == SOBEL) | ||||
| 		{ | ||||
| 			volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ)); | ||||
| 			const Vector3DFloat gradFloor = computeSobelGradient(volIter); | ||||
| 			if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5 | ||||
| 			{			 | ||||
| 				volIter.setPosition(static_cast<uint16_t>(posX+1.0),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ)); | ||||
| 			} | ||||
| 			if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5 | ||||
| 			{			 | ||||
| 				volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY+1.0),static_cast<uint16_t>(posZ)); | ||||
| 			} | ||||
| 			if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5 | ||||
| 			{			 | ||||
| 				volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ+1.0));					 | ||||
| 			} | ||||
| 			const Vector3DFloat gradCeil = computeSobelGradient(volIter); | ||||
| 			result = ((gradFloor + gradCeil) * -1.0f); | ||||
| 			if(result.lengthSquared() < 0.0001) | ||||
| 			{ | ||||
| 				//Operation failed - fall back on simple gradient estimation | ||||
| 				normalGenerationMethod = SIMPLE; | ||||
| 			} | ||||
| 		} | ||||
| 		if(normalGenerationMethod == CENTRAL_DIFFERENCE) | ||||
| 		{ | ||||
| 			volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ)); | ||||
| 			const Vector3DFloat gradFloor = computeCentralDifferenceGradient(volIter); | ||||
| 			if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5 | ||||
| 			{			 | ||||
| 				volIter.setPosition(static_cast<uint16_t>(posX+1.0),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ)); | ||||
| 			} | ||||
| 			if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5 | ||||
| 			{			 | ||||
| 				volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY+1.0),static_cast<uint16_t>(posZ)); | ||||
| 			} | ||||
| 			if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5 | ||||
| 			{			 | ||||
| 				volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ+1.0));					 | ||||
| 			} | ||||
| 			const Vector3DFloat gradCeil = computeCentralDifferenceGradient(volIter); | ||||
| 			result = ((gradFloor + gradCeil) * -1.0f); | ||||
| 			if(result.lengthSquared() < 0.0001) | ||||
| 			{ | ||||
| 				//Operation failed - fall back on simple gradient estimation | ||||
| 				normalGenerationMethod = SIMPLE; | ||||
| 			} | ||||
| 		} | ||||
| 		if(normalGenerationMethod == SIMPLE) | ||||
| 		{ | ||||
| 			volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ)); | ||||
| 			const uint8_t uFloor = volIter.getVoxel() > 0 ? 1 : 0; | ||||
| 			if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5 | ||||
| 			{					 | ||||
| 				uint8_t uCeil = volIter.peekVoxel1px0py0pz() > 0 ? 1 : 0; | ||||
| 				result = Vector3DFloat(static_cast<float>(uFloor - uCeil),0.0,0.0); | ||||
| 			} | ||||
| 			else if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5 | ||||
| 			{ | ||||
| 				uint8_t uCeil = volIter.peekVoxel0px1py0pz() > 0 ? 1 : 0; | ||||
| 				result = Vector3DFloat(0.0,static_cast<float>(uFloor - uCeil),0.0); | ||||
| 			} | ||||
| 			else if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5 | ||||
| 			{ | ||||
| 				uint8_t uCeil = volIter.peekVoxel0px0py1pz() > 0 ? 1 : 0; | ||||
| 				result = Vector3DFloat(0.0, 0.0,static_cast<float>(uFloor - uCeil));					 | ||||
| 			} | ||||
| 		} | ||||
| 		return result; | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										21
									
								
								PolyVoxCore/source/SurfaceAdjusters.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								PolyVoxCore/source/SurfaceAdjusters.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| #include "SurfaceAdjusters.h" | ||||
|  | ||||
| #include "IndexedSurfacePatch.h" | ||||
| #include "RegionGeometry.h" | ||||
|  | ||||
| #include <vector> | ||||
|  | ||||
| namespace PolyVox | ||||
| { | ||||
| 	void smoothRegionGeometry(BlockVolume<boost::uint8_t>* volumeData, RegionGeometry& regGeom) | ||||
| 	{ | ||||
| 		std::vector<SurfaceVertex>& vecVertices = regGeom.m_patchSingleMaterial->m_vecVertices; | ||||
| 		std::vector<SurfaceVertex>::iterator iterSurfaceVertex = vecVertices.begin(); | ||||
| 		while(iterSurfaceVertex != vecVertices.end()) | ||||
| 		{ | ||||
| 			iterSurfaceVertex->setPosition(iterSurfaceVertex->getPosition() + iterSurfaceVertex->getNormal()); | ||||
| 			//iterSurfaceVertex->setPosition(iterSurfaceVertex->getPosition() + Vector3DFloat(10.0f,0.0f,0.0f)); | ||||
| 			++iterSurfaceVertex; | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| @@ -1,5 +1,4 @@ | ||||
| #include "SurfaceExtractors.h" | ||||
| #include "SurfaceExtractorsDecimated.h" | ||||
|  | ||||
| #include "BlockVolume.h" | ||||
| #include "GradientEstimators.h" | ||||
| @@ -7,6 +6,8 @@ | ||||
| #include "MarchingCubesTables.h" | ||||
| #include "Region.h" | ||||
| #include "RegionGeometry.h" | ||||
| #include "SurfaceAdjusters.h" | ||||
| #include "SurfaceExtractorsDecimated.h" | ||||
| #include "VolumeChangeTracker.h" | ||||
| #include "BlockVolumeIterator.h" | ||||
|  | ||||
| @@ -31,6 +32,10 @@ namespace PolyVox | ||||
|  | ||||
| 			generateDecimatedMeshDataForRegion(volume.getVolumeData(), 0, *iterChangedRegions, regionGeometry.m_patchSingleMaterial); | ||||
|  | ||||
| 			computeNormalsForVertices(volume.getVolumeData(), regionGeometry, CENTRAL_DIFFERENCE); | ||||
|  | ||||
| 			//smoothRegionGeometry(volume.getVolumeData(), regionGeometry); | ||||
|  | ||||
| 			//genMultiFromSingle(regionGeometry.m_patchSingleMaterial, regionGeometry.m_patchMultiMaterial); | ||||
|  | ||||
| 			regionGeometry.m_bContainsSingleMaterialPatch = regionGeometry.m_patchSingleMaterial->getVertices().size() > 0; | ||||
| @@ -122,15 +127,6 @@ namespace PolyVox | ||||
| 		delete[] vertexIndicesY1; | ||||
| 		delete[] vertexIndicesZ0; | ||||
| 		delete[] vertexIndicesZ1; | ||||
|  | ||||
|  | ||||
| 		std::vector<SurfaceVertex>::iterator iterSurfaceVertex = singleMaterialPatch->getVertices().begin(); | ||||
| 		while(iterSurfaceVertex != singleMaterialPatch->getVertices().end()) | ||||
| 		{ | ||||
| 			Vector3DFloat tempNormal = computeNormal(volumeData, static_cast<Vector3DFloat>(iterSurfaceVertex->getPosition() + offset), CENTRAL_DIFFERENCE); | ||||
| 			const_cast<SurfaceVertex&>(*iterSurfaceVertex).setNormal(tempNormal); | ||||
| 			++iterSurfaceVertex; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	boost::uint32_t computeInitialRoughBitmaskForSlice(BlockVolumeIterator<uint8_t>& volIter, const Region& regSlice, const Vector3DFloat& offset, uint8_t* bitmask) | ||||
| @@ -428,9 +424,10 @@ namespace PolyVox | ||||
| 			{ | ||||
| 				if((x + offset.getX()) != regSlice.getUpperCorner().getX()) | ||||
| 				{ | ||||
| 					const uint8_t v100 = volIter.peekVoxel1px0py0pz(); | ||||
| 					const Vector3DFloat v3dPosition(x + 0.5f, y, z); | ||||
| 					const Vector3DFloat v3dNormal(1.0f, 0.0f, 0.0f); | ||||
| 					const uint8_t uMaterial = v000 | volIter.peekVoxel1px0py0pz(); //Because one of these is 0, the or operation takes the max. | ||||
| 					const Vector3DFloat v3dNormal(v000 > v100 ? 1.0f : -1.0f, 0.0f, 0.0f);					 | ||||
| 					const uint8_t uMaterial = v000 | v100; //Because one of these is 0, the or operation takes the max. | ||||
| 					const SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial, 1.0); | ||||
| 					singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); | ||||
| 					vertexIndicesX[getIndex(x,y)] = singleMaterialPatch->m_vecVertices.size()-1; | ||||
| @@ -440,9 +437,10 @@ namespace PolyVox | ||||
| 			{ | ||||
| 				if((y + offset.getY()) != regSlice.getUpperCorner().getY()) | ||||
| 				{ | ||||
| 					const uint8_t v010 = volIter.peekVoxel0px1py0pz(); | ||||
| 					const Vector3DFloat v3dPosition(x, y + 0.5f, z); | ||||
| 					const Vector3DFloat v3dNormal(0.0f, 1.0f, 0.0f); | ||||
| 					const uint8_t uMaterial = v000 | volIter.peekVoxel0px1py0pz(); | ||||
| 					const Vector3DFloat v3dNormal(0.0f, v000 > v010 ? 1.0f : -1.0f, 0.0f); | ||||
| 					const uint8_t uMaterial = v000 | v010; | ||||
| 					SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial, 1.0); | ||||
| 					singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); | ||||
| 					vertexIndicesY[getIndex(x,y)] = singleMaterialPatch->m_vecVertices.size()-1; | ||||
| @@ -452,9 +450,10 @@ namespace PolyVox | ||||
| 			{ | ||||
| 				//if((z + offset.getZ()) != upperCorner.getZ()) | ||||
| 				{ | ||||
| 					const uint8_t v001 = volIter.peekVoxel0px0py1pz(); | ||||
| 					const Vector3DFloat v3dPosition(x, y, z + 0.5f); | ||||
| 					const Vector3DFloat v3dNormal(0.0f, 0.0f, 1.0f); | ||||
| 					const uint8_t uMaterial = v000 | volIter.peekVoxel0px0py1pz(); | ||||
| 					const Vector3DFloat v3dNormal(0.0f, 0.0f, v000 > v001 ? 1.0f : -1.0f); | ||||
| 					const uint8_t uMaterial = v000 | v001; | ||||
| 					SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial, 1.0); | ||||
| 					singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); | ||||
| 					vertexIndicesZ[getIndex(x,y)] = singleMaterialPatch->m_vecVertices.size()-1; | ||||
| @@ -736,7 +735,7 @@ namespace PolyVox | ||||
|  | ||||
|  | ||||
| 		//for(std::map<uint8_t, IndexedSurfacePatch*>::iterator iterPatch = surfacePatchMapResult.begin(); iterPatch != surfacePatchMapResult.end(); ++iterPatch) | ||||
| 		{ | ||||
| 		/*{ | ||||
|  | ||||
| 			std::vector<SurfaceVertex>::iterator iterSurfaceVertex = singleMaterialPatch->getVertices().begin(); | ||||
| 			while(iterSurfaceVertex != singleMaterialPatch->getVertices().end()) | ||||
| @@ -745,101 +744,7 @@ namespace PolyVox | ||||
| 				const_cast<SurfaceVertex&>(*iterSurfaceVertex).setNormal(tempNormal); | ||||
| 				++iterSurfaceVertex; | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	Vector3DFloat computeNormal(BlockVolume<uint8_t>* volumeData, const Vector3DFloat& position, NormalGenerationMethod normalGenerationMethod) | ||||
| 	{ | ||||
| 		const float posX = position.getX(); | ||||
| 		const float posY = position.getY(); | ||||
| 		const float posZ = position.getZ(); | ||||
|  | ||||
| 		const uint16_t floorX = static_cast<uint16_t>(posX); | ||||
| 		const uint16_t floorY = static_cast<uint16_t>(posY); | ||||
| 		const uint16_t floorZ = static_cast<uint16_t>(posZ); | ||||
|  | ||||
| 		//Check all corners are within the volume, allowing a boundary for gradient estimation | ||||
| 		bool lowerCornerInside = volumeData->containsPoint(Vector3DInt32(floorX, floorY, floorZ),1); | ||||
| 		bool upperCornerInside = volumeData->containsPoint(Vector3DInt32(floorX+1, floorY+1, floorZ+1),1); | ||||
| 		if((!lowerCornerInside) || (!upperCornerInside)) | ||||
| 		{ | ||||
| 			normalGenerationMethod = SIMPLE; | ||||
| 		} | ||||
|  | ||||
| 		Vector3DFloat result; | ||||
|  | ||||
| 		BlockVolumeIterator<boost::uint8_t> volIter(*volumeData); //FIXME - save this somewhere - could be expensive to create? | ||||
|  | ||||
|  | ||||
| 		if(normalGenerationMethod == SOBEL) | ||||
| 		{ | ||||
| 			volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ)); | ||||
| 			const Vector3DFloat gradFloor = computeSobelGradient(volIter); | ||||
| 			if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5 | ||||
| 			{			 | ||||
| 				volIter.setPosition(static_cast<uint16_t>(posX+1.0),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ)); | ||||
| 			} | ||||
| 			if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5 | ||||
| 			{			 | ||||
| 				volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY+1.0),static_cast<uint16_t>(posZ)); | ||||
| 			} | ||||
| 			if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5 | ||||
| 			{			 | ||||
| 				volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ+1.0));					 | ||||
| 			} | ||||
| 			const Vector3DFloat gradCeil = computeSobelGradient(volIter); | ||||
| 			result = ((gradFloor + gradCeil) * -1.0f); | ||||
| 			if(result.lengthSquared() < 0.0001) | ||||
| 			{ | ||||
| 				//Operation failed - fall back on simple gradient estimation | ||||
| 				normalGenerationMethod = SIMPLE; | ||||
| 			} | ||||
| 		} | ||||
| 		if(normalGenerationMethod == CENTRAL_DIFFERENCE) | ||||
| 		{ | ||||
| 			volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ)); | ||||
| 			const Vector3DFloat gradFloor = computeCentralDifferenceGradient(volIter); | ||||
| 			if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5 | ||||
| 			{			 | ||||
| 				volIter.setPosition(static_cast<uint16_t>(posX+1.0),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ)); | ||||
| 			} | ||||
| 			if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5 | ||||
| 			{			 | ||||
| 				volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY+1.0),static_cast<uint16_t>(posZ)); | ||||
| 			} | ||||
| 			if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5 | ||||
| 			{			 | ||||
| 				volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ+1.0));					 | ||||
| 			} | ||||
| 			const Vector3DFloat gradCeil = computeCentralDifferenceGradient(volIter); | ||||
| 			result = ((gradFloor + gradCeil) * -1.0f); | ||||
| 			if(result.lengthSquared() < 0.0001) | ||||
| 			{ | ||||
| 				//Operation failed - fall back on simple gradient estimation | ||||
| 				normalGenerationMethod = SIMPLE; | ||||
| 			} | ||||
| 		} | ||||
| 		if(normalGenerationMethod == SIMPLE) | ||||
| 		{ | ||||
| 			volIter.setPosition(static_cast<uint16_t>(posX),static_cast<uint16_t>(posY),static_cast<uint16_t>(posZ)); | ||||
| 			const uint8_t uFloor = volIter.getVoxel() > 0 ? 1 : 0; | ||||
| 			if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5 | ||||
| 			{					 | ||||
| 				uint8_t uCeil = volIter.peekVoxel1px0py0pz() > 0 ? 1 : 0; | ||||
| 				result = Vector3DFloat(static_cast<float>(uFloor - uCeil),0.0,0.0); | ||||
| 			} | ||||
| 			else if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5 | ||||
| 			{ | ||||
| 				uint8_t uCeil = volIter.peekVoxel0px1py0pz() > 0 ? 1 : 0; | ||||
| 				result = Vector3DFloat(0.0,static_cast<float>(uFloor - uCeil),0.0); | ||||
| 			} | ||||
| 			else if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5 | ||||
| 			{ | ||||
| 				uint8_t uCeil = volIter.peekVoxel0px0py1pz() > 0 ? 1 : 0; | ||||
| 				result = Vector3DFloat(0.0, 0.0,static_cast<float>(uFloor - uCeil));					 | ||||
| 			} | ||||
| 		} | ||||
| 		return result; | ||||
| 		}*/ | ||||
| 	} | ||||
|  | ||||
| 	void generateSmoothMeshDataForRegion(BlockVolume<uint8_t>* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch) | ||||
|   | ||||
| @@ -434,10 +434,11 @@ namespace PolyVox | ||||
| 				{ | ||||
| 					if(x != regSlice.getUpperCorner().getX()) | ||||
| 					{ | ||||
| 						volIter.setPosition(x + uStepSize,y,z); | ||||
| 						const uint8_t v100 = volIter.getMaxedVoxel(uLevel); | ||||
| 						const Vector3DFloat v3dPosition(x - offset.getX() + 0.5f * uStepSize, y - offset.getY(), z - offset.getZ()); | ||||
| 						const Vector3DFloat v3dNormal(1.0,0.0,0.0); | ||||
| 						volIter.setPosition(x+uStepSize,y,z); | ||||
| 						const uint8_t uMaterial = v000 | volIter.getMaxedVoxel(uLevel); //Because one of these is 0, the or operation takes the max. | ||||
| 						const Vector3DFloat v3dNormal(v000 > v100 ? 1.0f : -1.0f,0.0,0.0); | ||||
| 						const uint8_t uMaterial = v000 | v100; //Because one of these is 0, the or operation takes the max. | ||||
| 						SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial, 1.0); | ||||
| 						singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); | ||||
| 						vertexIndicesX[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = singleMaterialPatch->m_vecVertices.size()-1; | ||||
| @@ -447,10 +448,11 @@ namespace PolyVox | ||||
| 				{ | ||||
| 					if(y != regSlice.getUpperCorner().getY()) | ||||
| 					{ | ||||
| 						volIter.setPosition(x,y + uStepSize,z); | ||||
| 						const uint8_t v010 = volIter.getMaxedVoxel(uLevel); | ||||
| 						const Vector3DFloat v3dPosition(x - offset.getX(), y - offset.getY() + 0.5f * uStepSize, z - offset.getZ()); | ||||
| 						const Vector3DFloat v3dNormal(0.0,1.0,0.0); | ||||
| 						volIter.setPosition(x,y+uStepSize,z); | ||||
| 						const uint8_t uMaterial = v000 | volIter.getMaxedVoxel(uLevel); //Because one of these is 0, the or operation takes the max. | ||||
| 						const Vector3DFloat v3dNormal(0.0,v000 > v010 ? 1.0f : -1.0f,0.0); | ||||
| 						const uint8_t uMaterial = v000 | v010; //Because one of these is 0, the or operation takes the max. | ||||
| 						SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial, 1.0); | ||||
| 						singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); | ||||
| 						vertexIndicesY[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = singleMaterialPatch->m_vecVertices.size()-1; | ||||
| @@ -460,10 +462,11 @@ namespace PolyVox | ||||
| 				{ | ||||
| 					//if(z != regSlice.getUpperCorner.getZ()) | ||||
| 					{ | ||||
| 						volIter.setPosition(x,y,z + uStepSize); | ||||
| 						const uint8_t v001 = volIter.getMaxedVoxel(uLevel); | ||||
| 						const Vector3DFloat v3dPosition(x - offset.getX(), y - offset.getY(), z - offset.getZ() + 0.5f * uStepSize); | ||||
| 						const Vector3DFloat v3dNormal(0.0,0.0,1.0); | ||||
| 						volIter.setPosition(x,y,z+uStepSize); | ||||
| 						const uint8_t uMaterial = v000 | volIter.getMaxedVoxel(uLevel); //Because one of these is 0, the or operation takes the max. | ||||
| 						const Vector3DFloat v3dNormal(0.0,0.0,v000 > v001 ? 1.0f : -1.0f); | ||||
| 						const uint8_t uMaterial = v000 | v001; //Because one of these is 0, the or operation takes the max. | ||||
| 						const SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial, 1.0); | ||||
| 						singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); | ||||
| 						vertexIndicesZ[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = singleMaterialPatch->m_vecVertices.size()-1; | ||||
|   | ||||
| @@ -79,8 +79,12 @@ namespace PolyVox | ||||
| 	void SurfaceVertex::setNormal(const Vector3DFloat& normalToSet) | ||||
| 	{ | ||||
| 		normal = normalToSet; | ||||
| 		normal.normalise(); | ||||
| 	}		 | ||||
| 	}	 | ||||
|  | ||||
| 	void SurfaceVertex::setPosition(const Vector3DFloat& positionToSet) | ||||
| 	{ | ||||
| 		position = positionToSet; | ||||
| 	} | ||||
|  | ||||
| 	std::string SurfaceVertex::tostring(void) const | ||||
| 	{ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user