Просмотр исходного кода

Kinect module fixes and screen sprite improvements. Disabled binding generation as default.

Ivan Safrin 14 лет назад
Родитель
Сommit
18e6e9fbde

+ 4 - 0
Bindings/Contents/LUA/API/Polycode/Mesh.lua

@@ -46,6 +46,10 @@ function Mesh:loadMesh(fileName)
 	local retVal = Polycore.Mesh_loadMesh(self.__ptr, fileName.__ptr)
 end
 
+function Mesh:clearMesh()
+	local retVal =  Polycore.Mesh_clearMesh(self.__ptr)
+end
+
 function Mesh:saveToFile(fileName)
 	local retVal = Polycore.Mesh_saveToFile(self.__ptr, fileName.__ptr)
 end

+ 4 - 0
Bindings/Contents/LUA/API/Polycode/ScreenSprite.lua

@@ -40,6 +40,10 @@ function ScreenSprite:Update()
 	local retVal =  Polycore.ScreenSprite_Update(self.__ptr)
 end
 
+function ScreenSprite:Pause(val)
+	local retVal = Polycore.ScreenSprite_Pause(self.__ptr, val)
+end
+
 
 
 function ScreenSprite:__delete()

+ 16 - 0
Bindings/Contents/LUA/Include/PolycodeLUAWrappers.h

@@ -4146,6 +4146,13 @@ static int Polycore_Mesh_loadMesh(lua_State *L) {
 	return 0;
 }
 
