You are on page 1of 25

Universitatea de Stat din Moldova

Facultatea de “Matematica si Informatica”

Laborator Nr.4
la
Grafica pe calculator

Tema: Desenarea literei “N” in DirectX cu format 3D,


utilizare material, lumina si texture

A efectuat: Ho Ngoc Trung


A verificat: Ghenadie Marin

Chisinau, 2017
1
Conditii:
Desenarea literei “N” in DirectX cu format 3D, utilizare material, lumina si texture

I. Utiliza Material si lumina

#include <windows.h>
#include <tchar.h>

#include "d3d9.h"

#include "d3dx9.h"

#include "mmsystem.h"
#pragma comment(lib, "winmm.lib")

#pragma comment (lib, "d3d9.lib")


#pragma comment (lib, "d3dx9.lib")

HWND hwnd; // Descriptorul ferestrei

IDirect3D9* pDirect3D = NULL;


IDirect3DDevice9* pDirect3DDevice = NULL;

struct CUSTOMVERTEX
{
float x, y, z; // vertex coordonates
DWORD color; // vertex color
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)

LPDIRECT3DVERTEXBUFFER9 pVertexBuffer = NULL;

struct CUSTOMVERTEX2
{
float x, y, z; // vertex coordonates
float nx, ny, nz; //vector normal coordonate
};
#define D3DFVF_CUSTOMVERTEX2 (D3DFVF_XYZ|D3DFVF_NORMAL)

LPDIRECT3DVERTEXBUFFER9 pVertexBuffer2 = NULL;

LPDIRECT3DINDEXBUFFER9 pIndexBuffer = NULL;

D3DMATERIAL9 Material, Material2;

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;

const float Z = 1.0f;


const DWORD COL = 0X00ff00ff;

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 },

//Punctul control pentru bezier 2


{ 0.0f, -1.1f },
{ -0.6f, -0.49f },
{ -0.6f, 1.f },
{ -0.8f, 1.5f },

//Punctul control pentru bezier 3


{ -0.8f, 1.5f },
{ -1.0f, -0.25f },
{ -3.f, -2.5f },
{ -2.4f, 0.0f },

//Punctul control pentru bezier 4


{ -2.4f, 0.0f },
{ -2.74f, -1.8f },
{ -0.85f, 0.5f },
{ -1.f, 3.f },

//Punctul control pentru bezier 5


{ -1.f, 3.f },
{ -0.5f, 2.5f },
{ -0.3f, 1.95f },
{ 0.0f, 0.0f },

//Punctul control pentru bezier 6


{ 0.f, 0.f },
{ 0.4f, 1.f },
{ 0.6f, 4.f },
{ 1.4f, 2.6f },

};

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;

tstep = 1.0f / (35 - 1);

pb[fc].x = pc[i].x;
pb[fc].y = pc[i].y;
pb[fc].z = Z;

pb[NV - 1].x = pc[i+3].x;


pb[NV - 1].y = pc[i+3].y;
pb[NV - 1].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]);
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;
}

const float AL = 4.0f;


const float AD = 0.2f;

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;
*/

// Deactivation of culling Direct3D


//pDirect3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
pDirect3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);

// Deactivation of ligthing Direct3D


pDirect3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

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 },

{ 0.0f, 0.0f, -AL, 0x00000000 }, // Z


{ 0.0f, 0.0f, AL, 0x000000ff },
{ 0.0f, AD / 2.0f, AL - AD, 0x000000ff },
{ 0.0f, 0.0f, AL, 0x000000ff },
{ 0.0f, -AD / 2.0f, AL - AD, 0x000000ff },
{ 0.0f, 0.0f, AL, 0x000000ff }
};

if (FAILED(pDirect3DDevice->CreateVertexBuffer(sizeof Vertexes, 0,
D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT,
&pVertexBuffer, NULL)))
return E_FAIL;

void *pVB = NULL;


if (FAILED(pVertexBuffer->Lock(0, sizeof Vertexes,
(void**)&pVB, 0)))
return E_FAIL;

memcpy(pVB, Vertexes, sizeof Vertexes);

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; // coordonate vector normal


Vertexes2[j * NV + i].ny = pn[i].y; // coordonate vector normal
Vertexes2[j * NV + i].nz = pn[i].z; // coordonate vector normal
}
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)))

6
return E_FAIL;

memcpy(pVB, Vertexes2, sizeof Vertexes2);

pVertexBuffer2->Unlock();

unsigned short Indexes[30000];

int ind = 0;

for (int j = 0; j < NH - 1; j++)


