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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- Created by texi2html 1.64 -->
<!--
Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author)
Karl Berry <karl@freefriends.org>
Olaf Bachmann <obachman@mathematik.uni-kl.de>
and many others.
Maintained by: Olaf Bachmann <obachman@mathematik.uni-kl.de>
Send bugs and suggestions to <texi2html@mathematik.uni-kl.de>
-->
<HTML>
<HEAD>
<TITLE>Crystal Space: Simple Creating Mesh</TITLE>
<META NAME="description" CONTENT="Crystal Space: Simple Creating Mesh">
<META NAME="keywords" CONTENT="Crystal Space: Simple Creating Mesh">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META NAME="Generator" CONTENT="texi2html 1.64">
</HEAD>
<BODY LANG="" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000">
<A NAME="SEC180"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_81.html#SEC179"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_83.html#SEC181"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_73.html#SEC171"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_79.html#SEC177"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_83.html#SEC181"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="index.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_285.html#SEC711">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<HR SIZE=1>
<H3> 5.3.3 Creating a Mesh from a Factory </H3>
<!--docid::SEC180::-->
<P>
First we need to add an additional include to the include section:
</P><P>
<TABLE><tr><td> </td><td class=example><pre>#include "imesh/sprite3d.h"
</pre></td></tr></table></P><P>
To create a mesh just add the following code in
<CODE>Simple::Initialize</CODE> (before <CODE>return true</CODE> and after
the code we just added to load the mesh factory):
</P><P>
<TABLE><tr><td> </td><td class=example><pre>bool Simple::Initialize (int argc, const char* const argv[])
{
<small>...</small>
// Add the sprite to the engine.
iMeshWrapper* sprite = engine->CreateMeshWrapper (
imeshfact, "MySprite", room,
csVector3 (-3, 5, 3));
csMatrix3 m; m.Identity (); m *= 5.;
sprite->GetMovable ()->SetTransform (m);
sprite->GetMovable ()->UpdateMove ();
iSprite3DState* spstate =
SCF_QUERY_INTERFACE (sprite->GetMeshObject (), iSprite3DState);
spstate->SetAction ("default");
imeshfact->DecRef ();
spstate->DecRef ();
sprite->SetZBufMode (CS_ZBUF_USE);
sprite->SetRenderPriority (engine->GetObjectRenderPriority ());
sprite->DeferUpdateLighting (CS_NLIGHT_STATIC|CS_NLIGHT_DYNAMIC, 10);
sprite->DecRef ();
return true;
}
</pre></td></tr></table></P><P>
The easiest way to create a mesh is to use <CODE>engine->CreateMeshWrapper()</CODE>.
This will take care of using the given mesh factory to create the mesh, give
it a name, and correctly place it at the given position in the world.
The name of a mesh can be useful for scripting so that a script can refer
to objects with their names.
</P><P>
Moving meshes (and things) is done through the <CODE>iMovable</CODE> interface to
which you can get a reference by doing <CODE>sprite->GetMovable()</CODE>. The calls
to <CODE>SetTransform()</CODE> and <CODE>SetPosition()</CODE> set up a transformation
matrix and vector to correctly place the mesh in the room. In this
particular case we use the identity matrix as a transform and scale it with
five to make the sprite five times bigger.
After doing movement (either updating the sectors or the position) you
<EM>must</EM> call <CODE>movable->UpdateMove()</CODE> to change internal data
structures (i.e. the engine may use this to recalculate visibility information).
</P><P>
If you read the mesh object documentation (see see section <A HREF="cs_201.html#SEC452">7.8 Mesh Object Plug-In System</A>)
carefully you will see that in order to control visual attributes
of mesh objects you will have to get the <EM>state</EM> interface for
either the object or the factory. In this example we query
<CODE>iSprite3DState</CODE> from the mesh in order to set the default action.
An action is a set of frames and is used to control animation. In our
simple example the sprite has only one frame so there is not much animation
to see.
</P><P>
Whenever you are done with some interface you obtained through
<CODE>SCF_QUERY_INTERFACE()</CODE> you should release the pointer with <CODE>DecRef()</CODE>.
</P><P>
The calls to <CODE>SetZBufMode()</CODE> and <CODE>SetRenderPriority()</CODE> are not
strictly required since the values set here are default for meshes anyway.
But the two lines serve as an example on how to set other values for these.
<CODE>SetZBufMode()</CODE> sets the Z-buffer mode that the engine will use to
render this object. With <CODE>CS_ZBUF_USE</CODE> this means that the Z-buffer
will be used fully. Other values are <CODE>CS_ZBUF_FILL</CODE> (only fill the
Z-buffer), <CODE>CS_ZBUF_TEST</CODE> (only test the Z-buffer), or <CODE>CS_ZBUF_NONE</CODE>
(ignore the Z-buffer). <CODE>CS_ZBUF_USE</CODE> is a good default for objects
in a room. <CODE>CS_ZBUF_FILL</CODE> is usually used for the outside walls of
a sector.
</P><P>
<CODE>SetRenderPriority()</CODE> controls when this object is rendered in this
sector. By default there are four render priorities: `<SAMP>sky</SAMP>', `<SAMP>wall</SAMP>',
`<SAMP>object</SAMP>', and `<SAMP>alpha</SAMP>'. Every object can be put in a render
priority queue. Objects in prior render priorities will be rendered before
objects in later render priorities. In addition some render priority queues
sort objects internally based on some other criterium. For example, the
`<SAMP>alpha</SAMP>' render priority will render all objects in that queue
from back to front. You can create your own additional render queues if
you want. The default render priority is `<SAMP>object</SAMP>' which you can
get with the call <CODE>engine->GetObjectRenderPriority()</CODE>. Most objects
with Z-buffer mode equal to <CODE>CS_ZBUF_USE</CODE> are placed in the
`<SAMP>object</SAMP>' render priority queue.
</P><P>
For more information on render priorities and Z-buffer mode you can look
at the HOWTO on this subject (see section <A HREF="cs_124.html#SEC260">5.8.8 Render Priorities and Objects in Sectors</A>).
</P><P>
The last call to <CODE>DeferUpdateLighting</CODE> is interesting. If you don't
call this then the sprite will be black. This call tells the engine
to update lighting on this sprite as soon as it is being rendered. You
need to call this function again when lights move or when the object
itself moves. Note that this function is not very expensive. It will only
recalculate lighting when the object is really visible.
</P><P>
This concludes the second Simple tutorial.
You can now go on to the next tutorial (see section <A HREF="cs_83.html#SEC181">5.4 Simple Tutorial 3: Map Loading</A>)) to learn
how you can load a level from a file instead of creating your rooms in
the code.
<A NAME="Tutorial Simple Map"></A>
<HR SIZE=1>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_81.html#SEC179"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_83.html#SEC181"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_73.html#SEC171"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_79.html#SEC177"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_83.html#SEC181"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="index.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_285.html#SEC711">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<BR>
<FONT SIZE="-1">
This document was generated
using <A HREF="http://www.mathematik.uni-kl.de/~obachman/Texi2html
"><I>texi2html</I></A>
</BODY>
</HTML>
|