raSystem  1.0 bata
raTree.cpp
Go to the documentation of this file.
1 #include "..\include\raMain.h"
2 
3 namespace System
4 {
5  raTree::raTree(raSmartPointer<raDirectX> dx, LPCSTR barkTextureFile, LPCSTR leafTextureFile, int height,
6  int nSlices, float r0, raMatrix *pTransform) : raEntity(dx, new raTexturedMaterial(dx.get(),
7  "raEffects\\MyEngine.fx", "RenderScene",
8  barkTextureFile)),
9  m_ParticleSystem(dx, new raTexturedMaterial(dx.get(),
10  "raEffects\\ParticleEffect.fx", "RenderParticle",
11  leafTextureFile), false, 2.0f, 0, 0)
12  {
13  m_nLevels = 4;
14  m_nChildren = 4;
15  m_rRatio = raGetRandFloat(0.25f, 0.75f);
16  m_lRatio = raGetRandFloat(0.5f, 0.75f);
17 
18  m_r0 = r0;
19  m_l0 = 10.0f;
20 
21  m_nHeightSegs = height;
22  m_nSlices = nSlices;
23 
24  m_nMaxBranches = (int) pow((float)m_nChildren, m_nLevels + 1) - 1;
25  m_nBranches = 1;
26 
27  m_nVertices = m_nMaxBranches * m_nSlices * m_nHeightSegs;
28  m_nIndices = m_nMaxBranches * 6 * m_nSlices * (m_nHeightSegs - 1);// nicht m_nSlices - 1, weil die Naht geschlossen wird
29 
30  m_pTransform = pTransform;
31  }
32  raTree::raTree(raSmartPointer<raDirectX> dx, raMaterial* barkMaterial, LPCSTR leafTexturefilename, int height, int nSlices,
33  float r0, raMatrix *pTransform) : raEntity(dx, barkMaterial),
34  m_ParticleSystem(dx, new raTexturedMaterial(dx,
35  "raEffects\\ParticleEffect.fx", "RenderParticle", leafTexturefilename), false, 2.0f, 0, 0)
36  {
37  m_nLevels = 4;
38  m_nChildren = 4;
39  m_rRatio = raGetRandFloat(0.25f, 0.75f);
40  m_lRatio = raGetRandFloat(0.5f, 0.75f);
41 
42  m_r0 = r0;
43  m_l0 = 10.0f;
44 
45  m_nHeightSegs = height;
46  m_nSlices = nSlices;
47 
48  m_nMaxBranches = (int) pow((float)m_nChildren, m_nLevels + 1) - 1;
49  m_nBranches = 1;
50 
51  m_nVertices = m_nMaxBranches * m_nSlices * m_nHeightSegs;
52  m_nIndices = m_nMaxBranches * 6 * m_nSlices * (m_nHeightSegs - 1);// nicht m_nSlices - 1, weil die Naht geschlossen wird
53 
54  m_pTransform = pTransform;
55  }
57  {
58  if(!raEntity::Create())
59  {
60  RERROR("Bei raEntity Create");
61  return false;
62  }
63  if(!m_ParticleSystem.Create())
64  {
65  RERROR("Beim erstellen des ParticleSystems");
66  return false;
67  }
68 
69  ROK("Baum Erstellt");
70  return true;
71  }
73  {
75  m_ParticleSystem.Destroy();
76  }
77 
79  {
81 
82  m_nVertices = 0;
83 
84  if(m_pTransform)
85  {
87  m_nVertices, m_pTransform, m_nLevels);
88  }
89  else
90  {
91  raMatrix identity;
92  identity = raMatrixIdentity();
94  m_nVertices, &identity, m_nLevels);
95  }
96  }
97 
98  int raTree::AddBranchVerts(VERTEXPOSITIONNORMALTEXTURED* vertices, int n, raMatrix* pParentMatrix, int level)
99  {
100  if(level == 0)
101  {
102  m_r1 = 0; //Ende des letzten Asts schließen
103 
104  raVector3 position(0, m_l0 * (m_nHeightSegs-1), 0);
105  raVector4 t;
106  D3DXVec3Transform((D3DXVECTOR4*)&t, (D3DXVECTOR3*)&position, (D3DXMATRIX*)pParentMatrix);
107  m_ParticleSystem.AddParticle(t.x, t.y, t.z);
108  }
109  else
110  m_r1 = m_rRatio * m_r0;
111 
112  for(int y = 0; y < m_nHeightSegs; y++)
113  {
114  for(int phi = 0; phi < m_nSlices; phi++)
115  {
116  float r0 = r(y);
117  float phi0 = 2 * PI * phi / (m_nSlices-1);
118 
119  raVector3 position(
120  (float) r0 * sin(phi0),
121  m_l0 * y,
122  (float) r0 * cos(phi0));
123 
124  raVector3 normal(
125  (float)position.x,
126  0.0f,
127  (float)position.z);
128 
129  D3DXVec3TransformCoord((D3DXVECTOR3*)&vertices[n].position, (D3DXVECTOR3*)&position,
130  (D3DXMATRIX*)pParentMatrix);
131 
132  D3DXVec3TransformNormal((D3DXVECTOR3*)&vertices[n].normal, (D3DXVECTOR3*)&normal,
133  (D3DXMATRIX*)pParentMatrix);
134 
135  vertices[n].texcoord.x = phi / (m_nSlices - 1.0f);
136  vertices[n].texcoord.y = y / (m_nHeightSegs - 1.0f);
137 
138  n+=1;
139  }
140  }
141 
142  if(level > 0)
143  {
144  raMatrix mtrans;
145  D3DXMatrixTranslation((D3DXMATRIX*)&mtrans, 0, m_l0 * (m_nHeightSegs-1), 0);
146 
147  int nChildren = raGetRandInt(3, m_nChildren);
148 
149  float pitch = PI / 4.0f;
150  float yaw = 2.0f * PI / nChildren;
151 
152  raMatrix mrot1, mrot2;
153  mrot1 = raMatrixRotationZ(pitch);
154 
155  raMatrix m0, m1, m2, m3, m4;
156  D3DXMatrixScaling((D3DXMATRIX*)&m0, m_rRatio, m_lRatio, m_rRatio);
157 
158  for(int child = 0; child < nChildren; child++)
159  {
160  mrot2 = raMatrixRotationY(child * yaw);
161 
162  m1 = m0 * mrot1;
163  m2 = m1 * mrot2;
164  m3 = m2 * mtrans;
165  m4 = m3 * (*pParentMatrix);
166 
167  n = AddBranchVerts(vertices, n, &m4, level - 1);
168  }
169  m_nBranches += nChildren;
170  }
171  return n;
172  }
173 
175  {
176  m_pIndices = new UINT32[m_nIndices];
177 
178  m_nIndices = 0;
179  for(int branch= 0; branch < m_nBranches; branch++)
180  {
181  int offs = branch * m_nSlices * m_nHeightSegs;
182  for(int y = 0; y < m_nHeightSegs - 1; y++)
183  {
184  //Naht schliessen - daher nicht bis 360 - 360 / m_nSlices, sondern bis 360
185  for(int phi = 0; phi < m_nSlices; phi++)
186  {
187  m_pIndices[m_nIndices + 0] = offs + y * m_nSlices + phi;
188  m_pIndices[m_nIndices + 1] = offs + y * m_nSlices +
189  (phi + 1) % m_nSlices;
190  m_pIndices[m_nIndices + 2] = offs + (y + 1) * m_nSlices + phi;
191  m_pIndices[m_nIndices + 3] = offs + (y + 1) * m_nSlices + phi;
192  m_pIndices[m_nIndices + 4] = offs + y * m_nSlices +
193  (phi + 1) % m_nSlices;
194  m_pIndices[m_nIndices + 5] = offs + (y + 1) * m_nSlices +
195  (phi + 1) % m_nSlices; ;
196 
197  m_nIndices+=6;
198  }
199  }
200  }
201  m_pSubsets[0].IndexStart = 0;
203  }
204 
205  void raTree::SetupEffectVariables(const raMatrix& pView, const raMatrix& pProj,
206  const raLight* pLight)
207  {
208  raEntity::SetupEffectVariables(pView, pProj, pLight);
209  m_ParticleSystem.SetupEffectVariables(pView, pProj, pLight);
210  }
211 
212  bool raTree::Render(UINT drawOrder, const raMatrix &pView, const raMatrix &pProj,
213  const raLight* pLight, LPCSTR techniqueName)
214  {
215  raEntity::Render(drawOrder,
216  pView, pProj, pLight, techniqueName);
217  m_ParticleSystem.Render(drawOrder,
218  pView, pProj, pLight, techniqueName);
219 
220  return true;
221  }
222 };
Definition: raLight.h:5
virtual void Destroy()
Definition: raVisual.cpp:100
raFloat x
Definition: raVector3.h:12
raTree(raSmartPointer< raDirectX > dx, LPCSTR barkTextureFile, LPCSTR leafTextureFile, int height, int nSlices=8, float r0=1.0f, raMatrix *pTransform=NULL)
Definition: raTree.cpp:5
virtual void Destroy()
Definition: raTree.cpp:72
raMatrix raMatrixIdentity()
Definition: raMatrix.h:211
raFloat x
Definition: raVector2.h:10
virtual bool Create()
Definition: raVisual.cpp:45
virtual void SetupEffectVariables(const raMatrix &pView, const raMatrix &pProj, const raLight *pLight)
Definition: raVisual.cpp:117
VERTEXPOSITIONNORMALTEXTURED * m_pVertices
Definition: raEntity.h:39
UINT64 IndexCount
Definition: raSDKmesh.h:142
virtual bool Create()
Definition: raTree.cpp:56
raFloat z
Definition: raVector3.h:14
virtual void SetupVertices()
Definition: raTree.cpp:78
virtual bool Render(UINT drawOrder, const raMatrix &pView, const raMatrix &pProj, const raLight *pLight, LPCSTR techniqueName="")
Definition: raTree.cpp:212
void AddParticle(float x=0, float y=0, float z=0, float vx=0, float vy=0, float vz=0)
RAPI raMatrix raMatrixRotationY(const float f)
Definition: raMatrix.cpp:28
SDKMESH_SUBSET * m_pSubsets
Definition: raVisual.h:121
RAPI raMatrix raMatrixRotationZ(const float f)
Definition: raMatrix.cpp:43
UINT64 IndexStart
Definition: raSDKmesh.h:141
virtual void SetupIndices()
Definition: raTree.cpp:174
raFloat y
Definition: raVector2.h:11
virtual bool Render(UINT drawOrder, const raMatrix &pView, const raMatrix &pProj, const raLight *pLight, LPCSTR techniqueName="")
Definition: raVisual.cpp:142
void RAPI ROK(raString x)
Definition: raMain.cpp:114
RAPI int raGetRandInt(int min, int max)
Definition: raUtility.cpp:190
#define PI
Definition: raMain.h:129
virtual void SetupEffectVariables(const raMatrix &pView, const raMatrix &pProj, const raLight *pLight)
Definition: raTree.cpp:205
RAPI float raGetRandFloat(float min, float max)
Definition: raUtility.cpp:195
void RAPI RERROR(raString x)
Definition: raMain.cpp:129