1 /*
2  * OpenBOR - http://www.LavaLit.com
3  * -----------------------------------------------------------------------
4  * Licensed under the BSD license, see LICENSE in OpenBOR root for details.
5  *
6  * Copyright (c) 2004 - 2011 OpenBOR Team
7  */
8 
9 //-----------------------------------------------------------------------------
10 // File: Panel.cpp
11 //
12 // Desc: Support class for rendering an image.
13 //-----------------------------------------------------------------------------
14 #include <xtl.h>
15 #include "Panel.h"
16 #include "GraphicsContext.h"
17 
18 // Here we create a structure that we can use to define a vertex.
19 struct sVertex
20 {
21     D3DXVECTOR3 position;	// The 3-D position for the vertex.
22 //	D3DXVECTOR3 normal;		// The Normal of the vertex (Used for lighting)
23     DWORD diffuse;			// Value used to hold the vertices diffuse colour value
24 // <**************************************** NEW ********************************************>
25 	// We've added another variable to the vertex structure for texturing.
26 	// When applying textures to polygons, DirectX needs to how it should position the texture
27 	// The tu is for the X axis and tv for the Y axis. We use u & v, as this is the standard
28 	// format used for texture coordinates, but we could've called them hamster and potato if
29 	// we wanted :)
30 	FLOAT       tu, tv;		// The texture coordinates
31 // <**************************************** NEW ********************************************>
32 };
33 
34 sVertex sCube[24];	// Create array of above structure to hold vertices for a cube
35 // <**************************************** NEW ********************************************>
36 // As we've added tu and tv variables into the sVertex structure above, we need to tell
37 // DirectX what data, this structure contains. This is very similar to the previous tutorial,
38 // but with the inclusion of the D3DFVF_TEX1 tag, which specdifies that the above sVertex
39 // structure includes texture coordinates aswell.
40 #define D3DFVF_sVertex (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)	// Custom FVF.
41 //#define D3DFVF_sVertex (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_NORMAL|D3DFVF_TEX1)	// Custom FVF.
42 
43 
44 //-----------------------------------------------------------------------------
45 // Name: CPanel()
46 // Desc: Help class constructor
47 //-----------------------------------------------------------------------------
CPanel()48 CPanel::CPanel()
49 {
50     m_pd3dDevice    = NULL;
51     m_pTexture		= NULL;
52     m_pVB           = NULL;
53 	g_pVBCube       = NULL ;
54 	m_nWidth		= 0;
55 	m_nHeight		= 0;
56 	m_bManaged		= FALSE;
57 	m_colDiffuse	= 0xFFFFFFFF;
58 	m_pFileBuf      = NULL ;
59 	m_nFileBufSize  = 0 ;
60 }
61 
62 
63 #ifdef __cplusplus
64 extern "C" {
65 #endif
66 
67 #ifdef __cplusplus
68 }
69 #endif
70 
71 
72 //-----------------------------------------------------------------------------
73 // Name: ~CPanel()
74 // Desc: Help class destructor
75 //-----------------------------------------------------------------------------
~CPanel()76 CPanel::~CPanel()
77 {
78     Destroy();
79 }
80 
81 
82 //-----------------------------------------------------------------------------
83 // Name: Create()
84 // Desc: Creates the panel class' internal objects
85 //-----------------------------------------------------------------------------
CreateMemory(LPDIRECT3DDEVICE8 pd3dDevice,char * filename,float width,float height)86 HRESULT CPanel::CreateMemory( LPDIRECT3DDEVICE8 pd3dDevice, char *filename, float width, float height )
87 {
88 	FILE *infile ;
89 
90 	infile = fopen( filename, "rb" ) ;
91 
92 	if ( !infile )
93 	{
94 		return D3DERR_NOTAVAILABLE ;
95 	}
96 
97 	fseek( infile, 0, SEEK_END ) ;
98 	m_nFileBufSize = ftell( infile ) ;
99 	fseek( infile, 0, SEEK_SET ) ;
100 
101 	if ( m_pFileBuf )
102 	{
103 		free( m_pFileBuf ) ;
104 		m_pFileBuf = NULL ;
105 	}
106 
107 	m_pFileBuf = (unsigned char*)malloc( m_nFileBufSize ) ;
108 
109 	if ( !m_pFileBuf )
110 	{
111 		fclose( infile ) ;
112 		return D3DERR_NOTAVAILABLE  ;
113 	}
114 
115 	fread( m_pFileBuf, 1, m_nFileBufSize, infile ) ;
116 	fclose( infile ) ;
117 
118 
119 	return (Create(pd3dDevice, NULL, FALSE, width, height)) ;
120 
121 
122 }
123 
124 
125 //-----------------------------------------------------------------------------
126 // Name: Create()
127 // Desc: Creates the panel class' internal objects
128 //-----------------------------------------------------------------------------
Create(LPDIRECT3DDEVICE8 pd3dDevice,LPDIRECT3DTEXTURE8 pd3dTexture,BOOL bManaged,FLOAT fSrcWidth,FLOAT fSrcHeight)129 HRESULT CPanel::Create( LPDIRECT3DDEVICE8 pd3dDevice, LPDIRECT3DTEXTURE8 pd3dTexture, BOOL bManaged, FLOAT fSrcWidth, FLOAT fSrcHeight)
130 {
131 
132 	InitVertices() ;
133 
134     if (m_pVB!=NULL)
135 	{
136 		m_pVB->Release();
137 		m_pVB=NULL;
138 	}
139 
140 	if ( m_pTexture )
141 	{
142 		m_pTexture->Release() ;
143 		m_pTexture = NULL ;
144 	}
145 
146     m_pd3dDevice = pd3dDevice;
147     m_pTexture = pd3dTexture;
148 	m_bManaged = bManaged;
149 
150 
151 	if (fSrcWidth > 0 && fSrcHeight > 0)
152 	{
153 
154 		m_nWidth = fSrcWidth;
155 		m_nHeight = fSrcHeight;
156 	}
157 	else
158 	{
159 		D3DSURFACE_DESC desc;
160 		m_pTexture->GetLevelDesc(0,&desc);
161 
162 		m_nWidth = (float) desc.Width;
163 		m_nHeight = (float) desc.Height;
164 	}
165 
166     m_pd3dDevice->CreateVertexBuffer( 4*sizeof(VERTEX), D3DUSAGE_WRITEONLY,
167                                     0L, D3DPOOL_DEFAULT, &m_pVB );
168 
169 
170 
171 	//TLVertex * v;
172 
173 	CPanel::VERTEX* v;
174     m_pVB->Lock( 0, 0, (BYTE**)&v, 0L );
175 
176 	FLOAT fWidth  = 640 ;
177 	FLOAT fHeight = 480 ;
178 
179     v[0].p = D3DXVECTOR4( 0 - 0.5f,			0 - 0.5f,			0, 0 );
180 	v[0].tu = 0;
181 	v[0].tv = 0;
182 	v[0].col= m_colDiffuse;
183 
184 	v[1].p = D3DXVECTOR4( fWidth - 0.5f,	0 - 0.5f,			0, 0 );
185 	v[1].tu = m_nWidth;
186 	v[1].tv = 0;
187 	v[1].col= m_colDiffuse;
188 
189     v[2].p = D3DXVECTOR4( fWidth - 0.5f,	fHeight - 0.5f,	0, 0 );
190 	v[2].tu = m_nWidth;
191 	v[2].tv = m_nHeight;
192 	v[2].col= m_colDiffuse;
193 
194     v[3].p = D3DXVECTOR4( 0 - 0.5f,			fHeight - 0.5f,	0, 0 );
195 	v[3].tu = 0;
196 	v[3].tv = m_nHeight;
197 	v[3].col= m_colDiffuse;
198 
199     m_pVB->Unlock();
200 
201 	SetAlpha( 0xFF ) ;
202 
203     return S_OK;
204 }
205 
206 
207 //-----------------------------------------------------------------------------
208 // Name: Create()
209 // Desc: Creates the panel class' internal objects
210 //-----------------------------------------------------------------------------
Recreate(LPDIRECT3DDEVICE8 pd3dDevice)211 HRESULT CPanel::Recreate( LPDIRECT3DDEVICE8 pd3dDevice)
212 {
213 
214     if (m_pVB!=NULL)
215 	{
216 		m_pVB->Release();
217 		m_pVB=NULL;
218 	}
219 
220     m_pd3dDevice = pd3dDevice;
221 
222 
223     m_pd3dDevice->CreateVertexBuffer( 4*sizeof(VERTEX), D3DUSAGE_WRITEONLY,
224                                     0L, D3DPOOL_DEFAULT, &m_pVB );
225 
226 
227 
228 
229 	CPanel::VERTEX* v;
230     m_pVB->Lock( 0, 0, (BYTE**)&v, 0L );
231 
232 	FLOAT fWidth  = 640 ;
233 	FLOAT fHeight = 480 ;
234 
235 
236 
237     v[0].p = D3DXVECTOR4( 0 - 0.5f,			0 - 0.5f,			0, 0 );
238 	v[0].tu = 0;
239 	v[0].tv = 0;
240 	v[0].col= m_colDiffuse;
241 
242 	v[1].p = D3DXVECTOR4( fWidth - 0.5f,	0 - 0.5f,			0, 0 );
243 	v[1].tu = m_nWidth;
244 	v[1].tv = 0;
245 	v[1].col= m_colDiffuse;
246 
247     v[2].p = D3DXVECTOR4( fWidth - 0.5f,	fHeight - 0.5f,	0, 0 );
248 	v[2].tu = m_nWidth;
249 	v[2].tv = m_nHeight;
250 	v[2].col= m_colDiffuse;
251 
252     v[3].p = D3DXVECTOR4( 0 - 0.5f,			fHeight - 0.5f,	0, 0 );
253 	v[3].tu = 0;
254 	v[3].tv = m_nHeight;
255 	v[3].col= m_colDiffuse;
256 
257     m_pVB->Unlock();
258 
259 	SetAlpha( 0xFF ) ;
260 
261     return S_OK;
262 }
263 
264 //-----------------------------------------------------------------------------
265 // Name: Create()
266 // Desc: Creates the panel class' internal objects
267 //-----------------------------------------------------------------------------
CreateSized(LPDIRECT3DDEVICE8 pd3dDevice,LPDIRECT3DTEXTURE8 pd3dTexture,FLOAT fX,FLOAT fY,FLOAT fSrcWidth,FLOAT fSrcHeight)268 HRESULT CPanel::CreateSized( LPDIRECT3DDEVICE8 pd3dDevice, LPDIRECT3DTEXTURE8 pd3dTexture, FLOAT fX, FLOAT fY, FLOAT fSrcWidth, FLOAT fSrcHeight)
269 {
270 
271 	if ( m_pTexture )
272 	{
273 		m_pTexture->Release() ;
274 	}
275 
276     m_pd3dDevice = pd3dDevice;
277     m_pTexture = pd3dTexture;
278 	m_bManaged = FALSE;
279 
280 	if (fSrcWidth > 0 && fSrcHeight > 0)
281 	{
282 
283 		m_nWidth = fSrcWidth;
284 		m_nHeight = fSrcHeight;
285 	}
286 	else
287 	{
288 		D3DSURFACE_DESC desc;
289 		m_pTexture->GetLevelDesc(0,&desc);
290 
291 		m_nWidth = (float) desc.Width;
292 		m_nHeight = (float) desc.Height;
293 	}
294 
295     // Create a vertex buffer for rendering the image
296     m_pd3dDevice->CreateVertexBuffer( 4*sizeof(VERTEX), D3DUSAGE_WRITEONLY,
297                                     0L, D3DPOOL_DEFAULT, &m_pVB );
298 
299 
300 
301 	//m_pd3dDevice->CreateVertexBuffer(
302                  //4*sizeof(TLVertex), VertexFVF,
303                  //D3DUSAGE_WRITEONLY,
304                  //D3DPOOL_DEFAULT, &m_pVB);
305 
306 
307 
308 	//TLVertex * v;
309 
310 	CPanel::VERTEX* v;
311     m_pVB->Lock( 0, 0, (BYTE**)&v, 0L );
312 
313 	FLOAT fWidth  = 640 ;
314 	FLOAT fHeight = 480 ;
315 
316 /*
317     v[0].x = 0 - 0.5f ; v[0].y = 0 - 0.5f ;
318 	v[0].z = 0 ; v[0].rhw = 0 ;
319 	v[0].tu = 0;
320 	v[0].tv = 0;
321 	v[0].col= 0;
322 	v[0].specular = 0;
323 
324 	v[1].x = fWidth - 0.5f ; v[1].y = 0 - 0.5f;
325 	v[1].z = 0 ; v[0].rhw = 0 ;
326 	v[1].tu = m_nWidth;
327 	v[1].tv = 0;
328 	v[1].col= 0;
329 	v[1].specular = 0;
330 
331     v[2].x = fWidth - 0.5f ; v[2].y = fHeight - 0.5f ;
332 	v[2].z = 0 ; v[0].rhw = 0 ;
333 	v[2].tu = m_nWidth;
334 	v[2].tv = m_nHeight;
335 	v[2].col= 0;
336 	v[2].specular = 0;
337 
338     v[3].x = 0 - 0.5f ; v[3].y = fHeight - 0.5f ;
339 	v[3].z = 0 ; v[0].rhw = 0 ;
340 	v[3].tu = 0;
341 	v[3].tv = m_nHeight;
342 	v[3].col= 0;
343 	v[3].specular = 0;
344 */
345 
346 
347 
348 	v[0].p = D3DXVECTOR4( fX - 0.5f,	fY - 0.5f,		0, 0 );
349 	v[0].tu = 0;
350 	v[0].tv = 0;
351 	v[0].col = m_colDiffuse;
352 
353     v[1].p = D3DXVECTOR4( fX+m_nWidth - 0.5f,	fY - 0.5f,		0, 0 );
354 	v[1].tu = 350;
355 	v[1].tv = 0;
356 	v[1].col = m_colDiffuse;
357 
358     v[2].p = D3DXVECTOR4( fX+m_nWidth - 0.5f,	fY+m_nHeight - 0.5f,	0, 0 );
359 	v[2].tu = 350;
360 	v[2].tv = 250;
361 	v[2].col = m_colDiffuse;
362 
363     v[3].p = D3DXVECTOR4( fX - 0.5f,	fY+m_nHeight - 0.5f,	0, 0 );
364 	v[3].tu = 0;
365 	v[3].tv = 250;
366 	v[3].col = m_colDiffuse;
367 
368 /*
369     v[0].p = D3DXVECTOR4( 0 - 0.5f,			0 - 0.5f,			0, 0 );
370 	v[0].tu = 0;
371 	v[0].tv = 0;
372 	v[0].col= m_colDiffuse;
373 
374 	v[1].p = D3DXVECTOR4( fWidth - 0.5f,	0 - 0.5f,			0, 0 );
375 	v[1].tu = m_nWidth;
376 	v[1].tv = 0;
377 	v[1].col= m_colDiffuse;
378 
379     v[2].p = D3DXVECTOR4( fWidth - 0.5f,	fHeight - 0.5f,	0, 0 );
380 	v[2].tu = m_nWidth;
381 	v[2].tv = m_nHeight;
382 	v[2].col= m_colDiffuse;
383 
384     v[3].p = D3DXVECTOR4( 0 - 0.5f,			fHeight - 0.5f,	0, 0 );
385 	v[3].tu = 0;
386 	v[3].tv = m_nHeight;
387 	v[3].col= m_colDiffuse;
388 */
389     m_pVB->Unlock();
390 
391     return S_OK;
392 }
393 
394 
395 
396 //-----------------------------------------------------------------------------
397 // Name: Destroy()
398 // Desc: Destroys the help class' internal objects/
399 //-----------------------------------------------------------------------------
Destroy()400 HRESULT CPanel::Destroy()
401 {
402     if (m_pVB!=NULL)
403 	{
404 		m_pVB->Release();
405 		m_pVB=NULL;
406 	}
407 
408     if (g_pVBCube!=NULL)
409 	{
410 		g_pVBCube->Release();
411 		g_pVBCube=NULL;
412 	}
413 
414 	if (m_pTexture)
415 	{
416 		if (m_bManaged)
417 		{
418 			m_pTexture->Release();
419 		}
420 
421 		m_pTexture = NULL;
422 	}
423 
424     return S_OK;
425 }
426 
427 
428 
429 
430 //-----------------------------------------------------------------------------
431 // Name: Render()
432 // Desc: Renders the image at the current position (typically 0,0).
433 //-----------------------------------------------------------------------------
Render()434 HRESULT CPanel::Render()
435 {
436 
437 	if ( m_pd3dDevice == NULL )
438 		return S_OK+1;
439 
440 	if ( m_pTexture == NULL )
441 	{
442 		if ( m_pFileBuf == NULL )
443 			return S_OK+1 ;
444 		else
445 		{
446 			if (D3DXCreateTextureFromFileInMemoryEx(m_pd3dDevice, m_pFileBuf, m_nFileBufSize,
447 				 m_nWidth, m_nHeight, 1, 0, D3DFMT_LIN_A8R8G8B8, D3DPOOL_MANAGED,
448 				 //D3DX_DEFAULT, D3DX_DEFAULT, 1, 0, D3DFMT_LIN_A8R8G8B8, D3DPOOL_MANAGED,
449 				 D3DX_FILTER_NONE , D3DX_FILTER_NONE, 0, NULL, NULL, &m_pTexture)!=D3D_OK)
450 			{
451 				return S_OK+1 ;
452 			}
453 		}
454 	}
455 
456     // Set state to render the image
457     m_pd3dDevice->SetTexture( 0, m_pTexture );
458     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
459     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
460     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
461     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE );
462     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
463     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
464     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_DISABLE );
465     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
466     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU,  D3DTADDRESS_CLAMP );
467     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV,  D3DTADDRESS_CLAMP );
468     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,      FALSE );
469     m_pd3dDevice->SetRenderState( D3DRS_FOGENABLE,    FALSE );
470     m_pd3dDevice->SetRenderState( D3DRS_FOGTABLEMODE, D3DFOG_NONE );
471     m_pd3dDevice->SetRenderState( D3DRS_FILLMODE,     D3DFILL_SOLID );
472     m_pd3dDevice->SetRenderState( D3DRS_CULLMODE,     D3DCULL_CCW );
473     m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
474     m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA );
475     m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
476     m_pd3dDevice->SetVertexShader( FVF_VERTEX );
477 
478     // Render the image
479     m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(VERTEX) );
480     m_pd3dDevice->DrawPrimitive( D3DPT_QUADLIST, 0, 1 );
481     m_pd3dDevice->SetTexture( 0, NULL );
482 
483     return S_OK;
484 }
485 
486 //-----------------------------------------------------------------------------
487 // Name: Render(float x, float y)
488 // Desc: Renders the image at a given position.
489 //-----------------------------------------------------------------------------
490 
Render(float x,float y,bool bLogical)491 HRESULT CPanel::Render(float x, float y, bool bLogical)
492 {
493 
494 	FLOAT fWidth  = m_nWidth;
495 	FLOAT fHeight = m_nHeight;
496 
497 	if ( m_pd3dDevice == NULL )
498 		return S_OK;
499 
500 	if (bLogical)
501 	{
502 		g_graphicsContext.Correct(x,y,fWidth,fHeight);
503 		g_graphicsContext.Offset(x,y);
504 	}
505 
506     // Set state to render the image
507 	CPanel::VERTEX* vertex;
508     m_pVB->Lock( 0, 0, (BYTE**)&vertex, 0L );
509 
510 	vertex[0].p = D3DXVECTOR4( x - 0.5f,		y - 0.5f,			0, 0 );
511 	vertex[0].tu = 0;
512 	vertex[0].tv = 0;
513 
514     vertex[1].p = D3DXVECTOR4( x+fWidth - 0.5f,	y - 0.5f,			0, 0 );
515 	vertex[1].tu = m_nWidth;
516 	vertex[1].tv = 0;
517 
518     vertex[2].p = D3DXVECTOR4( x+fWidth - 0.5f,	y+fHeight - 0.5f,	0, 0 );
519 	vertex[2].tu = m_nWidth;
520 	vertex[2].tv = m_nHeight;
521 
522     vertex[3].p = D3DXVECTOR4( x - 0.5f,		y+fHeight - 0.5f,	0, 0 );
523 	vertex[3].tu = 0;
524 	vertex[3].tv = m_nHeight;
525 
526     m_pVB->Unlock();
527 
528 	//SetAlpha( 0x80 ) ;
529 
530     // Set state to render the image
531     m_pd3dDevice->SetTexture( 0, m_pTexture );
532     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
533     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
534     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
535     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE );
536     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
537     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
538     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_DISABLE );
539     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
540     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU,  D3DTADDRESS_CLAMP );
541     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV,  D3DTADDRESS_CLAMP );
542     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,      FALSE );
543     m_pd3dDevice->SetRenderState( D3DRS_FOGENABLE,    FALSE );
544     m_pd3dDevice->SetRenderState( D3DRS_FOGTABLEMODE, D3DFOG_NONE );
545     m_pd3dDevice->SetRenderState( D3DRS_FILLMODE,     D3DFILL_SOLID );
546     m_pd3dDevice->SetRenderState( D3DRS_CULLMODE,     D3DCULL_CCW );
547     m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
548     m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA );
549     m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
550     m_pd3dDevice->SetVertexShader( FVF_VERTEX );
551 
552     // Render the image
553     m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(VERTEX) );
554     m_pd3dDevice->DrawPrimitive( D3DPT_QUADLIST, 0, 1 );
555     m_pd3dDevice->SetTexture( 0, NULL );
556 
557     return S_OK;
558 }
559 
560 
561 //-----------------------------------------------------------------------------
562 // Name: Render(float x, float y, float x2, float y2, width, height)
563 // Desc: Renders a portion of an image defined by x,y and w and h at a
564 //       given position x2,y2.
565 //-----------------------------------------------------------------------------
566 
Render(float x,float y,float w,float h,float x2,float y2,bool bLogical)567 HRESULT CPanel::Render(float x, float y, float w, float h, float x2, float y2, bool bLogical)
568 {
569 	float w2 = w;
570 	float h2 = h;
571 
572 	if ( m_pd3dDevice == NULL )
573 		return S_OK;
574 	if (bLogical)
575 	{
576 		g_graphicsContext.Correct(x2,y2,w2,h2);
577 		g_graphicsContext.Offset(x2,y2);
578 	}
579 
580     // Set state to render the image
581 	CPanel::VERTEX* vertex;
582     m_pVB->Lock( 0, 0, (BYTE**)&vertex, 0L );
583     vertex[0].p = D3DXVECTOR4(   x2 - 0.5f,   y2 - 0.5f, 0, 0 );  vertex[0].tu = x;		vertex[0].tv = y;
584     vertex[1].p = D3DXVECTOR4( x2+w2- 0.5f,   y2 - 0.5f, 0, 0 );  vertex[1].tu = x+w;	vertex[1].tv = y;
585     vertex[2].p = D3DXVECTOR4( x2+w2- 0.5f, y2+h2- 0.5f, 0, 0 );  vertex[2].tu = x+w;	vertex[2].tv = y+h;
586     vertex[3].p = D3DXVECTOR4(   x2 - 0.5f, y2+h2- 0.5f, 0, 0 );  vertex[3].tu = x;		vertex[3].tv = y+h;
587     m_pVB->Unlock();
588 
589 	//SetAlpha( 0x80 ) ;
590 
591     // Set state to render the image
592     m_pd3dDevice->SetTexture( 0, m_pTexture );
593     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
594     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
595     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
596     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE );
597     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
598     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
599     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_DISABLE );
600     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
601     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU,  D3DTADDRESS_CLAMP );
602     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV,  D3DTADDRESS_CLAMP );
603     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,      FALSE );
604     m_pd3dDevice->SetRenderState( D3DRS_FOGENABLE,    FALSE );
605     m_pd3dDevice->SetRenderState( D3DRS_FOGTABLEMODE, D3DFOG_NONE );
606     m_pd3dDevice->SetRenderState( D3DRS_FILLMODE,     D3DFILL_SOLID );
607     m_pd3dDevice->SetRenderState( D3DRS_CULLMODE,     D3DCULL_CCW );
608     m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
609     m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA );
610     m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
611     m_pd3dDevice->SetVertexShader(  FVF_VERTEX );
612 
613     // Render the image
614     m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(VERTEX) );
615     m_pd3dDevice->DrawPrimitive( D3DPT_QUADLIST, 0, 1 );
616     m_pd3dDevice->SetTexture( 0, NULL );
617 
618     return S_OK;
619 }
620 
621 
Render(float x,float y,float w,float h,float x2,float y2,float w2,float h2)622 HRESULT CPanel::Render(float x, float y, float w, float h, float x2, float y2, float w2, float h2)
623 {
624 	//float w2 = w;
625 	//float h2 = h;
626 	static int trtr = 0 ;
627 
628 	if ( m_pd3dDevice == NULL )
629 		return S_OK+1;
630 
631 	if ( m_pTexture == NULL )
632 	{
633 		if ( m_pFileBuf == NULL )
634 			return S_OK+1 ;
635 		else
636 		{
637 			if (D3DXCreateTextureFromFileInMemoryEx(m_pd3dDevice, m_pFileBuf, m_nFileBufSize,
638 				 m_nWidth, m_nHeight, 1, 0, D3DFMT_LIN_A8R8G8B8, D3DPOOL_MANAGED,
639 				 //D3DX_DEFAULT, D3DX_DEFAULT, 1, 0, D3DFMT_LIN_A8R8G8B8, D3DPOOL_MANAGED,
640 				 D3DX_FILTER_NONE , D3DX_FILTER_NONE, 0, NULL, NULL, &m_pTexture)!=D3D_OK)
641 			{
642 				return S_OK+1 ;
643 			}
644 		}
645 	}
646 
647 
648 
649     // Set state to render the image
650 	CPanel::VERTEX* vertex;
651     m_pVB->Lock( 0, 0, (BYTE**)&vertex, 0L );
652     vertex[0].p = D3DXVECTOR4(   x2 - 0.5f,   y2 - 0.5f, 0, 0 );  vertex[0].tu = x;		vertex[0].tv = y;
653     vertex[1].p = D3DXVECTOR4( x2+w2- 0.5f,   y2 - 0.5f, 0, 0 );  vertex[1].tu = x+w;	vertex[1].tv = y;
654     vertex[2].p = D3DXVECTOR4( x2+w2- 0.5f, y2+h2- 0.5f, 0, 0 );  vertex[2].tu = x+w;	vertex[2].tv = y+h;
655     vertex[3].p = D3DXVECTOR4(   x2 - 0.5f, y2+h2- 0.5f, 0, 0 );  vertex[3].tu = x;		vertex[3].tv = y+h;
656     m_pVB->Unlock();
657 
658 
659     // Set state to render the image
660     m_pd3dDevice->SetTexture( 0, m_pTexture );
661     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
662     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
663     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
664     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE );
665     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
666     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
667     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_DISABLE );
668     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
669     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU,  D3DTADDRESS_CLAMP );
670     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV,  D3DTADDRESS_CLAMP );
671     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,      FALSE );
672     m_pd3dDevice->SetRenderState( D3DRS_FOGENABLE,    FALSE );
673     m_pd3dDevice->SetRenderState( D3DRS_FOGTABLEMODE, D3DFOG_NONE );
674     m_pd3dDevice->SetRenderState( D3DRS_FILLMODE,     D3DFILL_SOLID );
675     m_pd3dDevice->SetRenderState( D3DRS_CULLMODE,     D3DCULL_CCW );
676     m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
677     m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA );
678     m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
679     m_pd3dDevice->SetVertexShader(  FVF_VERTEX );
680 
681     //D3DVIEWPORT8 vpBackBuffer = { 0, 0, 1920, 1080, 0.0f, 1.0f };
682     //m_pd3dDevice->SetRenderTarget( pBackBuffer, pZBuffer );
683     //m_pd3dDevice->SetViewport( &vpBackBuffer );
684 
685 
686     // Render the image
687     m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(VERTEX) );
688     m_pd3dDevice->DrawPrimitive( D3DPT_QUADLIST, 0, 1 );
689 
690 
691     //LPDIRECT3DSURFACE8 pBackBuffer, pZBuffer;
692     //m_pd3dDevice->GetRenderTarget( &pBackBuffer );
693     //m_pd3dDevice->GetDepthStencilSurface( &pZBuffer );
694     //pBackBuffer->Release();
695     //pZBuffer->Release();
696 
697 	m_pd3dDevice->SetTexture( 0, NULL );
698 
699 
700     return S_OK;
701 }
702 
703 
RenderOnCube(float x,float y,float w,float h,float x2,float y2,float w2,float h2,int face,float rx,float ry,float rz)704 HRESULT CPanel::RenderOnCube(float x, float y, float w, float h, float x2, float y2, float w2, float h2, int face, float rx, float ry, float rz)
705 {
706 	static int trtr = 0 ;
707 
708 	if ( ( m_pd3dDevice == NULL ) || ( m_pTexture == NULL ) )
709 		return S_OK;
710 
711     // Set state to render the image
712 	CPanel::VERTEX* vertex;
713     m_pVB->Lock( 0, 0, (BYTE**)&vertex, 0L );
714 
715 	switch ( face )
716 	{
717 		case 0 : //front
718 		{
719 			vertex[0].p = D3DXVECTOR4(   x2 - 0.5f,   y2 - 0.5f, 0, 0 );  vertex[0].tu = x;		vertex[0].tv = y;
720 			vertex[1].p = D3DXVECTOR4( x2+w2- 0.5f,   y2 - 0.5f, 0, 0 );  vertex[1].tu = x+w;	vertex[1].tv = y;
721 			vertex[2].p = D3DXVECTOR4( x2+w2- 0.5f, y2+h2- 0.5f, 0, 0 );  vertex[2].tu = x+w;	vertex[2].tv = y+h;
722 			vertex[3].p = D3DXVECTOR4(   x2 - 0.5f, y2+h2- 0.5f, 0, 0 );  vertex[3].tu = x;		vertex[3].tv = y+h;
723 			break ;
724 		}
725 		case 1 : //back
726 		{
727 			vertex[0].p = D3DXVECTOR4(   x2 - 0.5f,   y2 - 0.5f, w2 - 0.5f, 0 );  vertex[0].tu = x;		vertex[0].tv = y;
728 			vertex[1].p = D3DXVECTOR4( x2+w2- 0.5f,   y2 - 0.5f, w2 - 0.5f, 0 );  vertex[1].tu = x+w;	vertex[1].tv = y;
729 			vertex[2].p = D3DXVECTOR4( x2+w2- 0.5f, y2+h2- 0.5f, w2 - 0.5f, 0 );  vertex[2].tu = x+w;	vertex[2].tv = y+h;
730 			vertex[3].p = D3DXVECTOR4(   x2 - 0.5f, y2+h2- 0.5f, w2 - 0.5f, 0 );  vertex[3].tu = x;		vertex[3].tv = y+h;
731 			break ;
732 		}
733 		case 3 : //misc
734 		{
735 			vertex[0].p = D3DXVECTOR4( x2+(w2/2.0f)- 0.5f,   y2 - 0.5f, 0, 0 );  vertex[0].tu = x+(w/2.0f);	vertex[0].tv = y;
736 			vertex[1].p = D3DXVECTOR4( x2+w2- 0.5f,   y2 - 0.5f, w2/2.0f - 0.5f, 0 );  vertex[1].tu = x+w;	vertex[1].tv = y;
737 			vertex[2].p = D3DXVECTOR4( x2+w2- 0.5f,   y2+h2 - 0.5f, w2/2.0f - 0.5f, 0 );  vertex[2].tu = x+w;	vertex[2].tv = y+h;
738 			vertex[3].p = D3DXVECTOR4( x2+(w2/2.0f)- 0.5f,   y2+h2 - 0.5f, 0, 0 );  vertex[3].tu = x+(w/2.0f);	vertex[3].tv = y+h;
739 			break ;
740 		}
741 		case 2 : //right
742 		default :
743 		{
744 			vertex[0].p = D3DXVECTOR4( x2+w2- 0.5f,   y2 - 0.5f, 0, 0 );  vertex[0].tu = x+w;	vertex[0].tv = y;
745 			vertex[1].p = D3DXVECTOR4( x2+w2- 0.5f,   y2 - 0.5f, w2 - 0.5f, 0 );  vertex[1].tu = x+w;	vertex[1].tv = y;
746 			vertex[2].p = D3DXVECTOR4( x2+w2- 0.5f,   y2+h2 - 0.5f, w2 - 0.5f, 0 );  vertex[2].tu = x+w;	vertex[2].tv = y+h;
747 			vertex[3].p = D3DXVECTOR4( x2+w2- 0.5f,   y2+h2 - 0.5f, 0, 0 );  vertex[3].tu = x+w;	vertex[3].tv = y+h;
748 			break ;
749 		}
750 	}
751 
752     m_pVB->Unlock();
753 
754 
755     // Set state to render the image
756     m_pd3dDevice->SetTexture( 0, m_pTexture );
757 
758 
759 
760 
761     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
762     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
763     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
764     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE );
765     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
766     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
767     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_DISABLE );
768     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
769     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU,  D3DTADDRESS_CLAMP );
770     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV,  D3DTADDRESS_CLAMP );
771     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,      TRUE );
772     m_pd3dDevice->SetRenderState( D3DRS_FOGENABLE,    FALSE );
773     m_pd3dDevice->SetRenderState( D3DRS_FOGTABLEMODE, D3DFOG_NONE );
774     m_pd3dDevice->SetRenderState( D3DRS_FILLMODE,     D3DFILL_SOLID );
775     m_pd3dDevice->SetRenderState( D3DRS_CULLMODE,     D3DCULL_CCW );
776     m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
777     m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA );
778     m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
779 
780 
781 
782 	SetProjection(45.0f, 1.0f, 1000.0f, w2, h2);
783 
784 	SetView( 0.0f, 0.0f, -1.0f,	// The position of the camera along x, y and z axis
785 				 5.0f, 5.0f, 0.0f,	// This sets the position that the camera is looking at
786 				 0.0f, 1.0f,   0.0f);	// This sets the rotation of the camera.
787 
788 	ResetWorld();					// Reset/Clear (Whatever ya wanna call it) the matrix
789 	RotateWorld(rx,ry,rz);	// Rotate cube
790 
791 	m_pd3dDevice->SetVertexShader(  FVF_VERTEX );
792 
793     // Render the image
794     m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(VERTEX) );
795     m_pd3dDevice->DrawPrimitive( D3DPT_QUADLIST, 0, 1 );
796 	m_pd3dDevice->SetTexture( 0, NULL );
797 
798     return S_OK;
799 }
800 
801 
RenderOnCube2(float x,float y,float w,float h,float x2,float y2,float w2,float h2,int face,float rx,float ry,float rz,float scrw,float scrh,int alpha)802 HRESULT CPanel::RenderOnCube2(float x, float y, float w, float h, float x2, float y2, float w2, float h2, int face, float rx, float ry, float rz, float scrw, float scrh, int alpha)
803 {
804 	static int trtr = 0 ;
805 
806 	if ( m_pd3dDevice == NULL )
807 		return S_OK+1;
808 
809 	if ( m_pTexture == NULL )
810 	{
811 		if ( m_pFileBuf == NULL )
812 			return S_OK+1 ;
813 		else
814 		{
815 			if (D3DXCreateTextureFromFileInMemoryEx(m_pd3dDevice, m_pFileBuf, m_nFileBufSize,
816 				 m_nWidth, m_nHeight, 1, 0, D3DFMT_LIN_A8R8G8B8, D3DPOOL_MANAGED,
817 				 D3DX_FILTER_NONE , D3DX_FILTER_NONE, 0, NULL, NULL, &m_pTexture)!=D3D_OK)
818 			{
819 				return S_OK+1 ;
820 			}
821 		}
822 	}
823 
824 	m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );			// Turn on lighting
825 	m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );			// Enable depth testing
826 	m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );	// Turn off culling
827     m_pd3dDevice->SetTexture( 0, m_pTexture );
828 
829 	m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
830 	m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
831 
832 	SetProjection(90.0f, 1.0f, 1000.0f, w2,h2);
833 
834 	SetView( 0.0f, 0.0f, -8.0f - (scrw/scrh),	// The position of the camera along x, y and z axis
835 				 0.0f, 0.0f,   10.0f,	// This sets the position that the camera is looking at
836 				 0.0f, 1.0f,   0.0f);	// This sets the rotation of the camera.
837 
838 
839 	SetupVertices( x,y,w,h, x2, y2, w2, h2, scrw, scrh, alpha ) ;
840 
841 	ResetWorld();					// Reset/Clear (Whatever ya wanna call it) the matrix
842 
843 	if ( (int)scrw == 640 )
844 		ScaleWorld( 4.0, 4.0, 4.0 ) ;
845 	else
846 		ScaleWorld( 3.52, 3.52, 3.52 ) ;
847 
848 	RotateWorld( rx, ry, rz ) ;
849 
850 	m_pd3dDevice->SetVertexShader( D3DFVF_sVertex );	// Set vertex shader
851 
852 	m_pd3dDevice->SetStreamSource( 0, g_pVBCube, sizeof(sVertex) );
853 
854 	if ( face & 0x01 )
855 		m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN,  0, 2 );
856 	if ( face & 0x02 )
857 		m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN,  4, 2 );
858 	if ( face & 0x04 )
859 		m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN,  8, 2 );
860 	if ( face & 0x08 )
861 		m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 12, 2 );
862 	if ( face & 0x10 )
863 		m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 16, 2 );
864 	if ( face & 0x20 )
865 		m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 20, 2 );
866 
867 	m_pd3dDevice->SetTexture( 0, NULL );
868 
869     return S_OK;
870 }
871 
872 
IsValid()873 BOOL CPanel::IsValid()
874 {
875 	return (m_pTexture != NULL);
876 }
877 
878 // This is called from WinMain() just after the window and DirectX object have been created.
SetupVertices(float x,float y,float w,float h,float x2,float y2,float w2,float h2,float scrw,float scrh,int alpha)879 bool CPanel::SetupVertices( float x, float y, float w, float h, float x2, float y2, float w2, float h2, float scrw, float scrh, int alpha)
880 {
881 
882 
883 // <**************************************** NEW ********************************************>
884 	// Setup the values that will be contained within the vertex buffer for a cube
885 
886 	// This is almost identical to the previous tutorial for setting up the vertices of a cube
887 	// With just one addition per vertex.
888 	// This addition is the setting of the texture coordinates.
889 	// Above I said tu is for the X axis and tv is for the Y axis, now please let me explain
890 	// what I mean by this in more detail...
891 /*
892 
893 	Let's say we are drawing a quad which is made up of four vertices....
894 
895 	2----------4	Now say we want DirectX position the texture onto this quad, filling it
896 	| .        |	completely. For the vertices on the left hand side, tu ( x axis) would be
897 	|   .      |	 set to 0 and the vertices on the right, tu ( x axis ) set at 1.
898 	|     .    |	For the top vertices, tv ( y axis ) is set to 0, the bottom, tv set to 1.
899 	|       .  |	You'd think that for the Y axis texture coords would be set the other way
900 	|         .|	around, but they're not, this is because DirectX flips these. If you did put
901 	1----------3	the tv values the other way around, the texture image would be flipped upside
902 					down.
903 
904 					What if we wanted to tile the texture though, so that DirectX halves the size
905 					of it and repeats it like a chess board?
906 					Simple! Just double the texture values from 1 to 2 :)
907 					Also, if you only want a portion of the texture to be displayed, you'd
908 					use texture coordinates more than 0 and less than 1.
909 					For example, if tu (X axis) for the left vertices was   0.25 and if...
910 									tu (X axis) for the right vertices was  0.75 and if...
911 									tv (Y axis) for the top vertices was    0.25 and if...
912 									tv (Y axis) for the bottom vertices was 0.75 then...
913 					only the centre of the texture would be stretched onto the entire quad.
914 					Hard to explain, but kind of like trimming the edges off the texture, then
915 					taking that texture and stretching it over the whole quad.
916 		I hope that makes sence! If it doesn't and anyone posts a question about this in the
917 		forum, then I'll update this section again :)
918 */
919 	float minx, miny, maxx, maxy, minz, maxz ;
920 	DWORD color = ( alpha << 24 ) | 0xFFFFFF ;
921 
922 
923 	minx = ((2.0f*x2/scrw)-1.0f)*scrw/scrh ;
924 	maxx = ((2.0f*(x2+w2)/scrw)-1.0f)*scrw/scrh ;
925 
926 	maxy = -((2.0f*y2/scrh)-1.0f) ;
927 	miny = -((2.0f*(y2+h2)/scrh)-1.0f) ;
928 
929 	minz = -1.0f*(scrw/scrh) ;
930 	maxz = minz + w2*-2.0*minz/scrw ;
931 
932 	// Front  (White) (Half sized textures, tiled twice)
933 	sCube[0].position = D3DXVECTOR3( minx, miny , minz );	// Bottom left vertex position
934 	sCube[0].diffuse = color;			// Bottom left vertex colour
935 	//sCube[0].normal = D3DXVECTOR3( 0,0,-1);					// Forward pointing normal
936 	sCube[0].tu=x;	sCube[0].tv=h;							// * NEW * Texture coordinates
937 	sCube[1].position = D3DXVECTOR3( minx, maxy, minz );	// Top left vertex position
938 	sCube[1].diffuse = color;			// Top left vertex colour
939 	//sCube[1].normal = D3DXVECTOR3( 0,0,-1);					// Forward pointing normal
940 	sCube[1].tu=x;	sCube[1].tv=y;							// * NEW * Texture coordinates
941 	sCube[2].position = D3DXVECTOR3( maxx, maxy, minz );	// Top right vertex position
942 	sCube[2].diffuse = color;			// Top right vertex colour
943 	//sCube[2].normal = D3DXVECTOR3( 0,0,-1);					// Forward pointing normal
944 	sCube[2].tu=w;	sCube[2].tv=y;							// * NEW * Texture coordinates
945 	sCube[3].position = D3DXVECTOR3( maxx, miny, minz );	// Bottom right vertex position
946 	sCube[3].diffuse = color;			// Bottom right vertex colour
947 	//sCube[3].normal = D3DXVECTOR3( 0,0,-1);					// Forward pointing normal
948 	sCube[3].tu=w;	sCube[3].tv=h;							// * NEW * Texture coordinates
949 
950 
951 
952 	// Back (White)
953 	sCube[4].position = D3DXVECTOR3( minx, miny, maxz );	// Bottom left vertex position
954 	sCube[4].diffuse = color;			// Bottom left vertex colour
955 	//sCube[4].normal = D3DXVECTOR3( 0,0,1);					// Forward pointing normal
956 	sCube[4].tu=w;	sCube[4].tv=h;							// * NEW * Texture coordinates
957 	sCube[5].position = D3DXVECTOR3( minx, maxy, maxz );	// Top left vertex position
958 	sCube[5].diffuse = color;			// Top left vertex colour
959 	//sCube[5].normal = D3DXVECTOR3( 0,0,1);					// Forward pointing normal
960 	sCube[5].tu=w;	sCube[5].tv=y;							// * NEW * Texture coordinates
961 	sCube[6].position = D3DXVECTOR3(  maxx, maxy, maxz );	// Top right vertex position
962 	sCube[6].diffuse = color;			// Top right vertex colour
963 	//sCube[6].normal = D3DXVECTOR3( 0,0,1);					// Forward pointing normal
964 	sCube[6].tu=x;	sCube[6].tv=y;							// * NEW * Texture coordinates
965 	sCube[7].position = D3DXVECTOR3(  maxx, miny, maxz );	// Bottom right vertex position
966 	sCube[7].diffuse = color;			// Bottom right vertex colour
967 	//sCube[7].normal = D3DXVECTOR3( 0,0,1);					// Forward pointing normal
968 	sCube[7].tu=x;	sCube[7].tv=h;							// * NEW * Texture coordinates
969 
970 	// Left (White)
971 	sCube[8].position = D3DXVECTOR3( minx, miny, minz );	// Bottom left vertex position
972 	sCube[8].diffuse = color;			// Bottom left vertex colour)
973 	//sCube[8].normal = D3DXVECTOR3( -1,0,0);					// Forward pointing normal
974 	sCube[8].tu=w;	sCube[8].tv=h;							// * NEW * Texture coordinates
975 	sCube[9].position = D3DXVECTOR3( minx, maxy, minz );	// Top left vertex position
976 	sCube[9].diffuse = color;			// Top left vertex colour
977 	//sCube[9].normal = D3DXVECTOR3( -1,0,0);					// Forward pointing normal
978 	sCube[9].tu=w;	sCube[9].tv=y;							// * NEW * Texture coordinates
979 	sCube[10].position = D3DXVECTOR3( minx, maxy, maxz );	// Top right vertex position
980 	sCube[10].diffuse = color;			// Top right vertex colour
981 	//sCube[10].normal = D3DXVECTOR3( -1,0,0);				// Forward pointing normal
982 	sCube[10].tu=x;	sCube[10].tv=y;							// * NEW * Texture coordinates
983 	sCube[11].position = D3DXVECTOR3( minx, miny, maxz );	// Bottom right vertex position
984 	sCube[11].diffuse = color;			// Bottom right vertex colour
985 	//sCube[11].normal = D3DXVECTOR3( -1,0,0);				// Forward pointing normal
986 	sCube[11].tu=x;	sCube[11].tv=h;							// * NEW * Texture coordinates
987 
988 	// Right (White)
989 	sCube[12].position = D3DXVECTOR3( maxx, miny, minz );	// Bottom left vertex position
990 	sCube[12].diffuse = color;			// Bottom left vertex colour
991 	//sCube[12].normal = D3DXVECTOR3( 1,0,0);					// Forward pointing normal
992 	sCube[12].tu=x;	sCube[12].tv=h;							// * NEW * Texture coordinates
993 	sCube[13].position = D3DXVECTOR3( maxx, maxy, minz );	// Top left vertex position
994 	sCube[13].diffuse = color;			// Top left vertex colour
995 	//sCube[13].normal = D3DXVECTOR3( 1,0,0);					// Forward pointing normal
996 	sCube[13].tu=x;	sCube[13].tv=y;							// * NEW * Texture coordinates
997 	sCube[14].position = D3DXVECTOR3( maxx, maxy, maxz );	// Top right vertex position
998 	sCube[14].diffuse = color;			// Top right vertex colour
999 	//sCube[14].normal = D3DXVECTOR3( 1,0,0);					// Forward pointing normal
1000 	sCube[14].tu=w;	sCube[14].tv=y;							// * NEW * Texture coordinates
1001 	sCube[15].position = D3DXVECTOR3( maxx, miny, maxz );	// Bottom right vertex position
1002 	sCube[15].diffuse = color;			// Bottom right vertex colour
1003 	//sCube[15].normal = D3DXVECTOR3( 1,0,0);					// Forward pointing normal
1004 	sCube[15].tu=w;	sCube[15].tv=h;							// * NEW * Texture coordinates
1005 
1006 	// Top (White)
1007 	sCube[16].position = D3DXVECTOR3( minx, maxy, minz );	// Bottom left vertex position
1008 	sCube[16].diffuse = color;			// Bottom left vertex colour
1009 	//sCube[16].normal = D3DXVECTOR3( 0,1,0);					// Forward pointing normal
1010 	sCube[16].tu=x;	sCube[16].tv=h;							// * NEW * Texture coordinates
1011 	sCube[17].position = D3DXVECTOR3( minx, maxy, maxz );	// Top left vertex position
1012 	sCube[17].diffuse = color;			// Top left vertex colour
1013 	//sCube[17].normal = D3DXVECTOR3( 0,1,0);					// Forward pointing normal
1014 	sCube[17].tu=w;	sCube[17].tv=h;							// * NEW * Texture coordinates
1015 	sCube[18].position = D3DXVECTOR3( maxx, maxy, maxz );	// Top right vertex position
1016 	sCube[18].diffuse = color;			// Top right vertex colour
1017 	//sCube[18].normal = D3DXVECTOR3( 0,1,0);					// Forward pointing normal
1018 	sCube[18].tu=w;	sCube[18].tv=y;							// * NEW * Texture coordinates
1019 	sCube[19].position = D3DXVECTOR3( maxx, maxy, minz );	// Bottom right vertex position
1020 	sCube[19].diffuse = color;			// Bottom right vertex colour
1021 	//sCube[19].normal = D3DXVECTOR3( 0,1,0);					// Forward pointing normal
1022 	sCube[19].tu=x;	sCube[19].tv=y;							// * NEW * Texture coordinates
1023 
1024 /*
1025 	// Top (White)
1026 	sCube[16].position = D3DXVECTOR3( minx, maxy, minz );	// Bottom left vertex position
1027 	sCube[16].diffuse = color;			// Bottom left vertex colour
1028 	//sCube[16].normal = D3DXVECTOR3( 0,1,0);					// Forward pointing normal
1029 	sCube[16].tu=x;	sCube[16].tv=h;							// * NEW * Texture coordinates
1030 	sCube[17].position = D3DXVECTOR3( maxx, maxy, minz );	// Top left vertex position
1031 	sCube[17].diffuse = color;			// Top left vertex colour
1032 	//sCube[17].normal = D3DXVECTOR3( 0,1,0);					// Forward pointing normal
1033 	sCube[17].tu=w;	sCube[17].tv=h;							// * NEW * Texture coordinates
1034 	sCube[18].position = D3DXVECTOR3( maxx, maxy, 1.0f );	// Top right vertex position
1035 	sCube[18].diffuse = color;			// Top right vertex colour
1036 	//sCube[18].normal = D3DXVECTOR3( 0,1,0);					// Forward pointing normal
1037 	sCube[18].tu=w;	sCube[18].tv=y;							// * NEW * Texture coordinates
1038 	sCube[19].position = D3DXVECTOR3( minx, maxy, 1.0f );	// Bottom right vertex position
1039 	sCube[19].diffuse = color;			// Bottom right vertex colour
1040 	//sCube[19].normal = D3DXVECTOR3( 0,1,0);					// Forward pointing normal
1041 	sCube[19].tu=x;	sCube[19].tv=y;							// * NEW * Texture coordinates
1042 */
1043 
1044 	// Bottom (Multi coloured)
1045 	sCube[20].position = D3DXVECTOR3( minx, miny, minz );	// Bottom left vertex position
1046 	sCube[20].diffuse = color;				// Bottom left vertex colour (Red)
1047 	//sCube[20].normal = D3DXVECTOR3( 0,-1,0);				// Forward pointing normal
1048 	sCube[20].tu=x;	sCube[20].tv=y;							// * NEW * Texture coordinates
1049 	sCube[21].position = D3DXVECTOR3( maxx, miny, minz );	// Top left vertex position
1050 	sCube[21].diffuse = color;				// Top left vertex colour (Green)
1051 	//sCube[21].normal = D3DXVECTOR3( 0,-1,0);				// Forward pointing normal
1052 	sCube[21].tu=w;	sCube[21].tv=y;							// * NEW * Texture coordinates
1053 	sCube[22].position = D3DXVECTOR3( maxx, miny, maxz );	// Top right vertex position
1054 	sCube[22].diffuse = color;				// Top right vertex colour (Blue)
1055 	//sCube[22].normal = D3DXVECTOR3( 0,-1,0);				// Forward pointing normal
1056 	sCube[22].tu=w;	sCube[22].tv=h;							// * NEW * Texture coordinates
1057 	sCube[23].position = D3DXVECTOR3( minx, miny, maxz );	// Bottom right vertex position
1058 	sCube[23].diffuse = color;				// Bottom right vertex colour (Blue)
1059 	//sCube[23].normal = D3DXVECTOR3( 0,-1,0);				// Forward pointing normal
1060 	sCube[23].tu=x;	sCube[23].tv=h;							// * NEW * Texture coordinates
1061 // <**************************************** NEW ********************************************>
1062 
1063 
1064 	// Copy data to vertex buffer
1065 	VOID* pVertices;				// Temp memory to work with
1066 
1067 	if ( g_pVBCube == NULL )
1068 	{
1069 		return false ;
1070 	}
1071 
1072 	// Lock vertex buffer
1073 	if( FAILED( g_pVBCube->Lock( 0,	sizeof(sCube), (BYTE**)&pVertices, 0 ) ) )
1074 	{
1075 		return false;	// If something went wrong, say so!
1076 	}
1077 
1078 	// Copy the data from the array into the buffer
1079 	memcpy( pVertices, sCube, sizeof(sCube) );
1080 
1081 	// Unlock vertex buffer
1082 	g_pVBCube->Unlock();
1083 
1084 
1085 	return true;
1086 }
1087 
1088 
1089 // This is called from WinMain() just after the window and DirectX object have been created.
InitVertices()1090 bool CPanel::InitVertices()
1091 {
1092 
1093 	// Create an empty vertex buffer to hold the triangles information.
1094 	if ( g_pVBCube == NULL )
1095 		if( FAILED( m_pd3dDevice->CreateVertexBuffer( 24*sizeof(sVertex), 0, D3DFVF_sVertex, D3DPOOL_MANAGED, &g_pVBCube ) ) )
1096 			return false;	// If anything went wrong, quit.
1097 
1098 
1099 // <**************************************** NEW ********************************************>
1100 	// Setup the values that will be contained within the vertex buffer for a cube
1101 
1102 	// This is almost identical to the previous tutorial for setting up the vertices of a cube
1103 	// With just one addition per vertex.
1104 	// This addition is the setting of the texture coordinates.
1105 	// Above I said tu is for the X axis and tv is for the Y axis, now please let me explain
1106 	// what I mean by this in more detail...
1107 /*
1108 
1109 	Let's say we are drawing a quad which is made up of four vertices....
1110 
1111 	2----------4	Now say we want DirectX position the texture onto this quad, filling it
1112 	| .        |	completely. For the vertices on the left hand side, tu ( x axis) would be
1113 	|   .      |	 set to 0 and the vertices on the right, tu ( x axis ) set at 1.
1114 	|     .    |	For the top vertices, tv ( y axis ) is set to 0, the bottom, tv set to 1.
1115 	|       .  |	You'd think that for the Y axis texture coords would be set the other way
1116 	|         .|	around, but they're not, this is because DirectX flips these. If you did put
1117 	1----------3	the tv values the other way around, the texture image would be flipped upside
1118 					down.
1119 
1120 					What if we wanted to tile the texture though, so that DirectX halves the size
1121 					of it and repeats it like a chess board?
1122 					Simple! Just double the texture values from 1 to 2 :)
1123 					Also, if you only want a portion of the texture to be displayed, you'd
1124 					use texture coordinates more than 0 and less than 1.
1125 					For example, if tu (X axis) for the left vertices was   0.25 and if...
1126 									tu (X axis) for the right vertices was  0.75 and if...
1127 									tv (Y axis) for the top vertices was    0.25 and if...
1128 									tv (Y axis) for the bottom vertices was 0.75 then...
1129 					only the centre of the texture would be stretched onto the entire quad.
1130 					Hard to explain, but kind of like trimming the edges off the texture, then
1131 					taking that texture and stretching it over the whole quad.
1132 		I hope that makes sence! If it doesn't and anyone posts a question about this in the
1133 		forum, then I'll update this section again :)
1134 */
1135 	DWORD color = 0xFFFFFFFF ;
1136 
1137 	// Front  (White) (Half sized textures, tiled twice)
1138 	sCube[0].position = D3DXVECTOR3( -1.0f, -1.0f,-1.0f );	// Bottom left vertex position
1139 	sCube[0].diffuse = color;			// Bottom left vertex colour
1140 	//sCube[0].normal = D3DXVECTOR3( 0,0,-1);					// Forward pointing normal
1141 	sCube[0].tu=0;	sCube[0].tv=480;							// * NEW * Texture coordinates
1142 	sCube[1].position = D3DXVECTOR3( -1.0f,  1.0f,-1.0f );	// Top left vertex position
1143 	sCube[1].diffuse = color;			// Top left vertex colour
1144 	//sCube[1].normal = D3DXVECTOR3( 0,0,-1);					// Forward pointing normal
1145 	sCube[1].tu=0;	sCube[1].tv=0;							// * NEW * Texture coordinates
1146 	sCube[2].position = D3DXVECTOR3(  1.0f,  1.0f,-1.0f );	// Top right vertex position
1147 	sCube[2].diffuse = color;			// Top right vertex colour
1148 	//sCube[2].normal = D3DXVECTOR3( 0,0,-1);					// Forward pointing normal
1149 	sCube[2].tu=640;	sCube[2].tv=0;							// * NEW * Texture coordinates
1150 	sCube[3].position = D3DXVECTOR3(  1.0f, -1.0f,-1.0f );	// Bottom right vertex position
1151 	sCube[3].diffuse = color;			// Bottom right vertex colour
1152 	//sCube[3].normal = D3DXVECTOR3( 0,0,-1);					// Forward pointing normal
1153 	sCube[3].tu=640;	sCube[3].tv=480;							// * NEW * Texture coordinates
1154 
1155 	// Back (White)
1156 	sCube[4].position = D3DXVECTOR3( -1.0f, -1.0f, 1.0f );	// Bottom left vertex position
1157 	sCube[4].diffuse = color;			// Bottom left vertex colour
1158 	//sCube[4].normal = D3DXVECTOR3( 0,0,1);					// Forward pointing normal
1159 	sCube[4].tu=0;	sCube[4].tv=1;							// * NEW * Texture coordinates
1160 	sCube[5].position = D3DXVECTOR3( -1.0f,  1.0f, 1.0f );	// Top left vertex position
1161 	sCube[5].diffuse = color;			// Top left vertex colour
1162 	//sCube[5].normal = D3DXVECTOR3( 0,0,1);					// Forward pointing normal
1163 	sCube[5].tu=0;	sCube[5].tv=0;							// * NEW * Texture coordinates
1164 	sCube[6].position = D3DXVECTOR3(  1.0f,  1.0f, 1.0f );	// Top right vertex position
1165 	sCube[6].diffuse = color;			// Top right vertex colour
1166 	//sCube[6].normal = D3DXVECTOR3( 0,0,1);					// Forward pointing normal
1167 	sCube[6].tu=1;	sCube[6].tv=0;							// * NEW * Texture coordinates
1168 	sCube[7].position = D3DXVECTOR3(  1.0f, -1.0f, 1.0f );	// Bottom right vertex position
1169 	sCube[7].diffuse = color;			// Bottom right vertex colour
1170 	//sCube[7].normal = D3DXVECTOR3( 0,0,1);					// Forward pointing normal
1171 	sCube[7].tu=1;	sCube[7].tv=1;							// * NEW * Texture coordinates
1172 
1173 	// Left (White)
1174 	sCube[8].position = D3DXVECTOR3( -1.0f, -1.0f, -1.0f );	// Bottom left vertex position
1175 	sCube[8].diffuse = color;			// Bottom left vertex colour)
1176 	//sCube[8].normal = D3DXVECTOR3( -1,0,0);					// Forward pointing normal
1177 	sCube[8].tu=0;	sCube[8].tv=1;							// * NEW * Texture coordinates
1178 	sCube[9].position = D3DXVECTOR3( -1.0f,  1.0f, -1.0f );	// Top left vertex position
1179 	sCube[9].diffuse = color;			// Top left vertex colour
1180 	//sCube[9].normal = D3DXVECTOR3( -1,0,0);					// Forward pointing normal
1181 	sCube[9].tu=0;	sCube[9].tv=0;							// * NEW * Texture coordinates
1182 	sCube[10].position = D3DXVECTOR3( -1.0f,  1.0f, 1.0f );	// Top right vertex position
1183 	sCube[10].diffuse = color;			// Top right vertex colour
1184 	//sCube[10].normal = D3DXVECTOR3( -1,0,0);				// Forward pointing normal
1185 	sCube[10].tu=0;	sCube[10].tv=0;							// * NEW * Texture coordinates
1186 	sCube[11].position = D3DXVECTOR3( -1.0f, -1.0f, 1.0f );	// Bottom right vertex position
1187 	sCube[11].diffuse = color;			// Bottom right vertex colour
1188 	//sCube[11].normal = D3DXVECTOR3( -1,0,0);				// Forward pointing normal
1189 	sCube[11].tu=0;	sCube[11].tv=1;							// * NEW * Texture coordinates
1190 
1191 	// Right (White)
1192 	sCube[12].position = D3DXVECTOR3( 1.0f, -1.0f, -1.0f );	// Bottom left vertex position
1193 	sCube[12].diffuse = color;			// Bottom left vertex colour
1194 	//sCube[12].normal = D3DXVECTOR3( 1,0,0);					// Forward pointing normal
1195 	sCube[12].tu=1;	sCube[12].tv=1;							// * NEW * Texture coordinates
1196 	sCube[13].position = D3DXVECTOR3( 1.0f,  1.0f, -1.0f );	// Top left vertex position
1197 	sCube[13].diffuse = color;			// Top left vertex colour
1198 	//sCube[13].normal = D3DXVECTOR3( 1,0,0);					// Forward pointing normal
1199 	sCube[13].tu=1;	sCube[13].tv=0;							// * NEW * Texture coordinates
1200 	sCube[14].position = D3DXVECTOR3( 1.0f,  1.0f, 1.0f );	// Top right vertex position
1201 	sCube[14].diffuse = color;			// Top right vertex colour
1202 	//sCube[14].normal = D3DXVECTOR3( 1,0,0);					// Forward pointing normal
1203 	sCube[14].tu=0;	sCube[14].tv=0;							// * NEW * Texture coordinates
1204 	sCube[15].position = D3DXVECTOR3( 1.0f, -1.0f, 1.0f );	// Bottom right vertex position
1205 	sCube[15].diffuse = color;			// Bottom right vertex colour
1206 	//sCube[15].normal = D3DXVECTOR3( 1,0,0);					// Forward pointing normal
1207 	sCube[15].tu=1;	sCube[15].tv=0;							// * NEW * Texture coordinates
1208 
1209 	// Top (White)
1210 	sCube[16].position = D3DXVECTOR3(-1.0f, 1.0f, -1.0f );	// Bottom left vertex position
1211 	sCube[16].diffuse = color;			// Bottom left vertex colour
1212 	//sCube[16].normal = D3DXVECTOR3( 0,1,0);					// Forward pointing normal
1213 	sCube[16].tu=1;	sCube[16].tv=1;							// * NEW * Texture coordinates
1214 	sCube[17].position = D3DXVECTOR3( 1.0f,  1.0f, -1.0f );	// Top left vertex position
1215 	sCube[17].diffuse = color;			// Top left vertex colour
1216 	//sCube[17].normal = D3DXVECTOR3( 0,1,0);					// Forward pointing normal
1217 	sCube[17].tu=0;	sCube[17].tv=1;							// * NEW * Texture coordinates
1218 	sCube[18].position = D3DXVECTOR3( 1.0f,  1.0f, 1.0f );	// Top right vertex position
1219 	sCube[18].diffuse = color;			// Top right vertex colour
1220 	//sCube[18].normal = D3DXVECTOR3( 0,1,0);					// Forward pointing normal
1221 	sCube[18].tu=0;	sCube[18].tv=0;							// * NEW * Texture coordinates
1222 	sCube[19].position = D3DXVECTOR3(-1.0f, 1.0f, 1.0f );	// Bottom right vertex position
1223 	sCube[19].diffuse = color;			// Bottom right vertex colour
1224 	//sCube[19].normal = D3DXVECTOR3( 0,1,0);					// Forward pointing normal
1225 	sCube[19].tu=1;	sCube[19].tv=0;							// * NEW * Texture coordinates
1226 
1227 	// Bottom (Multi coloured)
1228 	sCube[20].position = D3DXVECTOR3(-1.0f,-1.0f, -1.0f );	// Bottom left vertex position
1229 	sCube[20].diffuse = D3DCOLOR_XRGB(255,0,1);				// Bottom left vertex colour (Red)
1230 	//sCube[20].normal = D3DXVECTOR3( 0,-1,0);				// Forward pointing normal
1231 	sCube[20].tu=1;	sCube[20].tv=1;							// * NEW * Texture coordinates
1232 	sCube[21].position = D3DXVECTOR3( 1.0f, -1.0f, -1.0f );	// Top left vertex position
1233 	sCube[21].diffuse = D3DCOLOR_XRGB(0,255,1);				// Top left vertex colour (Green)
1234 	//sCube[21].normal = D3DXVECTOR3( 0,-1,0);				// Forward pointing normal
1235 	sCube[21].tu=0;	sCube[21].tv=1;							// * NEW * Texture coordinates
1236 	sCube[22].position = D3DXVECTOR3( 1.0f, -1.0f, 1.0f );	// Top right vertex position
1237 	sCube[22].diffuse = D3DCOLOR_XRGB(0,0,255);				// Top right vertex colour (Blue)
1238 	//sCube[22].normal = D3DXVECTOR3( 0,-1,0);				// Forward pointing normal
1239 	sCube[22].tu=0;	sCube[22].tv=0;							// * NEW * Texture coordinates
1240 	sCube[23].position = D3DXVECTOR3(-1.0f, -1.0f, 1.0f );	// Bottom right vertex position
1241 	sCube[23].diffuse = D3DCOLOR_XRGB(0,0,255);				// Bottom right vertex colour (Blue)
1242 	//sCube[23].normal = D3DXVECTOR3( 0,-1,0);				// Forward pointing normal
1243 	sCube[23].tu=1;	sCube[23].tv=0;							// * NEW * Texture coordinates
1244 // <**************************************** NEW ********************************************>
1245 
1246 	// Copy data to vertex buffer
1247 	VOID* pVertices;				// Temp memory to work with
1248 
1249 	// Lock vertex buffer
1250 	if( FAILED( g_pVBCube->Lock( 0,	sizeof(sCube), (BYTE**)&pVertices, 0 ) ) )
1251 		return false;	// If something went wrong, say so!
1252 
1253 	// Copy the data from the array into the buffer
1254 	memcpy( pVertices, sCube, sizeof(sCube) );
1255 
1256 	// Unlock vertex buffer
1257 	g_pVBCube->Unlock();
1258 
1259 
1260 	return true;
1261 }
1262 
Render(float x,float y,float nw,float nh,bool bLogical)1263 HRESULT CPanel::Render(float x, float y, float nw, float nh, bool bLogical)
1264 {
1265 	if ( m_pd3dDevice == NULL )
1266 		return S_OK+1;
1267 
1268 	if ( m_pTexture == NULL )
1269 	{
1270 		if ( m_pFileBuf == NULL )
1271 			return S_OK+1 ;
1272 		else
1273 		{
1274 			if (D3DXCreateTextureFromFileInMemoryEx(m_pd3dDevice, m_pFileBuf, m_nFileBufSize,
1275 				 m_nWidth, m_nHeight, 1, 0, D3DFMT_LIN_A8R8G8B8, D3DPOOL_MANAGED,
1276 				 //D3DX_DEFAULT, D3DX_DEFAULT, 1, 0, D3DFMT_LIN_A8R8G8B8, D3DPOOL_MANAGED,
1277 				 D3DX_FILTER_NONE , D3DX_FILTER_NONE, 0, NULL, NULL, &m_pTexture)!=D3D_OK)
1278 			{
1279 				return S_OK+1 ;
1280 			}
1281 		}
1282 	}
1283 
1284 	if (bLogical)
1285 	{
1286 		g_graphicsContext.Correct(x,y,nw,nh);
1287 		g_graphicsContext.Offset(x,y);
1288 	}
1289 
1290     // Set state to render the image
1291 	CPanel::VERTEX* vertex;
1292     m_pVB->Lock( 0, 0, (BYTE**)&vertex, 0L );
1293 
1294 
1295 	vertex[0].p = D3DXVECTOR4( x - 0.5f,	y - 0.5f,		0, 0 );
1296 	vertex[0].tu = 0;
1297 	vertex[0].tv = 0;
1298 	vertex[0].col = m_colDiffuse;
1299 
1300     vertex[1].p = D3DXVECTOR4( x+nw - 0.5f,	y - 0.5f,		0, 0 );
1301 	vertex[1].tu = m_nWidth;
1302 	vertex[1].tv = 0;
1303 	vertex[1].col = m_colDiffuse;
1304 
1305     vertex[2].p = D3DXVECTOR4( x+nw - 0.5f,	y+nh - 0.5f,	0, 0 );
1306 	vertex[2].tu = m_nWidth;
1307 	vertex[2].tv = m_nHeight;
1308 	vertex[2].col = m_colDiffuse;
1309 
1310     vertex[3].p = D3DXVECTOR4( x - 0.5f,	y+nh - 0.5f,	0, 0 );
1311 	vertex[3].tu = 0;
1312 	vertex[3].tv = m_nHeight;
1313 	vertex[3].col = m_colDiffuse;
1314 
1315     m_pVB->Unlock();
1316 
1317     // Set state to render the image
1318     m_pd3dDevice->SetTexture( 0, m_pTexture );
1319     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
1320     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
1321     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
1322     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE );
1323     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
1324     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
1325     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_DISABLE );
1326     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
1327     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU,  D3DTADDRESS_CLAMP );
1328     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV,  D3DTADDRESS_CLAMP );
1329     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,      FALSE );
1330     m_pd3dDevice->SetRenderState( D3DRS_FOGENABLE,    FALSE );
1331     m_pd3dDevice->SetRenderState( D3DRS_FOGTABLEMODE, D3DFOG_NONE );
1332     m_pd3dDevice->SetRenderState( D3DRS_FILLMODE,     D3DFILL_SOLID );
1333     m_pd3dDevice->SetRenderState( D3DRS_CULLMODE,     D3DCULL_CCW );
1334     m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
1335     m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA );
1336     m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
1337     m_pd3dDevice->SetVertexShader( FVF_VERTEX );
1338 
1339     // Render the image
1340     m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(VERTEX) );
1341     m_pd3dDevice->DrawPrimitive( D3DPT_QUADLIST, 0, 1 );
1342     m_pd3dDevice->SetTexture( 0, NULL );
1343 
1344     return S_OK;
1345 }
1346 
SetAlpha(DWORD dwAlpha)1347 HRESULT CPanel::SetAlpha(DWORD dwAlpha)
1348 {
1349 	D3DCOLOR colour = (dwAlpha << 24) | 0xFFFFFF;
1350 	return SetColourDiffuse(colour);
1351 }
1352 
SetColourDiffuse(D3DCOLOR colour)1353 HRESULT CPanel::SetColourDiffuse(D3DCOLOR colour)
1354 {
1355 	if (colour!=m_colDiffuse)
1356 	{
1357 		if ( m_pVB != NULL )
1358 		{
1359 			//TLVertex * vertex;
1360 			CPanel::VERTEX* vertex;
1361 			m_pVB->Lock( 0, 0, (BYTE**)&vertex, 0L );
1362 			vertex[0].col = colour;
1363 			vertex[1].col = colour;
1364 			vertex[2].col = colour;
1365 			vertex[3].col = colour;
1366 			m_pVB->Unlock();
1367 
1368 			m_colDiffuse = colour;
1369 		}
1370 	}
1371 
1372 	return S_OK;
1373 }
1374 
1375 
1376 // This method's code is covered in a previous tutorial, it basically tells DirectX how
1377 // to how the scene is displayed on the 2D screen.
SetProjection(float FOVdegrees,float closeClippingPlane,float farClippingPlane,int scrWidth,int scrHeight)1378 void CPanel::SetProjection( float FOVdegrees, float closeClippingPlane, float farClippingPlane, int scrWidth, int scrHeight)
1379 {
1380 	// Set values for matrix
1381 	D3DXMatrixPerspectiveFovLH( &matrixProjection,	// Pointer to matrix
1382 						 D3DXToRadian(FOVdegrees),	// Field of view in degrees
1383 						(float)scrWidth/(float)scrHeight,	// Screen aspect ratio
1384 							   closeClippingPlane,	// Clipping plane
1385 								farClippingPlane);	// Clipping plane
1386 	// Set projection matrix
1387 	m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matrixProjection );
1388 }
1389 
1390 // This method's code is covered in a previous tutorial.
1391 // It sets the position and rotation of the camera
SetView(float Xpos,float Ypos,float Zpos,float Xtarget,float Ytarget,float Ztarget,float Xup,float Yup,float Zup)1392 void CPanel::SetView( float Xpos, float Ypos, float Zpos,			// Cams position
1393 			float Xtarget, float Ytarget, float Ztarget,	// Where it's looking
1394 			float Xup, float Yup, float Zup )				// Camera's up vector (usually 0,1,0)
1395 {
1396 	D3DXMatrixLookAtLH( &matrixView,			// The matrix that we are setting
1397 	&D3DXVECTOR3( Xpos, Ypos, Zpos ),			// Cams position
1398     &D3DXVECTOR3( Xtarget, Ytarget, Ztarget),	// Where it's looking
1399 	&D3DXVECTOR3( Xup, Yup, Zup ));				// This sets the rotation of the camera.
1400 	m_pd3dDevice->SetTransform( D3DTS_VIEW, &matrixView );	// Set matrix
1401 }
1402 
1403 // The rest of the methods are for the world view matrix.
1404 // The world view matrix is used to position, rotate and scale whatever polygons that we
1405 // are about to render.
1406 
1407 // One line in this method! Am I lazy or what!
1408 // This simpley sets the position and rotation values to zero for all 3 axis
ResetWorld(void)1409 void CPanel::ResetWorld(void)
1410 {
1411 	D3DXMatrixIdentity(&matrixWorld);	// Resets the world matrix
1412 }
1413 
1414 // This moves the matrix to a new position
TranslateWorld(float Xpos,float Ypos,float Zpos)1415 void CPanel::TranslateWorld( float Xpos, float Ypos, float Zpos)
1416 {
1417 	// First we reset the temporary matrix
1418 	D3DXMatrixIdentity(&matrixTemp);	// Reset temporary matrix
1419 
1420 	// Now for a new line! D3DXMatrixTranslation()
1421 	// It simply accepts 4 values.
1422 	// The first is the matrix that we want to apply the translation to
1423 	// and the next three are the values to move along X,Y & Z axis
1424 
1425 	// Now translate the temp matrix with the X,Y & Z position
1426 	D3DXMatrixTranslation(&matrixTemp, Xpos, Ypos, Zpos);	// Set the position
1427 
1428 	// Finally combine the new matrix position with the previous state of the matrix
1429 	D3DXMatrixMultiply( &matrixWorld, &matrixTemp,  &matrixWorld);
1430 
1431 	// Now that the matrixWorld matrix stores the old world matrix values with the new
1432 	// positional values, we simply apply the matrix and we are done.
1433 	m_pd3dDevice->SetTransform( D3DTS_WORLD, &matrixWorld );	// Finally set the matrix
1434 }
1435 
1436 
1437 // This rotates the matrix
1438 // This code is very similar to the previous tutorials rotation code
RotateWorld(float Xrot,float Yrot,float Zrot)1439 void CPanel::RotateWorld( float Xrot, float Yrot, float Zrot)		// Rotate world matrix
1440 {
1441 	D3DXMatrixIdentity(&matrixTemp);	// Reset temporary matrix
1442 
1443 	// Now we use the temporary matrix to store the combined rotations of the matrix
1444 
1445 	// Rotate the temp matrix along X
1446 	D3DXMatrixRotationX( &matrixTemp, D3DXToRadian(Xrot) );
1447 	// Multiply the temp matrix with the actual matrix.
1448 	// and store the combination into the actual matrix
1449 	D3DXMatrixMultiply( &matrixWorld, &matrixTemp, &matrixWorld);
1450 
1451 	// Rotate the temp matrix along Y
1452 	D3DXMatrixRotationY( &matrixTemp, D3DXToRadian(Yrot) );
1453 	// Multiply the temp matrix with the actual matrix
1454 	// (Which currently stores the X rotation so far)
1455 	D3DXMatrixMultiply( &matrixWorld, &matrixTemp, &matrixWorld);
1456 
1457 	// Rotate around the temp matrix along Z
1458 	D3DXMatrixRotationZ( &matrixTemp, D3DXToRadian(Zrot) );
1459 	D3DXMatrixMultiply( &matrixWorld, &matrixTemp, &matrixWorld);
1460 	// Multiply the temp matrix with the actual matrix
1461 	// Which currently stores the X and Y rotation
1462 
1463 	// Now matrixTemp matrix stores the old world matrix values with the new rotation values
1464 	// So we simply apply the matrix and we are done.
1465 	m_pd3dDevice->SetTransform( D3DTS_WORLD, &matrixWorld );	// Finally set the matrix
1466 }
1467 
1468 // This rotates the matrix
1469 // This code is very similar to the previous tutorials rotation code
ScaleWorld(float Xsca,float Ysca,float Zsca)1470 void CPanel::ScaleWorld( float Xsca, float Ysca, float Zsca)		// Rotate world matrix
1471 {
1472 	D3DXMatrixIdentity(&matrixTemp);	// Reset temporary matrix
1473 
1474 	// Now we use the temporary matrix to store the combined rotations of the matrix
1475 
1476 	// Rotate the temp matrix along X
1477 	D3DXMatrixScaling( &matrixTemp, Xsca, Ysca, Zsca );
1478 	// Multiply the temp matrix with the actual matrix.
1479 	// and store the combination into the actual matrix
1480 	D3DXMatrixMultiply( &matrixWorld, &matrixTemp, &matrixWorld);
1481 
1482 	// Now matrixTemp matrix stores the old world matrix values with the new rotation values
1483 	// So we simply apply the matrix and we are done.
1484 	m_pd3dDevice->SetTransform( D3DTS_WORLD, &matrixWorld );	// Finally set the matrix
1485 }
1486