1 | /*
|
---|
2 | -----------------------------------------------------------------------------
|
---|
3 | This source file is part of OGRE
|
---|
4 | (Object-oriented Graphics Rendering Engine)
|
---|
5 | For the latest info, see http://www.ogre3d.org/
|
---|
6 |
|
---|
7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
8 | Also see acknowledgements in Readme.html
|
---|
9 |
|
---|
10 | You may use this sample code for anything you like, it is not covered by the
|
---|
11 | LGPL like the rest of the engine.
|
---|
12 | -----------------------------------------------------------------------------
|
---|
13 | */
|
---|
14 |
|
---|
15 |
|
---|
16 | /**
|
---|
17 | \file
|
---|
18 | Bezier.h
|
---|
19 | \brief
|
---|
20 | Specialisation of OGRE's framework application to show off
|
---|
21 | the bezier patch support.
|
---|
22 | */
|
---|
23 |
|
---|
24 | #include "ExampleApplication.h"
|
---|
25 |
|
---|
26 | // Hack struct for test
|
---|
27 | PatchMeshPtr patch;
|
---|
28 | Entity* patchEntity;
|
---|
29 |
|
---|
30 | // Event handler to add ability to alter subdivision
|
---|
31 | class BezierListener : public ExampleFrameListener
|
---|
32 | {
|
---|
33 | protected:
|
---|
34 | public:
|
---|
35 | BezierListener(RenderWindow* win, Camera* cam)
|
---|
36 | : ExampleFrameListener(win, cam)
|
---|
37 | {
|
---|
38 |
|
---|
39 | }
|
---|
40 |
|
---|
41 | bool frameStarted(const FrameEvent& evt)
|
---|
42 | {
|
---|
43 | static Real timeLapse = 0.0f;
|
---|
44 | static Real factor = 0.0;
|
---|
45 | static bool wireframe = 0;
|
---|
46 |
|
---|
47 |
|
---|
48 | timeLapse += evt.timeSinceLastFrame;
|
---|
49 |
|
---|
50 | // Prgressively grow the patch
|
---|
51 | if (timeLapse > 1.0f)
|
---|
52 | {
|
---|
53 | factor += 0.2;
|
---|
54 |
|
---|
55 | if (factor > 1.0f)
|
---|
56 | {
|
---|
57 | wireframe = !wireframe;
|
---|
58 | //mCamera->setDetailLevel(wireframe ? SDL_WIREFRAME : SDL_SOLID);
|
---|
59 | patchEntity->setRenderDetail(wireframe ? SDL_WIREFRAME : SDL_SOLID);
|
---|
60 | factor = 0.0f;
|
---|
61 |
|
---|
62 | }
|
---|
63 |
|
---|
64 | patch->setSubdivision(factor);
|
---|
65 | mWindow->setDebugText("Bezier subdivision factor: " + StringConverter::toString(factor));
|
---|
66 | timeLapse = 0.0f;
|
---|
67 |
|
---|
68 | }
|
---|
69 |
|
---|
70 | // Call default
|
---|
71 | return ExampleFrameListener::frameStarted(evt);
|
---|
72 | }
|
---|
73 | };
|
---|
74 |
|
---|
75 |
|
---|
76 | class BezierApplication : public ExampleApplication
|
---|
77 | {
|
---|
78 | protected:
|
---|
79 | VertexDeclaration* patchDecl;
|
---|
80 | float* patchCtlPoints;
|
---|
81 |
|
---|
82 | public:
|
---|
83 | BezierApplication() : patchDecl(NULL), patchCtlPoints(NULL) { }
|
---|
84 | ~BezierApplication()
|
---|
85 | {
|
---|
86 | if (patchCtlPoints)
|
---|
87 | delete [] patchCtlPoints;
|
---|
88 |
|
---|
89 | // patch vertex declaration will be deleted automatically
|
---|
90 | }
|
---|
91 |
|
---|
92 | protected:
|
---|
93 |
|
---|
94 | #if OGRE_COMPILER == OGRE_COMPILER_MSVC
|
---|
95 | #pragma pack(push)
|
---|
96 | #pragma pack(1)
|
---|
97 | #endif
|
---|
98 | struct PatchVertex {
|
---|
99 | float x, y, z;
|
---|
100 | float nx, ny, nz;
|
---|
101 | float u, v;
|
---|
102 | };
|
---|
103 | #if OGRE_COMPILER == OGRE_COMPILER_MSVC
|
---|
104 | #pragma pack(pop)
|
---|
105 | #endif
|
---|
106 |
|
---|
107 | // Just override the mandatory create scene method
|
---|
108 | void createScene(void)
|
---|
109 | {
|
---|
110 | // Set ambient light
|
---|
111 | mSceneMgr->setAmbientLight(ColourValue(0.2, 0.2, 0.2));
|
---|
112 |
|
---|
113 | // Create a point light
|
---|
114 | Light* l = mSceneMgr->createLight("MainLight");
|
---|
115 | // Accept default settings: point light, white diffuse, just set position
|
---|
116 | // NB I could attach the light to a SceneNode if I wanted it to move automatically with
|
---|
117 | // other objects, but I don't
|
---|
118 | l->setType(Light::LT_DIRECTIONAL);
|
---|
119 | l->setDirection(-0.5, -0.5, 0);
|
---|
120 |
|
---|
121 | // Create patch
|
---|
122 | patchDecl = HardwareBufferManager::getSingleton().createVertexDeclaration();
|
---|
123 | patchDecl->addElement(0, 0, VET_FLOAT3, VES_POSITION);
|
---|
124 | patchDecl->addElement(0, sizeof(float)*3, VET_FLOAT3, VES_NORMAL);
|
---|
125 | patchDecl->addElement(0, sizeof(float)*6, VET_FLOAT2, VES_TEXTURE_COORDINATES, 0);
|
---|
126 |
|
---|
127 | // Make a 3x3 patch for test
|
---|
128 | patchCtlPoints = (float*)( new PatchVertex[9] );
|
---|
129 |
|
---|
130 | // Patch data
|
---|
131 | PatchVertex *pVert = (PatchVertex*)patchCtlPoints;
|
---|
132 |
|
---|
133 | pVert->x = -500.0; pVert->y = 200.0; pVert->z = -500.0;
|
---|
134 | pVert->nx = -0.5; pVert->ny = 0.5; pVert->nz = 0.0;
|
---|
135 | pVert->u = 0.0; pVert->v = 0.0;
|
---|
136 | pVert++;
|
---|
137 | pVert->x = 0.0; pVert->y = 500.0; pVert->z = -750.0;
|
---|
138 | pVert->nx = 0.0; pVert->ny = 0.5; pVert->nz = 0.0;
|
---|
139 | pVert->u = 0.5; pVert->v = 0.0;
|
---|
140 | pVert++;
|
---|
141 | pVert->x = 500.0; pVert->y = 1000.0; pVert->z = -500.0;
|
---|
142 | pVert->nx = 0.5; pVert->ny = 0.5; pVert->nz = 0.0;
|
---|
143 | pVert->u = 1.0; pVert->v = 0.0;
|
---|
144 | pVert++;
|
---|
145 |
|
---|
146 | pVert->x = -500.0; pVert->y = 0.0; pVert->z = 0.0;
|
---|
147 | pVert->nx = -0.5; pVert->ny = 0.5; pVert->nz = 0.0;
|
---|
148 | pVert->u = 0.0; pVert->v = 0.5;
|
---|
149 | pVert++;
|
---|
150 | pVert->x = 0.0; pVert->y = 500.0; pVert->z = 0.0;
|
---|
151 | pVert->nx = 0.0; pVert->ny = 0.5; pVert->nz = 0.0;
|
---|
152 | pVert->u = 0.5; pVert->v = 0.5;
|
---|
153 | pVert++;
|
---|
154 | pVert->x = 500.0; pVert->y = -50.0; pVert->z = 0.0;
|
---|
155 | pVert->nx = 0.5; pVert->ny = 0.5; pVert->nz = 0.0;
|
---|
156 | pVert->u = 1.0; pVert->v = 0.5;
|
---|
157 | pVert++;
|
---|
158 |
|
---|
159 | pVert->x = -500.0; pVert->y = 0.0; pVert->z = 500.0;
|
---|
160 | pVert->nx = -0.5; pVert->ny = 0.5; pVert->nz = 0.0;
|
---|
161 | pVert->u = 0.0; pVert->v = 1.0;
|
---|
162 | pVert++;
|
---|
163 | pVert->x = 0.0; pVert->y = 500.0; pVert->z = 500.0;
|
---|
164 | pVert->nx = 0.0; pVert->ny = 0.5; pVert->nz = 0.0;
|
---|
165 | pVert->u = 0.5; pVert->v = 1.0;
|
---|
166 | pVert++;
|
---|
167 | pVert->x = 500.0; pVert->y = 200.0; pVert->z = 800.0;
|
---|
168 | pVert->nx = 0.5; pVert->ny = 0.5; pVert->nz = 0.0;
|
---|
169 | pVert->u = 1.0; pVert->v = 1.0;
|
---|
170 | pVert++;
|
---|
171 |
|
---|
172 |
|
---|
173 | patch = MeshManager::getSingleton().createBezierPatch(
|
---|
174 | "Bezier1", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
|
---|
175 | patchCtlPoints, patchDecl,
|
---|
176 | 3, 3, 5, 5, PatchSurface::VS_BOTH);
|
---|
177 |
|
---|
178 | // Start patch at 0 detail
|
---|
179 | patch->setSubdivision(0.0f);
|
---|
180 | // Create entity based on patch
|
---|
181 | patchEntity = mSceneMgr->createEntity("Entity1", "Bezier1");
|
---|
182 |
|
---|
183 | MaterialPtr pMat = MaterialManager::getSingleton().create("TextMat",
|
---|
184 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
|
---|
185 | pMat->getTechnique(0)->getPass(0)->createTextureUnitState( "BumpyMetal.jpg" );
|
---|
186 | patchEntity->setMaterialName("TextMat");
|
---|
187 |
|
---|
188 | // Attach the entity to the root of the scene
|
---|
189 | mSceneMgr->getRootSceneNode()->attachObject(patchEntity);
|
---|
190 |
|
---|
191 | mCamera->setPosition(500,500, 1500);
|
---|
192 | mCamera->lookAt(0,200,-300);
|
---|
193 |
|
---|
194 | }
|
---|
195 | void destroyScene(void)
|
---|
196 | {
|
---|
197 | // free up the pointer before we shut down OGRE
|
---|
198 | patch.setNull();
|
---|
199 | }
|
---|
200 | void createFrameListener(void)
|
---|
201 | {
|
---|
202 | // This is where we instantiate our own frame listener
|
---|
203 | mFrameListener= new BezierListener(mWindow, mCamera);
|
---|
204 | mRoot->addFrameListener(mFrameListener);
|
---|
205 |
|
---|
206 | }
|
---|
207 |
|
---|
208 | };
|
---|