0% found this document useful (0 votes)
52 views

Rezolvare:: - de Facut Extrude in Directx

This document provides code for rendering an extruded object in DirectX. It includes functions for initializing DirectX, creating vertex and index buffers, setting materials and lighting, applying transformation matrices, and rendering the scene. Key steps are initializing DirectX, creating vertex and index buffers with geometry data, setting a material and directional light, applying world, view and projection matrices, and rendering the scene with the specified buffers, materials and transformations.

Uploaded by

Taisia Negară
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Rezolvare:: - de Facut Extrude in Directx

This document provides code for rendering an extruded object in DirectX. It includes functions for initializing DirectX, creating vertex and index buffers, setting materials and lighting, applying transformation matrices, and rendering the scene. Key steps are initializing DirectX, creating vertex and index buffers with geometry data, setting a material and directional light, applying world, view and projection matrices, and rendering the scene with the specified buffers, materials and transformations.

Uploaded by

Taisia Negară
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

- De facut extrude in DirectX.

Rezolvare:
#include <windows.h>
#include <tchar.h>
#include "d3d9.h"
#include "d3dx9.h"
#include "mmsystem.h"
#pragma comment(lib, "winmm.lib")

const float d = 2.0f,


l = 0.4f,
w = 0.1f;

HWND hwnd;

IDirect3D9* pDirect3D = NULL;


IDirect3DDevice9* pDirect3DDevice = NULL;
LPDIRECT3DVERTEXBUFFER9 pVertexBuffer = NULL;

LPDIRECT3DINDEXBUFFER9 pIndexBuffer = NULL;


#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)

struct CUSTOMVERTEX
{
float x, y, z; // vertex coordinates
float nx, ny, nz; // normal coordinates
};
D3DMATERIAL9 Material;
D3DLIGHT9 Light;
const int M = 4;

float t;
const int N = 16;
const int NL = 8;

const int NV = N * NL;


const int NI = (NL - 1) * (N - 1) * 2 * 3;

const int MNV = M * NV;


const int MNI = M * NI;

float step = 1.f / (N - 1);

const float Zmax = 2.0f;


float Zstep = Zmax / (NL - 1);
struct point {
float X, Y, Z;
};

point p[M][4] =
{
{
{ 0.f, 3.f, 0.f },
{ 0.f, 1.0f, 0.f },
{ 0.f, -0.f, 0.f },
{ 3.f, 0.f, 0.f }
},
{
{ 0.f, 3.f, 0.f },
{ 0.f, 1.0f, 0.f },
{ 0.f, -0.f, 0.f },
{ -3.f, 0.f, 0.f }
},
{
{ -3.f, 0.f, 0.f },
{ 0.f, 0.0f, 0.f },
{ 0.f, -1.f, 0.f },
{ 0.f, -3.f, 0.f }
},
{
{ 3.f, 0.f, 0.f },
{ 0.f, 0.0f, 0.f },
{ 0.f, -1.f, 0.f },
{ 0.f, -3.f, 0.f }
},

};

float a[4];
float b[4];

D3DXMATRIX MatrixWorldX; // world matrix


D3DXMATRIX MatrixWorldY; // world matrix
D3DXMATRIX MatrixWorldZ; // world matrix
D3DXMATRIX MatrixWorld; // world matrix

const float ca = 2.5f;


const float la = 0.1f;
const float wa = 0.04f;

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;
// Deactivation of culling Direct3D
//pDirect3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
pDirect3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
// Deactivation of ligthing Direct3D
pDirect3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
pDirect3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);

return S_OK;
}

HRESULT InitialVertexBuffer()
{
CUSTOMVERTEX Axes[] =
{ // x y z rhv color
{ -ca, 0.0f, 0.0f, 0x00000000 }, // -X
{ ca, 0.0f, 0.0f, 0x00ff0000 }, // +x
{ ca - la, wa, 0.0f, 0x00ff0000 }, //
{ ca, 0.0f, 0.0f, 0x00ff0000 }, //
{ ca - la, -wa, 0.0f, 0x00ff0000 }, //
{ ca, 0.0f, 0.0f, 0x00ff0000 }, //

{ 0.f, -ca, 0.0f, 0x00000000 }, // -Y


{ 0.f, ca, 0.0f, 0x0000ff00 }, // +Y
{ -wa, ca - la, 0.0f, 0x0000ff00 }, //
{ 0.f, ca, 0.0f, 0x0000ff00 }, //
{ wa, ca - la, 0.0f, 0x0000ff00 }, //
{ 0.f, ca, 0.0f, 0x0000ff00 }, //

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


{ 0.0f, 0.0f, ca, 0x000000ff }, // +Z
{ -wa, 0.0f, ca - la, 0x000000ff }, //
{ 0.0f, 0.0f, ca, 0x000000ff }, //
{ wa, 0.0f, ca - la, 0x000000ff }, //
{ 0.0f, 0.0f, ca, 0x000000ff } //
};
CUSTOMVERTEX Vertexes[] =
{
// x, y, z, nx, ny, nz
{ 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f },
{ 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f },
{ -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f },
{ -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f },

{ -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f },


{ -1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f },
{ -1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f },
{ -1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f },

{ -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f },


{ -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f },
{ 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f },

{ 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f },


{ 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f },
{ 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f },
{ 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f },

};

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

void *pVB = NULL;


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

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


return E_FAIL;
memcpy(pVB, Vertexes, sizeof Vertexes);
pVertexBuffer->Unlock();

const unsigned short di = 18;

unsigned short Indexes[MNI];

int ind = 0;
for (k = 0; k < M; k++)
{
for (i = 0; i < NL - 1; i++)
{
for (j = 0; j < N - 1; j++)
{
Indexes[ind + 0] = di + k * NV + i * N + j;
Indexes[ind + 1] = di + k * NV + (i + 1) * N + j;
Indexes[ind + 2] = di + k * NV + i * N + (j + 1);

Indexes[ind + 3] = di + k * NV + i * N + (j + 1);
Indexes[ind + 4] = di + k * NV + (i + 1) * N + j;
Indexes[ind + 5] = di + k * NV + (i + 1) * N + (j + 1);
ind += 6;
}
}
}

if (FAILED(pDirect3DDevice->CreateIndexBuffer(
MNI * sizeof(unsigned short), 0, D3DFMT_INDEX16,
D3DPOOL_DEFAULT, &pIndexBuffer, NULL)))
return E_FAIL;

void *pIB = NULL;


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

return S_OK;
}

