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

VSync on Mac, anisothropic filtering options, 3D physics framerate idependence

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

+ 4 - 4
Bindings/Contents/LUA/API/Polycode/Core.lua

@@ -148,12 +148,12 @@ function Core:openFolderPicker()
 	end
 end
 
-function Core:setVideoModeIndex(index, fullScreen, aaLevel)
-	local retVal = Polycore.Core_setVideoModeIndex(self.__ptr, index, fullScreen, aaLevel)
+function Core:setVideoModeIndex(index, fullScreen, vSync, aaLevel, anisotropyLevel)
+	local retVal = Polycore.Core_setVideoModeIndex(self.__ptr, index, fullScreen, vSync, aaLevel, anisotropyLevel)
 end
 
-function Core:setVideoMode(xRes, yRes, fullScreen, aaLevel)
-	local retVal = Polycore.Core_setVideoMode(self.__ptr, xRes, yRes, fullScreen, aaLevel)
+function Core:setVideoMode(xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel)
+	local retVal = Polycore.Core_setVideoMode(self.__ptr, xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel)
 end
 
 function Core:resizeTo(xRes, yRes)

+ 9 - 0
Bindings/Contents/LUA/API/Polycode/Renderer.lua

@@ -388,6 +388,15 @@ function Renderer:getYRes()
 	return retVal
 end
 
+function Renderer:setAnisotropyAmount(amount)
+	local retVal = Polycore.Renderer_setAnisotropyAmount(self.__ptr, amount)
+end
+
+function Renderer:getAnisotropyAmount()
+	local retVal =  Polycore.Renderer_getAnisotropyAmount(self.__ptr)
+	return retVal
+end
+
 function Renderer:cullFrontFaces(val)
 	local retVal = Polycore.Renderer_cullFrontFaces(self.__ptr, val)
 end

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

@@ -185,6 +185,10 @@ function Scene:addLight(light)
 	local retVal = Polycore.Scene_addLight(self.__ptr, light.__ptr)
 end
 
+function Scene:removeLight(light)
+	local retVal = Polycore.Scene_removeLight(self.__ptr, light.__ptr)
+end
+
 function Scene:getNearestLight(pos)
 	local retVal = Polycore.Scene_getNearestLight(self.__ptr, pos.__ptr)
 	if retVal == nil then return nil end

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

@@ -24,6 +24,10 @@ function TweenManager:addTween(tween)
 	local retVal = Polycore.TweenManager_addTween(self.__ptr, tween.__ptr)
 end
 
+function TweenManager:removeTween(tween)
+	local retVal = Polycore.TweenManager_removeTween(self.__ptr, tween.__ptr)
+end
+
 function TweenManager:Update()
 	local retVal =  Polycore.TweenManager_Update(self.__ptr)
 end

+ 48 - 6
Bindings/Contents/LUA/Include/PolycodeLUAWrappers.h

@@ -1301,9 +1301,13 @@ static int Polycore_Core_setVideoModeIndex(lua_State *L) {
 	int index = lua_tointeger(L, 2);
 	luaL_checktype(L, 3, LUA_TBOOLEAN);
 	bool fullScreen = lua_toboolean(L, 3);
-	luaL_checktype(L, 4, LUA_TNUMBER);
-	int aaLevel = lua_tointeger(L, 4);
-	inst->setVideoModeIndex(index, fullScreen, aaLevel);
+	luaL_checktype(L, 4, LUA_TBOOLEAN);
+	bool vSync = lua_toboolean(L, 4);
+	luaL_checktype(L, 5, LUA_TNUMBER);
+	int aaLevel = lua_tointeger(L, 5);
+	luaL_checktype(L, 6, LUA_TNUMBER);
+	int anisotropyLevel = lua_tointeger(L, 6);
+	inst->setVideoModeIndex(index, fullScreen, vSync, aaLevel, anisotropyLevel);
 	return 0;
 }
 
@@ -1316,9 +1320,13 @@ static int Polycore_Core_setVideoMode(lua_State *L) {
 	int yRes = lua_tointeger(L, 3);
 	luaL_checktype(L, 4, LUA_TBOOLEAN);
 	bool fullScreen = lua_toboolean(L, 4);
-	luaL_checktype(L, 5, LUA_TNUMBER);
-	int aaLevel = lua_tointeger(L, 5);
-	inst->setVideoMode(xRes, yRes, fullScreen, aaLevel);
+	luaL_checktype(L, 5, LUA_TBOOLEAN);
+	bool vSync = lua_toboolean(L, 5);
+	luaL_checktype(L, 6, LUA_TNUMBER);
+	int aaLevel = lua_tointeger(L, 6);
+	luaL_checktype(L, 7, LUA_TNUMBER);
+	int anisotropyLevel = lua_tointeger(L, 7);
+	inst->setVideoMode(xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel);
 	return 0;
 }
 