{
hcur += hstep;
for (int i = 0; i < NV - 1; i++)
{
Indexes[ind++] = j * NV + i;
Indexes[ind++] = (j + 1)* NV + i;
Indexes[ind++] = (j + 1)* NV + i + 1;

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;

void *pIB = NULL;


if (FAILED(pIndexBuffer->Lock(0, sizeof Indexes,
(void**)&pIB, 0)))
return E_FAIL;
memcpy(pIB, Indexes, sizeof Indexes);
pIndexBuffer->Unlock();
return S_OK;
}

void LightSet() // Seteaza lumina


{
ZeroMemory(&Light, sizeof(D3DLIGHT9));
Light.Type = D3DLIGHT_DIRECTIONAL;
Light.Diffuse.r = 1.0f;
Light.Diffuse.g = 1.0f;
Light.Diffuse.b = 1.0f;

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);
}

void MaterialIntern() //Seteaza material pentru suprafata interior


{
ZeroMemory(&Material, sizeof(D3DMATERIAL9));

//Seteaza lumina difuză


Material.Diffuse.r = 0.0f;
Material.Diffuse.g = 0.0f;
Material.Diffuse.b = 1.0f;
Material.Diffuse.a = 1.0f;

//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);

void MaterialExtern() //Seteaza material pentru suprafata exterior


{
ZeroMemory(&Material2, sizeof(D3DMATERIAL9));
//Seteaza lumina difuză
Material2.Diffuse.r = Material2.Ambient.r = 1.0f;
Material2.Diffuse.g = Material2.Ambient.g = 0.0f;
Material2.Diffuse.b = Material2.Ambient.b = 0.0f;
Material2.Diffuse.a = Material2.Ambient.a = 1.0f;

//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
};

D3DXMATRIX MatrixView; // viewport matrix


D3DXMATRIX MatrixProjection; // projection matrix

// 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);

D3DXMatrixMultiply(&MatrixWorld, &MatrixWorldY, &MatrixWorldX);

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
};

D3DXMATRIX MatrixView; // viewport matrix


D3DXMATRIX MatrixProjection; // projection matrix

// 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);

D3DXMatrixMultiply(&MatrixWorld, &MatrixWorldY, &MatrixWorldX);

D3DXMatrixRotationY(&MatrixWorldZ, Angle);

D3DXMatrixMultiply(&MatrixWorld, &MatrixWorldZ, &MatrixWorld);

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();

LightSet(); //seteaza lumina

MaterialExtern(); //seteaza material suprafata exterior


//Desena suprafara exterior
pDirect3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
pDirect3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, NVT, 0, NVT*2);

MaterialIntern();//seteaza material suprafata exterior


//Desena suprafara exterior
pDirect3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
pDirect3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, NVT, 0, NVT*2);

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();
}

LRESULT CALLBACK MainWinProc(


HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
switch (msg)
{
//
case WM_PAINT:
RenderingDirect3D();
ValidateRect(hwnd, NULL);
break;
//
case WM_DESTROY:
DeleteDirect3D();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}

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);

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;

MSG msg; // Identificatoru mesajului


ZeroMemory(&msg, sizeof msg);

if (SUCCEEDED(InitialDirect3D()))
{
if (SUCCEEDED(InitialVertexBuffer()))
{
ShowWindow(hwnd, SW_SHOWDEFAULT); // Desenarea ferestrei
UpdateWindow(hwnd); // Reinoirea ferestrei

while (msg.message != WM_QUIT)


{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
RenderingDirect3D();
}
}
}

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")

#pragma comment (lib, "d3d9.lib")


#pragma comment (lib, "d3dx9.lib")

HWND hwnd; // Descriptorul ferestrei

IDirect3D9* pDirect3D = NULL;


IDirect3DDevice9* pDirect3DDevice = NULL;

struct CUSTOMVERTEX
{
float x, y, z; // vertex coordonates
DWORD color; // vertex color
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)

LPDIRECT3DVERTEXBUFFER9 pVertexBuffer = NULL;

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)

LPDIRECT3DVERTEXBUFFER9 pVertexBuffer2 = NULL;


IDirect3DTexture9* g_pTexture;
14
LPDIRECT3DINDEXBUFFER9 pIndexBuffer = NULL;

D3DMATERIAL9 Material, Material2;

D3DLIGHT9 Light;

const int NH = 8;
static int NV = 35;

const int NVT = NV * NH;


const int NIT = (NH - 1) * (NV - 1) * 2 * 3;
const int NPT = (NH - 1) * (NV - 1) * 2;

const float Z = 1.0f;


