void OpticalFlow::baseCalculate(cv::Mat& Im1, cv::Mat& Im2, flowUV& UV, const OpticalFlowParams& params){
	int rows = Im1.rows;
	int cols = Im1.cols;

	FlowOperator flowOp(rows, cols);
	FArray X0(2 * rows * cols, false);	

	FArray dUdV(2 * rows * cols, true, 0);
	cv::Mat Ix1(rows, cols, OPTFLOW_TYPE);
	cv::Mat Iy1(rows, cols, OPTFLOW_TYPE); 
	cv::Mat Ix(rows, cols, OPTFLOW_TYPE);
	cv::Mat Iy(rows, cols, OPTFLOW_TYPE); 
	getDXsCV(Im1, Ix1, Iy1);
	for (int i = 0; i < params.getIters(); ++i){
		cv::Mat Ix2(rows, cols, OPTFLOW_TYPE);
		cv::Mat Iy2(rows, cols, OPTFLOW_TYPE); 
		cv::Mat It(rows, cols, OPTFLOW_TYPE); 

		cv::Mat im2Warpped(rows, cols, Im1.type());
		WarpImage(Im2, UV.getU(), UV.getV(), im2Warpped);	
		
		getDXsCV(im2Warpped, Ix2, Iy2);
		Ix = params.getWeightedDeriveFactor() * (Ix1 + Ix2);
		Iy = params.getWeightedDeriveFactor() * (Iy1 + Iy2);
		cv::subtract(im2Warpped, Im1, It);

		if (params.getDisplayDerivativs()){
			cv::imshow("Derivative Ix", Ix);
			cv::imshow("Derivative Iy", Iy);
			cv::waitKey(1);
		}
		
		cv::Mat Du(rows, cols, OPTFLOW_TYPE, cv::Scalar(0));
		cv::Mat Dv(rows, cols, OPTFLOW_TYPE, cv::Scalar(0));


		for (int j = 0; j < params.getLinearIters(); ++j){
#if OPTFLOW_VERBOSE
			cout << "solving Ax=b with SOR ";
			clock_t start = std::clock();	
#endif		
			flowOp.construct(UV, Du, Dv, Ix, Iy, It, params);
			
			memcpy(X0.ptr, UV.getU().data, rows * cols * sizeof(float));
			memcpy(X0.ptr + (rows * cols), UV.getV().data, rows * cols * sizeof(float));
			//UtilsDebug::printCRSSparseMat(flowOp.getA(), "aaaa.txt");
			if (params.getCheckResidualTolerance()){
				LinearSolver::sparseMatSor(flowOp.getA(), X0 ,dUdV, flowOp.getb(), params.getOverRelaxation(), params.getSorIters(), params.getResidualTolerance());
			}else{
				//LinearSolver::multigrid(10,10,flowOp.getA(),flowOp.getb(), params.getResidualTolerance(), dUdV, 20, 20, LinearSolver::vCycle);
				LinearSolver::sparseMatSorNoResidual(flowOp.getA(), X0 ,dUdV, flowOp.getb(), params.getOverRelaxation(), params.getSorIters());
			}
#if OPTFLOW_VERBOSE
		std::cout<<" --- "<< (std::clock() - start) / (double)CLOCKS_PER_SEC <<'\n';
#endif

#if OPTFLOW_DEBUG
			for(int i = 0; i < dUdV.size(); ++i){
				if (!(dUdV.ptr[i] == dUdV.ptr[i])){
					cout << "ERROR - NAN";
				}
			}
#endif

			UtilsMat::clamp(dUdV, -1, 1);

			memcpy(Du.data, dUdV.ptr, rows * cols * sizeof(float));
			memcpy(Dv.data, dUdV.ptr + (rows * cols), rows * cols * sizeof(float));
			
			flowUV UV0(UV);
			UV.getU() += Du;
			UV.getV() += Dv;

			cv::Mat tmpU, tmpV;
			UV.getU().copyTo(tmpU);
			UV.getV().copyTo(tmpV);

			WeightedMedianFilter::computeMedianFilter(UV.getU(), UV.getV(), Im1, Im2, params.getMedianFilterRadius());

			Du = UV.getU() - UV0.getU();
			Dv = UV.getV() - UV0.getV();

			UV0.getU().copyTo(UV.getU());
			UV0.getV().copyTo(UV.getV());

			UV0.getU() += Du;
			UV0.getV() += Dv;
			if (params.isDisplay())
				UtilsFlow::DrawFlow(UV0.getU(), UV0.getV(), "Flow");
		}
		UV.getU() += Du;
		UV.getV() += Dv;
	}
}
void USlateBrushThumbnailRenderer::Draw(UObject* Object, int32 X, int32 Y, uint32 Width, uint32 Height, FRenderTarget* RenderTarget, FCanvas* Canvas)
{
	USlateBrushAsset* SlateBrushAsset = Cast<USlateBrushAsset>(Object);
	if (SlateBrushAsset)
	{
		FSlateBrush Brush = SlateBrushAsset->Brush;
		UTexture2D* Texture = Cast<UTexture2D>( Brush.GetResourceObject() );

		// Draw the background checkboard pattern
		const int32 CheckerDensity = 8;
		auto* Checker = UThumbnailManager::Get().CheckerboardTexture;
		Canvas->DrawTile(
			0.0f, 0.0f, Width, Height,							// Dimensions
			0.0f, 0.0f, CheckerDensity, CheckerDensity,			// UVs
			FLinearColor::White, Checker->Resource);			// Tint & Texture

		if (Texture)
		{
			switch(Brush.DrawAs)
			{
			case ESlateBrushDrawType::Image:
				{
					FCanvasTileItem CanvasTile( FVector2D( X, Y ), Texture->Resource, FVector2D( Width,Height ), Brush.TintColor.GetSpecifiedColor() );
					CanvasTile.BlendMode = SE_BLEND_Translucent;
					CanvasTile.Draw( Canvas );
				}
				break;
			case ESlateBrushDrawType::Border:
				{
					FCanvasTileItem CanvasTile( FVector2D( X, Y ), Texture->Resource, FVector2D( Width,Height ), Brush.TintColor.GetSpecifiedColor() );
					CanvasTile.BlendMode = SE_BLEND_Translucent;
					CanvasTile.Draw( Canvas );
				}
				break;
			case ESlateBrushDrawType::Box:
				{
					float NaturalWidth = Texture->GetSurfaceWidth();
					float NaturalHeight = Texture->GetSurfaceHeight();

					float TopPx = FMath::Clamp<float>(NaturalHeight * Brush.Margin.Top, 0, Height);
					float BottomPx = FMath::Clamp<float>(NaturalHeight * Brush.Margin.Bottom, 0, Height);
					float VerticalCenterPx = FMath::Clamp<float>(Height - TopPx - BottomPx, 0, Height);
					float LeftPx = FMath::Clamp<float>(NaturalWidth * Brush.Margin.Left, 0, Width);
					float RightPx = FMath::Clamp<float>(NaturalWidth * Brush.Margin.Right, 0, Width);
					float HorizontalCenterPx = FMath::Clamp<float>(Width - LeftPx - RightPx, 0, Width);

					// Top-Left
					FVector2D TopLeftSize( LeftPx, TopPx );
					{
						FVector2D UV0( 0, 0 );
						FVector2D UV1( Brush.Margin.Left, Brush.Margin.Top );

						FCanvasTileItem CanvasTile( FVector2D( X, Y ), Texture->Resource, TopLeftSize, UV0, UV1, Brush.TintColor.GetSpecifiedColor() );
						CanvasTile.BlendMode = SE_BLEND_Translucent;
						CanvasTile.Draw( Canvas );
					}

					// Bottom-Left
					FVector2D BottomLeftSize( LeftPx, BottomPx );
					{
						FVector2D UV0( 0, 1 - Brush.Margin.Bottom );
						FVector2D UV1( Brush.Margin.Left, 1 );

						FCanvasTileItem CanvasTile( FVector2D( X, Y + Height - BottomPx ), Texture->Resource, BottomLeftSize, UV0, UV1, Brush.TintColor.GetSpecifiedColor() );
						CanvasTile.BlendMode = SE_BLEND_Translucent;
						CanvasTile.Draw( Canvas );
					}

					// Top-Right
					FVector2D TopRightSize( RightPx, TopPx );
					{
						FVector2D UV0( 1 - Brush.Margin.Right, 0 );
						FVector2D UV1( 1, Brush.Margin.Top );

						FCanvasTileItem CanvasTile( FVector2D( X + Width - RightPx, Y ), Texture->Resource, TopRightSize, UV0, UV1, Brush.TintColor.GetSpecifiedColor() );
						CanvasTile.BlendMode = SE_BLEND_Translucent;
						CanvasTile.Draw( Canvas );
					}

					// Bottom-Right
					FVector2D BottomRightSize( RightPx, BottomPx );
					{
						FVector2D UV0( 1 - Brush.Margin.Right, 1 - Brush.Margin.Bottom );
						FVector2D UV1( 1, 1 );

						FCanvasTileItem CanvasTile( FVector2D( X + Width - RightPx, Y + Height - BottomPx ), Texture->Resource, BottomRightSize, UV0, UV1, Brush.TintColor.GetSpecifiedColor() );
						CanvasTile.BlendMode = SE_BLEND_Translucent;
						CanvasTile.Draw( Canvas );
					}

					//-----------------------------------------------------------------------

					// Center-Vertical-Left
					FVector2D CenterVerticalLeftSize( LeftPx, VerticalCenterPx );
					{
						FVector2D UV0( 0, Brush.Margin.Top );
						FVector2D UV1( Brush.Margin.Left, 1 - Brush.Margin.Bottom );

						FCanvasTileItem CanvasTile( FVector2D( X, Y + TopPx), Texture->Resource, CenterVerticalLeftSize, UV0, UV1, Brush.TintColor.GetSpecifiedColor() );
						CanvasTile.BlendMode = SE_BLEND_Translucent;
						CanvasTile.Draw( Canvas );
					}

					// Center-Vertical-Right
					FVector2D CenterVerticalRightSize( RightPx, VerticalCenterPx );
					{
						FVector2D UV0( 1 - Brush.Margin.Right, Brush.Margin.Top );
						FVector2D UV1( 1, 1 - Brush.Margin.Bottom );

						FCanvasTileItem CanvasTile( FVector2D( X + Width - RightPx, Y + TopPx), Texture->Resource, CenterVerticalRightSize, UV0, UV1, Brush.TintColor.GetSpecifiedColor() );
						CanvasTile.BlendMode = SE_BLEND_Translucent;
						CanvasTile.Draw( Canvas );
					}

					//-----------------------------------------------------------------------

					// Center-Horizontal-Top
					FVector2D CenterHorizontalTopSize( HorizontalCenterPx, TopPx );
					{
						FVector2D UV0( Brush.Margin.Left, 0 );
						FVector2D UV1( 1 - Brush.Margin.Right, Brush.Margin.Top );

						FCanvasTileItem CanvasTile( FVector2D( X + LeftPx, Y), Texture->Resource, CenterHorizontalTopSize, UV0, UV1, Brush.TintColor.GetSpecifiedColor() );
						CanvasTile.BlendMode = SE_BLEND_Translucent;
						CanvasTile.Draw( Canvas );
					}

					// Center-Horizontal-Bottom
					FVector2D CenterHorizontalBottomSize( HorizontalCenterPx, BottomPx );
					{
						FVector2D UV0( Brush.Margin.Left, 1 - Brush.Margin.Bottom );
						FVector2D UV1( 1 - Brush.Margin.Right, 1 );

						FCanvasTileItem CanvasTile( FVector2D( X + LeftPx, Y + Height - BottomPx ), Texture->Resource, CenterHorizontalBottomSize, UV0, UV1, Brush.TintColor.GetSpecifiedColor() );
						CanvasTile.BlendMode = SE_BLEND_Translucent;
						CanvasTile.Draw( Canvas );
					}

					//-----------------------------------------------------------------------

					// Center
					FVector2D CenterSize( HorizontalCenterPx, VerticalCenterPx );
					{
						FVector2D UV0( Brush.Margin.Left, Brush.Margin.Top );
						FVector2D UV1( 1 - Brush.Margin.Right, 1 - Brush.Margin.Bottom );

						FCanvasTileItem CanvasTile( FVector2D( X + LeftPx, Y + TopPx), Texture->Resource, CenterSize, UV0, UV1, Brush.TintColor.GetSpecifiedColor() );
						CanvasTile.BlendMode = SE_BLEND_Translucent;
						CanvasTile.Draw( Canvas );
					}
				}
				break;
			case ESlateBrushDrawType::NoDrawType:
				{
					FCanvasTileItem CanvasTile( FVector2D( X, Y ), Texture->Resource, FVector2D( Width,Height ), Brush.TintColor.GetSpecifiedColor() );
					CanvasTile.BlendMode = SE_BLEND_Translucent;
					CanvasTile.Draw( Canvas );
				}
				break;
			default:

				check(false);
			}
		}
	}
}
Esempio n. 3
0
void obj_display() {

	// 只有一个物体
	mesh *object = globalscene->mList[0].obejct;
	//bind texture 0
	// glUnifor1i( 0) for colorTexture
	glActiveTexture( GL_TEXTURE0 );
	glBindTexture(GL_TEXTURE_2D, globalscene->mList[0].ambTextureId);
	GLint location0 = glGetUniformLocation(MyShader, "colorTexture");
	if(location0 == -1)
		printf("Cant find texture name: colorTexture\n");
	else
		glUniform1i(location0, 0);
	//bind texture 1
	// glUnifor1i( 1) for diffuse Texture
	glActiveTexture( GL_TEXTURE1 );
	glBindTexture(GL_TEXTURE_2D, globalscene->mList[0].difTextureId);
	GLint location1 = glGetUniformLocation(MyShader, "diffuseTex");
	if(location1 == -1)
		printf("Cant find texture name: diffuseTex\n");
	else
		glUniform1i(location1, 1);
	//bind texture 2
	// glUnifor1i( 2) for specular Texture
	glActiveTexture( GL_TEXTURE2 );
	glBindTexture(GL_TEXTURE_2D, globalscene->mList[0].spcTextureId);
	GLint location2 = glGetUniformLocation(MyShader, "specularTex");
	if(location2 == -1)
		printf("Cant find texture name: specularTex\n");
	else
		glUniform1i(location2, 2);

	// 确定顶点属性的 location,算完切向量后,再设置属性值
	tangent_loc = glGetAttribLocation(MyShader, "tangent");
	bitangent_loc = glGetAttribLocation(MyShader, "bitangent");

	int lastMaterial = -1;
	for(size_t i=0;i < object->fTotal;++i)
	{
		// set material property if this face used different material
		if(lastMaterial != object->faceList[i].m)
		{
			lastMaterial = (int)object->faceList[i].m;
			glMaterialfv(GL_FRONT, GL_AMBIENT  , object->mList[lastMaterial].Ka);
			glMaterialfv(GL_FRONT, GL_DIFFUSE  , object->mList[lastMaterial].Kd);
			glMaterialfv(GL_FRONT, GL_SPECULAR , object->mList[lastMaterial].Ks);
			glMaterialfv(GL_FRONT, GL_SHININESS, &object->mList[lastMaterial].Ns);

			//you can obtain the texture name by object->mList[lastMaterial].map_Kd
			//load them once in the main function before mainloop
			//bind them in display function here
		}

		// 取得三个顶点 p0 p1 p2 坐标
		float *vertex0 = object->vList[object->faceList[i][0].v].ptr;
		float *vertex1 = object->vList[object->faceList[i][1].v].ptr;
		float *vertex2 = object->vList[object->faceList[i][2].v].ptr;
		glm::vec3 p0(vertex0[0], vertex0[1], vertex0[2]);
		glm::vec3 p1(vertex1[0], vertex1[1], vertex1[2]);
		glm::vec3 p2(vertex2[0], vertex2[1], vertex2[2]);
		// 取得贴图三点 对应贴图坐标
		float *texture0 = object->tList[object->faceList[i][0].t].ptr;
		float *texture1 = object->tList[object->faceList[i][1].t].ptr;
		float *texture2 = object->tList[object->faceList[i][2].t].ptr;
		glm::vec2 UV0(texture0[0], texture0[1]);
		glm::vec2 UV1(texture1[0], texture1[1]);
		glm::vec2 UV2(texture2[0], texture2[1]);
		// 得到两边
		glm::vec3 Edge1 = p1 - p0;
		glm::vec3 Edge2 = p2 - p0;
		glm::vec2 Edge1uv = UV1 - UV0;
		glm::vec2 Edge2uv = UV2 - UV0;
		// 计算切向量,副切向量
		glm::vec3 tangent, bitangent;
		float cp = Edge1uv.x * Edge2uv.y - Edge2uv.x * Edge1uv.y;
		if(cp != 0.0f) {
			float mul = 1.0f /cp;
			tangent = (Edge1 * Edge2uv.y + Edge2 * -Edge1uv.y) * mul;
			bitangent = (Edge1 * -Edge2uv.x + Edge2 * Edge1uv.x) * mul;
		}
		// specify the value of a generic vertex attribute 设置顶点属性
		glVertexAttrib3f(tangent_loc, tangent.x, tangent.y, tangent.z);
		glVertexAttrib3f(bitangent_loc, bitangent.x, bitangent.y, bitangent.z);

		glBegin(GL_TRIANGLES);
		for (size_t j=0;j<3;++j)
		{
			//textex corrd. 
			glMultiTexCoord2fv(GL_TEXTURE0, object->tList[object->faceList[i][j].t].ptr);
			glMultiTexCoord2fv(GL_TEXTURE1, object->tList[object->faceList[i][j].t].ptr);
			glMultiTexCoord2fv(GL_TEXTURE2, object->tList[object->faceList[i][j].t].ptr);
			glNormal3fv(object->nList[object->faceList[i][j].n].ptr);
			glVertex3fv(object->vList[object->faceList[i][j].v].ptr);	
		}
		glEnd();
	}
}