+static int Polycore_Mesh_clearMesh(lua_State *L) {
+	luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
+	Mesh *inst = (Mesh*)lua_topointer(L, 1);
+	inst->clearMesh();
+	return 0;
+}
+
 static int Polycore_Mesh_saveToFile(lua_State *L) {
 	luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
 	Mesh *inst = (Mesh*)lua_topointer(L, 1);
@@ -9394,6 +9401,15 @@ static int Polycore_ScreenSprite_Update(lua_State *L) {
 	return 0;
 }
 
+static int Polycore_ScreenSprite_Pause(lua_State *L) {
+	luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
+	ScreenSprite *inst = (ScreenSprite*)lua_topointer(L, 1);
+	luaL_checktype(L, 2, LUA_TBOOLEAN);
+	bool val = lua_toboolean(L, 2);
+	inst->Pause(val);
+	return 0;
+}
+
 static int Polycore_delete_ScreenSprite(lua_State *L) {
 	luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
 	ScreenSprite *inst = (ScreenSprite*)lua_topointer(L, 1);

+ 2 - 0
Bindings/Contents/LUA/Source/PolycodeLUA.cpp

@@ -438,6 +438,7 @@ int luaopen_Polycode(lua_State *L) {
 		{"Mesh", Polycore_Mesh},
 		{"Mesh_addPolygon", Polycore_Mesh_addPolygon},
 		{"Mesh_loadMesh", Polycore_Mesh_loadMesh},
+		{"Mesh_clearMesh", Polycore_Mesh_clearMesh},
 		{"Mesh_saveToFile", Polycore_Mesh_saveToFile},
 		{"Mesh_loadFromFile", Polycore_Mesh_loadFromFile},
 		{"Mesh_getPolygonCount", Polycore_Mesh_getPolygonCount},
@@ -985,6 +986,7 @@ int luaopen_Polycode(lua_State *L) {
 		{"ScreenSprite_addAnimation", Polycore_ScreenSprite_addAnimation},
 		{"ScreenSprite_playAnimation", Polycore_ScreenSprite_playAnimation},
 		{"ScreenSprite_Update", Polycore_ScreenSprite_Update},
+		{"ScreenSprite_Pause", Polycore_ScreenSprite_Pause},
 		{"delete_ScreenSprite", Polycore_delete_ScreenSprite},
 		{"Shader_get_numSpotLights", Polycore_Shader_get_numSpotLights},
 		{"Shader_get_numAreaLights", Polycore_Shader_get_numAreaLights},

+ 50 - 0
CMake/FindFreenect.cmake

@@ -0,0 +1,50 @@
+# Based on http://freya3d.org/browser/CMakeFind/FindFreenect.cmake
+# Based on http://www.daimi.au.dk/~cgd/code/extensions/Freenect/FindFreenect.cmake
+# - Try to find Freenect
+# Once done this will define
+#
+#  FREENECT_FOUND - system has Freenect
+#  FREENECT_INCLUDE_DIR - the Freenect include directory
+#  FREENECT_LIBRARY - Link these to use Freenect
+#  FREENECT_LIBRARIES
+
+SET(FREENECT_SEARCH_PATHS
+	${POLYCODE_RELEASE_DIR}/Framework/Modules/Dependencies/lib
+	${POLYCODE_RELEASE_DIR}/Framework/Modules/Dependencies/include
+)
+
+
+find_path (FREENECT_INCLUDE_DIR NAMES libfreenect.h
+	HINTS
+	NO_DEFAULT_PATH
+	NO_CMAKE_ENVIRONMENT_PATH
+	NO_CMAKE_SYSTEM_PATH
+	NO_SYSTEM_ENVIRONMENT_PATH
+	NO_CMAKE_PATH
+	CMAKE_FIND_FRAMEWORK NEVER
+	PATH_SUFFIXES lib lib64 win32/Dynamic_Release "Win32/${MSVC_YEAR_NAME}/x64/Release" "Win32/${MSVC_YEAR_NAME}/Win32/Release"
+	PATHS ${FREENECT_SEARCH_PATHS}
+)
+ 
+find_library (FREENECT_LIBRARY_DEBUG NAMES freenectd libfreenect_d PATHS ${FREENECT_SEARCH_PATHS})
+find_library (FREENECT_LIBRARY_RELEASE NAMES freenect PATHS ${FREENECT_SEARCH_PATHS})
+
+if (FREENECT_INCLUDE_DIR AND FREENECT_LIBRARY_RELEASE)
+  set(FREENECT_FOUND TRUE)
+endif()
+
+if (FREENECT_LIBRARY_RELEASE)
+    set (FREENECT_LIBRARY ${FREENECT_LIBRARY_RELEASE})
+endif()
+
+if (FREENECT_LIBRARY_DEBUG AND FREENECT_LIBRARY_RELEASE)
+    set (FREENECT_LIBRARY debug ${FREENECT_LIBRARY_DEBUG} optimized ${FREENECT_LIBRARY_RELEASE} )
+endif()
+
+
+if (FREENECT_FOUND)
+  MESSAGE("-- Found Freenect ${FREENECT_LIBRARIES}")
+  mark_as_advanced (FREENECT_INCLUDE_DIR FREENECT_LIBRARY FREENECT_LIBRARIES)
+endif()
+
+

+ 1 - 1
CMakeLists.txt

@@ -19,7 +19,7 @@ ENDIF(NOT CMAKE_BUILD_TYPE)
 # Options for what components to build
 #OPTION(POLYCODE_BUILD_SHARED "Build Polycode shared libraries" OFF)
 #OPTION(POLYCODE_BUILD_STATIC "Build Polycode static libraries" ON)
-OPTION(POLYCODE_BUILD_BINDINGS "Build Polycode Lua bindings" ON)
+#OPTION(POLYCODE_BUILD_BINDINGS "Build Polycode Lua bindings" ON)
 OPTION(POLYCODE_BUILD_MODULES "Build Polycode modules" ON)
 OPTION(POLYCODE_BUILD_PLAYER "Build Polycode standalone player" OFF)
 OPTION(POLYCODE_BUILD_TOOLS "Build Polycode tools" ON)

+ 1 - 1
Core/Contents/Include/PolyCoreServices.h

@@ -24,6 +24,7 @@ THE SOFTWARE.
 #include "PolyGlobals.h"
 #include "PolyString.h"
 #include "PolyEventDispatcher.h"
+#include "PolyMaterialManager.h"
 #include <map>
 
 namespace Polycode {
@@ -32,7 +33,6 @@ namespace Polycode {
 	class Renderer;
 	class Config;
 	class FontManager;
-	class MaterialManager;
 	class SceneManager;
 	class ScreenManager;
 	class TimerManager;

+ 6 - 0
Core/Contents/Include/PolyMesh.h

@@ -135,6 +135,12 @@ namespace Polycode {
 			* @param fileName Path to mesh file.
 			*/			
 			void loadMesh(const String& fileName);
+			
+			/**
+			* Clears mesh data.
+			*/
+			
+			void clearMesh();
 
 			/**
 			* Saves mesh to a file.

+ 4 - 0
Core/Contents/Include/PolyScreenSprite.h

@@ -68,8 +68,12 @@ class _PolyExport ScreenSprite : public ScreenShape
 		void playAnimation(const String& name, int startFrame, bool once);
 		void Update();
 		
+		void Pause(bool val);
+		
 	protected:
 	
+		bool paused;
+	
 		Number spriteWidth;
 		Number spriteHeight;
 			

+ 6 - 0
Core/Contents/Source/PolyMesh.cpp

@@ -59,12 +59,17 @@ namespace Polycode {
 	
 	
 	Mesh::~Mesh() {
+		clearMesh();
+	}
+	
+	void Mesh::clearMesh() {
 		for(int i=0; i < polygons.size(); i++) {	
 			delete polygons[i];
 		}
 		polygons.clear();
 		if(vertexBuffer)
 			delete vertexBuffer;
+		vertexBuffer = NULL;
 		
 		for(int i=0; i < 16; i++) {
 			if(renderDataArrays[i]) {
@@ -72,6 +77,7 @@ namespace Polycode {
 				delete renderDataArrays[i];
 			}
 		}
+	
 	}
 	
 	VertexBuffer *Mesh::getVertexBuffer() {

+ 13 - 1
Core/Contents/Source/PolyScreenSprite.cpp

@@ -40,6 +40,8 @@ ScreenSprite::ScreenSprite(const String& fileName, Number spriteWidth, Number sp
 	
 	currentFrame = 0;
 	currentAnimation = NULL;
+	
+	paused = false;
 }
 
 ScreenSprite::~ScreenSprite() {
@@ -72,10 +74,13 @@ void ScreenSprite::addAnimation(const String& name, const String& frames, Number
 }
 
 void ScreenSprite::playAnimation(const String& name, int startFrame, bool once) {
+	paused = false;
 	for(int i=0; i < animations.size(); i++) {
 		if(animations[i]->name == name) {
-			currentAnimation = animations[i];
+			if(currentAnimation == animations[i])
+				return;
 			currentFrame = 0;			
+			currentAnimation = animations[i];
 			if(currentFrame < currentAnimation->numFrames)
 				currentFrame = startFrame;
 			playingOnce = once;
@@ -84,6 +89,10 @@ void ScreenSprite::playAnimation(const String& name, int startFrame, bool once)
 	}
 }
 
+void ScreenSprite::Pause(bool val) {
+	paused = val;
+}
+
 void ScreenSprite::Update() {
 	if(!currentAnimation)
 		return;
@@ -92,6 +101,9 @@ void ScreenSprite::Update() {
 	
 	Number elapsed = newTick - lastTick;
 	
+	if(paused)
+		return;
+	
 	if(elapsed > currentAnimation->speed) {
 	currentFrame++;
 	if(currentFrame >= currentAnimation->numFrames) {

+ 6 - 1
Modules/Contents/CMakeLists.txt

@@ -9,5 +9,10 @@ FIND_PACKAGE(Bullet)
 IF(BULLET_FOUND)
     ADD_SUBDIRECTORY(3DPhysics)
 ENDIF(BULLET_FOUND)
-   
+ 
+FIND_PACKAGE(freenect)
+IF(FREENECT_FOUND)
+    ADD_SUBDIRECTORY(Kinect)
+ENDIF(FREENECT_FOUND)
+  
 ADD_SUBDIRECTORY(Networking)

+ 54 - 0
Modules/Contents/Kinect/CMakeLists.txt

@@ -0,0 +1,54 @@
+INCLUDE(PolycodeIncludes)
+
+SET(polycodeKinect_SRCS
+    Source/PolycodeKinect.cpp
+)
+
+SET(polycodeKinect_HDRS
+    Include/PolycodeKinect.h
+)
+
+INCLUDE_DIRECTORIES(
+    ${FREENECT_INCLUDE_DIR}
+    Include
+)
+
+SET(CMAKE_DEBUG_POSTFIX "_d")
+
+#IF(POLYCODE_BUILD_SHARED)
+#ADD_LIBRARY(PolycodeKinect SHARED ${polycodeKinect_SRCS} ${polycodeKinect_HDRS})
+#TARGET_LINK_LIBRARIES(PolycodeKinect ${BOX2D_LIBRARIES})
+#IF(APPLE)
+#    TARGET_LINK_LIBRARIES(PolycodeKinect
+#        Polycore
+#        ${OPENGL_LIBRARIES}
+#        ${OPENAL_LIBRARY}
+#        ${PNG_LIBRARIES}
+#        ${FREETYPE_LIBRARIES}
+#        ${PHYSFS_LIBRARY}
+#        ${VORBISFILE_LIBRARY}
+#        "-framework Cocoa")
+#ENDIF(APPLE)
+#ENDIF(POLYCODE_BUILD_SHARED)
+
+#IF(POLYCODE_BUILD_STATIC)
+ADD_LIBRARY(PolycodeKinect ${polycodeKinect_SRCS} ${polycodeKinect_HDRS})
+#ENDIF(POLYCODE_BUILD_STATIC)
+
+IF(POLYCODE_INSTALL_FRAMEWORK)
+    
+    # install headers
+    INSTALL(FILES ${polycodeKinect_HDRS} DESTINATION Modules/include)
+
+    # install libraries
+#    IF(POLYCODE_BUILD_SHARED)
+    INSTALL(TARGETS PolycodeKinect EXPORT PolycodeKinect-targets DESTINATION Modules/lib)
+#    ENDIF(POLYCODE_BUILD_SHARED)
+#    IF(POLYCODE_BUILD_STATIC)
+#        INSTALL(TARGETS PolycodeKinect_static DESTINATION ${POLYCODE_RELEASE_DIR}/Framework/Modules/lib)
+#    ENDIF(POLYCODE_BUILD_STATIC)
+
+    INSTALL(EXPORT PolycodeKinect-targets DESTINATION
+        Modules/lib/PolycodeKinect)
+    
+ENDIF(POLYCODE_INSTALL_FRAMEWORK)

+ 7 - 7
Modules/Contents/Kinect/Include/PolycodeKinect.h

@@ -30,9 +30,6 @@ using namespace Polycode;
 void depth_cb(freenect_device *dev, void *v_depth, uint32_t timestamp);
 void rgb_cb(freenect_device *dev, void *rgb, uint32_t timestamp);
 
-
-#define MAX_KINECT_POINTS	8000
-
 class PolycodeRunner : public Threaded {
 public:
 	PolycodeRunner();
@@ -60,7 +57,7 @@ public:
 		
 	freenect_context *f_ctx;
 	freenect_device *f_dev;
-	int depthMap[FREENECT_FRAME_PIX];	
+	int depthMap[640*480];	
 	
 protected:
 	
@@ -76,7 +73,7 @@ protected:
 
 class PolycodeKinect : EventHandler {
 	public:
-		PolycodeKinect(bool calculatePoints, int depthStart, int depthEnd, int pixelSkip);
+		PolycodeKinect(int frequency, bool calculatePoints, int pointCount, int depthStart, int depthEnd, int pixelSkip);
 		~PolycodeKinect();
 	
 		Texture *getRGBTexture();
@@ -87,11 +84,14 @@ class PolycodeKinect : EventHandler {
 		void Level();
 	
 		void handleEvent(Event *event);
+		
+		Mesh *mesh;
 	
-		Vector3 points[MAX_KINECT_POINTS];	
-		Color colors[MAX_KINECT_POINTS];
+		Vector3 *points;
+		Color *colors;
 protected:
 	
+	int pointCount;
 	int depthStart;
 	int depthEnd;
 	int pixelSkip;

+ 28 - 12
Modules/Contents/Kinect/Source/PolycodeKinect.cpp

@@ -83,11 +83,11 @@ void PolycodeRunner::runThread() {
 	
 	
 	freenect_set_tilt_degs(f_dev,freenect_angle);
-	freenect_set_led(f_dev,LED_BLINK_YELLOW);
+	freenect_set_led(f_dev,LED_BLINK_GREEN);
 	freenect_set_depth_callback(f_dev, depth_cb);
 	freenect_set_video_callback(f_dev, rgb_cb);
-	freenect_set_video_format(f_dev, current_format);
-	freenect_set_depth_format(f_dev, FREENECT_DEPTH_11BIT);
+	freenect_set_video_mode(f_dev, freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, current_format));
+	freenect_set_depth_mode(f_dev, freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT));
 	freenect_set_video_buffer(f_dev, rgb_back);
 	
 	freenect_start_depth(f_dev);
@@ -105,7 +105,7 @@ void PolycodeRunner::runThread() {
 		
 		if (requested_format != current_format) {
 			freenect_stop_video(f_dev);
-			freenect_set_video_format(f_dev, requested_format);
+			freenect_set_video_mode(f_dev, freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, requested_format));
 			freenect_start_video(f_dev);
 			current_format = requested_format;
 		}
@@ -142,7 +142,7 @@ void PolycodeRunner::depthCallback(freenect_device *dev, void *v_depth, uint32_t
 	
 //	pthread_mutex_lock(&gl_backbuf_mutex);
 	
-	for (i=0; i<FREENECT_FRAME_PIX; i++) {
+	for (i=0; i<640*480; i++) {
 		int pval = t_gamma[depth[i]];
 		int lb = pval & 0xff;
 		depthMap[i] = pval;
@@ -282,17 +282,26 @@ void PolycodeRunner::Level() {
 	
 }
 
-PolycodeKinect::PolycodeKinect(bool calculatePoints, int depthStart, int depthEnd, int pixelSkip) : EventHandler() {
+PolycodeKinect::PolycodeKinect(int frequency, bool calculatePoints, int pointCount, int depthStart, int depthEnd, int pixelSkip) {
 	
-	this->pixelSkip = pixelSkip;
+	this->mesh = new Mesh(Mesh::POINT_MESH);
+	
+	this->pixelSkip = pixelSkip+1;
 	this->depthStart = depthStart;
 	this->depthEnd = depthEnd;	
+	this->pointCount = pointCount;
+	
+	points = (Vector3*)malloc(sizeof(Vector3) * pointCount);
+	colors = (Color*)malloc(sizeof(Color) * pointCount);
 	
 	this->calculatePoints = calculatePoints;
 	this->calculateColors = true;
 	rgbTexture = NULL;
 	
-	updateTimer = new Timer(true, 10);
+	if(frequency < 1)
+		frequency = 1;
+		
+	updateTimer = new Timer(true, frequency);
 	updateTimer->addEventListener(this, Timer::EVENT_TRIGGER);
 	
 	
@@ -314,10 +323,14 @@ void PolycodeKinect::handleEvent(Event *event) {
 		newImage = new Image((char*)runner->depth_mid, 640, 480, Image::IMAGE_RGB);
 		closeDepthTexture->setImageData(newImage);
 		closeDepthTexture->recreateFromImageData();
-		delete newImage;
+		delete newImage;				
 
-		if(calculatePoints || calculateColors) {
-			for(int i=0; i < MAX_KINECT_POINTS; i++) {
+
+		if(calculatePoints && calculateColors) {
+		
+			this->mesh->clearMesh();
+		
+			for(int i=0; i < this->pointCount; i++) {
 				points[i] = Vector3(0,0,0);
 				colors[i].setColorRGB(0,0,0);
 			}
@@ -338,7 +351,7 @@ void PolycodeKinect::handleEvent(Event *event) {
 					int pval = runner->depthMap[x + (y*640)];
 						
 					if(pval > depthStart && pval < depthEnd) {
-						if(ptIndex < MAX_KINECT_POINTS) {							
+						if(ptIndex < this->pointCount) {							
 							if(calculateColors) {
 								colors[ptIndex].setColorHexRGB(testArrColor);
 							}
@@ -346,6 +359,9 @@ void PolycodeKinect::handleEvent(Event *event) {
 								points[ptIndex] = Vector3(x, y, pval);
 							}
 							
+							Polygon *poly = new Polygon();
+							poly->addVertex(x,y,pval);
+							mesh->addPolygon(poly);					
 							ptIndex++;
 						} else {
 							return;