@@ -6481,6 +6489,22 @@ static int Polycore_Renderer_getYRes(lua_State *L) {
 	return 1;
 }
 
+static int Polycore_Renderer_setAnisotropyAmount(lua_State *L) {
+	luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
+	Renderer *inst = (Renderer*)lua_topointer(L, 1);
+	luaL_checktype(L, 2, LUA_TNUMBER);
+	Number amount = lua_tonumber(L, 2);
+	inst->setAnisotropyAmount(amount);
+	return 0;
+}
+
+static int Polycore_Renderer_getAnisotropyAmount(lua_State *L) {
+	luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
+	Renderer *inst = (Renderer*)lua_topointer(L, 1);
+	lua_pushnumber(L, inst->getAnisotropyAmount());
+	return 1;
+}
+
 static int Polycore_Renderer_cullFrontFaces(lua_State *L) {
 	luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
 	Renderer *inst = (Renderer*)lua_topointer(L, 1);
@@ -7141,6 +7165,15 @@ static int Polycore_Scene_addLight(lua_State *L) {
 	return 0;
 }
 
+static int Polycore_Scene_removeLight(lua_State *L) {
+	luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
+	Scene *inst = (Scene*)lua_topointer(L, 1);
+	luaL_checktype(L, 2, LUA_TLIGHTUSERDATA);
+	SceneLight * light = (SceneLight *)lua_topointer(L, 2);
+	inst->removeLight(light);
+	return 0;
+}
+
 static int Polycore_Scene_getNearestLight(lua_State *L) {
 	luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
 	Scene *inst = (Scene*)lua_topointer(L, 1);
@@ -10773,6 +10806,15 @@ static int Polycore_TweenManager_addTween(lua_State *L) {
 	return 0;
 }
 
+static int Polycore_TweenManager_removeTween(lua_State *L) {
+	luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
+	TweenManager *inst = (TweenManager*)lua_topointer(L, 1);
+	luaL_checktype(L, 2, LUA_TLIGHTUSERDATA);
+	Tween * tween = (Tween *)lua_topointer(L, 2);
+	inst->removeTween(tween);
+	return 0;
+}
+
 static int Polycore_TweenManager_Update(lua_State *L) {
 	luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
 	TweenManager *inst = (TweenManager*)lua_topointer(L, 1);

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

@@ -692,6 +692,8 @@ int luaopen_Polycode(lua_State *L) {
 		{"Renderer_drawScreenQuad", Polycore_Renderer_drawScreenQuad},
 		{"Renderer_getXRes", Polycore_Renderer_getXRes},
 		{"Renderer_getYRes", Polycore_Renderer_getYRes},
+		{"Renderer_setAnisotropyAmount", Polycore_Renderer_setAnisotropyAmount},
+		{"Renderer_getAnisotropyAmount", Polycore_Renderer_getAnisotropyAmount},
 		{"Renderer_cullFrontFaces", Polycore_Renderer_cullFrontFaces},
 		{"Renderer_clearLights", Polycore_Renderer_clearLights},
 		{"Renderer_addLight", Polycore_Renderer_addLight},
@@ -759,6 +761,7 @@ int luaopen_Polycode(lua_State *L) {
 		{"Scene_loadScene", Polycore_Scene_loadScene},
 		{"Scene_generateLightmaps", Polycore_Scene_generateLightmaps},
 		{"Scene_addLight", Polycore_Scene_addLight},
+		{"Scene_removeLight", Polycore_Scene_removeLight},
 		{"Scene_getNearestLight", Polycore_Scene_getNearestLight},
 		{"Scene_writeEntityMatrix", Polycore_Scene_writeEntityMatrix},
 		{"Scene_writeString", Polycore_Scene_writeString},
@@ -1147,6 +1150,7 @@ int luaopen_Polycode(lua_State *L) {
 		{"delete_QuaternionTween", Polycore_delete_QuaternionTween},
 		{"TweenManager", Polycore_TweenManager},
 		{"TweenManager_addTween", Polycore_TweenManager_addTween},
+		{"TweenManager_removeTween", Polycore_TweenManager_removeTween},
 		{"TweenManager_Update", Polycore_TweenManager_Update},
 		{"delete_TweenManager", Polycore_delete_TweenManager},
 		{"Vector2", Polycore_Vector2},

+ 2 - 2
Core/Contents/Include/PolyCocoaCore.h

@@ -66,13 +66,13 @@ namespace Polycode {
 	class _PolyExport CocoaCore : public Core {		
 	public:
 		
-		CocoaCore(PolycodeView *view, int xRes, int yRes, bool fullScreen, int aaLevel, int frameRate);
+		CocoaCore(PolycodeView *view, int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate);
 		virtual ~CocoaCore();
 		
 		void enableMouse(bool newval);
 		unsigned int getTicks();		
 		bool Update();		
-		void setVideoMode(int xRes, int yRes, bool fullScreen, int aaLevel);		
+		void setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel);		
 		void resizeTo(int xRes, int yRes);
 		void createThread(Threaded *target);		
 		

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

@@ -88,7 +88,7 @@ namespace Polycode {
 			}
 			
 			bool operator != (const Color& c2) {
-				return (((int)255.0*r) != ((int)255.0*c2.r) && ((int)255.0*g) != ((int)255.0*c2.g) && ((int)255.0*b) != ((int)255.0*c2.b) && ((int)255.0*a) != ((int)255.0*c2.a));
+				return (((int)255.0*r) != ((int)255.0*c2.r) || ((int)255.0*g) != ((int)255.0*c2.g) || ((int)255.0*b) != ((int)255.0*c2.b) || ((int)255.0*a) != ((int)255.0*c2.a));
 			}
 			
 

+ 3 - 3
Core/Contents/Include/PolyCore.h

@@ -83,7 +83,7 @@ namespace Polycode {
 		* @param aaLevel Level of anti-aliasing. Possible values are 2,4 and 6.
 		* @param frameRate Frame rate that the core will update and render at.
 		*/			
-		Core(int xRes, int yRes, bool fullScreen, int aaLevel, int frameRate);
+		Core(int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate);
 		virtual ~Core();
 		
 		virtual bool Update() = 0;
@@ -236,7 +236,7 @@ namespace Polycode {
 		*/																							
 		virtual vector<string> openFilePicker(vector<CoreFileExtension> extensions, bool allowMultiple) = 0;
 		
-		void setVideoModeIndex(int index, bool fullScreen, int aaLevel);
+		void setVideoModeIndex(int index, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel);
 		
 		/**
 		* Sets a new video mode.
@@ -245,7 +245,7 @@ namespace Polycode {
 		* @param fullScreen True to launch in fullscreen, false to launch in window.
 		* @param aaLevel Level of anti-aliasing. Possible values are 2,4 and 6.
 		*/																									
-		virtual void setVideoMode(int xRes, int yRes, bool fullScreen, int aaLevel) = 0;
+		virtual void setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel) = 0;
 		
 		/**
 		* Resizes the renderer.

+ 1 - 0
Core/Contents/Include/PolyGLTexture.h

@@ -31,6 +31,7 @@ THE SOFTWARE.
 #include "PolyGlobals.h"
 #include "PolyTexture.h"
 #include "PolyGLRenderer.h"
+#include "PolyCoreServices.h"
 #if defined(__APPLE__) && defined(__MACH__)
 #include <OpenGL/gl.h>
 #include <OpenGL/glext.h>

+ 5 - 0
Core/Contents/Include/PolyRenderer.h

@@ -184,6 +184,9 @@ namespace Polycode {
 		int getXRes();
 		int getYRes();
 		
+		void setAnisotropyAmount(Number amount);
+		Number getAnisotropyAmount();
+		
 		virtual void cullFrontFaces(bool val) = 0;
 		
 		void clearLights();
@@ -243,6 +246,8 @@ namespace Polycode {
 		vector<LightInfo> getSpotLights() { return spotLights;	}
 		
 	protected:
+	
+		Number anisotropy;
 		Matrix4 currentModelMatrix;
 		LightSorter sorter;	
 	

+ 7 - 0
Core/Contents/Include/PolyScene.h

@@ -125,6 +125,13 @@ namespace Polycode {
 		* @param light Light to add to the scene.
 		*/
 		void addLight(SceneLight *light);
+		
+		/**
+		* Removes a light from the scene.
+		* @param light Light to remove from the scene.
+		*/		
+		void removeLight(SceneLight *light);
+		
 		SceneLight *getNearestLight(Vector3 pos);
 		
 		void writeEntityMatrix(SceneEntity *entity, OSFILE *outFile);

+ 1 - 0
Core/Contents/Include/PolyTweenManager.h

@@ -37,6 +37,7 @@ namespace Polycode {
 			TweenManager();
 			~TweenManager();
 			void addTween(Tween *tween);
+			void removeTween(Tween *tween);	
 			void Update();
 		
 		private:

+ 13 - 5
Core/Contents/Source/PolyCocoaCore.mm

@@ -29,7 +29,7 @@ long getThreadID() {
 	return (long)pthread_self();
 }
 
-CocoaCore::CocoaCore(PolycodeView *view, int xRes, int yRes, bool fullScreen,int aaLevel, int frameRate) : Core(xRes, yRes, fullScreen,aaLevel, frameRate) {	
+CocoaCore::CocoaCore(PolycodeView *view, int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate) : Core(xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel, frameRate) {	
 	eventMutex = createMutex();
 	
 //	NSLog(@"BUNDLE: %@", [[NSBundle mainBundle] bundlePath]);
@@ -142,7 +142,7 @@ CocoaCore::CocoaCore(PolycodeView *view, int xRes, int yRes, bool fullScreen,int
 	renderer = new OpenGLRenderer();
 	services->setRenderer(renderer);	
 	//[view unlockContext];			
-	setVideoMode(xRes,yRes,fullScreen,aaLevel);		
+	setVideoMode(xRes,yRes,fullScreen, vSync, aaLevel, anisotropyLevel);		
 
 	CoreServices::getInstance()->installModule(new GLSLShaderModule());	
 
@@ -166,12 +166,13 @@ String CocoaCore::getClipboardString() {
 	
 }
 
-void CocoaCore::setVideoMode(int xRes, int yRes, bool fullScreen, int aaLevel) {
+void CocoaCore::setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, int aaLeve, int anisotropyLevel) {
 	this->xRes = xRes;
 	this->yRes = yRes;
 	this->fullScreen = fullScreen;
 	this->aaLevel = aaLevel;
-	
+	renderer->setAnisotropyAmount(anisotropyLevel);
+		
 	renderer->Resize(xRes, yRes);
 //	CoreServices::getInstance()->getMaterialManager()->reloadProgramsAndTextures();	
 	dispatchEvent(new Event(), EVENT_CORE_RESIZE);	
@@ -195,7 +196,14 @@ void CocoaCore::setVideoMode(int xRes, int yRes, bool fullScreen, int aaLevel) {
 																	   nil]];
 		
 	}
-		
+	
+	GLint sync = 0;
+	if(vSync) {
+		sync =1 ;
+	} 
+	
+	[context setValues:&sync forParameter:NSOpenGLCPSwapInterval];	
+				
 	/*
 	if(aaLevel > 0) {
 		glEnable( GL_MULTISAMPLE_ARB );	

+ 3 - 3
Core/Contents/Source/PolyCore.cpp

@@ -28,7 +28,7 @@
 
 namespace Polycode {
 	
-	Core::Core(int xRes, int yRes, bool fullScreen, int aaLevel, int frameRate) : EventDispatcher() {
+	Core::Core(int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate) : EventDispatcher() {
 		services = CoreServices::getInstance();
 		input = new CoreInput();
 		services->setCore(this);
@@ -83,12 +83,12 @@ namespace Polycode {
 		return ((Number)getTicks())/1000.0f;		
 	}
 	
-	void Core::setVideoModeIndex(int index, bool fullScreen, int aaLevel) {
+	void Core::setVideoModeIndex(int index, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel) {
 		vector<Rectangle> resList = getVideoModes();
 		if(index >= resList.size())
 			return;
 		
-		setVideoMode(resList[index].w, resList[index].h, fullScreen, aaLevel);
+		setVideoMode(resList[index].w, resList[index].h, fullScreen, vSync, aaLevel, anisotropyLevel);
 	}
 	
 	void Core::updateCore() {

+ 15 - 6
Core/Contents/Source/PolyGLTexture.cpp

@@ -38,6 +38,8 @@ OpenGLTexture::OpenGLTexture(unsigned int width, unsigned int height, char *text
 
 void OpenGLTexture::recreateFromImageData() {
 	
+	Number anisotropy = CoreServices::getInstance()->getRenderer()->getAnisotropyAmount();
+	
 	if(glTextureLoaded)
 		glDeleteTextures(1, &textureID);
 	
@@ -52,19 +54,26 @@ void OpenGLTexture::recreateFromImageData() {
 	}
 	switch(filteringMode) {
 		case Renderer::TEX_FILTERING_LINEAR:
-			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+		
+			if(anisotropy > 0) {
+				glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy);
+			}
+		
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+			if(textureData) {
+				gluBuild2DMipmaps(GL_TEXTURE_2D, glTextureType, width, height, glTextureType, GL_UNSIGNED_BYTE, textureData );
+			}			
 			break;
 		case Renderer::TEX_FILTERING_NEAREST:
 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);		
+			if(textureData) {
+				glTexImage2D(GL_TEXTURE_2D, 0, glTextureType, width, height, 0, glTextureType, GL_UNSIGNED_BYTE, textureData);							
+			}			
 			break;
 	}	
 	
-	if(textureData) {
-		glTexImage2D(GL_TEXTURE_2D, 0, glTextureType, width, height, 0, glTextureType, GL_UNSIGNED_BYTE, textureData);							
-//		gluBuild2DMipmaps(GL_TEXTURE_2D, 3, glTextureType, width, height, glTextureType, GL_UNSIGNED_BYTE, textureData);
-	}
 	glTextureLoaded = true;
 }
 

+ 9 - 0
Core/Contents/Source/PolyRenderer.cpp

@@ -25,6 +25,7 @@
 using namespace Polycode;
 
 Renderer::Renderer() : currentTexture(NULL), xRes(0), yRes(0), renderMode(0), orthoMode(false), lightingEnabled(false), clearColor(0.2f, 0.2f, 0.2f, 0.0) {
+	anisotropy = 0;
 	textureFilteringMode = TEX_FILTERING_LINEAR;
 	currentMaterial = NULL;
 	numLights = 0;
@@ -168,6 +169,14 @@ void Renderer::addLight(int lightImportance, Vector3 position, Vector3 direction
 	}
 }
 
+Number Renderer::getAnisotropyAmount() {
+	return anisotropy;
+}
+
+void Renderer::setAnisotropyAmount(Number amount) {
+	anisotropy = amount;
+}
+
 Matrix4 Renderer::getCameraMatrix() {
 	return cameraMatrix;
 }

+ 10 - 0
Core/Contents/Source/PolyScene.cpp

@@ -266,6 +266,16 @@ void Scene::addLight(SceneLight *light) {
 	addEntity(light);	
 }
 
+void Scene::removeLight(SceneLight *light) {
+	removeEntity(light);
+	for(int i=0; i < lights.size(); i++) {
+		if(lights[i] == light) {
+			lights.erase(lights.begin()+i);
+			return;
+		}		
+	}
+}
+
 SceneLight *Scene::getNearestLight(Vector3 pos) {
 	if(lights.size() > 0)
 		return lights[0];

+ 1 - 0
Core/Contents/Source/PolySceneMesh.cpp

@@ -64,6 +64,7 @@ void SceneMesh::setMesh(Mesh *mesh) {
 
 
 SceneMesh::~SceneMesh() {
+	delete mesh;
 }
 
 Mesh *SceneMesh::getMesh() {

+ 2 - 1
Core/Contents/Source/PolyTween.cpp

@@ -56,7 +56,8 @@ void Tween::setSpeed(Number speed) {
 
 Tween::~Tween() {
 	tweenTimer->removeEventListener(this, 0);
-	CoreServices::getInstance()->getTimerManager()->removeTimer(tweenTimer);
+	delete tweenTimer;
+	CoreServices::getInstance()->getTweenManager()->removeTween(this);	
 }
 
 bool Tween::isComplete() {

+ 8 - 0
Core/Contents/Source/PolyTweenManager.cpp

@@ -36,6 +36,14 @@ void TweenManager::addTween(Tween *tween) {
 	tweens.push_back(tween);
 }
 
+void TweenManager::removeTween(Tween *tween) {
+	for(int i=0;i<tweens.size();i++) {
+		if(tweens[i] == tween) {
+			tweens.erase(tweens.begin()+i);
+		}
+	}
+}
+
 void TweenManager::Update() {
 	Tween *tween;
 	for(int i=0;i<tweens.size();i++) {

+ 1 - 1
Modules/Contents/3DPhysics/Source/PolyPhysicsScene.cpp

@@ -66,7 +66,7 @@ void PhysicsScene::Update() {
 	
 	
 	Number elapsed = CoreServices::getInstance()->getCore()->getElapsed();
-	physicsWorld->stepSimulation(elapsed);	
+	physicsWorld->stepSimulation(elapsed, 7);	
 	CollisionScene::Update();
 	
 }