How to: Create custom textured hatch pattern (3DF)

The following sample code uses an image driver to create a custom hatch pattern. This image is then textured onto a shell (quad).

You can copy/paste the sample code to HOOPS PartViewer’s OnExtraSlot5


static void buildLines(double range, double z, double count, int priority) {
	double const d = range;
	HC_Set_Color("black");
	for (double i = -d + d / count; i < d; i += d / count) {
		HC_KEY key = HC_Insert_Line(-d, i, z, d, i, z);
		HC_Set_Priority(key, priority);
	}
}

static void renderToTexture(HC_KEY scene, const char *image_driver, const char *name,int width, int height)
{
	HC_KEY image_key;
	char buff[4096];

	HPoint pos(0, 0, 10);
	HPoint tar(0, 0, -19);
	HPoint up(0, 1, 0);


	float const d = 10;
	float const z1 = -10;
	int const priority = 1337;

	HC_Open_Segment ("/null");
	{
		HC_Set_Visibility("images");
		sprintf(buff, "rgba, name=%s", name);
		image_key = HC_KInsert_Image(0.0, 0.0, 0.0, buff, width, height, 0);
	}HC_Close_Segment ();

	HC_Open_Segment(image_driver);
	{			
		sprintf(buff, "isolated, debug=0x0100000, use window id=0x%p", image_key);
		HC_Set_Driver_Options(buff);
		HC_Set_Window_Frame("off");
		HC_KEY ret = HC_Open_Segment("lines"); {		
			HC_Set_Camera(&pos, &tar, &up, 8, 8, "orthographic");
			HC_Set_Visibility("lines=on");
			HC_Set_Color("windows=white");
			// For simiplicity, I am only using a single line pattern
			buildLines(d, z1 - 9, 20, priority + 1);
			HC_Set_Line_Pattern("-..");
		} HC_Close_Segment();
		HC_Update_One_Display(".");
	}HC_Close_Segment();



	//  edit image to change all white pixels and make it transparent

	unsigned char *		bytes = new unsigned char[width * height * 4];
	float				x, y, z;
	int					w, h;
	char				format[256];
	HC_Show_Image(image_key, &x, &y, &z, format, &w, &h, bytes);
	unsigned char *		current = bytes;

	for (int i = 0; i < width * height; ++i) {
		if (current[i] == 255  && current[i+1] == 255 && current[i+2] == 255)
			current[i+3] = 0;

		current += 3;
	}
	HC_Edit_Image(image_key, 0, 0, width, height, bytes);
	delete[] bytes;
	
	// image driver is no longer needed
	HC_Delete_Segment(image_driver);
}



void CSolidHoopsView::OnExtraSlot5()
{


	HC_KEY object_key, image_key;
	HPoint points[5];
	points[4].x = -0.5;	 points[4].y = -0.5;	 points[4].z = 0;
	points[3].x = 0.5;	 points[3].y = -0.5;	 points[3].z = 0;
	points[2].x = 0.5;	 points[2].y = 0.5;	 points[2].z = 0;
	points[1].x = -0.5;	 points[1].y = 0.5;	 points[1].z = 0;
	points[0].x = -0.5;	 points[0].y = -0.5;	 points[0].z = 0;
	
	char imageDriver[256];
	sprintf(imageDriver, "?driver/%s/image", m_pHView->GetDriverType());

	renderToTexture(m_pHView->GetSceneKey(), imageDriver, "texture1", 528, 528);

	HC_Open_Segment_By_Key(m_pHView->GetModelKey()); {
		float points[] = { 0,0,0, 1,0,0, 1,1,0, 0,1,0 };
		int flist[] = { 4, 0,1,2,3 };

		HC_Open_Segment(""); {
			HC_KEY k = HC_KInsert_Shell(4, points, 5, flist);
			HC_MSet_Vertex_Parameters(k, 0, 4, 3, points);
			HC_Set_Color("faces = (texture1)");
		} HC_Close_Segment();

	} HC_Close_Segment();

	m_pHView->Update();

}

Sphere is inserted to show that texture is transparent