Exemplo n.º 1
0
/*
Here's how we generate the 3D normalised direction vector that we
use with our picking ray. We use our new unproject function /twice/
and then normalise the result. Here's how this works! We unproject
once right up against the near plane, and then once again, right
up against the far plane. This gives us 2 positions in space, both
'under' the mouse pointer from the camera's perspective. We can
then get the direction vector between them and normalise it,
giving us a direction vector that comes from our camera position,
and passes through the mouse pointer.

		Near				Far
		|					|
		|					|
		|					|
O->		|*				   *|
		|					|
		|					|
*/
Vector3 Renderer::GetMouseDirectionVector3(float aspect, float fov, Camera &cam) {
	Vector2 mpos = Window::GetMouse()->GetAbsolutePosition();

	//We remove the y axis mouse position from height as OpenGL is 'upside down',
	//and thinks the bottom left is the origin, instead of the top left!
	Vector3 nearPos = Vector3(mpos.x,
							height - mpos.y,
							0.0f
	);

	//We also don't use exactly 1.0 (the normalised 'end' of the far plane) as this
	//causes the unproject function to go a bit weird. 
	Vector3 farPos = Vector3(mpos.x,
							height - mpos.y,
							0.99999999f
	);

	Vector3 a = UnProject(nearPos,aspect,fov,cam);
	Vector3 b = UnProject(farPos,aspect,fov,cam);
	Vector3 c = b-a;

	c.Normalise();

	return c;
}
Exemplo n.º 2
0
void CIATestMainWindow::OnMouseDown(int nButton,double x,double y)
{
	if(m_pSelectedEntity)
	{
		CVector vPos=UnProject(x,y);
		m_pSelectedEntity->SetPosition(vPos);
	}
}
Exemplo n.º 3
0
eeVector3f cGL::UnProjectCurrent( const eeVector3f& point ) {
	GLfloat projMat[16];
	GetCurrentMatrix( GL_PROJECTION_MATRIX, projMat );

	GLfloat modelMat[16];
	GetCurrentMatrix( GL_MODELVIEW_MATRIX, modelMat );

	GLint viewPort[4];
	GetViewport( viewPort );

	eeVector3f fPoint( point );
	fPoint.y = viewPort[3] - point.y;

	Vector3<GLfloat> tv3;

	UnProject( (GLfloat)fPoint.x, (GLfloat)fPoint.y, (GLfloat)fPoint.z, projMat, modelMat, viewPort, &tv3.x, &tv3.y, &tv3.z );

	return eeVector3f( tv3.x, tv3.y, tv3.z );
}
Exemplo n.º 4
0
void CPortalsRender::Draw3D(){
	if ( !portals.show_3d || portals.portal_count < 1 ) {
		return;
	}

	g_QglTable.m_pfn_qglPushAttrib( GL_ALL_ATTRIB_BITS );

	double cam[3];
	double proj_m[16];
	double model_m[16];
	float min_check[3];
	float max_check[3];
	float trans = ( 100.0f - portals.trans_3d ) / 100.0f;
	int view[4];

	g_QglTable.m_pfn_qglGetDoublev( GL_PROJECTION_MATRIX, proj_m );
	g_QglTable.m_pfn_qglGetDoublev( GL_MODELVIEW_MATRIX, model_m );
	g_QglTable.m_pfn_qglGetIntegerv( GL_VIEWPORT, view );

	UnProject( 0.5 * (double)view[2], 0.5 * (double)view[3], 0.0, model_m, proj_m, view, cam, cam + 1, cam + 2 );

	min_check[0] = (float)cam[0] + ( portals.clip_range * 64.0f );
	min_check[1] = (float)cam[1] + ( portals.clip_range * 64.0f );
	min_check[2] = (float)cam[2] + ( portals.clip_range * 64.0f );
	max_check[0] = (float)cam[0] - ( portals.clip_range * 64.0f );
	max_check[1] = (float)cam[1] - ( portals.clip_range * 64.0f );
	max_check[2] = (float)cam[2] - ( portals.clip_range * 64.0f );

	g_QglTable.m_pfn_qglHint( GL_FOG_HINT, GL_NICEST );

	g_QglTable.m_pfn_qglDisable( GL_CULL_FACE );

	g_QglTable.m_pfn_qglDisable( GL_LINE_SMOOTH );
	g_QglTable.m_pfn_qglDisable( GL_POLYGON_SMOOTH );

	g_QglTable.m_pfn_qglPolygonMode( GL_FRONT_AND_BACK, GL_FILL );

	g_QglTable.m_pfn_qglShadeModel( GL_SMOOTH );

	g_QglTable.m_pfn_qglEnable( GL_BLEND );
	g_QglTable.m_pfn_qglBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
	g_QglTable.m_pfn_qglEnable( GL_POLYGON_SMOOTH );

	if ( portals.aa_3d ) {
		g_QglTable.m_pfn_qglEnable( GL_LINE_SMOOTH );
	}
	else{
		g_QglTable.m_pfn_qglDisable( GL_LINE_SMOOTH );
	}

	if ( portals.fog ) {
		g_QglTable.m_pfn_qglEnable( GL_FOG );

		g_QglTable.m_pfn_qglFogi( GL_FOG_MODE, GL_EXP );
		g_QglTable.m_pfn_qglFogf( GL_FOG_DENSITY, 0.001f );
		g_QglTable.m_pfn_qglFogf( GL_FOG_START, 10.0f );
		g_QglTable.m_pfn_qglFogf( GL_FOG_END, 10000.0f );
		g_QglTable.m_pfn_qglFogi( GL_FOG_INDEX, 0 );
		g_QglTable.m_pfn_qglFogfv( GL_FOG_COLOR, portals.fp_color_fog );
	}
	else
	{
		g_QglTable.m_pfn_qglDisable( GL_FOG );
	}

	switch ( portals.zbuffer )
	{
	case 1:
		g_QglTable.m_pfn_qglEnable( GL_DEPTH_TEST );
		g_QglTable.m_pfn_qglDepthMask( GL_FALSE );
		break;
	case 2:
		g_QglTable.m_pfn_qglDisable( GL_DEPTH_TEST );
		break;
	default:
		g_QglTable.m_pfn_qglEnable( GL_DEPTH_TEST );
		g_QglTable.m_pfn_qglDepthMask( GL_TRUE );
	}

	g_QglTable.m_pfn_qglLineWidth( portals.width_3d * 0.5f );

	unsigned int n, p;

	if ( portals.polygons ) {
		if ( portals.zbuffer != 0 ) {
			float d;

			for ( n = 0; n < portals.portal_count; n++ )
			{
				d = (float)cam[0] - portals.portal[n].center.p[0];
				portals.portal[n].dist = d * d;

				d = (float)cam[1] - portals.portal[n].center.p[1];
				portals.portal[n].dist += d * d;

				d = (float)cam[2] - portals.portal[n].center.p[2];
				portals.portal[n].dist += d * d;

				portals.portal_sort[n] = n;
			}

			qsort( portals.portal_sort, portals.portal_count, 4, compare );

			for ( n = 0; n < portals.portal_count; n++ )
			{
				if ( portals.polygons == 2 && !portals.portal[portals.portal_sort[n]].hint ) {
					continue;
				}

				if ( portals.clip ) {
					if ( min_check[0] < portals.portal[portals.portal_sort[n]].min[0] ) {
						continue;
					}
					else if ( min_check[1] < portals.portal[portals.portal_sort[n]].min[1] ) {
						continue;
					}
					else if ( min_check[2] < portals.portal[portals.portal_sort[n]].min[2] ) {
						continue;
					}
					else if ( max_check[0] > portals.portal[portals.portal_sort[n]].max[0] ) {
						continue;
					}
					else if ( max_check[1] > portals.portal[portals.portal_sort[n]].max[1] ) {
						continue;
					}
					else if ( max_check[2] > portals.portal[portals.portal_sort[n]].max[2] ) {
						continue;
					}
				}

				g_QglTable.m_pfn_qglColor4f( portals.portal[portals.portal_sort[n]].fp_color_random[0], portals.portal[portals.portal_sort[n]].fp_color_random[1],
											 portals.portal[portals.portal_sort[n]].fp_color_random[2], trans );

				g_QglTable.m_pfn_qglBegin( GL_POLYGON );

				for ( p = 0; p < portals.portal[portals.portal_sort[n]].point_count; p++ )
					g_QglTable.m_pfn_qglVertex3fv( portals.portal[portals.portal_sort[n]].point[p].p );

				g_QglTable.m_pfn_qglEnd();
			}
		}
		else
		{
			for ( n = 0; n < portals.portal_count; n++ )
			{
				if ( portals.polygons == 2 && !portals.portal[n].hint ) {
					continue;
				}

				if ( portals.clip ) {
					if ( min_check[0] < portals.portal[n].min[0] ) {
						continue;
					}
					else if ( min_check[1] < portals.portal[n].min[1] ) {
						continue;
					}
					else if ( min_check[2] < portals.portal[n].min[2] ) {
						continue;
					}
					else if ( max_check[0] > portals.portal[n].max[0] ) {
						continue;
					}
					else if ( max_check[1] > portals.portal[n].max[1] ) {
						continue;
					}
					else if ( max_check[2] > portals.portal[n].max[2] ) {
						continue;
					}
				}

				g_QglTable.m_pfn_qglColor4f( portals.portal[n].fp_color_random[0], portals.portal[n].fp_color_random[1],
											 portals.portal[n].fp_color_random[2], trans );

				g_QglTable.m_pfn_qglBegin( GL_POLYGON );

				for ( p = 0; p < portals.portal[n].point_count; p++ )
					g_QglTable.m_pfn_qglVertex3fv( portals.portal[n].point[p].p );

				g_QglTable.m_pfn_qglEnd();
			}
		}
	}

	if ( portals.lines ) {
		g_QglTable.m_pfn_qglColor4fv( portals.fp_color_3d );

		for ( n = 0; n < portals.portal_count; n++ )
		{
			if ( portals.lines == 2 && !portals.portal[n].hint ) {
				continue;
			}

			if ( portals.clip ) {
				if ( min_check[0] < portals.portal[n].min[0] ) {
					continue;
				}
				else if ( min_check[1] < portals.portal[n].min[1] ) {
					continue;
				}
				else if ( min_check[2] < portals.portal[n].min[2] ) {
					continue;
				}
				else if ( max_check[0] > portals.portal[n].max[0] ) {
					continue;
				}
				else if ( max_check[1] > portals.portal[n].max[1] ) {
					continue;
				}
				else if ( max_check[2] > portals.portal[n].max[2] ) {
					continue;
				}
			}

			g_QglTable.m_pfn_qglBegin( GL_LINE_LOOP );

			for ( p = 0; p < portals.portal[n].point_count; p++ )
				g_QglTable.m_pfn_qglVertex3fv( portals.portal[n].inner_point[p].p );

			g_QglTable.m_pfn_qglEnd();
		}
	}

	g_QglTable.m_pfn_qglPopAttrib();
}
Exemplo n.º 5
0
vec3 EditorToolBar::getGroundPickPos(float elevation) const
{
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	g_Camera.setCamera();




	vec3 groundPos;
	groundPos.zero();

	// Get the point on the ground plane that the mouse is hovering over.

	// Get a ray for the mouse
	vec3 mouseRay[] =
	{
		UnProject(0.0f),
		UnProject(1.0f),
	};

	// Get the location that the mouse ray intersect the ground plane at.
	// Check the mouse ray against the ground plane (XZ plane)
	float originDistance=0.0f;
	vec3 normal;

	// A polygon on the intersection plane
	vec3 a(0,elevation,0);
	vec3 b(0,elevation,1);
	vec3 c(1,elevation,0);
	vec3 vPoly[3] = {a, b, c};

	if(IntersectedPlane(vPoly, mouseRay, normal, originDistance) == true)
	{
		// Get the intersection point
		groundPos = IntersectionPoint(normal, mouseRay, (double)originDistance);

		// Snap to grid
		if(snapToGrid)
		{
			float snap = 0.25f;

			float snappedX = (snap) * floorf(groundPos.x / snap);
			float snappedZ = (snap) * floorf(groundPos.z / snap);

			groundPos.x = snappedX;
			groundPos.z = snappedZ;
		}
	}




	glMatrixMode(GL_PROJECTION);
	glPopMatrix();

	glMatrixMode(GL_MODELVIEW);
	glPopMatrix();



	return groundPos;
}