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

Started on reworking input events to be based on ray/box intersection

Ivan Safrin 12 лет назад
Родитель
Сommit
78a335bf20

BIN
Assets/Icons/Icon_base.psd


Разница между файлами не показана из-за своего большого размера
+ 27 - 0
Assets/Icons/icons.ai


+ 2 - 0
Core/Contents/CMakeLists.txt

@@ -79,6 +79,7 @@ SET(polycore_SRCS
     Source/PolyPeer.cpp
     Source/PolyClient.cpp
     Source/PolyServer.cpp
+    Source/PolyRay.cpp
 )
 
 SET(polycore_HDRS
@@ -162,6 +163,7 @@ SET(polycore_HDRS
     Include/PolyClient.h
     Include/PolyServer.h
     Include/PolyServerWorld.h
+    Include/PolyRay.h
 )
 
 SET(CMAKE_DEBUG_POSTFIX "_d")

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

@@ -153,6 +153,9 @@ namespace Polycode {
 			*/			
 			Material *getScreenShaderMaterial() { return filterShaderMaterial; }
 			
+			
+			Matrix4 getProjectionMatrix();
+			
 			/**
 			* Toggles the frustum culling of the camera. (Defaults to true).
 			*/
@@ -162,6 +165,8 @@ namespace Polycode {
 			
 		protected:
 		
+			Matrix4 projectionMatrix;
+		
 			Number orthoSizeX;
 			Number orthoSizeY;
 			

+ 17 - 2
Core/Contents/Include/PolyEntity.h

@@ -28,6 +28,7 @@
 #include "PolyQuaternion.h"
 #include "PolyColor.h"
 #include "PolyRectangle.h"
+#include "PolyRay.h"
 #include "PolyEventDispatcher.h"
 #include <vector>
 
@@ -35,6 +36,12 @@ namespace Polycode {
 
 	class Renderer;
 
+	class _PolyExport MouseEventResult {
+		public:
+			bool hit;
+			bool blocked;
+	};
+
 	class _PolyExport EntityProp {
 	public:
 		String propName;
@@ -76,7 +83,7 @@ namespace Polycode {
 			*/			
 			virtual void Update(){};			
 
-			virtual void transformAndRender();		
+			void transformAndRender();		
 
 			void renderChildren();					
 		
@@ -507,7 +514,13 @@ namespace Polycode {
 			*/
 			void setBBoxRadius(Number rad);		
 			
-					
+			MouseEventResult _onMouseDown(const Ray &ray, int mouseButton, int timestamp);
+			MouseEventResult _onMouseUp(const Ray &ray, int mouseButton, int timestamp);
+			MouseEventResult _onMouseMove(const Ray &ray, int timestamp);
+			MouseEventResult _onMouseWheelUp(const Ray &ray, int timestamp);
+			MouseEventResult _onMouseWheelDown(const Ray &ray, int timestamp);
+
+			virtual bool hitTest() { return false; }
 
 			//@}			
 			// ----------------------------------------------------------------------------------------------------------------
@@ -735,6 +748,8 @@ namespace Polycode {
 
 			bool snapToPixels;
 			
+			bool mouseOver;
+			
 			//@}		
 		protected:
 		

+ 2 - 1
Core/Contents/Include/PolyGLRenderer.h

@@ -152,7 +152,8 @@ namespace Polycode {
 		void enableScissor(bool val);
 		void setScissorBox(Polycode::Rectangle box);		
 		
-		Vector3 projectRayFrom2DCoordinate(Number x, Number y);
+		Vector3 projectRayFrom2DCoordinate(Number x, Number y, Matrix4 cameraMatrix, Matrix4 projectionMatrix);
+
 		
 		void setLineSize(Number lineSize);
 		

+ 47 - 0
Core/Contents/Include/PolyRay.h

@@ -0,0 +1,47 @@
+/*
+ Copyright (C) 2013 by Ivan Safrin
+ 
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+ 
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+ 
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ */
+
+#pragma once
+#include "PolyGlobals.h"
+#include "PolyVector3.h"
+#include "PolyMatrix4.h"
+
+namespace Polycode {
+
+	/**
+	* Ray class. 
+	*/
+	class _PolyExport Ray : public PolyBase {
+		public:
+			Ray(const Vector3 &origin, const Vector3 &direction);
+	
+			bool boxIntersect(const Vector3 &box, const Matrix4 &transformMatrix, float near = 0.0, float far = 9999.0) const;			
+			Ray tranformByMatrix(const Matrix4& matrix) const;
+		
+			Vector3 origin;
+			Vector3 direction;
+			
+			Vector3 inv_direction;
+			int sign[3];
+	};
+	
+}

+ 2 - 6
Core/Contents/Include/PolyRenderer.h

@@ -240,9 +240,7 @@ namespace Polycode {
 		
 		void setExposureLevel(Number level);
 		
-		bool rayTriangleIntersect(Vector3 ray_origin, Vector3 ray_direction, Vector3 vert0, Vector3 vert1, Vector3 vert2, Vector3 *hitPoint);
-		
-		virtual Vector3 projectRayFrom2DCoordinate(Number x, Number y) = 0;
+		virtual Vector3 projectRayFrom2DCoordinate(Number x, Number y, Matrix4 cameraMatrix, Matrix4 projectionMatrix) = 0;
 		
 		void enableShaders(bool flag);
 		
@@ -253,9 +251,7 @@ namespace Polycode {
 		void setRendererShaderParams(Shader *shader, ShaderBinding *binding);
 		
 		void addShaderModule(PolycodeShaderModule *module);
-		
-		virtual bool test2DCoordinateInPolygon(Number x, Number y, Polygon *poly, const Matrix4 &matrix, bool ortho, bool testBackfacing, bool billboardMode, bool reverseDirection = false, Matrix4 *adjustMatrix = NULL);
-		
+				
 		virtual Matrix4 getProjectionMatrix() = 0;
 		virtual Matrix4 getModelviewMatrix() = 0;
 		

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

@@ -26,6 +26,7 @@ THE SOFTWARE.
 #include "PolyColor.h"
 #include "PolyVector3.h"
 #include "PolyEntity.h"
+#include "PolyCore.h"
 #include "PolyEventDispatcher.h"
 
 #include <vector>
@@ -125,6 +126,8 @@ namespace Polycode {
 		void Render(Camera *targetCamera = NULL);
 		void RenderDepthOnly(Camera *targetCamera);
 		
+		void handleEvent(Event *event);
+		
 		/**
 		* Adds a light to the scene.
 		* @param light Light to add to the scene.
@@ -190,6 +193,8 @@ namespace Polycode {
 		Camera *defaultCamera;
 		Camera *activeCamera;
 		
+		Core *core;
+		
 		bool lightingEnabled;
 		bool fogEnabled;
 		int fogMode;

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

@@ -83,6 +83,7 @@
 #include "PolyServer.h"
 #include "PolyServerWorld.h"
 #include "PolySocket.h"
+#include "PolyRay.h"
 #include "PolyGlobals.h"
 
 #ifdef _WINDOWS

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

@@ -392,6 +392,10 @@ void Camera::drawFilter(Texture *targetTexture, Number targetTextureWidth, Numbe
 
 }
 
+Matrix4 Camera::getProjectionMatrix() {
+	return projectionMatrix;
+}
+
 void Camera::doCameraTransform() {
 
 	CoreServices::getInstance()->getRenderer()->setClippingPlanes(nearClipPlane, farClipPlane);
@@ -400,6 +404,8 @@ void Camera::doCameraTransform() {
 			CoreServices::getInstance()->getRenderer()->setFOV(fov);
 	CoreServices::getInstance()->getRenderer()->setExposureLevel(exposureLevel);
 
+	projectionMatrix = CoreServices::getInstance()->getRenderer()->getProjectionMatrix();
+
 	if(matrixDirty) {
 		rebuildTransformMatrix();
 	}

+ 61 - 0
Core/Contents/Source/PolyEntity.cpp

@@ -21,6 +21,7 @@
 */
 #include "PolyEntity.h"
 #include "PolyRenderer.h"
+#include "PolyInputEvent.h"
 
 using namespace Polycode;
 
@@ -62,6 +63,7 @@ Entity::Entity() : EventDispatcher() {
 	hasFocus = false;
 	snapToPixels = false;
 	tags = NULL;
+	mouseOver = false;
 	yAdjust = 1.0;
 	positionMode = POSITION_CENTER;
 }
@@ -854,4 +856,63 @@ void Entity::setPositionMode(int newPositionMode) {
 
 int Entity::getPositionMode() const {
 	return positionMode;
+}
+
+MouseEventResult Entity::_onMouseDown(const Ray &ray, int mouseButton, int timestamp) {
+	MouseEventResult ret;
+	return ret;
+}
+
+MouseEventResult Entity::_onMouseUp(const Ray &ray, int mouseButton, int timestamp) {
+	MouseEventResult ret;
+	return ret;
+}
+
+MouseEventResult Entity::_onMouseMove(const Ray &ray, int timestamp) {
+	MouseEventResult ret;
+	ret.hit = false;
+	ret.blocked = false;
+	
+	if(processInputEvents && enabled) {
+		if(ray.boxIntersect(bBox, getConcatenatedMatrix())) {
+			ret.hit = true;			
+			dispatchEvent(new InputEvent(Vector2(), timestamp), InputEvent::EVENT_MOUSEMOVE);
+			
+			if(!mouseOver) {
+				dispatchEvent(new InputEvent(Vector2(), timestamp), InputEvent::EVENT_MOUSEOVER);
+				mouseOver = true;
+			}			
+			
+			if(blockMouseInput) {
+				ret.blocked = true;
+			}
+		} else {
+			if(mouseOver) {
+				dispatchEvent(new InputEvent(Vector2(), timestamp), InputEvent::EVENT_MOUSEOUT);
+				mouseOver = false;
+			}		
+		}
+		
+		for(int i=0; i < children.size(); i++) {
+			MouseEventResult childRes = children[i]->_onMouseMove(ray, timestamp);
+				if(childRes.hit)
+					ret.hit = true;
+				
+				if(childRes.blocked) {
+					ret.blocked = true;
+					break;
+				}			
+		}
+	}
+	return ret;
+}
+
+MouseEventResult Entity::_onMouseWheelUp(const Ray &ray, int timestamp) {
+	MouseEventResult ret;
+	return ret;
+}
+
+MouseEventResult Entity::_onMouseWheelDown(const Ray &ray, int timestamp) {
+	MouseEventResult ret;
+	return ret;
 }

+ 15 - 10
Core/Contents/Source/PolyGLRenderer.cpp

@@ -253,31 +253,36 @@ Vector3 OpenGLRenderer::Unproject(Number x, Number y) {
 	
 }
 
-Vector3 OpenGLRenderer::projectRayFrom2DCoordinate(Number x, Number y) {
+Vector3 OpenGLRenderer::projectRayFrom2DCoordinate(Number x, Number y, Matrix4 cameraMatrix, Matrix4 projectionMatrix) {
 	GLdouble nearPlane[3],farPlane[3];
-	
+
 	GLdouble mv[16];
 	Matrix4 camInverse = cameraMatrix.Inverse();	
 	Matrix4 cmv;
 	cmv.identity();
 	cmv = cmv * camInverse;
-	
+
 	for(int i=0; i < 16; i++) {
 		mv[i] = cmv.ml[i];
 	}
-	
+
 	GLint vp[4];
 	glGetIntegerv( GL_VIEWPORT, vp );
-	
-	gluUnProject(x, yRes - y, 0.0, mv, sceneProjectionMatrix, vp,  &nearPlane[0], &nearPlane[1], &nearPlane[2]);
-	gluUnProject(x, yRes - y, 1.0, mv, sceneProjectionMatrix, vp,  &farPlane[0], &farPlane[1], &farPlane[2]);
-	
+
+	GLdouble _sceneProjectionMatrix[16];
+	for(int i=0; i < 16; i++) {
+		_sceneProjectionMatrix[i] = projectionMatrix.ml[i];
+	}	
+
+	gluUnProject(x, yRes - y, 0.0, mv, _sceneProjectionMatrix, vp, &nearPlane[0], &nearPlane[1], &nearPlane[2]);
+	gluUnProject(x, yRes - y, 1.0, mv, _sceneProjectionMatrix, vp, &farPlane[0], &farPlane[1], &farPlane[2]);
+
 	Vector3 nearVec(nearPlane[0], nearPlane[1], nearPlane[2]);
 	Vector3 farVec(farPlane[0], farPlane[1], farPlane[2]);
-		
+
 	Vector3 dirVec = (farVec) - (nearVec);	
 	dirVec.Normalize();
-	
+
 	return dirVec;
 }
 

+ 81 - 0
Core/Contents/Source/PolyRay.cpp

@@ -0,0 +1,81 @@
+/*
+ Copyright (C) 2013 by Ivan Safrin
+ 
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+ 
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+ 
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+*/
+
+#include "PolyRay.h"
+#include <stdio.h>
+
+using namespace Polycode;
+
+Ray::Ray(const Vector3 &origin, const Vector3 &direction) {
+	inv_direction = Vector3(1.0/direction.x, 1.0/direction.y, 1.0/direction.z);
+	sign[0] = (inv_direction.x < 0);
+	sign[1] = (inv_direction.y < 0);
+	sign[2] = (inv_direction.z < 0);
+	this->origin = origin;
+	this->direction = direction;
+}
+
+Ray Ray::tranformByMatrix(const Matrix4& matrix) const {
+	Vector3 pos = matrix * origin;
+	Vector3 dir = matrix.rotateVector(direction);
+	dir.Normalize();	
+	return Ray(pos, dir);
+}
+
+
+bool Ray::boxIntersect(const Vector3 &box, const Matrix4 &transformMatrix, float near, float far) const {	
+
+	Ray r  = tranformByMatrix(transformMatrix.Inverse());
+
+	Vector3 bounds[2];
+	bounds[0] = Vector3(-box.x * 0.5, -box.y * 0.5, -box.z * 0.5);
+	bounds[1] = Vector3(box.x * 0.5, box.y * 0.5, box.z * 0.5);	
+	
+	float tmin, tmax, tymin, tymax, tzmin, tzmax;
+	tmin = (bounds[r.sign[0]].x - r.origin.x) * r.inv_direction.x;
+	tmax = (bounds[1-r.sign[0]].x - r.origin.x) * r.inv_direction.x;
+	tymin = (bounds[r.sign[1]].y - r.origin.y) * r.inv_direction.y;
+	tymax = (bounds[1-r.sign[1]].y - r.origin.y) * r.inv_direction.y;
+
+	if ( (tmin > tymax) || (tymin > tmax) )
+		return false;
+
+	if (tymin > tmin)
+		tmin = tymin;
+
+	if (tymax < tmax)
+		tmax = tymax;
+
+	tzmin = (bounds[r.sign[2]].z - r.origin.z) * r.inv_direction.z;
+	tzmax = (bounds[1-r.sign[2]].z - r.origin.z) * r.inv_direction.z;
+	
+	if ( (tmin > tzmax) || (tzmin > tmax) )
+		return false;
+
+	if (tzmin > tmin)
+		tmin = tzmin;
+
+	if (tzmax < tmax)
+		tmax = tzmax;
+		
+	return ( (tmin < far) && (tmax > near) );
+}

+ 1 - 148
Core/Contents/Source/PolyRenderer.cpp

@@ -72,159 +72,12 @@ void Renderer::clearLights() {
 	lights.clear();
 	areaLights.clear();
 	spotLights.clear();
-//	shadowMapTextures.clear();
 }
-/*
-void Renderer::addShadowMap(Texture *texture) {
-	shadowMapTextures.push_back(texture);
-}
-*/
+
 void Renderer::setExposureLevel(Number level) {
 	exposureLevel = level;
 }
 
-
-bool Renderer::test2DCoordinateInPolygon(Number x, Number y, Polycode::Polygon *poly, const Matrix4 &matrix, bool ortho, bool testBackfacing, bool billboardMode, bool reverseDirection, Matrix4 *adjustMatrix) {
-
-	Vector3 dirVec;
-	Vector3 origin;
-	
-	if(ortho) {
-		origin = Vector3(((x/(Number)xRes)*orthoSizeX) - (orthoSizeX*0.5), (((yRes-y)/(Number)yRes)*orthoSizeY) - (orthoSizeY*0.5), 0.0);
-		origin = cameraMatrix * origin;
-
-		dirVec = Vector3(0.0, 0.0, -1.0);
-		dirVec = cameraMatrix.rotateVector(dirVec);	
-	} else {
-		dirVec = projectRayFrom2DCoordinate(x, y);
-		origin = cameraMatrix.getPosition();	
-	}
-	
-	Vector3 hitPoint;
-	
-	Matrix4 fullMatrix = matrix;
-	
-	if(billboardMode) {
-		Matrix4 camInverse = cameraMatrix.Inverse();
-		fullMatrix = fullMatrix * camInverse;
-		
-		fullMatrix.m[0][0] = 1;
-		fullMatrix.m[0][1] = 0;
-		fullMatrix.m[0][2] = 0;
-
-		fullMatrix.m[1][0] = 0;
-		fullMatrix.m[1][1] = 1;
-		fullMatrix.m[1][2] = 0;
-
-		fullMatrix.m[2][0] = 0;
-		fullMatrix.m[2][1] = 0;
-		fullMatrix.m[2][2] = 1;		
-		
-		origin = camInverse * origin;
-		dirVec = camInverse.rotateVector(dirVec);
-	}
-	
-	if(adjustMatrix) {
-			fullMatrix = (*adjustMatrix) * fullMatrix;
-	}	
-		
-	bool retStatus = false;	
-	
-	
-	if(poly->getVertexCount() == 3) {
-	
-		if(reverseDirection) {
-			retStatus = rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(2)), fullMatrix  * (*poly->getVertex(1)), fullMatrix *  (*poly->getVertex(0)), &hitPoint);
-			if(testBackfacing && !retStatus) {
-			retStatus = rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(0)), fullMatrix  * (*poly->getVertex(1)), fullMatrix *  (*poly->getVertex(2)), &hitPoint);
-		
-			}		
-		} else {
-			retStatus = rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(0)), fullMatrix  * (*poly->getVertex(1)), fullMatrix *  (*poly->getVertex(2)), &hitPoint);
-			if(testBackfacing && !retStatus) {
-			retStatus = rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(2)), fullMatrix  * (*poly->getVertex(1)), fullMatrix *  (*poly->getVertex(0)), &hitPoint);
-		
-			}
-		}
-	} else if(poly->getVertexCount() == 4) {
-	
-		if(reverseDirection) {
-		
-		retStatus = (rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(0)), fullMatrix  * (*poly->getVertex(1)), fullMatrix *  (*poly->getVertex(2)), &hitPoint) ||
-				rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(2)), fullMatrix  * (*poly->getVertex(3)), fullMatrix *  (*poly->getVertex(0)), &hitPoint));
-		if(testBackfacing && !retStatus) {
-			retStatus = (rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(2)), fullMatrix  * (*poly->getVertex(1)), fullMatrix *  (*poly->getVertex(0)), &hitPoint) ||
-				rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(0)), fullMatrix  * (*poly->getVertex(3)), fullMatrix *  (*poly->getVertex(2)), &hitPoint));
-		
-		}	
-		
-		
-		} else {
-		
-		retStatus = (rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(2)), fullMatrix  * (*poly->getVertex(1)), fullMatrix *  (*poly->getVertex(0)), &hitPoint) ||
-				rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(0)), fullMatrix  * (*poly->getVertex(3)), fullMatrix *  (*poly->getVertex(2)), &hitPoint));
-		if(testBackfacing && !retStatus) {
-			retStatus = (rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(0)), fullMatrix  * (*poly->getVertex(1)), fullMatrix *  (*poly->getVertex(2)), &hitPoint) ||
-				rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(2)), fullMatrix  * (*poly->getVertex(3)), fullMatrix *  (*poly->getVertex(0)), &hitPoint));
-		
-		}	
-		
-		}			
-	} else {
-		retStatus = false;
-	}
-	
-	return retStatus;
-}
-
-bool Renderer::rayTriangleIntersect(Vector3 ray_origin, Vector3 ray_direction, Vector3 vert0, Vector3 vert1, Vector3 vert2, Vector3 *hitPoint)
-{
-
-//	printf("TESTING RAY\nORIGIN: %f,%f,%f\nDIR: %f,%f,%f\nVERT0: %f,%f,%f\nnVERT1: %f,%f,%f\nnVERT2: %f,%f,%f\n", ray_origin.x, ray_origin.y, ray_origin.z, ray_direction.x, ray_direction.y, ray_direction.z, vert0.x, vert0.y, vert0.z, vert1.x, vert1.y, vert1.z, vert2.x, vert2.y, vert2.z);	
-
-
-	Number t,u,v;
-	t = 0; u = 0; v = 0;
-	
-	Vector3 edge1 = vert1 - vert0;
-	Vector3 edge2 = vert2 - vert0;
-	
-	Vector3 tvec, pvec, qvec;
-	Number det, inv_det;
-	
-	
-	pvec = ray_direction.crossProduct(edge2);
-	det = edge1.dot(pvec);
-	
-	if (det > -0.00001f)
-		return false;
-	
-	inv_det = 1.0f / det;
-	
-	tvec = ray_origin - vert0;
-	
-	u = tvec.dot(pvec) * inv_det;
-	if (u < -0.001f || u > 1.001f)
-		return false;
-	
-	qvec = tvec.crossProduct(edge1);
-	
-	v = ray_direction.dot(qvec) * inv_det;
-	if (v < -0.001f || u + v > 1.001f)
-		return false;
-	
-	t = edge2.dot(qvec) * inv_det;
-	
-	if (t <= 0)
-		return false;
-	
-	hitPoint->x = ray_origin.x+t*ray_direction.x;
-	hitPoint->y = ray_origin.y+t*ray_direction.y;
-	hitPoint->z = ray_origin.z+t*ray_direction.z;	
-	
-	return true;	
-}
-
 void Renderer::addShaderModule(PolycodeShaderModule *module) {
 	shaderModules.push_back(module);
 }

