History: MadMarx Tutorial 3
Source of version: 2
Copy to clipboard
!!Tutorial Description
In this program, I present one way to create a 3D object from scratch.
This is useful if you intend to load your own 3D data format without difficulties (shapefile, ...),
or to create some procedural stuff.
!!A Simplistic presentation of 3D
A simplistic presentation of some realtime 3D basics.
In 3D you need most of the time :
a vertex table Vt + an index table Idt + a kind of primitive P + an associated material M.
Let's say P is "triangle" (most common), then Idt will contain
a list of index taken from Vt, where each group of 3 index means a triangle.
so (Idt[0], Idt[1],Idt[2]) is the first triangle, (Idt[3],Idt[4],Idt[5]) is the second, etc...
When times come to render this whole thing,
it will be multiplied by 2 matrix.
1/ a matrix representing transformation(position/orientation/scale) of the thing relative to the camera.
2/ a perspective matrix, corresponding to the parameters of the viewing camera.
Ogre will prepare both matrix for you. But you can also provide them if you want.
This calculation can be modified in vertex shaders.
Once it has been projected, the graphic card uses the buffers (mainly colour and depth)
, vertex attributes and material informations (from M) to draw things.
vertex attributes are used to 'light', 'colourize', and 'texture' the meshes.
This calculation can be partially modified in pixel shaders.
This was a very simplistic presentation of 3D rendering ^^.
!!New Include
{CODE(wrap="0",colors="c++")}
¤#include "OGRE/OgreManualObject.h"
{CODE}
!!After viewport creation
Here, I create a 3D element, by using the interface of ManualObject.
ManualObject is very close to the opengl old simple way to specify geometry.
There are other interfaces (Hardwarebuffers), you can check the ogremanual fo them and wiki.
For each vertex I will provide positions and attributes (normal, vertex color, texture coordinates...).
Then for each primitive (given its type : triangle, line, line strip etc...),
I give the corresponding group of vertex index.
{CODE(wrap="0",colors="c++")}
¤ Ogre::ManualObject* lManualObject = NULL;
{
{CODE}
The manualObject creation requires a name.
{CODE(wrap="0",colors="c++")}
¤ Ogre::String lManualObjectName = "SomeQuad";
lManualObject = lScene->createManualObject(lManualObjectName);
{CODE}
Always tell if you want to update the 3D (vertex/index) later or not.
{CODE(wrap="0",colors="c++")}
¤ bool lDoIWantToUpdateItLater = false;
lManualObject->setDynamic(lDoIWantToUpdateItLater);
{CODE}
BaseWhiteNoLighting is the name of a material that already exist inside Ogre.
Ogre::RenderOperation::OT_TRIANGLE_LIST is a kind of primitive.
{CODE(wrap="0",colors="c++")}
¤ lManualObject->begin("BaseWhiteNoLighting", Ogre::RenderOperation::OT_TRIANGLE_LIST);
{
{CODE}
a vertex
{CODE(wrap="0",colors="c++")}
¤ lManualObject->position(0.0,0.0,0.0);
{CODE}
its attributes. Here I only set 1 colour.
{CODE(wrap="0",colors="c++")}
¤ lManualObject->colour(1.0,0.0,0.0);
{CODE}
a vertex
{CODE(wrap="0",colors="c++")}
¤ lManualObject->position(0.0,1.0,0.0);
lManualObject->colour(0.0,1.0,0.0);
{CODE}
a vertex
{CODE(wrap="0",colors="c++")}
¤ lManualObject->position(1.0,1.0,0.0);
lManualObject->colour(0.0,0.0,1.0);
{CODE}
a vertex
{CODE(wrap="0",colors="c++")}
¤ lManualObject->position(1.0,0.0,0.0);
lManualObject->colour(1.0,1.0,1.0);
{CODE}
a group of 3 vertex
{CODE(wrap="0",colors="c++")}
¤ lManualObject->triangle(3,2,1);
{CODE}
a group of 3 vertex
{CODE(wrap="0",colors="c++")}
¤ lManualObject->triangle(1,0,3);
{CODE}
I could have used quad(3,2,1,0) instead of 2 triangles.
{CODE(wrap="0",colors="c++")}
¤ }
lManualObject->end();
{CODE}
Here I have finished my ManualObject construction.
It is possible to add more begin()-end() thing, in order to use
different rendering operation types, or different materials in 1 ManualObject.
{CODE(wrap="0",colors="c++")}
¤ }
{CODE}
Now I attach it to a scenenode, so that it becomes present in the scene.
{CODE(wrap="0",colors="c++")}
¤ Ogre::SceneNode* lNode = lRootSceneNode->createChildSceneNode();
lNode->attachObject(lManualObject);
{CODE}
I move the SceneNode so that it is visible to the camera.
{CODE(wrap="0",colors="c++")}
¤ lNode->translate(0.0,0.0,-10.0);
{CODE}