1 #ifndef __BezierPatch_H__
2 #define __BezierPatch_H__
3 
4 #include "SdkSample.h"
5 
6 using namespace Ogre;
7 using namespace OgreBites;
8 
9 class _OgreSampleClassExport Sample_BezierPatch : public SdkSample
10 {
11 public:
12 
Sample_BezierPatch()13 	Sample_BezierPatch()
14 	{
15 		mInfo["Title"] = "Bezier Patch";
16 		mInfo["Description"] = "A demonstration of the Bezier patch support.";
17 		mInfo["Thumbnail"] = "thumb_bezier.png";
18 		mInfo["Category"] = "Geometry";
19 	}
20 
checkBoxToggled(CheckBox * box)21 	void checkBoxToggled(CheckBox* box)
22 	{
23 		mPatchPass->setPolygonMode(box->isChecked() ? PM_WIREFRAME : PM_SOLID);
24 
25 #ifdef INCLUDE_RTSHADER_SYSTEM
26 
27 		// Invalidate material in order to reflect polygon mode change in the generated shader based pass.
28 		mShaderGenerator->invalidateMaterial(RTShader::ShaderGenerator::DEFAULT_SCHEME_NAME, mPatchPass->getParent()->getParent()->getName());
29 #endif
30 	}
31 
sliderMoved(Slider * slider)32 	void sliderMoved(Slider* slider)
33 	{
34 		mPatch->setSubdivision(slider->getValue());
35 	}
36 
37 protected:
38 
39 #if OGRE_COMPILER == OGRE_COMPILER_MSVC
40 #	pragma pack(push, 1)
41 #endif
42     struct PatchVertex
43 	{
44         float x, y, z;
45         float nx, ny, nz;
46         float u, v;
47     };
48 #if OGRE_COMPILER == OGRE_COMPILER_MSVC
49 #	pragma pack(pop)
50 #endif
51 
setupContent()52 	void setupContent()
53 	{
54 		// setup some basic lighting for our scene
55 		mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
56         mSceneMgr->createLight()->setPosition(100, 100, 100);
57 
58 		// define the control point vertices for our patch
59 		PatchVertex verts[9] =
60 		{
61 			{-50, -35, -50, -0.5, 0.5, 0.0, 0.0, 0.0},
62 			{  0,   0, -50,  0.0, 0.5, 0.0, 0.5, 0.0},
63 			{ 50,  35, -50,  0.5, 0.5, 0.0, 1.0, 0.0},
64 			{-50,   0,   0, -0.5, 0.5, 0.0, 0.0, 0.5},
65 			{  0,   0,   0,  0.0, 0.5, 0.0, 0.5, 0.5},
66 			{ 50,   0,   0,  0.5, 0.5, 0.0, 1.0, 0.5},
67 			{-50,  35,  50, -0.5, 0.5, 0.0, 0.0, 1.0},
68 			{  0,   0,  50,  0.0, 0.5, 0.0, 0.5, 1.0},
69 			{ 50, -35,  50,  0.5, 0.5, 0.0, 1.0, 1.0}
70 		};
71 
72 		// specify a vertex format declaration for our patch: 3 floats for position, 3 floats for normal, 2 floats for UV
73         mDecl = HardwareBufferManager::getSingleton().createVertexDeclaration();
74         mDecl->addElement(0, 0, VET_FLOAT3, VES_POSITION);
75         mDecl->addElement(0, sizeof(float) * 3, VET_FLOAT3, VES_NORMAL);
76         mDecl->addElement(0, sizeof(float) * 6, VET_FLOAT2, VES_TEXTURE_COORDINATES, 0);
77 
78 		// create a patch mesh using vertices and declaration
79         mPatch = MeshManager::getSingleton().createBezierPatch("patch",
80 			ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, (float*)verts, mDecl, 3, 3, 5, 5, PatchSurface::VS_BOTH);
81 
82         mPatch->setSubdivision(0);   // start at 0 detail
83 
84 		// create a patch entity from the mesh, give it a material, and attach it to the origin
85         Entity* ent = mSceneMgr->createEntity("Patch", "patch");
86 		ent->setMaterialName("Examples/BumpyMetal");
87         mSceneMgr->getRootSceneNode()->attachObject(ent);
88 
89 		// save the main pass of the material so we can toggle wireframe on it
90 		mPatchPass = ent->getSubEntity(0)->getMaterial()->getTechnique(0)->getPass(0);
91 
92 		// use an orbit style camera
93 		mCameraMan->setStyle(CS_ORBIT);
94 		mCameraMan->setYawPitchDist(Degree(0), Degree(30), 250);
95 
96 		mTrayMgr->showCursor();
97 
98 		// create slider to adjust detail and checkbox to toggle wireframe
99 		mTrayMgr->createThickSlider(TL_TOPLEFT, "Detail", "Detail", 120, 44, 0, 1, 6);
100 		mTrayMgr->createCheckBox(TL_TOPLEFT, "Wireframe", "Wireframe", 120);
101 	}
102 
cleanupContent()103     void cleanupContent()
104     {
105         HardwareBufferManager::getSingleton().destroyVertexDeclaration(mDecl);
106 		mPatchPass->setPolygonMode(PM_SOLID);
107 		MeshManager::getSingleton().remove(mPatch->getHandle());
108     }
109 
110     VertexDeclaration* mDecl;
111 	PatchMeshPtr mPatch;
112 	Pass* mPatchPass;
113 };
114 
115 #endif
116