Experiment 2: create basic game scenarios

Source: Internet
Author: User

Experiment 2: create basic game scenarios

I,Lab content:

1. Establish a simple game scenario;

2. Scene switching through the camera;

3. Use different light to implement the shadow effect;

II,Purpose:

1. Understand how the Ogre program works and the coordinate system of the Ogre scenario, and master the concept of the scenario manager, scenario nodes, and entities and their specific applications.

2. Master the concepts of cameras and views and their specific applications in game programming.

3. Learn how to use different types of light sources and how to generate shadows.

Iii. Experiment steps

(1), First try

The first step is to make a simple window,

Orge currently uses the plug-in mechanism in the Sample, which makes debugging inconvenient,

It is not conducive to understanding the real Orge process,

So I read some documents and started to do it directly,

1. First create a new project

Enter the code:

# Include <Ogre. h>

# Include <ExampleApplication. h>

# Include <windows. h>

# Include <windowsx. h>

Int WINAPI WinMain (_ in HINSTANCE hInstance, _ in_opt HINSTANCE hPrevInstance, _ in_opt LPSTR lpCmdLine, _ in int nShowCmd)

{

// Execute application initialization:

Root * g_ogreRoot = new Root ("plugins_d.cfg ");

// Display the Configuration window

Bool rtn = g_ogreRoot-> showConfigDialog ();

// If the configuration file already exists (showConfigDialog has been executed at least once ),

// You can use the following sentence to read the configuration without displaying the Configuration window.

G_ogreRoot-> restoreConfig ();

// Create a rendering window

G_ogreRoot-> initialise (true, "My Render Window"); // true indicates that the ogre Window is automatically created.

RenderWindow * renderWindow = g_ogreRoot-> getAutoCreatedWindow ();

RenderWindow-> setAutoUpdated (true );

// Create a scenario manager. This TerrainSceneManager refers to the terrain scenario manager.

SceneManager * mSceneMgr = g_ogreRoot-> createSceneManager ("TerrainSceneManager ");

// Create a camera

Camera * mCamera = mSceneMgr-> createCamera ("PlayerCam ");

MCamera-> setPosition (Vector3 (300,-); // camera position

MCamera-> lookAt (Vector3 (0, 0, 800); // camera orientation

// Set the rendering window's viewport (bound to our camera)

Viewport * vp = renderWindow-> addViewport (mCamera );

Vp-> setBackgroundColour (ColourValue (0, 0, 0 ));

// Define the read directory/file of the resource, and put dragon.zip in the execution directory. This file can be found in the OgreSDK \ media \ packs directory.

ResourceGroupManager: getSingleton (). addResourceLocation (

"Dragon.zip", "Zip", "General"

);

// I copied the script directory from OgreSDK \ media \ materials \ scripts to the execution directory. A script is required for dragon skin coloring.

ResourceGroupManager: getSingleton (). addResourceLocation (

"Scripts", "FileSystem", "General"

);

// Initialize the Resource Manager

ResourceGroupManager: getSingleton (). initialiseAllResourceGroups ();

// Read the Dragon Model

Entity * ent = mSceneMgr-> createEntity ("dragon", "dragon. mesh ");

// Put the model into the scenario Manager

MSceneMgr-> getRootSceneNode ()-> createChildSceneNode ()-> attachObject (ent );

// Start Rendering

G_ogreRoot-> startRendering ();

Return 0;

}

The code is not hard to understand. It is very easy to establish a complete DX manually,

Continue. Define a plugins_d.cfg file as the Orge Initialization Configuration.

# Defines plugins to load

# Define plugin folder

PluginFolder =.

# Define plugins

Plugin = RenderSystem_Direct3D9_d

# Plugin = RenderSystem_Direct3D10_d

# Plugin = RenderSystem_Direct3D11_d

Plugin = RenderSystem_GL_d

# Plugin = RenderSystem_GLES_d

Plugin = plugin_participant fx_d

Plugin = Plugin_BSPSceneManager_d

Plugin = Plugin_CgProgramManager_d

Plugin = Plugin_PCZSceneManager_d

Plugin = Plugin_OctreeZone_d

Plugin = Plugin_OctreeSceneManager_d

The configuration file defines the relevant plug-in configuration, which is xp and cannot run DX10/11,

It seems that CMake has made a good decision, and the RenderSystem of DX10 cannot be found...

2. configuration items

Coding is far from enough. It is critical to add all the accessories of Orge,

DXSDK has already been configured. Here we will add Orge one by one:

1.Include files

\ Ogre-v1-7-0 \ Dependencies \ include

\ Ogre-v1-7-0 \ Samples \ Common \ include

Ogre-v1-7-0 \ OgreMain \ include

Ogre-v1-7-0 \ include

2.Library files

\ Ogre-v1-7-0 \ Dependencies \ lib \ Release

\ Ogre-v1-7-0 \ Dependencies \ lib \ Debug

\ Ogre-v1-7-0 \ lib \ Release

\ Ogre-v1-7-0 \ lib \ Debug

These are indeed the top priorities. After careful check, F7, F5

3. debugging

The compilation was very smooth. Compared with OrgeSDK, this was a return to modern civilization from the Stone Age,

It's so easy to get beautiful things,

Error reported,

There is no way, so we can't hide the water,

Pick up the debugging tool,

After several traces, we found that;

It should be a problem with the dynamic library.

Finally found: RenderSystem_Direct3D9_d.dll !!

But at the moment, I still cannot understand Orge's complex read mechanism, so I had to crack it directly,

Find the DLL under Orge

Copy directly to the system directory

Solve the problem:

4. Debug II

But it's still too early to celebrate the victory now. The mistake is coming again,

Haha, it's too big. Media forgot to copy it,

F5, everything is done:

(2). These are not enough. I want to get started.

1, Architecture

The current framework is still very immature,

After visiting the Orge official website, I decided to use the Ogre Wiki Tutorial Framework for the following work.

In an episode, After decompressing the compressed package, it turned out to be a file without a suffix. I guess it was still a compressed package, and I added a rar to it.

A new window is coming ~~~

2. Main Loop

Read

The general structure,

Rewrite one

Void TutorialApplication: createScene (void)

{

MCamera-> setPosition (Vector3 (300,-); // camera position

MCamera-> lookAt (Vector3 (0, 0, 800); // camera orientation

// Define the read directory/file of the resource, and put dragon.zip in the execution directory. This file can be found in the OgreSDK \ media \ packs directory.

ResourceGroupManager: getSingleton (). addResourceLocation (

"Dragon.zip", "Zip", "General"

);

// I copied the script directory from OgreSDK \ media \ materials \ scripts to the execution directory. A script is required for dragon skin coloring.

ResourceGroupManager: getSingleton (). addResourceLocation (

"Scripts", "FileSystem", "General"

);

// Initialize the Resource Manager

ResourceGroupManager: getSingleton (). initialiseAllResourceGroups ();

// Read the Dragon Model

Entity * ent = mSceneMgr-> createEntity ("dragon", "dragon. mesh ");

// Put the model into the scenario Manager

MSceneMgr-> getRootSceneNode ()-> createChildSceneNode ()-> attachObject (ent );

// Start Rendering

SceneNode * node = mSceneMgr-> getRootSceneNode ();

Float scal = 1.0f;

}

The results were very smooth and the entire 3D world was at your fingertips:

(Input control is encapsulated in the framework, and camera operations can be performed directly)

3. Let the Flying Dragon go up:

Join the code to gain more control

:

If (arg. key = OIS: KC_ B)

{

M_pNode-> showBoundingBox (! M_pNode-> getShowBoundingBox ());

}

If (arg. key = OIS: KC_Z)

{

M_fScale = 1.1f;

M_pNode-> scale (m_fScale, m_fScale, m_fScale );

}

Else if (arg. key = OIS: KC_X)

{

M_fScale = 0.9f;

M_pNode-> scale (m_fScale, m_fScale, m_fScale );

}

If (arg. key = OIS: KC_W)

{

M_pNode-> pitch (Radian (0.1f ));

}

Else if (arg. key = OIS: KC_S)

{

M_pNode-> pitch (Radian (-0.1f ));

}

If (arg. key = OIS: KC_A)

{

M_pNode-> yaw (Radian (0.1f ));

}

Else if (arg. key = OIS: KC_D)

{

M_pNode-> yaw (Radian (-0.1f ));

}

4. Details

At the beginning, scaling can only be scaled down or kept,

After reading the source code, we found that the scal parameter under Orge is passed in using *,

After understanding, pass in 1.1/0.9 to solve the problem.

Next, there will always be problems with rotation,

It is found that the shortcut key conflicts with the shortcut key of the existing Camera,

After the correction, another problem is that one frame is separated, that is, it is necessary to keep pressing. This is very depressing,

Write a state to record the key position...

4. Continue to improve

A KeyState is added to record the key position information,

Add Update as a response:

Response to key position changes:

The overall operation is now very smooth:

(3) God said, to have light

The basic operations have been implemented, so add the effect of light and shade:

When a new project is created, the base class is directly rewritten, And the architecture written in the previous step is too arrogant,

The others are basically the same. Change createScene, load the model, and add the light.

Void basictutorial: createScene (void)

{

MSceneMgr-> setAmbientLight (Ogre: ColourValue (0, 0, 0 ));

MSceneMgr-> setShadowTechnique (Ogre: SHADOWTYPE_STENCIL_ADDITIVE );

Ogre: Entity * entNinja = mSceneMgr-> createEntity ("Ninja", "ninja. mesh ");

EntNinja-> setCastShadows (true );

// MSceneMgr-> getRootSceneNode ()-> createChildSceneNode ()-> attachObject (entNinja );

M_pNode-> attachObject (entNinja );

Ogre: Plane plane (Ogre: Vector3: UNIT_Y, 0 );

Ogre: MeshManager: getSingleton (). createPlane ("ground", Ogre: ResourceGroupManager: DEFAULT_RESOURCE_GROUP_NAME,

Plane, 1500,150 0, 20, 20, true, 1, 5, 5, Ogre: Vector3: UNIT_Z );

Ogre: Entity * entGround = mSceneMgr-> createEntity ("GroundEntity", "ground ");

MSceneMgr-> getRootSceneNode ()-> createChildSceneNode ()-> attachObject (entGround );

EntGround-> setMaterialName ("Examples/Rockwall ");

EntGround-> setCastShadows (false );

Ogre: Light * pointLight = mSceneMgr-> createLight ("pointLight ");

PointLight-> setType (Ogre: Light: LT_POINT );

PointLight-> setPosition (Ogre: Vector3 (0,150,250 ));

PointLight-> setDiffuseColour (1.0, 0.0, 0.0 );

PointLight-> setSpecularColour (1.0, 0.0, 0.0 );

Ogre: Light * directionalLight = mSceneMgr-> createLight ("directionalLight ");

DirectionalLight-> setType (Ogre: Light: LT_DIRECTIONAL );

DirectionalLight-> setDiffuseColour (Ogre: ColourValue (. 25,. 25, 0 ));

DirectionalLight-> setSpecularColour (Ogre: ColourValue (. 25,. 25, 0 ));

DirectionalLight-> setDirection (Ogre: Vector3 (0,-1, 1 ));

Ogre: Light * spotLight = mSceneMgr-> createLight ("spotLight ");

SpotLight-> setType (Ogre: Light: LT_SPOTLIGHT );

SpotLight-> setDiffuseColour (0, 0, 1.0 );

SpotLight-> setSpecularColour (0, 0, 1.0 );

SpotLight-> setDirection (-1,-1, 0 );

SpotLight-> setPosition (Ogre: Vector3 (300,300, 0 ));

SpotLight-> setSpotlightRange (Ogre: Degree (35), Ogre: Degree (50 ));

}

Debug it, as long as the SDK is properly configured, everything is fine,

I have to say that Ogre is really too convenient. I want to learn about ShadowVolume and ShadowMap, but I don't know how many brain cells are dead and how many twists and turns have been spent. Now it's better, So easy.

(4), Bone Animation

Iv. experiment data and processing results

The results are all given, the code is included with the project...

5. Think about the question or experience or suggestions for improving the experiment

Code Ogre is really very simple, especially after a certain foundation, it is really a pleasure to see his code. Well, keep up on it and continue exploring it!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.