Coments on codesnippet before wikification

Anything and everything that's related to OGRE or the wider graphics field that doesn't fit into the other forums.
Post Reply
Lijat
Halfling
Posts: 51
Joined: Tue Aug 02, 2005 10:51 pm

Coments on codesnippet before wikification

Post by Lijat »

I have been trying to learn Ogre (again) and thought that I should share the minimal example I made.

I will post this to the wiki (unless someone tells me not to) but I would like to get some comments on it first, I would also appreciate suggestions on where in the wiki to put this.

The code is only creates a window and prepare a directory for loading resources. I didn't want to use the rendersystem selection dialog or the usual config files as I feel that thous would be unwieldy to use in a larger project (just my opinion), similarly I wanted to control my own mainloop.

Code below.

Code: Select all

#include <Ogre.h>

using namespace Ogre; 

void initResources(){
        ResourceGroupManager &resources=ResourceGroupManager::getSingleton();
        resources.addResourceLocation("data","FileSystem");
        resources.initialiseAllResourceGroups();
}
Root* initOgre(){
        Root* root = new Root;
        root->loadPlugin("/usr/local/lib/OGRE/RenderSystem_GL");

        RenderSystemList *rs = root->getAvailableRenderers();
        if(rs&&rs->size()&&rs->at(0)->getName().compare("RenderSystem_GL")){
                RenderSystem * r=rs->at(0);
                root->setRenderSystem(r);

                r->setConfigOption("Full Screen","No");  
                r->setConfigOption("Video Mode","800 x 600 @ 16-bit colour");
        }else{
                exit(1);
        }

        initResources();

        return root;
}
Camera* createCamera(SceneManager *sceneMgr,RenderWindow *window){
        Camera* cam = sceneMgr->createCamera("SimpleCamera"); 
        cam->setPosition(Vector3(0.0f,0.0f,500.0f));
        cam->lookAt(Vector3(0.0f,0.0f,0.0f));
        cam->setNearClipDistance(5.0f);
        cam->setFarClipDistance(5000.0f);

        Viewport* v = window->addViewport(cam);
        v->setBackgroundColour(ColourValue(0.5,0.5,0.5));

        cam->setAspectRatio(Real(v->getActualWidth())/v->getActualHeight());

        return cam;
}
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 
#define WIN32_LEAN_AND_MEAN 
#include "windows.h" 
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT){
#else 
int main(int argc, char **argv){
#endif 
        Root *root=initOgre();

        RenderWindow* window = root->initialise(true, "Simple Ogre App");
        SceneManager* sceneMgr = root->createSceneManager(ST_GENERIC); 

        Camera *cam=createCamera(sceneMgr,window);
        sceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(cam);

        int running=1000;
        while(running--)
        {
                WindowEventUtilities::messagePump();
                root->renderOneFrame();
                printf("%d\n",running);
        }

        delete root;
        return 0; 
}

[/quote]
User avatar
nebukadnezzar
Halfling
Posts: 93
Joined: Sun Jun 11, 2006 4:40 pm
Location: Germany
x 1
Contact:

Re: Coments on codesnippet before wikification

Post by nebukadnezzar »

Isn't there already a minimal example?
Lijat
Halfling
Posts: 51
Joined: Tue Aug 02, 2005 10:51 pm

Post by Lijat »

Depends on what you consider minimal. The other one uses the config dialog and does not run it's own mainloop. I can call it something else if two snippets with similar names would be confusing. (I though I should post it as it solves some problems that I did not find any examples for myself)
User avatar
Game_Ender
Ogre Magi
Posts: 1269
Joined: Wed May 25, 2005 2:31 am
Location: Rockville, MD, USA

Post by Game_Ender »

You should take a look at the practical application in the wiki. This is a "Minimal Practical Application", which is the right kind off minimal example in my opinion. It shows you just what you need to do start up ogre, while keeping control. So I would call is "Minimal Practical Application", possible with a few more things thrown in after you read the article a talked about above.
Lijat
Halfling
Posts: 51
Joined: Tue Aug 02, 2005 10:51 pm

Post by Lijat »

Posted to wiki here.
User avatar
jomunoz
Goblin
Posts: 228
Joined: Wed Apr 13, 2005 5:07 pm
Location: Medellin - Colombia

Post by jomunoz »

The printf function may not work in Win32, try putting a MessageBox when using Win32.
Lijat
Halfling
Posts: 51
Joined: Tue Aug 02, 2005 10:51 pm

Post by Lijat »

wouldn't that block the rest of the application until clicking ok? The reason the printf is there is to give the aplication something to do so that the loop does not finish imideatly so if no info is shown, no big loss.
Fredz
Greenskin
Posts: 122
Joined: Wed May 26, 2004 1:45 pm
Location: Perpignan, France

Post by Fredz »

what about a

Code: Select all

cout<<running<<endl;
instead of

Code: Select all

printf("%d\n",running);
?
Lijat
Halfling
Posts: 51
Joined: Tue Aug 02, 2005 10:51 pm

Post by Lijat »

Fredz wrote:what about a

Code: Select all

cout<<running<<endl;
instead of

Code: Select all

printf("%d\n",running);
?
What difference would that make?
Fredz
Greenskin
Posts: 122
Joined: Wed May 26, 2004 1:45 pm
Location: Perpignan, France

Post by Fredz »

No difference in the result, but as jomunoz said, the printf function may not work in Win32, and cout is the standard way to display text in C++.
User avatar
Kentamanos
Minaton
Posts: 980
Joined: Sat Aug 07, 2004 12:08 am
Location: Dallas, TX

Post by Kentamanos »

I would guess he's more concerned about there being a console at all. A straight Windows application won't have a console typically.
Lijat
Halfling
Posts: 51
Joined: Tue Aug 02, 2005 10:51 pm

Post by Lijat »

Fredz wrote:No difference in the result, but as jomunoz said, the printf function may not work in Win32, and cout is the standard way to display text in C++.
Is cout more reliable under win32???
Kentamanos wrote:I would guess he's more concerned about there being a console at all. A straight Windows application won't have a console typically.
Not much to do then. I actually thought about taking the text output out entirely before posting the code but decided to leave it so that the loop actually did something visible (as I did not populate the scene).
User avatar
jomunoz
Goblin
Posts: 228
Joined: Wed Apr 13, 2005 5:07 pm
Location: Medellin - Colombia

Post by jomunoz »

Kentamanos wrote:I would guess he's more concerned about there being a console at all. A straight Windows application won't have a console typically.

Yes that's what i meant. I didn't notice that the printf was inside the while. :oops:


You may want to use a log file for that.
User avatar
Kristian
Hobgoblin
Posts: 542
Joined: Sun Jan 12, 2003 7:35 pm
Location: Copenhagen, Denmark

Post by Kristian »

printf is just the ansi c equilent to cout. printf is part of the standard libery ans resides in 'cstdio' and it will run on any platform that has a full implementation of the standard libery.
Kristian, crying out loud: "If it works. Don't fix it!" :p
HCA
HCA2
GuppyLife
Edulix
Gnoblar
Posts: 1
Joined: Sun Oct 07, 2007 11:51 am

Post by Edulix »

That minimal practical application is great! I'm learning Ogre3D from scratch (i.e. a newbie) and it looks good. But I would try to fix some things:

* Is the INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT){ mandatory? I guess you can also a normal main() in windows for ogre.
* You should change printf to cout because we're using C++, and even if printf should probably work, cout is to printf what C++ is to C.
* I don't know much about ogre as I've already said, but I think that the loadPlugin() function is not very correct for Windows, or even for other distros. For example, I have ubuntu gutsy and /usr/local/lib/OGRE/RenderSystem_GL should be /usr//lib/OGRE/RenderSystem_GL in my case. Isn't there a more general way of loading plugins without specifying the path of the plugin? I will check this once I start the tutorial etc =)
User avatar
NickM
Bronze Sponsor
Bronze Sponsor
Posts: 65
Joined: Sat Jul 01, 2006 10:41 pm
Contact:

Post by NickM »

I think this would have helped me when I was first starting to learn, so well done for doing it. :)
Post Reply