Browse Source

Enable touch input

Panagiotis Christopoulos Charitos 4 years ago
parent
commit
eaf35fb7a5

+ 5 - 0
AnKi/Core/NativeWindow.h

@@ -58,6 +58,11 @@ public:
 		return m_height;
 	}
 
+	F32 getAspectRatio() const
+	{
+		return F32(m_width) / F32(m_height);
+	}
+
 	void setWindowTitle(CString title);
 
 	ANKI_INTERNAL HeapAllocator<U8> getAllocator() const

+ 31 - 11
AnKi/Input/InputAndroid.cpp

@@ -51,6 +51,14 @@ void Input::deleteInstance(Input* input)
 
 Error Input::handleEvents()
 {
+	for(U32& k : m_touchPointers)
+	{
+		if(k)
+		{
+			++k;
+		}
+	}
+
 	int ident;
 	int events;
 	android_poll_source* source;
@@ -132,29 +140,41 @@ int InputAndroid::handleAndroidInput(android_app* app, AInputEvent* event)
 		}
 		else if(source & AINPUT_SOURCE_TOUCHSCREEN)
 		{
+			auto update = [event, this](U32 index, U32 pressValue) {
+				const F32 x = AMotionEvent_getX(event, index);
+				const F32 y = AMotionEvent_getY(event, index);
+				const I32 id = AMotionEvent_getPointerId(event, index);
+
+				m_touchPointerPosWin[id] = UVec2(U32(x), U32(y));
+
+				m_touchPointerPosNdc[id].x() = F32(x) / F32(m_nativeWindow->getWidth()) * 2.0f - 1.0f;
+				m_touchPointerPosNdc[id].y() = -(F32(y) / F32(m_nativeWindow->getHeight()) * 2.0f - 1.0f);
+
+				if(pressValue == 0 || pressValue == 1)
+				{
+					m_touchPointers[id] = pressValue;
+				}
+			};
+
 			switch(action)
 			{
 			case AMOTION_EVENT_ACTION_DOWN:
 			case AMOTION_EVENT_ACTION_POINTER_DOWN:
-			{
-				F32 x = AMotionEvent_getX(event, index);
-				F32 y = AMotionEvent_getY(event, index);
-				int id = AMotionEvent_getPointerId(event, index);
-
-				ANKI_LOGI("Pointer down %f %f %d", x, y, id);
+				update(index, 1);
 				break;
-			}
-
 			case AMOTION_EVENT_ACTION_MOVE:
 			{
+				const U32 count = U32(AMotionEvent_getPointerCount(event));
+				for(U32 i = 0; i < count; i++)
+				{
+					update(i, 2);
+				}
 				break;
 			}
-
 			case AMOTION_EVENT_ACTION_UP:
 			case AMOTION_EVENT_ACTION_POINTER_UP:
-			{
+				update(index, 0);
 				break;
-			}
 
 			default:
 				break;

+ 2 - 1
AnKi/Input/KeyCode.h

@@ -299,7 +299,8 @@ enum class TouchPointer : U8
 	_14,
 	_15,
 
-	COUNT
+	COUNT,
+	FIRST = _0
 };
 ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(TouchPointer)
 

+ 1 - 1
AnKi/Renderer/IndirectDiffuse.h

@@ -56,7 +56,7 @@ private:
 		ShaderProgramResourcePtr m_prog;
 		ShaderProgramPtr m_grProg;
 		F32 m_radius;
-		U32 m_sampleCount = 8.0f;
+		U32 m_sampleCount = 8;
 		F32 m_ssaoStrength = 2.5f;
 		F32 m_ssaoBias = -0.1f;
 	} m_main;

+ 4 - 1
AnKi/Resource/ShaderProgramResourceSystem.cpp

@@ -61,6 +61,7 @@ Error ShaderProgramResourceSystem::compileAllShaders(CString cacheDir, GrManager
 
 	ANKI_RESOURCE_LOGI("Compiling shader programs");
 	U32 shadersCompileCount = 0;
+	U32 shadersTotalCount = 0;
 
 	ThreadHive threadHive(getCpuCoresCount(), alloc, false);
 
@@ -79,6 +80,8 @@ Error ShaderProgramResourceSystem::compileAllShaders(CString cacheDir, GrManager
 			return Error::NONE;
 		}
 
+		++shadersTotalCount;
+
 		if(fname.find("/Rt") != CString::NPOS && !gr.getDeviceCapabilities().m_rayTracingEnabled)
 		{
 			// Skip RT programs when RT is disabled
@@ -238,7 +241,7 @@ Error ShaderProgramResourceSystem::compileAllShaders(CString cacheDir, GrManager
 		return Error::NONE;
 	}));
 
