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
|
#include <Windows.h>
#include <TriBase.h>
#include "..\\..\\Beispiele\\Allgemeines\\InitWindow.h"
#include "..\\..\\Beispiele\\Allgemeines\\Direct3DEnum.h"
#include "..\\..\\Beispiele\\Allgemeines\\InitDirect3D.h"
#include "Resource.h"
//globale Variablen
float g_fTime= 0.0f;
SDirect3DParameters g_Direct3DParameters;
struct SVertex
{
tbVector3 vPosition; //Vertex Coord
DWORD dwColor; //Vertex Farbe
static const DWORD dwFVF;
};
SVertex g_aTriangleVertex[3];//bezug auf die struktur svertex
const DWORD SVertex::dwFVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;
tbResult InitScene()
{
HRESULT hResult;
if(FAILED(hResult = g_pD3DDevice->SetFVF(SVertex::dwFVF)))
{
TB_ERROR_DIRECTX("g_pD3DDevice->SetFVF", hResult, TB_ERROR);
}
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE); //Beleuchtung aus
g_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); //Culling aus
g_pD3DDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE); //Dithering für bessere Farbverläufe
float fAspect = (float)(g_Direct3DParameters.VideoMode.Width) / (float)(g_Direct3DParameters.VideoMode.Height);
//Projektionsmatrix erzrugen
tbMatrix mProjection = tbMatrixProjection(TB_DEG_TO_RAD(90.0f), fAspect, 0.0f, 100.0f);
//und schliesslich einsetzen
g_pD3DDevice->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*) (&mProjection));
//Dreieck initialisieren
//#####################################################################################################
g_aTriangleVertex[0].vPosition = tbVector3( 0.0f, 1.0f, 1.0f);
g_aTriangleVertex[1].vPosition = tbVector3( 1.0f, -1.0f, 1.0f);
g_aTriangleVertex[2].vPosition = tbVector3( -1.0f, 0.0f, 1.0f);
g_aTriangleVertex[0].dwColor = tbColor(1.0f, 0.0f, 0.0f);
g_aTriangleVertex[1].dwColor = tbColor(0.0f, 1.0f, 0.0f);
g_aTriangleVertex[2].dwColor = tbColor(0.0f, 0.0f, 1.0f);
return TB_OK;
}
tbResult Render(float fNumSecsPassed)
{
HRESULT hResult;
tbMatrix mRotation = tbMatrixRotationY(TB_DEG_TO_RAD(g_fTime * 90.0f));
tbMatrix mTranslation = tbMatrixTranslation(tbVector3(0.0f, 0.0f, 2.0f));
tbMatrix mWorld = mRotation * mTranslation;
g_pD3DDevice->SetTransform(D3DTS_WORLD, (D3DMATRIX*) (&mWorld));
//Bildpuffer leeren
if(FAILED(hResult = g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 63, 63), 1.0f, 0)))
{
//Fehler beim leeren
TB_ERROR_DIRECTX("g_pD3DDevice->Clear", hResult, TB_STOP);
}
//Hier wird dann das dreieck gerendert
//Szene beginnen
g_pD3DDevice->BeginScene();
if(FAILED(hResult = g_pD3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, g_aTriangleVertex, sizeof(SVertex))))
{
TB_ERROR_DIRECTX("g_pD3DDevice->DrawPrimitiveUP", hResult, TB_STOP);
}
//Szene beenden
g_pD3DDevice->EndScene();
//Bildpuffer wird sichtbar
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
return TB_OK; //das gleiche wie return 0, Makro von der enum tbresult
}
tbResult Move(float fNumSecsPassed)
{
g_fTime += fNumSecsPassed;
return TB_OK;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char* pcCmdLine, int iShowCmd)
{
SDirect3DParameters Direct3DParameters;
//initialisieren
tbInit();
//parameter abfrage
tbResult Result = GetDirect3DParameters(&Direct3DParameters);
if(Result == TB_ERROR)
{
MessageBoxA(NULL, "Fehler beim Abzählen!!!", "Fehler", MB_OK);
tbExit();
return 1;
}
else if(Result == TB_CANCELED)//vom benutzer abgebrochen
{
tbExit();
return 0;
}
if(InitWindow(Direct3DParameters.VideoMode.Width,
Direct3DParameters.VideoMode.Height,
"Beispiel", LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1))))
{
MessageBoxA(NULL, "Fehler beim erstellen des Fensters", "Failure", MB_OK);
tbExit();
return 1;
}
if(InitDirect3D(&Direct3DParameters, g_hWindow))
{
MessageBoxA(g_hWindow, "Fehler beim Initialisieren von D3D", "Failure", MB_OK);
ExitWindow();
tbExit();
return 1;
}
if(InitScene())
{
// Fehler!
MessageBox(g_hWindow, "Fehler beim Initialisieren der Szene!",
"Fehler", MB_OK | MB_ICONINFORMATION);
ExitWindow();
tbExit();
return TB_ERROR;
}
tbDoMessageLoop(Render, Move);
ExitDirect3D();
ExitWindow();
tbExit();
return 0;
}
|