Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/Samples/BezierPatch/include/Bezier.h @ 1

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