Directx Material and Texture
Directx Material and Texture
Directx Material and Texture
Laborator Nr.4
la
Grafica pe calculator
Chisinau, 2017
1
Conditii:
Desenarea literei “N” in DirectX cu format 3D, utilizare material, lumina si texture
#include <windows.h>
#include <tchar.h>
#include "d3d9.h"
#include "d3dx9.h"
#include "mmsystem.h"
#pragma comment(lib, "winmm.lib")
struct CUSTOMVERTEX
{
float x, y, z; // vertex coordonates
DWORD color; // vertex color
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
struct CUSTOMVERTEX2
{
float x, y, z; // vertex coordonates
float nx, ny, nz; //vector normal coordonate
};
#define D3DFVF_CUSTOMVERTEX2 (D3DFVF_XYZ|D3DFVF_NORMAL)
D3DLIGHT9 Light;
static int NH = 8;
static int NV = 35;
2
const int NIT = (NH - 1) * (NV - 1) * 2 * 3;
const int NPT = (NH - 1) * (NV - 1) * 2;
struct point2D
{
float x, y;
};
point2D pc[30] = {
//Punctul control pentru bezier 1
{ 1.4f, 2.6f },
{ 0.8f, 2.89f },
{ 0.77f, 1.f },
{ 0.0f, -1.1f },
};
struct point3D
{
3
float x, y, z;
};
point3D pb[1000];
point3D pn[1000];
int fc = 0;
void MyBezier()
{
for (int i = 0; i < 24; i=i+4)
{
float a[4], b[4], t, tstep;
pb[fc].x = pc[i].x;
pb[fc].y = pc[i].y;
pb[fc].z = Z;
t = 0.0f;
for (int i = fc+1; i < NV - 1; i++)
{
t += tstep;
pb[i].x = ((a[0] * t + a[1]) * t + a[2]) * t + a[3];
pb[i].y = ((b[0] * t + b[1]) * t + b[2]) * t + b[3];
pb[i].z = Z;
}
t = 0.0f;
for (int i = fc; i < NV; i++)
{
pn[i].x = -(3.0f * b[0] * t * t + 2.0f * b[1] * t + b[2]);
pn[i].y = 3.0f * a[0] * t * t + 2.0f * a[1] * t + a[2];
/*pn[i].x = (3.0f * b[0] * t * t + 2.0f * b[1] * t + b[2]);
pn[i].y = -(3.0f * a[0] * t * t + 2.0f * a[1] * t + a[2]);*/
pn[i].z = 0.0;
t += tstep;
}
fc = fc + 35;
NV = fc + 36;
}
4
static const int NVT = 10000;
HRESULT InitialDirect3D()
{
if ((pDirect3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
return E_FAIL;
D3DDISPLAYMODE Display;
if (FAILED(pDirect3D->
GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Display)))
return E_FAIL;
D3DPRESENT_PARAMETERS Direct3DParameter;
ZeroMemory(&Direct3DParameter, sizeof Direct3DParameter);
Direct3DParameter.Windowed = TRUE;
Direct3DParameter.SwapEffect = D3DSWAPEFFECT_DISCARD;
Direct3DParameter.BackBufferFormat = Display.Format;
Direct3DParameter.EnableAutoDepthStencil = TRUE;
Direct3DParameter.AutoDepthStencilFormat = D3DFMT_D16;
if (FAILED(pDirect3D->
CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&Direct3DParameter, &pDirect3DDevice)))
return E_FAIL;
/*
if(FAILED(pDirect3D->
CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hvnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&Direct3DParameter, &pDirect3DDevice)))
return E_FAIL;
*/
pDirect3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
return S_OK;
}
HRESULT InitialVertexBuffer()
{
CUSTOMVERTEX Vertexes[18] =
{
// x y z color
{ -AL, 0.0f, 0.0f, 0x00000000 }, // X
{ AL, 0.0f, 0.0f, 0x00ff0000 },
{ AL - AD, AD / 2.0f, 0.0f, 0x00ff0000 },
{ AL, 0.0f, 0.0f, 0x00ff0000 },
{ AL - AD, -AD / 2.0f, 0.0f, 0x00ff0000 },
{ AL, 0.0f, 0.0f, 0x00ff0000 },
5
{ 0.0f, -AL, 0.0f, 0x00000000 }, // Y
{ 0.0f, AL, 0.0f, 0x0000ff00 },
{ AD / 2.0f, AL - AD, 0.0f, 0x0000ff00 },
{ 0.0f, AL, 0.0f, 0x0000ff00 },
{ -AD / 2.0f, AL - AD, 0.0f, 0x0000ff00 },
{ 0.0f, AL, 0.0f, 0x0000ff00 },
if (FAILED(pDirect3DDevice->CreateVertexBuffer(sizeof Vertexes, 0,
D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT,
&pVertexBuffer, NULL)))
return E_FAIL;
pVertexBuffer->Unlock();
CUSTOMVERTEX2 Vertexes2[NVT];
MyBezier();
NV = NV - 35;
float hstep = Z / (NH - 1);
float hcur = 0.0f;
for (int j = 0; j < NH; j++)
{
for (int i = 0; i < NV; i++)
{
Vertexes2[j * NV + i].x = pb[i].x;
Vertexes2[j * NV + i].y = pb[i].y;
Vertexes2[j * NV + i].z = hcur;
if (FAILED(pDirect3DDevice->CreateVertexBuffer(sizeof Vertexes2, 0,
D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT,
&pVertexBuffer2, NULL)))
return E_FAIL;
pVB = NULL;
if (FAILED(pVertexBuffer2->Lock(0, sizeof Vertexes2,
(void**)&pVB, 0)))
6
return E_FAIL;
pVertexBuffer2->Unlock();
int ind = 0;
Indexes[ind++] = j * NV + i;
Indexes[ind++] = (j + 1)* NV + i + 1;
Indexes[ind++] = j * NV + i + 1;
}
}
if (FAILED(pDirect3DDevice->CreateIndexBuffer(
sizeof Indexes, 0, D3DFMT_INDEX16,
D3DPOOL_DEFAULT, &pIndexBuffer, NULL)))
return E_FAIL;
Light.Ambient.r = 0.0f;
Light.Ambient.g = 0.0f;
Light.Ambient.b = 0.0f;
Light.Range = 1000.0f;
D3DXVECTOR3 VectorDir = D3DXVECTOR3(1.0f, 1.0f, 0.0f); // Set the direction that
this light will generate light from
D3DXVec3Normalize((D3DXVECTOR3*)&Light.Direction, &VectorDir);
pDirect3DDevice->SetLight(0, &Light);
pDirect3DDevice->LightEnable(0, TRUE);
7
pDirect3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
pDirect3DDevice->SetRenderState(D3DRS_AMBIENT, FALSE);
}
//lumina înconjurătoare
Material.Ambient.r = 0.0f;
Material.Ambient.g = 0.0f;
Material.Ambient.b = 1.0f;
Material.Ambient.a = 1.0f;
pDirect3DDevice->SetMaterial(&Material);
//lumina înconjurătoare
Material.Ambient.r = 1.0f;
Material.Ambient.g = 0.0f;
Material.Ambient.b = 0.0f;
Material.Ambient.a = 1.0f;
pDirect3DDevice->SetMaterial(&Material2);
void Matrix0()
{
D3DXMATRIX MatrixWorldX;
D3DXMATRIX MatrixWorldY;
D3DXMATRIX MatrixWorldZ;
D3DXMATRIX MatrixWorld =
{
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
8
0.0f, 0.0f, 0.0f, 1.0f
};
// MatrixWorld
UINT Time = timeGetTime() % 5000;
static float Angle = 0.0f;
Angle += 0.01f;
pDirect3DDevice->SetTransform(D3DTS_WORLD, &MatrixWorld);
// MatrixView
D3DXMatrixLookAtLH(&MatrixView, &D3DXVECTOR3(0.f, 0.f, -10.f),
&D3DXVECTOR3(0.f, 0.f, 0.f),
&D3DXVECTOR3(0.f, 1.f, 0.f));
pDirect3DDevice->SetTransform(D3DTS_VIEW, &MatrixView);
// MatrixProjection
D3DXMatrixPerspectiveFovLH(&MatrixProjection, D3DX_PI / 4.f, 1.0f,
1.0f, 100.f);
pDirect3DDevice->SetTransform(D3DTS_PROJECTION,
&MatrixProjection);
}
void Matrix()
{
D3DXMATRIX MatrixWorldX;
D3DXMATRIX MatrixWorldY;
D3DXMATRIX MatrixWorldZ;
D3DXMATRIX MatrixWorld =
{
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
// MatrixWorld
UINT Time = timeGetTime() % 5000;
static float Angle = 0.0f;
Angle += 0.01f;
D3DXMatrixRotationX(&MatrixWorldX, -D3DX_PI / 5.8f);
D3DXMatrixRotationY(&MatrixWorldY, D3DX_PI * 0.74f);
D3DXMatrixRotationY(&MatrixWorldZ, Angle);
9
pDirect3DDevice->SetTransform(D3DTS_WORLD, &MatrixWorld);
// MatrixView
D3DXMatrixLookAtLH(&MatrixView, &D3DXVECTOR3(0.f, 0.f, -10.f),
&D3DXVECTOR3(0.f, 0.f, 0.f),
&D3DXVECTOR3(0.f, 1.f, 0.f));
pDirect3DDevice->SetTransform(D3DTS_VIEW, &MatrixView);
// MatrixProjection
D3DXMatrixPerspectiveFovLH(&MatrixProjection, D3DX_PI / 4.f, 1.f,
1.f, 100.f);
pDirect3DDevice->SetTransform(D3DTS_PROJECTION,
&MatrixProjection);
}
HRESULT RenderingDirect3D()
{
if (pDirect3DDevice == NULL)
return E_FAIL;
pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(50, 50, 50), 1.f, 0);
pDirect3DDevice->BeginScene();
pDirect3DDevice->SetStreamSource(0, pVertexBuffer, 0,
sizeof(CUSTOMVERTEX));
pDirect3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
pDirect3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
pDirect3DDevice->SetRenderState(D3DRS_AMBIENT, TRUE);
Matrix0();
pDirect3DDevice->DrawPrimitive(D3DPT_LINELIST, 0, 9);
pDirect3DDevice->SetStreamSource(0, pVertexBuffer2, 0,
sizeof(CUSTOMVERTEX2));
pDirect3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX2);
pDirect3DDevice->SetIndices(pIndexBuffer);
Matrix();
pDirect3DDevice->EndScene();
10
pDirect3DDevice->Present(NULL, NULL, NULL, NULL);
return S_OK;
}
void DeleteDirect3D()
{
if (pIndexBuffer)
pIndexBuffer->Release();
if (pVertexBuffer != NULL)
pVertexBuffer->Release();
if (pDirect3DDevice)
pDirect3DDevice->Release();
if (pDirect3D)
pDirect3D->Release();
}
WNDCLASSEX windowsclass;
windowsclass.cbSize = sizeof(WNDCLASSEX);
windowsclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
windowsclass.lpfnWndProc = MainWinProc;
windowsclass.cbClsExtra = 0, windowsclass.cbWndExtra = 0;
windowsclass.hInstance = hinstance;
windowsclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowsclass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowsclass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
windowsclass.lpszMenuName = NULL;
windowsclass.lpszClassName = _T("WINDOWSCLASS");
windowsclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
11
if (!RegisterClassEx(&windowsclass))
return 0;
if (!(hwnd = CreateWindowEx(
NULL,
_T("WINDOWSCLASS"),
_T("Litere N - Material si Lumina"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
100, 50,
// CW_USEDEFAULT, 0,
1200, 1000,
// CW_USEDEFAULT, 0,
NULL,
NULL,
hinstance,
NULL)))
return 0;
if (SUCCEEDED(InitialDirect3D()))
{
if (SUCCEEDED(InitialVertexBuffer()))
{
ShowWindow(hwnd, SW_SHOWDEFAULT); // Desenarea ferestrei
UpdateWindow(hwnd); // Reinoirea ferestrei
return msg.wParam;
12
Rezultatul:
13
II. Utiliza textura:
#include <windows.h>
#include <tchar.h>
#include "d3d9.h"
#include "d3dx9.h"
#include "mmsystem.h"
#pragma comment(lib, "winmm.lib")
struct CUSTOMVERTEX
{
float x, y, z; // vertex coordonates
DWORD color; // vertex color
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
struct CUSTOMVERTEX2
{
float x, y, z; // vertex coordonates
float nx, ny, nz; // vertex color
float tu, tv; //texture coordonate
};
#define D3DFVF_CUSTOMVERTEX2 (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)
D3DLIGHT9 Light;
const int NH = 8;
static int NV = 35;
struct point2D
{
float x, y;
};
point2D pc[30] = {
//Punctul control pentru bezier 1
{ 1.4f, 2.6f },
{ 0.8f, 2.89f },
{ 0.77f, 1.f },
{ 0.0f, -1.1f },
15
{ 0.6f, 4.f },
{ 1.4f, 2.6f },
};
struct point3D
{
float x, y, z;
};
point3D pb[1000];
point3D pn[1000];
int fc = 0;
void MyBezier()
{
for (int i = 0; i < 24; i = i + 4)
{
float a[4], b[4], t, tstep;
pb[fc].x = pc[i].x;
pb[fc].y = pc[i].y;
pb[fc].z = Z;
a[0] = -pc[i].x + 3.0f * pc[i + 1].x - 3.0f * pc[i + 2].x + pc[i + 3].x;
a[1] = 3.0f * pc[i].x - 6.0f * pc[i + 1].x + 3.0f * pc[i + 2].x;
a[2] = -3.0f * pc[i].x + 3.0f * pc[i + 1].x;
a[3] = pc[i].x;
b[0] = -pc[i].y + 3.0f * pc[i + 1].y - 3.0f * pc[i + 2].y + pc[i + 3].y;
b[1] = 3.0f * pc[i].y - 6.0f * pc[i + 1].y + 3.0f * pc[i + 2].y;
b[2] = -3.0f * pc[i].y + 3.0f * pc[i + 1].y;
b[3] = pc[i].y;
t = 0.0f;
for (int i = fc + 1; i < NV - 1; i++)
{
t += tstep;
pb[i].x = ((a[0] * t + a[1]) * t + a[2]) * t + a[3];
pb[i].y = ((b[0] * t + b[1]) * t + b[2]) * t + b[3];
pb[i].z = Z;
}
t = 0.0f;
for (int i = fc; i < NV; i++)
{
//pn[i].x = -(3.0f * b[0] * t * t + 2.0f * b[1] * t + b[2]);
//pn[i].y = 3.0f * a[0] * t * t + 2.0f * a[1] * t + a[2];
pn[i].x = (3.0f * b[0] * t * t + 2.0f * b[1] * t + b[2]);
16
pn[i].y = -(3.0f * a[0] * t * t + 2.0f * a[1] * t + a[2]);
pn[i].z = 0.0;
t += tstep;
}
fc = fc + 35;
NV = fc + 36;
}
Direct3DParameter.Windowed = TRUE;
Direct3DParameter.SwapEffect = D3DSWAPEFFECT_DISCARD;
Direct3DParameter.BackBufferFormat = Display.Format;
Direct3DParameter.EnableAutoDepthStencil = TRUE;
Direct3DParameter.AutoDepthStencilFormat = D3DFMT_D16;
if (FAILED(pDirect3D->
CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&Direct3DParameter, &pDirect3DDevice)))
return E_FAIL;
/* или
if(FAILED(pDirect3D->
CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hvnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&Direct3DParameter, &pDirect3DDevice)))
return E_FAIL;
*/
pDirect3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
return S_OK;
}
HRESULT InitialVertexBuffer()
{
17
CUSTOMVERTEX Vertexes[18] =
{
// x y z color
{ -AL, 0.0f, 0.0f, 0x00000000 }, // X
{ AL, 0.0f, 0.0f, 0x00ff0000 },
{ AL - AD, AD / 2.0f, 0.0f, 0x00ff0000 },
{ AL, 0.0f, 0.0f, 0x00ff0000 },
{ AL - AD, -AD / 2.0f, 0.0f, 0x00ff0000 },
{ AL, 0.0f, 0.0f, 0x00ff0000 },
if (FAILED(pDirect3DDevice->CreateVertexBuffer(sizeof Vertexes, 0,
D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT,
&pVertexBuffer, NULL)))
return E_FAIL;
pVertexBuffer->Unlock();
CUSTOMVERTEX2 Vertexes2[NVT];
MyBezier();
NV = NV - 35;
float hstep = Z / (NH - 1);
float hcur = 0.0f;
for (int j = 0; j < NH; j++)
{
for (int i = 0; i < NV; i++)
{
Vertexes2[j * NV + i].x = pb[i].x;
Vertexes2[j * NV + i].y = pb[i].y;
Vertexes2[j * NV + i].z = hcur;
Vertexes2[j * NV + i].nx = pn[i].x;
Vertexes2[j * NV + i].ny = pn[i].y;
Vertexes2[j * NV + i].nz = pn[i].z;
if (i % 3 == 0)
{
Vertexes2[j*NV + i].tu = 0.0f;
18
Vertexes2[j*NV + i].tv = 0.0f;
}
if (i % 3 == 1)
{
Vertexes2[j*NV + i].tu = 0.0f;
Vertexes2[j*NV + i].tv = 1.0f;
}
if (i % 3 == 2)
{
Vertexes2[j*NV + i].tu = 1.0f;
Vertexes2[j*NV + i].tv = 1.0f;
}
}
hcur += hstep;
}
if (FAILED(pDirect3DDevice->CreateVertexBuffer(sizeof Vertexes2, 0,
D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT,
&pVertexBuffer2, NULL)))
return E_FAIL;
pVB = NULL;
if (FAILED(pVertexBuffer2->Lock(0, sizeof Vertexes2,
(void**)&pVB, 0)))
return E_FAIL;
pVertexBuffer2->Unlock();
int ind = 0;
Indexes[ind++] = j * NV + i;
Indexes[ind++] = (j + 1)* NV + i + 1;
Indexes[ind++] = j * NV + i + 1;
}
}
if (FAILED(pDirect3DDevice->CreateIndexBuffer(
sizeof Indexes, 0, D3DFMT_INDEX16,
D3DPOOL_DEFAULT, &pIndexBuffer, NULL)))
return E_FAIL;
19
memcpy(pIB, Indexes, sizeof Indexes);
pIndexBuffer->Unlock();
return S_OK;
}
void Matrix0()
{
D3DXMATRIX MatrixWorldX;
D3DXMATRIX MatrixWorldY;
D3DXMATRIX MatrixWorldZ;
D3DXMATRIX MatrixWorld =
{
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
// MatrixWorld
UINT Time = timeGetTime() % 5000;
static float Angle = 0.0f;
Angle += 0.01f;
pDirect3DDevice->SetTransform(D3DTS_WORLD, &MatrixWorld);
// MatrixView
D3DXMatrixLookAtLH(&MatrixView, &D3DXVECTOR3(0.f, 0.f, -10.f),
&D3DXVECTOR3(0.f, 0.f, 0.f),
&D3DXVECTOR3(0.f, 1.f, 0.f));
pDirect3DDevice->SetTransform(D3DTS_VIEW, &MatrixView);
// MatrixProjection
D3DXMatrixPerspectiveFovLH(&MatrixProjection, D3DX_PI / 4.f, 1.0f,
1.0f, 100.f);
pDirect3DDevice->SetTransform(D3DTS_PROJECTION,
&MatrixProjection);
}
void Matrix()
{
D3DXMATRIX MatrixWorldX;
D3DXMATRIX MatrixWorldY;
D3DXMATRIX MatrixWorldZ;
D3DXMATRIX MatrixWorld =
{
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
20
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
// MatrixWorld
UINT Time = timeGetTime() % 5000;
static float Angle = 0.0f;
Angle += 0.01f;
D3DXMatrixRotationX(&MatrixWorldX, -D3DX_PI / 5.8f);
D3DXMatrixRotationY(&MatrixWorldY, D3DX_PI * 0.74f);
D3DXMatrixRotationY(&MatrixWorldZ, Angle);
pDirect3DDevice->SetTransform(D3DTS_WORLD, &MatrixWorld);
// MatrixView
D3DXMatrixLookAtLH(&MatrixView, &D3DXVECTOR3(0.f, 0.f, -10.f),
&D3DXVECTOR3(0.f, 0.f, 0.f),
&D3DXVECTOR3(0.f, 1.f, 0.f));
pDirect3DDevice->SetTransform(D3DTS_VIEW, &MatrixView);
// MatrixProjection
D3DXMatrixPerspectiveFovLH(&MatrixProjection, D3DX_PI / 4.f, 1.f,
1.f, 100.f);
pDirect3DDevice->SetTransform(D3DTS_PROJECTION,
&MatrixProjection);
}
HRESULT RenderingDirect3D()
{
if (pDirect3DDevice == NULL)
return E_FAIL;
pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(100, 100, 100), 1.f, 0);
pDirect3DDevice->BeginScene();
pDirect3DDevice->SetStreamSource(0, pVertexBuffer, 0,
sizeof(CUSTOMVERTEX));
pDirect3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
pDirect3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
pDirect3DDevice->SetRenderState(D3DRS_AMBIENT, TRUE);
Matrix0();
pDirect3DDevice->DrawPrimitive(D3DPT_LINELIST, 0, 9);
//load textura
//Seteza textura
pDirect3DDevice->SetTexture(0, g_pTexture);
21
pDirect3DDevice->SetStreamSource(0, pVertexBuffer2, 0,
sizeof(CUSTOMVERTEX2));
pDirect3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX2);
pDirect3DDevice->SetIndices(pIndexBuffer);
Matrix();
pDirect3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
pDirect3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, NVT, 0, NVT*2);
pDirect3DDevice->EndScene();
return S_OK;
}
void DeleteDirect3D()
{
if (pIndexBuffer)
pIndexBuffer->Release();
if (pVertexBuffer != NULL)
pVertexBuffer->Release();
if (pDirect3DDevice)
pDirect3DDevice->Release();
if (pDirect3D)
pDirect3D->Release();
}
22
int WINAPI WinMain(
HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX windowsclass;
windowsclass.cbSize = sizeof(WNDCLASSEX);
windowsclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
windowsclass.lpfnWndProc = MainWinProc;
windowsclass.cbClsExtra = 0, windowsclass.cbWndExtra = 0;
windowsclass.hInstance = hinstance;
windowsclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowsclass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowsclass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
windowsclass.lpszMenuName = NULL;
windowsclass.lpszClassName = _T("WINDOWSCLASS");
windowsclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&windowsclass))
return 0;
if (!(hwnd = CreateWindowEx(
NULL,
_T("WINDOWSCLASS"),
_T("LITERE N - TEXTURE"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
100, 50,
// CW_USEDEFAULT, 0,
1200, 1000,
// CW_USEDEFAULT, 0,
NULL,
NULL,
hinstance,
NULL)))
return 0;
if (SUCCEEDED(InitialDirect3D()))
{
if (SUCCEEDED(InitialVertexBuffer()))
{
ShowWindow(hwnd, SW_SHOWDEFAULT); // Desenarea ferestrei
UpdateWindow(hwnd); // Reinoirea ferestrei
23
RenderingDirect3D();
}
}
}
return msg.wParam;
24
Rezultatul:
25