-	ANKI_RESOURCE_LOGI("Compiled %u shader programs", shadersCompileCount);
+	ANKI_RESOURCE_LOGI("Compiled %u shader programs out of %u", shadersCompileCount, shadersTotalCount);
 	return Error::NONE;
 }
 

+ 2 - 2
README.md

@@ -84,8 +84,8 @@ Alternatively, recent Visual Studio versions support building CMake projects fro
 - Visual Studio will automatically understand that AnKi is a CMake project and it will populate the CMake cache
 - Press "build all"
 
-2.3 On Android
---------------
+2.3 For Android
+---------------
 
 Prerequisites:
 

+ 60 - 0
Samples/Common/Framework.cpp

@@ -257,6 +257,66 @@ Error SampleApp::userMainLoop(Bool& quit, Second elapsedTime)
 			angles.z() = 0.0f;
 			mover->setLocalRotation(Mat3x4(Vec3(0.0f), angles));
 		}
+
+		static TouchPointer rotateCameraTouch = TouchPointer::COUNT;
+		static Vec2 rotateEventInitialPos = Vec2(0.0f);
+		for(TouchPointer touch : EnumIterable<TouchPointer>())
+		{
+			if(rotateCameraTouch == TouchPointer::COUNT && in.getTouchPointer(touch) == 1
+			   && in.getTouchPointerNdcPosition(touch).x() > 0.1f)
+			{
+				rotateCameraTouch = touch;
+				rotateEventInitialPos = in.getTouchPointerNdcPosition(touch) * getWindow().getAspectRatio();
+				break;
+			}
+		}
+
+		if(rotateCameraTouch != TouchPointer::COUNT && in.getTouchPointer(rotateCameraTouch) == 0)
+		{
+			rotateCameraTouch = TouchPointer::COUNT;
+		}
+
+		if(rotateCameraTouch != TouchPointer::COUNT && in.getTouchPointer(rotateCameraTouch) > 1)
+		{
+			Vec2 velocity =
+				in.getTouchPointerNdcPosition(rotateCameraTouch) * getWindow().getAspectRatio() - rotateEventInitialPos;
+			velocity *= 0.3f;
+
+			Euler angles(mover->getLocalRotation().getRotationPart());
+			angles.x() += velocity.y() * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
+			angles.x() = clamp(angles.x(), toRad(-90.0f), toRad(90.0f)); // Avoid cycle in Y axis
+			angles.y() += -velocity.x() * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
+			angles.z() = 0.0f;
+			mover->setLocalRotation(Mat3x4(Vec3(0.0f), angles));
+		}
+
+		static TouchPointer moveCameraTouch = TouchPointer::COUNT;
+		static Vec2 moveEventInitialPos = Vec2(0.0f);
+		for(TouchPointer touch : EnumIterable<TouchPointer>())
+		{
+			if(moveCameraTouch == TouchPointer::COUNT && in.getTouchPointer(touch) == 1
+			   && in.getTouchPointerNdcPosition(touch).x() < -0.1f)
+			{
+				moveCameraTouch = touch;
+				moveEventInitialPos = in.getTouchPointerNdcPosition(touch) * getWindow().getAspectRatio();
+				break;
+			}
+		}
+
+		if(moveCameraTouch != TouchPointer::COUNT && in.getTouchPointer(moveCameraTouch) == 0)
+		{
+			moveCameraTouch = TouchPointer::COUNT;
+		}
+
+		if(moveCameraTouch != TouchPointer::COUNT && in.getTouchPointer(moveCameraTouch) > 0)
+		{
+			Vec2 velocity =
+				in.getTouchPointerNdcPosition(moveCameraTouch) * getWindow().getAspectRatio() - moveEventInitialPos;
+			velocity *= 2.0f;
+
+			mover->moveLocalX(moveDistance * velocity.x());
+			mover->moveLocalZ(moveDistance * -velocity.y());
+		}
 	}
 	else
 	{

+ 8 - 2
Tools/Android/GenerateAndroidProject.py

@@ -78,11 +78,17 @@ def main():
     os.symlink(ctx.asserts_dir, os.path.join(project_dir, "assets/Assets"))
 
     # Write the asset directory structure to a file
-    dir_structure_file = open(os.path.join(assets_dir, "DirStructure.txt"), "w")
+    dir_structure_file = open(os.path.join(assets_dir, "DirStructure.txt"), "w", newline="\n")
     for root, dirs, files in os.walk(assets_dir, followlinks=True):
         for f in files:
+            if f.find("DirStructure.txt") >= 0:
+                continue
+
             filename = os.path.join(root, f)
-            filename = filename.replace(assets_dir + "/", "")
+            filename = filename.replace(assets_dir, "")
+            filename = filename.replace("\\", "/")
+            if filename[0] == '/':
+                filename = filename[1:]
             dir_structure_file.write("%s\n" % filename)
     dir_structure_file.close()