const DWORD COL = 0X00ff00ff;

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 },

//Punctul control pentru bezier 2


{ 0.0f, -1.1f },
{ -0.6f, -0.49f },
{ -0.6f, 1.f },
{ -0.8f, 1.5f },

//Punctul control pentru bezier 3


{ -0.8f, 1.5f },
{ -1.0f, -0.25f },
{ -3.f, -2.5f },
{ -2.4f, 0.0f },

//Punctul control pentru bezier 4


{ -2.4f, 0.0f },
{ -2.74f, -1.8f },
{ -0.85f, 0.5f },
{ -1.f, 3.f },

//Punctul control pentru bezier 5


{ -1.f, 3.f },
{ -0.5f, 2.5f },
{ -0.3f, 1.95f },
{ 0.0f, 0.0f },

//Punctul control pentru bezier 6


{ 0.f, 0.f },
{ 0.4f, 1.f },

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;

tstep = 1.0f / (35 - 1);

pb[fc].x = pc[i].x;
pb[fc].y = pc[i].y;
pb[fc].z = Z;

pb[NV - 1].x = pc[i + 3].x;


pb[NV - 1].y = pc[i + 3].y;
pb[NV - 1].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;
}

const float AL = 4.0f;


const float AD = 0.2f;
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;
*/

// Deactivation of culling Direct3D


//pDirect3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
pDirect3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);

// Deactivation of ligthing Direct3D


pDirect3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

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 },

{ 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 },

{ 0.0f, 0.0f, -AL, 0x00000000 }, // Z


{ 0.0f, 0.0f, AL, 0x000000ff },
{ 0.0f, AD / 2.0f, AL - AD, 0x000000ff },
{ 0.0f, 0.0f, AL, 0x000000ff },
{ 0.0f, -AD / 2.0f, AL - AD, 0x000000ff },
{ 0.0f, 0.0f, AL, 0x000000ff }
};

if (FAILED(pDirect3DDevice->CreateVertexBuffer(sizeof Vertexes, 0,
D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT,
&pVertexBuffer, NULL)))
return E_FAIL;

void *pVB = NULL;


if (FAILED(pVertexBuffer->Lock(0, sizeof Vertexes,
(void**)&pVB, 0)))
return E_FAIL;

memcpy(pVB, Vertexes, sizeof Vertexes);

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;

memcpy(pVB, Vertexes2, sizeof Vertexes2);

pVertexBuffer2->Unlock();

unsigned short Indexes[30000];

int ind = 0;

for (int j = 0; j < NH - 1; j++)


{
hcur += hstep;
for (int i = 0; i < NV - 1; i++)
{
Indexes[ind++] = j * NV + i;
Indexes[ind++] = (j + 1)* NV + i;
Indexes[ind++] = (j + 1)* NV + i + 1;

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;

void *pIB = NULL;


if (FAILED(pIndexBuffer->Lock(0, sizeof Indexes,
(void**)&pIB, 0)))
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
};

D3DXMATRIX MatrixView; // viewport matrix


D3DXMATRIX MatrixProjection; // projection matrix

// 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);

D3DXMatrixMultiply(&MatrixWorld, &MatrixWorldY, &MatrixWorldX);

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
};

D3DXMATRIX MatrixView; // viewport matrix


D3DXMATRIX MatrixProjection; // projection matrix

// 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);

D3DXMatrixMultiply(&MatrixWorld, &MatrixWorldY, &MatrixWorldX);

D3DXMatrixRotationY(&MatrixWorldZ, Angle);

D3DXMatrixMultiply(&MatrixWorld, &MatrixWorldZ, &MatrixWorld);

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

D3DXCreateTextureFromFile(pDirect3DDevice, "wood.jpg", &g_pTexture);

//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();

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();
}

LRESULT CALLBACK MainWinProc(


HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
switch (msg)
{
//
case WM_PAINT:
RenderingDirect3D();
ValidateRect(hwnd, NULL);
break;
//
case WM_DESTROY:
DeleteDirect3D();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}

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;

MSG msg; // Identificatoru mesajului


ZeroMemory(&msg, sizeof msg);

if (SUCCEEDED(InitialDirect3D()))
{
if (SUCCEEDED(InitialVertexBuffer()))
{
ShowWindow(hwnd, SW_SHOWDEFAULT); // Desenarea ferestrei
UpdateWindow(hwnd); // Reinoirea ferestrei

while (msg.message != WM_QUIT)


{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else

23
RenderingDirect3D();
}
}
}

return msg.wParam;

24
Rezultatul:

25

You might also like