+ 56 - 1
Core/Contents/Source/PolyScene.cpp

@@ -31,8 +31,9 @@
 #include "PolyResource.h"
 #include "PolyResourceManager.h"
 #include "PolySceneLight.h"
+#include "PolyInputEvent.h"
 #include "PolySceneMesh.h"
-#include "PolyCore.h"
+#include "PolyRay.h"
 #include "PolySceneManager.h"
 
 using std::vector;
@@ -48,6 +49,7 @@ Scene::Scene(int sceneType, bool virtualScene) : EventDispatcher() {
 
 void Scene::initScene(int sceneType, bool virtualScene) {
 
+	core = CoreServices::getInstance()->getCore();
 	this->sceneType = sceneType;
 	defaultCamera = new Camera(this);
 	activeCamera = defaultCamera;	
@@ -81,6 +83,12 @@ void Scene::initScene(int sceneType, bool virtualScene) {
 			defaultCamera->setClippingPlanes(1.0, 1000.0);
 		break;		
 	}
+	
+	core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
+	core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEUP);
+	core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEMOVE);
+	core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEWHEEL_UP);
+	core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEWHEEL_DOWN);	
 }
 
 void Scene::setActiveCamera(Camera *camera) {
@@ -263,6 +271,53 @@ void Scene::RenderDepthOnly(Camera *targetCamera) {
 	CoreServices::getInstance()->getRenderer()->cullFrontFaces(false);	
 }
 
+void Scene::handleEvent(Event *event) {
+	if(event->getDispatcher() == core->getInput() && rootEntity.processInputEvents) {
+		InputEvent *inputEvent = (InputEvent*) event;
+		Vector3 dir =  renderer->projectRayFrom2DCoordinate(inputEvent->mousePosition.x, inputEvent->mousePosition.y, activeCamera->getConcatenatedMatrix(), activeCamera->getProjectionMatrix());				
+		Vector3 pos;
+		
+		
+		switch(sceneType) {
+			case SCENE_2D:
+			{
+				Number orthoSizeX = activeCamera->getOrthoSizeX();
+				Number orthoSizeY = activeCamera->getOrthoSizeY();			
+				pos = Vector3(((inputEvent->mousePosition.x/(Number)core->getXRes())*orthoSizeX) - (orthoSizeX*0.5), (((core->getYRes()-inputEvent->mousePosition.y)/(Number)core->getYRes())*orthoSizeY) - (orthoSizeY*0.5), 0.0);
+				pos = activeCamera->getConcatenatedMatrix() * pos;	
+			}
+			break;
+			case SCENE_2D_TOPLEFT:
+				pos = Vector3(inputEvent->mousePosition.x, core->getYRes()-inputEvent->mousePosition.y, 0.0);
+				pos = activeCamera->getConcatenatedMatrix() * pos;			
+			break;
+			case SCENE_3D:
+				Vector3 pos = activeCamera->getConcatenatedMatrix().getPosition();
+			break;		
+		}
+				
+		Ray ray(pos, dir);
+		
+		switch(inputEvent->getEventCode()) {
+			case InputEvent::EVENT_MOUSEDOWN:
+				rootEntity._onMouseDown(ray, inputEvent->mouseButton, inputEvent->timestamp);
+			break;
+			case InputEvent::EVENT_MOUSEMOVE:
+				rootEntity._onMouseMove(ray, inputEvent->timestamp);
+			break;
+			case InputEvent::EVENT_MOUSEUP:
+				rootEntity._onMouseUp(ray, inputEvent->mouseButton, inputEvent->timestamp);
+			break;
+			case InputEvent::EVENT_MOUSEWHEEL_UP:
+				rootEntity._onMouseWheelUp(ray, inputEvent->timestamp);
+			break;
+			case InputEvent::EVENT_MOUSEWHEEL_DOWN:
+				rootEntity._onMouseWheelDown(ray,inputEvent->timestamp);	
+			break;	
+		}
+	}
+}
+
 void Scene::addLight(SceneLight *light) {
 	lights.push_back(light);
 	addEntity(light);	

+ 1 - 1
Core/Contents/Source/PolySceneLabel.cpp

@@ -78,7 +78,7 @@ void SceneLabel::updateFromLabel() {
 	
 	bBox.x = label->getWidth()*labelScale;
 	bBox.y = label->getHeight()*labelScale;
-	bBox.z = 0;
+	bBox.z = 0.001;
 	
 	if(useVertexBuffer)
 		CoreServices::getInstance()->getRenderer()->createVertexBufferForMesh(mesh);

+ 2 - 2
Core/Contents/Source/PolyScenePrimitive.cpp

@@ -43,14 +43,14 @@ void ScenePrimitive::recreatePrimitive() {
 		case TYPE_PLANE:
 			mesh->createPlane(v1,v2);
 			bBox.x = v1;
-			bBox.y = 0;
+			bBox.y = 0.001;
 			bBox.z = v2;
 		break;
 		case TYPE_VPLANE:
 			mesh->createVPlane(v1,v2);
 			bBox.x = v1;
 			bBox.y = v2;
-			bBox.z = 0;
+			bBox.z = 0.001;
 		break;		
 		case TYPE_BOX:
 			mesh->createBox(v1,v2,v3);

Некоторые файлы не были показаны из-за большого количества измененных файлов