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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
//hier test ich mal was ich so kann :P
#include <Windows.h>
#include <TriBase.h>
#include "InitWindow.h"
#include "Direct3DEnum.h"
#include "InitDirect3D.h"
#include "resource.h"
struct SVertex
{
tbVector3 vPosition;
float x;
float y;
float z;
DWORD dwColor;
tbVector2 vTexture; // Texturkoordinaten
static const DWORD dwFVF;
};
const DWORD SVertex::dwFVF = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1;
struct STriangle
{
tbVector3 vPosition; // Position
tbVector3 vVelocity; // Bewegung (Richtung und Geschwindigkeit)
tbVector3 vRotation; // Rotationszustand
tbVector3 vRotVelocity; // Rotationsbewegung
float fSize; // Größe
SVertex aVertex[4]; // Die drei Vertizes
};
STriangle g_aTriangle[2];
SDirect3DParameters SD3D;
PDIRECT3DTEXTURE9 g_pTexture = NULL; // Die Textur
//////////////////die x,y, und z Werte/////////////////////////////////////////////////
tbResult Render(float fSecsPassed)
{
HRESULT hResult;
tbMatrix mScaling; // Rotationsmatrix
tbMatrix mTranslation; // Translationsmatrix
tbMatrix mWorld; // Vereinende Weltmatrix
// -------------------------------------------------------------
// Den Bildpuffer und den Z-Buffer leeren
if(FAILED(hResult = g_pD3DDevice->Clear(0,
NULL,
D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0, 0, 63),
1.0f,
0)))
{
// Fehler beim Leeren!
MessageBox(g_hWindow, "Fehler beim Leeren der Puffer!",
"Fehler", MB_OK | MB_ICONEXCLAMATION);
TB_ERROR_DIRECTX("g_pD3DDevice->Clear", hResult, TB_STOP);
}
g_pD3DDevice->BeginScene();
// Bilineare Filter mit linearem MIP-Mapping
g_pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
g_pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
g_pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
// Rotations- und Translationsmatrix des Dreiecks erzeugen
mScaling = tbMatrixScaling(tbVector3(0.5f));
mTranslation = tbMatrixTranslation(tbVector3(0.0f, 0.0f, 2.0f));
// Beide Matrizen kombinieren und als Weltmatrix einsetzen
mWorld = mScaling * mTranslation;
g_pD3DDevice->SetTransform(D3DTS_WORLD, (D3DMATRIX*)(&mWorld));
// Nun das Dreieck zeichnen
if(FAILED(hResult = g_pD3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, // Dreiecksliste
2, // 1 Dreieck
g_aTriangle[0].aVertex, // Vertexdaten
sizeof(SVertex)))) // Vertexgröße
{
// Fehler beim Zeichnen!
MessageBox(g_hWindow, "Fehler beim Zeichnen des Dreiecks!",
"Fehler", MB_OK | MB_ICONEXCLAMATION);
TB_ERROR_DIRECTX("g_pD3DDevice->DrawPrimitiveUP", hResult, TB_STOP);
}
// Szene beenden
g_pD3DDevice->EndScene();
// Der große Moment: den Bildpuffer sichtbar machen
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
return TB_OK;
}
tbResult InitScene()
{
tbInit();
HRESULT hResult;
tbColor VertexColor;
float fAspect;
tbMatrix mProjection;
tbResult Result = GetDirect3DParameters(&SD3D);
g_aTriangle[0].aVertex[0].x = -1.0f;
g_aTriangle[0].aVertex[0].y = -1.0f;
g_aTriangle[0].aVertex[1].x = 1.0f;
g_aTriangle[0].aVertex[1].y = -1.0f;
g_aTriangle[0].aVertex[2].x = 1.0f;
g_aTriangle[0].aVertex[2].y = 1.0f;
g_aTriangle[0].aVertex[3].x = -1.0f;
g_aTriangle[0].aVertex[3].y = 1.0f;
if(Result == TB_ERROR)
{
MessageBox(NULL,"Fehler beim Azählen", "Fehler!", MB_OK | MB_ICONEXCLAMATION);
tbExit();
}
else if(Result == TB_CANCELED)
{
tbExit();
}
InitWindow(800,600,"JO! Fenster und so ALTAR!",LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1)));
InitDirect3D(&SD3D, g_hWindow);
// Vertexformat setzen - Positions-, Farb- und Texturkoordinatenangabe
if(FAILED(hResult = g_pD3DDevice->SetFVF(SVertex::dwFVF)))
{
// Fehler beim Setzen des Vertexformats!
TB_ERROR_DIRECTX("g_pD3DDevice->SetFVF", hResult, TB_ERROR);
}
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
g_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
g_pD3DDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE);
// Das Bildseitenverhältnis berechnen
fAspect = (float)(SD3D.VideoMode.Width)
/ (float)(SD3D.VideoMode.Height);
// Die Projektionsmatrix erzeugen
mProjection = tbMatrixProjection(TB_DEG_TO_RAD(90.0f), // Sichtfeld: 90°
fAspect, // Bildseitenverhältnis
0.1f, // Nahe Clipping-Ebene
100.0f); // Ferne Clipping-Ebene
g_pD3DDevice->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)(&mProjection));
// Position
g_aTriangle[0].aVertex[0].vPosition = tbVector3(-1.0f, 1.0f, 1.0f);
g_aTriangle[0].aVertex[1].vPosition = tbVector3(-1.0f,-1.0f, 1.0f);
g_aTriangle[0].aVertex[2].vPosition = tbVector3( 1.0f, 1.0f, 1.0f);
g_aTriangle[0].aVertex[3].vPosition = tbVector3( 1.0f,-1.0f, 1.0f);
for(int iVertex = 0; iVertex < 3; iVertex++)
{
// Farbe
VertexColor.r = 1.0f;
VertexColor.g = 1.0f;
VertexColor.b = 1.0f;
g_aTriangle[0].aVertex[iVertex].dwColor = (DWORD)(VertexColor);
g_aTriangle[1].aVertex[iVertex].dwColor = (DWORD)(VertexColor);
}
// Texturkoordinaten zufällig zwischen -1 und 2 erzeugen
g_aTriangle[0].aVertex[0].vTexture.u = 0.0f;
g_aTriangle[0].aVertex[0].vTexture.v = 1.0f;
g_aTriangle[0].aVertex[1].vTexture.u = 1.0f;
g_aTriangle[0].aVertex[1].vTexture.v = 1.0f;
g_aTriangle[0].aVertex[2].vTexture.u = 1.0f;
g_aTriangle[0].aVertex[2].vTexture.v = 0.0f;
g_aTriangle[0].aVertex[3].vTexture.u = 0.0f;
g_aTriangle[0].aVertex[3].vTexture.v = 0.0f;
// Die Textur laden
if(FAILED(hResult = D3DXCreateTextureFromFileEx(g_pD3DDevice, // Device
"Texture.bmp", // Dateiname
D3DX_DEFAULT, // Breite
D3DX_DEFAULT, // Höhe
D3DX_DEFAULT, // MIP-Maps
0, // Verwendungszweck
D3DFMT_UNKNOWN, // Format
D3DPOOL_MANAGED, // Speicherklasse
D3DX_FILTER_NONE, // Filter
D3DX_DEFAULT, // MIP-Map-Filter
0, // Color-Key
NULL, // Unwichtig
NULL, // Unwichtig
&g_pTexture))) // Die Textur
{
// Fehler!
TB_ERROR_DIRECTX("D3DXCreateTextureFromFileEx", hResult, TB_ERROR);
}
// Und nun die Textur einsetzen
g_pD3DDevice->SetTexture(0, g_pTexture);
return TB_OK;
}
tbResult Move(float fSecsPassed)
{
///hier kommt noch was ;)
return TB_OK;
}
tbResult ExitScene()
{
// Textur deaktivieren und löschen
g_pD3DDevice->SetTexture(0, NULL);
TB_SAFE_RELEASE(g_pTexture);
return TB_OK;
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
char* pcCmdLine,
int iShowCmd)
{
InitScene();
tbDoMessageLoop(Move, Render);
ExitScene();
ExitDirect3D();
ExitWindow();
tbExit();
return 0;
}
|