1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
class FACES {
public:
int verIndX, verIndY, verIndZ;
int texIndX, texIndY, texIndZ;
} Face;
class VERTICES {
public:
float x;
float y;
float z;
} Vector;
std::ifstream Datei;
Datei.open("C:/2.obj", std::ios::in);
numVert = 0;
numInd = 0;
if(!Datei.good())
{
Datei.close();
MessageBox(HWND_DESKTOP , "Datei konnte nicht geöffnet werden.", "Fehler", MB_OK | MB_ICONWARNING | MB_TASKMODAL);
}
std::vector<VERTICES> Vectoren;
std::vector<FACES> Gesichter;
while(!Datei.eof())
{
std::getline(Datei, Line);
const std::string::size_type Koordinaten = Line.find("v");
const std::string::size_type StrFace = Line.find("f");
if(Koordinaten != std::string::npos)
{
Vector.x = NachFloat(Zeilen(Line, 1));
Vector.y = NachFloat(Zeilen(Line, 2));
Vector.z = NachFloat(Zeilen(Line, 3));
Vectoren.push_back(Vector);
numVert+= 3;
}
else if(StrFace != std::string::npos)
{
size_t x = Line.find("/");
if(x == std::string::npos)
{
Face.verIndX = NachInt(Zeilen(Line, 1));
Face.verIndY = NachInt(Zeilen(Line, 2));
Face.verIndZ = NachInt(Zeilen(Line, 3));
numInd++;
} else if(x != std::string::npos)
{
Line.replace(Line.find("/"), 1, " ");
Face.verIndX = NachInt(Zeilen(Line, 1));
Face.texIndX = NachInt(Zeilen(Line, 2));
Line.replace(Line.find("/"), 1, " ");
Face.verIndY = NachInt(Zeilen(Line, 3));
Face.texIndY = NachInt(Zeilen(Line, 4));
Line.replace(Line.find("/"), 1, " ");
Face.verIndZ = NachInt(Zeilen(Line, 5));
Face.texIndZ = NachInt(Zeilen(Line, 6));
Gesichter.push_back(Face);
numInd++;
}
}
}
Datei.clear();
// DirectX
d3d = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = FALSE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = fnk_Width(0, 'n');
d3dpp.BackBufferHeight = fnk_Height(0, 'n');
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);
d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);
d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);
d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));
d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
d3ddev->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
d3ddev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
d3ddev->SetSamplerState(0, D3DSAMP_MAXANISOTROPY, 16);
d3ddev->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);
d3ddev->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
d3ddev->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(120, 120, 0), 1.0f, 0);
d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->BeginScene();
d3ddev->SetFVF(CUSTOMFVF);
D3DXMATRIX matView;
D3DXVECTOR3 position = D3DXVECTOR3(0.0f, 8.0f, 16.0f);
D3DXVECTOR3 lookat = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
D3DXMatrixLookAtLH(&matView, &position, &lookat, &up);
d3ddev->SetTransform(D3DTS_VIEW, &matView);
d3ddev->CreateVertexBuffer(numVert * sizeof(Vectoren), 0, CUSTOMFVF, D3DPOOL_DEFAULT, &vertexbuffer, 0);
d3ddev->CreateIndexBuffer(sizeof(Gesichter), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &indexbuffer, 0);
void* pVertices;
vertexbuffer->Lock(0, /*numVert * */sizeof(Vectoren), (void**)&pVertices, 0);
memcpy(pVertices, &Vectoren, sizeof(Vectoren));
vertexbuffer->Unlock();
void* pIndices;
indexbuffer->Lock(0, sizeof(Gesichter), reinterpret_cast<void**>(&pIndices), 0);
memcpy(pIndices, &Gesichter, sizeof(Gesichter));
indexbuffer->Unlock();
d3ddev->SetStreamSource(0, vertexbuffer, 0, sizeof(Vectoren));
d3ddev->SetIndices(indexbuffer);
if(FAILED(d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, numVert * 8, 0, numVert * 12)))
{
MessageBox(HWND_DESKTOP , "Vertex konnte nicht gezeichnet werden.", "Fehler", MB_OK | MB_ICONWARNING | MB_TASKMODAL);
}
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
|