forked from giogix2/PlanetGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
238 lines (188 loc) · 6.7 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* Disclaimer: For demo use only! It is one main()-blob by design,
* or rather because of lack of design.
*
* No animals were harmed while making of this program...
* ok, i consumed some tuna while coding, but those were dead already.
*/
#include <iostream>
#include <exception>
#include "OGRE/Ogre.h"
#include "PSphere.h"
#include "OgreConfigFile.h"
#include "GeneratorFrameListener.h"
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif
#ifdef OGRE_PLATFORM_LINUX
#include <sys/stat.h>
#endif
class initOgre
{
public:
Ogre::Root *Root;
Ogre::SceneManager *Scene;
Ogre::SceneNode *RootSceneNode;
Ogre::RenderWindow *Window;
Ogre::Camera *Camera;
GeneratorFrameListener *FrameListener;
Ogre::OverlaySystem *OverlaySystem;
initOgre()
{
FrameListener = 0;
Root = 0;
OverlaySystem = 0;
}
int start();
void CreateFrameListener();
};
int initOgre::start()
{
Root = new Ogre::Root("", "", "Generator.LOG");
OverlaySystem = new Ogre::OverlaySystem();
Ogre::String PluginName;
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
PluginName.append("RenderSystem_Direct3D9");
#else
/* Use Posix-function stat to check existence of a file.
* This is needed because Ogre makes zero effort to find its own plugins. */
struct stat statBuf;
Ogre::String PluginFile[3];
PluginFile[0].append("/usr/lib/OGRE/RenderSystem_GL.so");
PluginFile[1].append("/usr/lib/local/OGRE/RenderSystem_GL.so");
// Ubuntu
#ifdef __x86_64__
PluginFile[2].append("/usr/lib/x86_64-linux-gnu/OGRE-");
#elif __i386__
PluginFile[2].append("/usr/lib/i386-linux-gnu/OGRE-");
#endif
PluginFile[2] += Ogre::StringConverter::toString(OGRE_VERSION_MAJOR) + ".";
PluginFile[2] += Ogre::StringConverter::toString(OGRE_VERSION_MINOR) + ".";
PluginFile[2] += Ogre::StringConverter::toString(OGRE_VERSION_PATCH);
PluginFile[2] += "/RenderSystem_GL.so";
int i;
for(i=0; i < 3; i++)
{
if( stat(PluginFile[i].c_str(), &statBuf) == 0 )
{
PluginFile[i].resize(PluginFile[i].size()-3); // strip ".so"
PluginName.assign( PluginFile[i] );
break; // Exit loop if file exists
}
}
if(PluginName == "")
PluginName.append("RenderSystem_GL"); // When all else fails...
#endif
if(OGRE_DEBUG_MODE)
PluginName.append("_d"); // RenderSystem_GL_d if using Ogre debug mode
// Loads renderer plugin
Root->loadPlugin(PluginName);
// Check renderer availability
const Ogre::RenderSystemList& RenderSystemList = Root->getAvailableRenderers();
if( RenderSystemList.size() == 0 )
{
Ogre::LogManager::getSingleton().logMessage("Rendersystems not found.");
return 1;
}
Ogre::RenderSystem *RenderSystem = RenderSystemList[0];
Root->setRenderSystem(RenderSystem);
// Do not create window automatically
Root->initialise(false);
Ogre::NameValuePairList renderParams;
renderParams["FSAA"] = "0";
renderParams["vsync"] = "true"; // Waits for vertical sync
// Actual call to create window, bool value is fullscreen flag
Window = Root->createRenderWindow("My little planet", 800, 600, false, &renderParams);
// Start creating scene
Scene = Root->createSceneManager(Ogre::ST_GENERIC);
RootSceneNode = Scene->getRootSceneNode();
if(OverlaySystem)
Scene->addRenderQueueListener(OverlaySystem);
return 0;
}
void initOgre::CreateFrameListener()
{
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE_IOS
FrameListener= new GeneratorFrameListener(Window, Camera, true, true, true);
#else
FrameListener= new GeneratorFrameListener(Window, Camera,RootSceneNode);
#endif
FrameListener->showDebugOverlay(true);
Root->addFrameListener(FrameListener);
}
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
int main(int argc, char *argv[])
#endif
{
try
{
initOgre myOgre;
myOgre.start();
// Create camera
myOgre.Camera = myOgre.Scene->createCamera("VertCamera");
// Attach camera to sceneNode
Ogre::SceneNode *CameraNode = myOgre.RootSceneNode->createChildSceneNode("DefaultCameraNode");
CameraNode->attachObject(myOgre.Camera);
// Camera position
myOgre.Camera->setPosition(Ogre::Vector3(0,0,20));
// Camera looks toward origo
myOgre.Camera->lookAt(Ogre::Vector3(0,0,0));
// Create viewport
Ogre::Viewport *vp = myOgre.Window->addViewport(myOgre.Camera);
vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
// Draw distances
myOgre.Camera->setNearClipDistance(1.5f);
myOgre.Camera->setFarClipDistance(3000.0f); // note to self: do not set if using stencil shadows
// Let there be light
myOgre.Scene->setAmbientLight(Ogre::ColourValue(0.02f, 0.02f, 0.02f));
Ogre::Light *light = myOgre.Scene->createLight( "PointLight" );
light->setType(Ogre::Light::LT_POINT);
light->setPosition(200, 40, 150);
// Draw a sphere
PSphere mySphere;
mySphere.create(15.0f, 0.6f, 100);
mySphere.loadToBuffers("CustomMesh", "sphereTex");
mySphere.destroy();
Ogre::Entity *entity1 = myOgre.Scene->createEntity("CustomEntity", "CustomMesh");
Ogre::SceneNode *sphere1 = myOgre.Scene->getRootSceneNode()->createChildSceneNode("planetSphere");
sphere1->attachObject(entity1);
// No need for this anymore
Ogre::MeshManager::getSingleton().remove("CustomMesh");
Ogre::MaterialPtr textureMap = Ogre::MaterialManager::getSingleton()
.create("TextureObject",Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
textureMap->getTechnique(0)->getPass(0)->createTextureUnitState("sphereTex");
textureMap->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
// Set texture for the sphere
entity1->setMaterial(textureMap);
sphere1->setOrientation(1.3003361e-01f, -1.5604560e-01f, -7.5052901e-01f, 6.2884596e-01f);
//createFrameListener
myOgre.CreateFrameListener();
//start Rendering
myOgre.Root->startRendering();
// Clean up our mess before exiting
Ogre::MaterialManager::getSingleton().remove("TextureObject");
Ogre::TextureManager::getSingleton().remove("sphereTex");
myOgre.Scene->destroyEntity("CustomEntity");
myOgre.Scene->destroySceneNode(sphere1);
myOgre.Scene->destroySceneNode(CameraNode);
myOgre.Scene->destroyCamera(myOgre.Camera);
myOgre.Scene->destroyLight(light);
Ogre::Root::getSingleton().getRenderSystem()->destroyRenderWindow("My little planet");
myOgre.Scene->clearScene();
myOgre.Root->shutdown();
myOgre.Root->destroySceneManager(myOgre.Scene);
Ogre::LogManager::getSingleton().logMessage("The end is near");
}
// Let's make informative messages in case there is an error
catch(Ogre::Exception &error)
{
std::cout << "Ogre makes an exception! " << error.what() << std::endl;
}
catch(std::exception &error)
{
std::cout << "Shit hit the fan! For details, see the fan " << error.what() << std::endl;
}
return 0;
}