1 #include "dxut\dxstdafx.h"
2 #include "tokamaksampleApp.h"
3 
4 IDirect3DDevice9 * g_pD3dDevice;
5 
6 ID3DXEffect*            g_pEffect = NULL;       // D3DX effect interface
7 CDXUTMesh				g_Cell;                 // Cell mesh object
8 D3DXMATRIXA16           g_mCellWorld;           // World matrix for cell mesh
9 D3DXHANDLE              g_hMatW;                // Handles to transformation matrices in effect
10 D3DXHANDLE              g_hMatV;
11 D3DXHANDLE              g_hMatP;
12 D3DXHANDLE              g_hMatWV;
13 D3DXHANDLE              g_hMatWVP;
14 D3DXHANDLE              g_hDiffuse;             // Handles to material parameters in effect
15 D3DXHANDLE              g_hSpecular;
16 D3DXHANDLE              g_hTex;
17 D3DXHANDLE              g_hPower;
18 //IDirect3DTexture9*      g_pDefaultTex;          // Default texture for un-textured geometry.
19 D3DXHANDLE              g_hShaderTech;          // Technique to use when using programmable rendering path
20 
21 D3DXHANDLE				g_hAmbient;
22 D3DXHANDLE				g_hLightTokamak[NUM_LIGHT];
23 D3DXHANDLE				g_hLightColorTokamak[NUM_LIGHT];
24 D3DXHANDLE				g_hViewPos;
25 
26 extern ID3DXFont*              g_pFont;         // Font for drawing text
27 extern ID3DXSprite*			g_pTextSprite;   // Sprite for batching draw text calls
28 
OnMyAppCreateDevice(IDirect3DDevice9 * pd3dDevice,const D3DSURFACE_DESC * pBackBufferSurfaceDesc,void * pUserContext)29 HRESULT CALLBACK OnMyAppCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
30 {
31 	HRESULT hr;
32 
33     // Read the D3DX effect file
34     WCHAR str[MAX_PATH];
35     V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"Main.fx" ) );
36 
37     // Define DEBUG_VS and/or DEBUG_PS to debug vertex and/or pixel shaders with the
38     // shader debugger. Debugging vertex shaders requires either REF or software vertex
39     // processing, and debugging pixel shaders requires REF.  The
40     // D3DXSHADER_FORCE_*_SOFTWARE_NOOPT flag improves the debug experience in the
41     // shader debugger.  It enables source level debugging, prevents instruction
42     // reordering, prevents dead code elimination, and forces the compiler to compile
43     // against the next higher available software target, which ensures that the
44     // unoptimized shaders do not exceed the shader model limitations.  Setting these
45     // flags will cause slower rendering since the shaders will be unoptimized and
46     // forced into software.  See the DirectX documentation for more information about
47     // using the shader debugger.
48     DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE;
49 
50     #if defined( DEBUG ) || defined( _DEBUG )
51     // Set the D3DXSHADER_DEBUG flag to embed debug information in the shaders.
52     // Setting this flag improves the shader debugging experience, but still allows
53     // the shaders to be optimized and to run exactly the way they will run in
54     // the release configuration of this program.
55     dwShaderFlags |= D3DXSHADER_DEBUG;
56     #endif
57 
58     #ifdef DEBUG_VS
59         dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT;
60     #endif
61     #ifdef DEBUG_PS
62         dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT;
63     #endif
64 
65     // If this fails, there should be debug output as to
66     // they the .fx file failed to compile
67     V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, dwShaderFlags,
68                                         NULL, &g_pEffect, NULL ) );
69 
70     // Obtain handles
71     g_hMatW = g_pEffect->GetParameterByName( NULL, "g_mWorld" );
72     g_hMatV = g_pEffect->GetParameterByName( NULL, "g_mView" );
73     g_hMatP = g_pEffect->GetParameterByName( NULL, "g_mProj" );
74     g_hMatWV = g_pEffect->GetParameterByName( NULL, "g_mWorldView" );
75     g_hMatWVP = g_pEffect->GetParameterByName( NULL, "g_mWorldViewProj" );
76     g_hDiffuse = g_pEffect->GetParameterByName( NULL, "g_matDiffuse" );
77     g_hSpecular = g_pEffect->GetParameterByName( NULL, "g_matSpecular" );
78     g_hTex = g_pEffect->GetParameterByName( NULL, "g_txScene" );
79     g_hPower = g_pEffect->GetParameterByName( NULL, "g_matPower" );
80 
81 	g_hLightTokamak[0] = g_pEffect->GetParameterByName(NULL, "g_LightTokamak0"); // light is object space
82 	g_hLightTokamak[1] = g_pEffect->GetParameterByName(NULL, "g_LightTokamak1"); // light is object space
83 
84 	g_hLightColorTokamak[0] = g_pEffect->GetParameterByName(NULL, "g_LightColorTokamak0");
85 	g_hLightColorTokamak[1] = g_pEffect->GetParameterByName(NULL, "g_LightColorTokamak1");
86 
87 	g_hAmbient = g_pEffect->GetParameterByName(NULL, "g_Ambient");
88 	g_hViewPos = g_pEffect->GetParameterByName(NULL, "g_ViewPos");
89 
90     // Create mesh
91     WCHAR wsz[MAX_PATH];
92 //    V_RETURN( DXUTFindDXSDKMediaFileCch( wsz, MAX_PATH, L"misc\\cell.x" ) );
93 //	V_RETURN( DXUTFindDXSDKMediaFileCch( wsz, MAX_PATH, L"cell.x" ) );
94 //    g_Cell.Create( pd3dDevice, wsz );
95     // Tessellate the mesh for better per-vertex lighting result.
96 //    ID3DXMesh *pTessMesh;
97 //    D3DXTessellateNPatches( g_Cell.m_pMesh, NULL, 8, false, &pTessMesh, NULL );
98 //    g_Cell.m_pMesh->Release();
99 //    g_Cell.m_pMesh = pTessMesh;
100 
101     // Create the 1x1 white default texture
102 //    V_RETURN( pd3dDevice->CreateTexture( 1, 1, 1, 0, D3DFMT_A8R8G8B8,
103 //                                         D3DPOOL_MANAGED, &g_pDefaultTex, NULL ) );
104 //    D3DLOCKED_RECT lr;
105 //    V_RETURN( g_pDefaultTex->LockRect( 0, &lr, NULL, 0 ) );
106 //    *(LPDWORD)lr.pBits = D3DCOLOR_RGBA( 255, 255, 255, 255 );
107 //    V_RETURN( g_pDefaultTex->UnlockRect( 0 ) );
108 
109 	//g_pEffect->FindNextValidTechnique( NULL, &g_hShaderTech );
110 	g_hShaderTech = g_pEffect->GetTechniqueByName("RenderScenePS20");
111 
112 	g_pEffect->SetTechnique( g_hShaderTech );
113 
114 	g_pD3dDevice = pd3dDevice;
115 
116 	g_hShaderTechTokamak = g_pEffect->GetTechniqueByName("RenderSceneTokamak");
117 
118 	return D3D_OK;
119 }
120 
OnMyAppLostDevice(void * pUserContext)121 void CALLBACK OnMyAppLostDevice( void* pUserContext )
122 {
123     if( g_pEffect )
124         g_pEffect->OnLostDevice();
125 }
126 
OnMyAppResetDevice(IDirect3DDevice9 * pd3dDevice,const D3DSURFACE_DESC * pBackBufferSurfaceDesc,void * pUserContext)127 HRESULT CALLBACK OnMyAppResetDevice( IDirect3DDevice9* pd3dDevice,
128                             const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
129 {
130 	HRESULT hr;
131 
132     if( g_pEffect )
133         V_RETURN( g_pEffect->OnResetDevice() );
134 
135 	return D3D_OK;
136 }
137 
MyRenderText(WCHAR ** StrList,int NumStr)138 void MyRenderText(WCHAR ** StrList, int NumStr)
139 {
140 //    const D3DSURFACE_DESC* pd3dsdBackBuffer = DXUTGetBackBufferSurfaceDesc();
141     CDXUTTextHelper txtHelper( g_pFont, g_pTextSprite, 15 );
142 
143     // Output statistics
144     txtHelper.Begin();
145     txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
146 
147 	for (int i = 0; i < NumStr; i++)
148 	{
149 		txtHelper.SetInsertionPos( 5, 5 + i * 15);
150 		txtHelper.DrawTextLine( *(StrList++) );
151 	}
152 
153 	txtHelper.End();
154 }
155 
CRenderPrimitive()156 CRenderPrimitive::CRenderPrimitive()
157 {
158 	mIsCylinder = 0;
159 
160 	memset(&mMaterial, 0, sizeof(mMaterial));
161 
162 	mMaterial.MatD3D.Diffuse.r = 0.7f;
163 	mMaterial.MatD3D.Diffuse.g = 0.7f;
164 	mMaterial.MatD3D.Diffuse.b = 0.7f;
165 	mMaterial.MatD3D.Diffuse.a = 1.0f;
166 
167 	mMaterial.MatD3D.Ambient.r = 0.5f;
168 	mMaterial.MatD3D.Ambient.g = 0.5f;
169 	mMaterial.MatD3D.Ambient.b = 0.5f;
170 	mMaterial.MatD3D.Ambient.a = 1.0f;
171 
172 	mMaterial.MatD3D.Specular.r = 1.0f;
173 	mMaterial.MatD3D.Specular.g = 1.0f;
174 	mMaterial.MatD3D.Specular.b = 1.0f;
175 	mMaterial.MatD3D.Specular.a = 1.0f;
176 
177 	mMaterial.MatD3D.Power = 40;
178 }
179 
SetDiffuseColor(const D3DXCOLOR & c)180 void CRenderPrimitive::SetDiffuseColor(const D3DXCOLOR &c)
181 {
182 	mMaterial.MatD3D.Diffuse.r = c.r;
183 	mMaterial.MatD3D.Diffuse.g = c.g;
184 	mMaterial.MatD3D.Diffuse.b = c.b;
185 	mMaterial.MatD3D.Diffuse.a = c.a;
186 }
187 
SetGraphicBox(float Width,float Height,float Depth)188 void CRenderPrimitive::SetGraphicBox(float Width, float Height, float Depth)
189 {
190 	//create the render mesh
191 	ID3DXMesh *pMesh;
192 
193 	D3DXCreateBox( g_pD3dDevice,
194 		Width, Height, Depth,
195 		&pMesh, NULL);
196 
197 	mMesh.Create(g_pD3dDevice, pMesh, &mMaterial, 1);
198 
199 	pMesh->Release();
200 }
201 
SetGraphicSphere(float radius)202 void CRenderPrimitive::SetGraphicSphere(float radius)
203 {
204 	ID3DXMesh *pMesh;
205 
206 	D3DXCreateSphere( g_pD3dDevice, radius,
207 		8, 8, &pMesh, NULL);
208 
209 	mMesh.Create(g_pD3dDevice, pMesh, &mMaterial, 1);
210 
211 	pMesh->Release();
212 }
213 
SetGraphicCylinder(float radius,float length)214 void CRenderPrimitive::SetGraphicCylinder(float radius, float length)
215 {
216 	ID3DXMesh *pMesh;
217 
218 	D3DXCreateCylinder( g_pD3dDevice, radius, radius, length,
219 		8, 1, &pMesh, NULL);
220 
221 	mMesh.Create(g_pD3dDevice, pMesh, &mMaterial, 1);
222 
223 	pMesh->Release();
224 
225 	mIsCylinder = 1;
226 }
227 
Render(IDirect3DDevice9 * pd3dDevice,neT3 * matrix)228 void CRenderPrimitive::Render(IDirect3DDevice9* pd3dDevice, neT3 * matrix)
229 {
230 	HRESULT hr;
231 
232 	D3DXMATRIXA16 mView;
233 	D3DXMATRIXA16 mProj;
234 	D3DXMATRIXA16 mWorldView;
235 	D3DXMATRIXA16 mWorldViewProjection;
236 
237 	D3DXMATRIXA16 mWorld;
238 
239 	memcpy(&mWorld, matrix, sizeof(mWorld));
240 
241 	mProj = *g_Camera.GetProjMatrix();
242 	mView = *g_Camera.GetViewMatrix();
243 
244 	if (mIsCylinder)
245 	{
246 		neV3 rotVect; rotVect.Set(NE_PI * 0.5f, 0.0f, 0.0f);
247 
248 		neT3 rot; rot.SetIdentity();
249 
250 		rot.rot.RotateXYZ(rotVect);
251 
252 		mWorld = *(D3DXMATRIX*)&rot * mWorld;
253 	}
254 
255 	mWorldViewProjection = mWorld * mView * mProj;
256 
257 	mWorldView = mWorld * mView;
258 
259 	V( g_pEffect->SetMatrix( g_hMatWVP, &mWorldViewProjection ) );
260 	V( g_pEffect->SetMatrix( g_hMatWV, &mWorldView ) );
261 	V( g_pEffect->SetMatrix( g_hMatW, &mWorld ) );
262 
263 	for (int i = 0; i < NUM_LIGHT; i++)
264 	{
265 		g_pEffect->SetVector(g_hLightTokamak[i], &vLightWorld[i]);
266 		g_pEffect->SetVector(g_hLightColorTokamak[i], &vLightColor[i]);
267 	}
268 	g_pEffect->SetVector(g_hViewPos, (D3DXVECTOR4*)g_Camera.GetEyePt());
269 
270 	pd3dDevice->SetFVF( mMesh.m_pMesh->GetFVF() );
271 
272 	g_pEffect->SetTechnique( g_hShaderTechTokamak );
273 
274 	mMesh.RenderTokamak( g_pEffect, g_hDiffuse, (D3DXVECTOR4*)&mMaterial.MatD3D.Diffuse,
275 									g_hSpecular, (D3DXVECTOR4*)&mMaterial.MatD3D.Specular,
276 									g_hAmbient, (D3DXVECTOR4*)&mMaterial.MatD3D.Ambient,
277 									g_hPower, mMaterial.MatD3D.Power,
278 									true, false );
279 
280 	//mMesh.Render( g_pEffect, NULL, g_hDiffuse, 0, g_hSpecular, 0, g_hPower, true, false );
281 }
282 
283