void InitGraphics(void)
{
D3DXVECTOR3 norm;
LPCUSTOMVERTEX t_vert;
TRIANGLE t[1];
D3DXCreateFont( d3ddev,
25,
12,
FW_BOLD,
1,
FALSE,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE,
"Time New Roman",
&g_pFont );
D3DXCreateSprite( d3ddev, &g_pFontSprite );
// Once initilizing the TRIANGLE the normals are computed automaticaly
t[0] = TRIANGLE( D3DXVECTOR3(1.0f, -1.0f, 0.0f), D3DXVECTOR3(-1.0f, -1.0f, 0.0f), D3DXVECTOR3(1.0f, 1.0f, 0.0f) );
t[1] = TRIANGLE( D3DXVECTOR3(-1.0f, -1.0f, 0.0f), D3DXVECTOR3(-1.0f, 1.0f, 0.0f), D3DXVECTOR3(1.0f, 1.0f, 0.0f) );
d3ddev->CreateVertexBuffer( 6*sizeof(CUSTOMVERTEX),
0,
NORMAL_FVF,
D3DPOOL_MANAGED,
&t_buffer,
NULL );
t_buffer->Lock( 0, 0, (void**)&t_vert, 0 );
t_vert[0] = CUSTOMVERTEX( t[0].p0, t[0].n0 );
t_vert[1] = CUSTOMVERTEX( t[0].p1, t[0].n1 );
t_vert[2] = CUSTOMVERTEX( t[0].p2, t[0].n2 );
t_vert[3] = CUSTOMVERTEX( t[1].p0, t[1].n0 );
t_vert[4] = CUSTOMVERTEX( t[1].p1, t[1].n1 );
t_vert[5] = CUSTOMVERTEX( t[1].p2, t[1].n2 );
t_buffer->Unlock();
D3DXCreateBox(d3ddev, 1.0f, 1.0f, 1.0f, &meshSphere, NULL);
if( FAILED( D3DXCreateTextureFromFile(d3ddev, "Metal.png", &texSphere) ) ) {
MessageBox(m_hWnd, "Could not load texture file Metal.png", 0, 0);
}
}
...
void RenderFrame(float timeDelta)
{
RECT rc;
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(100, 100, 100), 1.0f, 0);
d3ddev->BeginScene();
{
D3DXMATRIX matRotate;
D3DXMATRIX matTrans;
static float angle = 0.0f; angle+= 1.0f;
D3DXMatrixRotationX( &matRotate, D3DXToRadian(angle) );
// Draw the box
d3ddev->SetTransform( D3DTS_WORLD, &(matRotate) );
d3ddev->SetTexture(0, texSphere);
meshSphere->DrawSubset(0);
g_pFontSprite->Begin(D3DXSPRITE_ALPHABLEND | D3DXSPRITE_SORT_TEXTURE);
{
SetRect( &rc, 0, 0, 50, 20 );
g_pFont->DrawTextA( g_pFontSprite, "Direct3D", -1, &rc, DT_NOCLIP, 0xaaff0000 );
}
g_pFontSprite->End();
}
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
...
void InitLights(UINT uLight)
{
D3DLIGHT9 light;
D3DMATERIAL9 mat;
ZeroMemory( &light, sizeof(D3DLIGHT9) );
light.Type = D3DLIGHT_DIRECTIONAL;
light.Diffuse = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f);
light.Direction = D3DXVECTOR3(1.0f, 0.0f, 1.0f);
d3ddev->SetLight(0, &light);
d3ddev->LightEnable(0, true);
mat.Ambient = mat.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
d3ddev->SetMaterial(&mat);
}
...
HRESULT InitDirect3D()
{
D3DPRESENT_PARAMETERS d3dpp;
D3DCAPS9 caps;
HRESULT hr;
int vp;
d3d = Direct3DCreate9(D3D_SDK_VERSION);
if( NULL == d3d )
{
::MessageBox( m_hWnd, "Direct3DCreate9() - Failed.", 0, 0 );
return E_FAIL;
}
d3d->GetDeviceCaps( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps );
if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
{
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
}
else
{
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
}
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.Windowed = WINDOWED;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = m_hWnd;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
d3dpp.Flags = 0;
hr = d3d->CreateDevice(
D3DADAPTER_DEFAULT, // Main adapter
D3DDEVTYPE_HAL, // Using the hardware abstaction layer. Use D3DDEVTYPE_REF for software
m_hWnd, // Handle to our main window
vp, // Our supported vertex processing
&d3dpp, // Address of our present parameters struc
&d3ddev); // Address to our device pointer
if( FAILED(hr) )
{
::MessageBox(m_hWnd, "CreateDevice() - Failed.", 0, 0 );
return E_FAIL;
}
d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);
d3ddev->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));
return hr;
}
Texture won't get displayed !
Hi folks,
I've been working this for like 2 hours and I'm giving up. It seams so simple but it does not work, I just want to load a texture file "Metal.png" to a created mesh with D3DXCreateBox and It won't display ! When I load the texture everything is OK no error from Direct3D, then when I used SetTexture(0, texSphere), my mesh gets darker than supposed but no texture... I'll post a sample of my codes, if anyone can help solve that I will be very, very grateful.
TIA,
Max.
I am not sure what CUSTOMVERTEX is, but if the constructor takes a position and normal only, then I am doubting it has texture coords too.
MSDN Texture Coordinates
Regards,
ViLiO
MSDN Texture Coordinates
Regards,
ViLiO
Hi there,
Thanks for your answer. Actually what I was trying to do is apply the texture to the box (which was originaly a sphere, hence the meshSphere name), but I have tried to apply my texture to a created shape and it worked correctly ! But then, impossible to apply to the box... I tweaked my code a little, but could it be an issue about the material applied to the box or something like that ?
Here is what CUSTOMVERTEX is (wich changed name to Vertex)
And again my new code...
TIA,
Max.
Thanks for your answer. Actually what I was trying to do is apply the texture to the box (which was originaly a sphere, hence the meshSphere name), but I have tried to apply my texture to a created shape and it worked correctly ! But then, impossible to apply to the box... I tweaked my code a little, but could it be an issue about the material applied to the box or something like that ?
Here is what CUSTOMVERTEX is (wich changed name to Vertex)
// CDirect3D.h#include <D3dx9core.h>#define DEFAULT_FVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)// Some colorsconst D3DXCOLOR WHITE D3DCOLOR_XRGB(255, 255, 255);const D3DXCOLOR BLACK D3DCOLOR_XRGB(0, 0, 0);const D3DXCOLOR RED D3DCOLOR_XRGB(255, 0, 0);const D3DXCOLOR GREEN D3DCOLOR_XRGB(0, 255, 0);const D3DXCOLOR BLUE D3DCOLOR_XRGB(0, 0, 255);const D3DXCOLOR YELLOW D3DCOLOR_XRGB(255, 255, 0);const D3DXCOLOR CYAN D3DCOLOR_XRGB(0, 255, 255);const D3DXCOLOR MAGENTA D3DCOLOR_XRGB(255, 0, 255);typedef struct Vertex {public: Vertex() {}; Vertex( float _X, float _Y, float _Z, float _nX, float _nY, float _nZ, float _U, float _V ); Vertex( D3DXVECTOR3 p, D3DXVECTOR3 n, float _U, float _V ); ~Vertex() {}; float X, Y, Z; float nX, nY, nZ; float U, V;} Vertex, *LPVertex;// Vertex Declarations -------------------------------------------------------------- //Vertex::Vertex( float _X, float _Y, float _Z, float _nX, float _nY, float _nZ, float _U, float _V ){ X = _X; Y = _Y; Z = _Z; nX = _nX; nY = _nY; nZ = _nZ; U = _U; V = _V;}Vertex::Vertex(D3DXVECTOR3 p, D3DXVECTOR3 n, float _U, float _V ){ X = p.x; Y = p.y; Z = p.z; nX = n.x; nY = n.y; nZ = n.z; U = _U; V = _V;}
And again my new code...
// main.cpp#include "main.h"#include "CDirect3D.h"HRESULT InitDirect3D(){ D3DPRESENT_PARAMETERS d3dpp; D3DCAPS9 caps; HRESULT hr; int vp; d3d = Direct3DCreate9(D3D_SDK_VERSION); if( NULL == d3d ) { ::MessageBox( m_hWnd, "Direct3DCreate9() - Failed.", 0, 0 ); return E_FAIL; } d3d->GetDeviceCaps( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps ); if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT ) { vp = D3DCREATE_HARDWARE_VERTEXPROCESSING; strcat_s(szSettings, "Vertex Processing: Hardware\n"); } else { vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING; strcat_s(szSettings, "Vertex Processing: Software\n"); } ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.BackBufferWidth = SCREEN_WIDTH; d3dpp.BackBufferHeight = SCREEN_HEIGHT; d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; d3dpp.BackBufferCount = 1; d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; d3dpp.MultiSampleQuality = 0; d3dpp.Windowed = WINDOWED; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.hDeviceWindow = m_hWnd; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; d3dpp.Flags = 0; hr = d3d->CreateDevice( D3DADAPTER_DEFAULT, // Main adapter D3DDEVTYPE_HAL, // Using the hardware abstaction layer. Use D3DDEVTYPE_REF for software m_hWnd, // Handle to our main window vp, // Our supported vertex processing &d3dpp, // Address of our present parameters struc &d3ddev); // Address to our device pointer if( FAILED(hr) ) { ::MessageBox(m_hWnd, "CreateDevice() - Failed.", 0, 0 ); return E_FAIL; } d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE); d3ddev->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE); d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50)); d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE); return hr;}void RenderFrame(float timeDelta){ RECT rc; d3ddev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); d3ddev->BeginScene(); { D3DXMATRIX matRotate; D3DXMATRIX matTrans; static float angle = 0.0f; angle+= 1.0f; D3DXMatrixRotationX( &matRotate, D3DXToRadian(angle) ); D3DXMatrixTranslation( &matTrans, 0.0f, 0.0f, 0.0f ); // Render the scene here // Draw the square d3ddev->SetStreamSource( 0, t_buffer, 0, sizeof( Vertex ) ); d3ddev->SetFVF(DEFAULT_FVF); d3ddev->SetTransform( D3DTS_WORLD, &(matTrans) ); d3ddev->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 ); // Draw the box d3ddev->SetTransform( D3DTS_WORLD, &(matRotate) ); meshSphere->DrawSubset(0); g_pFontSprite->Begin(D3DXSPRITE_ALPHABLEND | D3DXSPRITE_SORT_TEXTURE); { SetRect( &rc, 0, 0, 50, 20 ); g_pFont->DrawTextA( g_pFontSprite, szSettings, -1, &rc, DT_NOCLIP, 0xaaff0000 ); } g_pFontSprite->End(); } d3ddev->EndScene(); d3ddev->Present(NULL, NULL, NULL, NULL);}void InitGraphics(void){ D3DXVECTOR3 norm; Vertex* t_vert; D3DXCreateFont( d3ddev, 25, 12, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Time New Roman", &g_pFont ); D3DXCreateSprite( d3ddev, &g_pFontSprite ); d3ddev->CreateVertexBuffer( 4*sizeof(Vertex), 0, DEFAULT_FVF, D3DPOOL_MANAGED, &t_buffer, NULL ); t_buffer->Lock( 0, 0, (void**)&t_vert, 0 ); t_vert[0] = Vertex(1.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0, 1); t_vert[1] = Vertex(1.0f, -1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1, 0); t_vert[2] = Vertex(-1.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1, 0); t_vert[3] = Vertex(-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0, 1); t_buffer->Unlock(); D3DXCreateBox(d3ddev, 1.0f, 1.0f, 1.0f, &meshSphere, NULL); if( FAILED( D3DXCreateTextureFromFile(d3ddev, "Metal.bmp", &texSphere) ) ) { MessageBox(m_hWnd, "Could not load texture file Metal.png", 0, 0); } d3ddev->SetTexture(0, texSphere);}void InitLights(UINT uLight){ D3DLIGHT9 light; // create the light struct D3DMATERIAL9 mat; ZeroMemory(&light, sizeof(light)); // clear out the light struct for use light.Type = D3DLIGHT_DIRECTIONAL; // make the light type 'directional light' light.Diffuse.r = 0.5f; // .5 red light.Diffuse.g = 0.5f; // .5 green light.Diffuse.b = 0.5f; // .5 blue light.Diffuse.a = 1.0f; // full alpha (we'll get to that soon) D3DVECTOR vecDirection = {0.0f, 0.0f, 1.0f}; // the direction of the light light.Direction = vecDirection; // set the direction d3ddev->SetLight(0, &light); // send the light struct properties to light #0 d3ddev->LightEnable(0, TRUE); // turn on light #0 ZeroMemory( &mat, sizeof(D3DMATERIAL9) ); mat.Ambient.a = mat.Diffuse.a = 1.0f; mat.Ambient.r = mat.Diffuse.r = 1.0f; mat.Ambient.g = mat.Diffuse.g = 1.0f; mat.Ambient.b = mat.Diffuse.b = 1.0f; d3ddev->SetMaterial(&mat);}
TIA,
Max.
The problem is that D3DXCreateBox doesn't create a mesh with texture coords. It creates one with FVF of D3DFVF_XYZ | D3DFVF_NORMAL. So you'd need to clone the mesh into a mesh with an FVF that has texture coords (like D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1).
Even then though, the cloning won't generate the texture coords for you, it just reserves the space for them. You will have to lock that new meshes' vertex buffer and write in the texture coords yourself.
Good Luck,
ViLiO
Even then though, the cloning won't generate the texture coords for you, it just reserves the space for them. You will have to lock that new meshes' vertex buffer and write in the texture coords yourself.
Good Luck,
ViLiO
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement