Browse Source

Made per-pixel light normalization optional to save time on real-time rendering.

David Piuva 5 years ago
parent
commit
0cf6f402da
1 changed files with 13 additions and 6 deletions
  1. 13 6
      Source/SDK/sandbox/sprite/spriteAPI.cpp

+ 13 - 6
Source/SDK/sandbox/sprite/spriteAPI.cpp

@@ -10,7 +10,8 @@
 #define DIRTY_RECTANGLE_OPTIMIZATION
 #define DIRTY_RECTANGLE_OPTIMIZATION
 
 
 namespace dsr {
 namespace dsr {
-	
+
+template <bool HIGH_QUALITY>
 static IRect renderModel(Model model, OrthoView view, ImageF32 depthBuffer, ImageRgbaU8 diffuseTarget, ImageRgbaU8 normalTarget, FVector2D worldOrigin, Transform3D modelToWorldSpace);
 static IRect renderModel(Model model, OrthoView view, ImageF32 depthBuffer, ImageRgbaU8 diffuseTarget, ImageRgbaU8 normalTarget, FVector2D worldOrigin, Transform3D modelToWorldSpace);
 
 
 struct SpriteConfig {
 struct SpriteConfig {
@@ -226,7 +227,7 @@ static IRect drawSprite(const Sprite& sprite, const OrthoView& ortho, const IVec
 }
 }
 
 
 static IRect drawModel(const ModelInstance& instance, const OrthoView& ortho, const IVector2D& worldCenter, ImageF32 targetHeight, ImageRgbaU8 targetColor, ImageRgbaU8 targetNormal) {
 static IRect drawModel(const ModelInstance& instance, const OrthoView& ortho, const IVector2D& worldCenter, ImageF32 targetHeight, ImageRgbaU8 targetColor, ImageRgbaU8 targetNormal) {
-	return renderModel(instance.visibleModel, ortho, targetHeight, targetColor, targetNormal, FVector2D(worldCenter.x, worldCenter.y), instance.location);
+	return renderModel<false>(instance.visibleModel, ortho, targetHeight, targetColor, targetNormal, FVector2D(worldCenter.x, worldCenter.y), instance.location);
 }
 }
 
 
 // The camera transform for each direction
 // The camera transform for each direction
@@ -851,6 +852,7 @@ static IRect getBackCulledTriangleBound(LVector2D a, LVector2D b, LVector2D c) {
 //   Returns the dirty pixel bound based on projected positions
 //   Returns the dirty pixel bound based on projected positions
 // worldOrigin is the perceived world's origin in target pixel coordinates
 // worldOrigin is the perceived world's origin in target pixel coordinates
 // modelToWorldSpace is used to place the model freely in the world
 // modelToWorldSpace is used to place the model freely in the world
+template <bool HIGH_QUALITY>
 static IRect renderModel(Model model, OrthoView view, ImageF32 depthBuffer, ImageRgbaU8 diffuseTarget, ImageRgbaU8 normalTarget, FVector2D worldOrigin, Transform3D modelToWorldSpace) {
 static IRect renderModel(Model model, OrthoView view, ImageF32 depthBuffer, ImageRgbaU8 diffuseTarget, ImageRgbaU8 normalTarget, FVector2D worldOrigin, Transform3D modelToWorldSpace) {
 	// Combine position transforms
 	// Combine position transforms
 	Transform3D objectToScreenSpace = modelToWorldSpace * Transform3D(FVector3D(worldOrigin.x, worldOrigin.y, 0.0f), view.worldSpaceToScreenDepth);
 	Transform3D objectToScreenSpace = modelToWorldSpace * Transform3D(FVector3D(worldOrigin.x, worldOrigin.y, 0.0f), view.worldSpaceToScreenDepth);
@@ -949,11 +951,16 @@ static IRect renderModel(Model model, OrthoView view, ImageF32 depthBuffer, Imag
 								float height = interpolateUsingAffineWeight(pointA.z, pointB.z, pointC.z, weight);
 								float height = interpolateUsingAffineWeight(pointA.z, pointB.z, pointC.z, weight);
 								if (height > *heightPixel) {
 								if (height > *heightPixel) {
 									FVector3D vertexColor = interpolateUsingAffineWeight(vertexColorA, vertexColorB, vertexColorC, weight);
 									FVector3D vertexColor = interpolateUsingAffineWeight(vertexColorA, vertexColorB, vertexColorC, weight);
-									FVector3D normal = (normalize(interpolateUsingAffineWeight(normalA, normalB, normalC, weight)) + 1.0f) * 127.5f;
-									// Write data directly without saturation (Do not use colors outside of the visible range!)
 									*heightPixel = height;
 									*heightPixel = height;
+									// Write data directly without saturation (Do not use colors outside of the visible range!)
 									*diffusePixel = ((uint32_t)vertexColor.x) | ENDIAN_POS_ADDR(((uint32_t)vertexColor.y), 8) | ENDIAN_POS_ADDR(((uint32_t)vertexColor.z), 16) | ENDIAN_POS_ADDR(255, 24);
 									*diffusePixel = ((uint32_t)vertexColor.x) | ENDIAN_POS_ADDR(((uint32_t)vertexColor.y), 8) | ENDIAN_POS_ADDR(((uint32_t)vertexColor.z), 16) | ENDIAN_POS_ADDR(255, 24);
-									*normalPixel = ((uint32_t)normal.x) | ENDIAN_POS_ADDR(((uint32_t)normal.y), 8) | ENDIAN_POS_ADDR(((uint32_t)normal.z), 16) | ENDIAN_POS_ADDR(255, 24);
+									if (HIGH_QUALITY) {
+										FVector3D normal = (normalize(interpolateUsingAffineWeight(normalA, normalB, normalC, weight)) + 1.0f) * 127.5f;
+										*normalPixel = ((uint32_t)normal.x) | ENDIAN_POS_ADDR(((uint32_t)normal.y), 8) | ENDIAN_POS_ADDR(((uint32_t)normal.z), 16) | ENDIAN_POS_ADDR(255, 24);
+									} else {
+										FVector3D normal = (interpolateUsingAffineWeight(normalA, normalB, normalC, weight) + 1.0f) * 127.5f;
+										*normalPixel = ((uint32_t)normal.x) | ENDIAN_POS_ADDR(((uint32_t)normal.y), 8) | ENDIAN_POS_ADDR(((uint32_t)normal.z), 16) | ENDIAN_POS_ADDR(255, 24);
+									}
 								}
 								}
 							}
 							}
 							diffusePixel += 1;
 							diffusePixel += 1;
@@ -1017,7 +1024,7 @@ void sprite_generateFromModel(ImageRgbaU8& targetAtlas, String& targetConfigText
 			image_fill(colorImage[a], ColorRgbaI32(0, 0, 0, 0));
 			image_fill(colorImage[a], ColorRgbaI32(0, 0, 0, 0));
 			importer_generateNormalsIntoTextureCoordinates(visibleModel);
 			importer_generateNormalsIntoTextureCoordinates(visibleModel);
 			FVector2D origin = FVector2D((float)width * 0.5f, (float)height * 0.5f);
 			FVector2D origin = FVector2D((float)width * 0.5f, (float)height * 0.5f);
-			renderModel(visibleModel, ortho.view[a], depthBuffer, colorImage[a], normalImage[a], origin, Transform3D());
+			renderModel<true>(visibleModel, ortho.view[a], depthBuffer, colorImage[a], normalImage[a], origin, Transform3D());
 			// Convert height into an 8 bit channel for saving
 			// Convert height into an 8 bit channel for saving
 			for (int y = 0; y < height; y++) {
 			for (int y = 0; y < height; y++) {
 				for (int x = 0; x < width; x++) {
 				for (int x = 0; x < width; x++) {