void LightMaterial()
{
ZeroMemory(&Material, sizeof(D3DMATERIAL9));
Material.Diffuse.r = Material.Ambient.r = 0.8f;
Material.Diffuse.g = Material.Ambient.g = 0.8f;
Material.Diffuse.b = Material.Ambient.b = 0.0f;
Material.Diffuse.a = Material.Ambient.a = 1.0f;
pDirect3DDevice->SetMaterial(&Material);

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

Light.Range = 1000.0f;
D3DXVECTOR3 VectorDir = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
D3DXVec3Normalize((D3DXVECTOR3*)&Light.Direction, &VectorDir);
pDirect3DDevice->SetLight(0, &Light);

pDirect3DDevice->LightEnable(0, TRUE);

pDirect3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
pDirect3DDevice->SetRenderState(D3DRS_AMBIENT, FALSE);

void Matrix()
{
D3DXMATRIX MatrixView; // viewport matrix
D3DXMATRIX MatrixProjection; // projection matrix
float Angle = D3DX_PI / 4.0f;

D3DXMatrixRotationX(&MatrixWorldX, Angle);
D3DXMatrixRotationY(&MatrixWorldY, -Angle);
D3DXMatrixMultiply(&MatrixWorld, &MatrixWorldY, &MatrixWorldX);

pDirect3DDevice->SetTransform(D3DTS_WORLD, &MatrixWorld);

// MatrixView
D3DXMatrixLookAtLH(&MatrixView, &D3DXVECTOR3(0.f, 0.f, -28.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 / 10.f, 1.0f,
1.f, 100.f);
pDirect3DDevice->SetTransform(D3DTS_PROJECTION, &MatrixProjection);
}

void Matrix2()
{
// MatrixWorld
UINT Time = timeGetTime() % 10000;
float Angle = Time * (2.f * D3DX_PI) / 5000.f;

// float Angle = 0.0f;

D3DXMatrixRotationX(&MatrixWorldX, Angle);
D3DXMatrixRotationY(&MatrixWorldY, Angle);
D3DXMatrixRotationZ(&MatrixWorldZ, Angle);

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


D3DXMatrixMultiply(&MatrixWorld, &MatrixWorldY, &MatrixWorld);
D3DXMatrixMultiply(&MatrixWorld, &MatrixWorldX, &MatrixWorld);
pDirect3DDevice->SetTransform(D3DTS_WORLD, &MatrixWorld);
}

HRESULT RenderingDirect3D()
{
if (pDirect3DDevice == NULL)
return E_FAIL;
//pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
// D3DCOLOR_XRGB(255, 255, 0), 1.f, 0);
pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(249, 199, 128), 1.f, 0);
// D3DCOLOR_XRGB(60, 100, 150), 1.f, 0);
////chemarea metodei pentru lumini
pDirect3DDevice->BeginScene(); //
//metoda pentru lumina si materie
LightMaterial();
Matrix();
//// desenarea scenei
pDirect3DDevice->SetStreamSource(0, pVertexBuffer, 0, sizeof(CUSTOMVERTEX));
pDirect3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
pDirect3DDevice->SetIndices(pIndexBuffer);

pDirect3DDevice->DrawPrimitive(D3DPT_LINELIST, 0, 9);

Matrix2();

pDirect3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0,
18 + MNV, 0, M * (NL - 1) * (N - 1) * 2);
//pDirect3DDevice->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0,
// 18 + MNV, 0, M * (NL - 1) * (N - 1) * 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);
}

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(NULL_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("Based Window for DirectX"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0, 0,
// CW_USEDEFAULT, 0,
500, 400,
// CW_USEDEFAULT, 0,
NULL,
NULL,
hinstance,
NULL)))
return 0;

MSG msg;
ZeroMemory(&msg, sizeof msg);

if (SUCCEEDED(InitialDirect3D()))
{
InitialVertexBuffer();
ShowWindow(hwnd, SW_SHOWDEFAULT);
UpdateWindow(hwnd);

while (msg.message != WM_QUIT)


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

return msg.wParam;
}

Rezultat:

You might also like