Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
// Kapitel 3 // Beispielprogramm 02 // =================== // Dieses Programm demonstriert den Umgang mit der tbModel-Klasse. #include <TriBase.h> #include "Resource.h" // ****************************************************************** // Struktur für einen Vertex der Sky-Box struct SSkyBoxVertex { tbVector3 vPosition; // Position tbVector3 vTexture; // 3D-Texturkoordinaten static const DWORD dwFVF; // Vertexformat }; const DWORD SSkyBoxVertex::dwFVF = D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE3(0); // ****************************************************************** // Globale Variablen tbConfig g_Config; // Konfigurationsstruktur tbVertexBuffer* g_pSkyBoxVB = NULL; // Sky-Box-Vertex-Buffer tbIndexBuffer* g_pSkyBoxIB = NULL; // Sky-Box-Index-Buffer PDIRECT3DCUBETEXTURE9 g_pSkyBoxTexture = NULL; // Sky-Box-Textur tbEffect* g_pSkyBoxEffect = NULL; // Sky-Box-Effekt tbModel* g_pCityModel = NULL; // Modell der Stadt tbModel* g_pShipModel = NULL; // Raumschiffmodell tbVector3 g_vCameraPos; // Kameraposition float g_fCameraRot; // Rotation der Kamera float g_fCameraUpDown; // Schaut die Kamera hoch oder runter? float g_fFOV = TB_DEG_TO_RAD(90.0f); // Sichtfeld float g_fTime = 0.0f; // Globaler Zeitzähler // ****************************************************************** // Die Move-Funktion tbResult MoveProc(float fNumSecsPassed) { tbVector3 vCameraDir; // Den Zeitzähler aktualisieren g_fTime += fNumSecsPassed; // Tastatursteuerung... vCameraDir = tbVector3(sinf(g_fCameraRot) * cosf(g_fCameraUpDown), sinf(g_fCameraUpDown), cosf(g_fCameraRot) * cosf(g_fCameraUpDown)); if(GetAsyncKeyState(VK_LEFT)) g_fCameraRot -= 1.0f * fNumSecsPassed; if(GetAsyncKeyState(VK_RIGHT)) g_fCameraRot += 1.0f * fNumSecsPassed; if(GetAsyncKeyState(VK_UP)) g_fCameraUpDown -= 1.0f * fNumSecsPassed; if(GetAsyncKeyState(VK_DOWN)) g_fCameraUpDown += 1.0f * fNumSecsPassed; if(GetAsyncKeyState(VK_ADD)) g_vCameraPos += vCameraDir * 25.0f * fNumSecsPassed; if(GetAsyncKeyState(VK_SUBTRACT)) g_vCameraPos -= vCameraDir * 25.0f * fNumSecsPassed; // Die Tasten "Bild auf" und "Bild ab" verändern das Sichtfeld. // So kann man in das Bild "hineinzoomen", mit 15 Grad pro Sekunde. if(GetAsyncKeyState(VK_PRIOR)) g_fFOV -= TB_DEG_TO_RAD(15.0f) * fNumSecsPassed; if(GetAsyncKeyState(VK_NEXT)) g_fFOV += TB_DEG_TO_RAD(15.0f) * fNumSecsPassed; // Das Sichtfeld darf 180° und 0° nicht erreichen. if(g_fFOV >= TB_DEG_TO_RAD(180.0f)) g_fFOV = TB_DEG_TO_RAD(179.9f); else if(g_fFOV <= TB_DEG_TO_RAD(0.0f)) g_fFOV = TB_DEG_TO_RAD(0.1f); return TB_OK; } // ****************************************************************** // Die Render-Funktion tbResult RenderProc(float fNumSecsPassed) { tbMatrix mProjection; tbMatrix mCamera; tbMatrix mWorld; tbMatrix mScall; tbVector3 vCameraDir; D3DLIGHT9 Light; int iNumPasses; // Z-Buffer leeren und die Szene beginnen tbDirect3D& D3D = tbDirect3D::Instance(); D3D->SetRenderState(D3DRS_LIGHTING, FALSE); D3D->Clear(0, NULL, D3DCLEAR_ZBUFFER, 0, 1.0f, 0); D3D->BeginScene(); // Projektionsmatrix erstellen und einsetzen mProjection = tbMatrixProjection(g_fFOV, D3D.GetAspect(), 0.1f, 300.0f); D3D.SetTransform(D3DTS_PROJECTION, mProjection); // Kameramatrix erstellen und einsetzen vCameraDir = tbVector3(sinf(g_fCameraRot) * cosf(g_fCameraUpDown), sinf(g_fCameraUpDown), cosf(g_fCameraRot) * cosf(g_fCameraUpDown)); mCamera = tbMatrixCamera(g_vCameraPos, g_vCameraPos + vCameraDir); D3D.SetTransform(D3DTS_VIEW, mCamera); // ------------------------------------------------------------------ if(g_pSkyBoxTexture != NULL && g_pSkyBoxVB != NULL && g_pSkyBoxIB != NULL) { // Die Sky-Box-Textur setzen D3D.SetTexture(0, g_pSkyBoxTexture); // Weltmatrix erstellen, die die Sky-Box zur Kamera hin schiebt mWorld = tbMatrixTranslation(g_vCameraPos); D3D.SetTransform(D3DTS_WORLD, mWorld); // Vertex- und Index-Buffer aktivieren und das Vertexformat setzen D3D->SetStreamSource(0, g_pSkyBoxVB->GetVB(), 0, sizeof(SSkyBoxVertex)); D3D->SetIndices(g_pSkyBoxIB->GetIB()); D3D.SetFVF(SSkyBoxVertex::dwFVF); // Zeichnen! iNumPasses = g_pSkyBoxEffect->Begin(); for(int iPass = 0; iPass < iNumPasses; iPass++) { g_pSkyBoxEffect->Pass(iPass); D3D->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12); } g_pSkyBoxEffect->End(); } // ------------------------------------------------------------------ /*// Ein Richtungslicht erstellen mit der Richtung der Kamera ZeroMemory(&Light, sizeof(D3DLIGHT9)); Light.Type = D3DLIGHT_DIRECTIONAL; Light.Diffuse = tbColor(0.5f, 0.5f, 0.5f); Light.Ambient = tbColor(0.5f, 0.5f, 0.5f); Light.Specular = tbColor(0.5f, 0.5f, 0.5f); Light.Direction = vCameraDir; D3D->SetLight(0, &Light); D3D->LightEnable(0, TRUE);*/ // Linearer schwarzer Nebel! D3D.SetRS(D3DRS_FOGENABLE, TRUE); D3D.SetRS(D3DRS_FOGVERTEXMODE, D3DFOG_LINEAR); D3D.SetRS(D3DRS_RANGEFOGENABLE, TRUE); D3D.SetRS(D3DRS_FOGCOLOR, 0); D3D.SetRSF(D3DRS_FOGSTART, 100.0f); D3D.SetRSF(D3DRS_FOGEND, 350.0f); // Weltmatrix zurücksetzen D3D.SetTransform(D3DTS_WORLD, tbMatrixIdentity()); // Das Modell rendern (zuerst alle opaken Effekte, dann alle transparenten) //g_pCityModel->Render(-1, -1, TRUE, FALSE); //g_pCityModel->Render(-1, -1, FALSE, TRUE); // ------------------------------------------------------------------ // Raumschiff rendern! Es kreist über der Stadt. mWorld = tbMatrixRotationZ(0) * tbMatrixTranslation(tbVector3(0.0f, 0.0f, 10.0f)) * tbMatrixRotationY(0); D3D.SetTransform(D3DTS_WORLD, mWorld); // Rendern g_pShipModel->Render(); // ------------------------------------------------------------------ // Kein Nebel mehr! D3D.SetRS(D3DRS_FOGENABLE, FALSE); // ------------------------------------------------------------------ // Szene beenden D3D->EndScene(); return TB_OK; } // ****************************************************************** // Initialisierung der Sky-Box tbResult InitSkyBox() { SSkyBoxVertex aVertex[8]; // Vertex- und Index-Buffer erstellen. 8 Vertizes, 36 Indizes. g_pSkyBoxVB = new tbVertexBuffer; if(g_pSkyBoxVB->Init(8 * sizeof(SSkyBoxVertex), sizeof(SSkyBoxVertex), SSkyBoxVertex::dwFVF, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DPOOL_DEFAULT)) { // Fehler! return TB_ERROR; } // Nun der Index-Buffer (16 Bits pro Index) g_pSkyBoxIB = new tbIndexBuffer; if(g_pSkyBoxIB->Init(36 * sizeof(WORD), sizeof(WORD), D3DFMT_INDEX16)) { // Fehler! return TB_ERROR; } // ------------------------------------------------------------------ // Die Vertizes erstellen aVertex[0].vPosition = tbVector3(-1.0f, 1.0f, 1.0f); aVertex[1].vPosition = tbVector3( 1.0f, 1.0f, 1.0f); aVertex[2].vPosition = tbVector3( 1.0f, 1.0f, -1.0f); aVertex[3].vPosition = tbVector3(-1.0f, 1.0f, -1.0f); aVertex[4].vPosition = tbVector3(-1.0f, -1.0f, 1.0f); aVertex[5].vPosition = tbVector3( 1.0f, -1.0f, 1.0f); aVertex[6].vPosition = tbVector3( 1.0f, -1.0f, -1.0f); aVertex[7].vPosition = tbVector3(-1.0f, -1.0f, -1.0f); // Die Texturkoordinaten entsprechen den Vertexpositionen for(int iVertex = 0; iVertex < 8; iVertex++) { // Texturkoordinate eintragen und Sky-Box skalieren aVertex[iVertex].vTexture = aVertex[iVertex].vPosition; aVertex[iVertex].vPosition *= 10.0f; } // Alle Vertizes eintragen und den Vertex-Buffer aktualisieren g_pSkyBoxVB->AddVertices(8, aVertex); if(g_pSkyBoxVB->Update()) return TB_ERROR; // ------------------------------------------------------------------ // Den Index-Buffer ausfüllen WORD awIndex[36] = {7, 3, 0, 4, 7, 0, // Vorderseite 5, 1, 2, 6, 5, 2, // Hinterseite 4, 0, 1, 5, 4, 1, // Linke Seite 6, 2, 3, 7, 6, 3, // Rechte Seite 2, 1, 0, 3, 2, 0, // Oberseite 4, 5, 6, 7, 4, 6}; // Unterseite g_pSkyBoxIB->AddIndices(36, awIndex); if(g_pSkyBoxIB->Update()) return TB_ERROR; // ------------------------------------------------------------------ // Die Textur der Sky-Box laden g_pSkyBoxTexture = tbTextureManager::Instance().GetCubeTexture("SkyBox.dds@Data\\SkyBox.zip"); if(g_pSkyBoxTexture == NULL) return TB_ERROR; // Effekt laden g_pSkyBoxEffect = new tbEffect; if(g_pSkyBoxEffect == NULL) return TB_ERROR; if(g_pSkyBoxEffect->Init("SkyBox.fx@Data\\SkyBox.zip")) return TB_ERROR; return TB_OK; } // ****************************************************************** // Aufräumen tbResult CleanUp() { // Alles löschen TB_SAFE_DELETE(g_pSkyBoxVB); TB_SAFE_DELETE(g_pSkyBoxIB); TB_SAFE_DELETE(g_pSkyBoxEffect); TB_SAFE_DELETE(g_pCityModel); TB_SAFE_DELETE(g_pShipModel); tbDirect3D::Instance().Exit(); tbTextureManager::Instance().Exit(); // Die TriBase-Engine herunterfahren tbExit(); return TB_OK; } // ****************************************************************** // Windows-Hauptfunktion int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char* pcCommandLine, int iShowCommand) { // TriBase-Engine initialisieren tbInit(); // Konfiguration abfragen tbResult r; if(r = tbDoConfigDialog(&g_Config)) { if(r == TB_CANCELED) return 0; else { // Fehler! MessageBox(NULL, "Fehler im Konfigurationsdialog!", "Fehler", MB_OK | MB_ICONEXCLAMATION); return 1; } } // Direct3D initialisieren if(tbDirect3D::Instance().Init(&g_Config, "Beispielprogramm Nr. 2: Modelle", NULL, LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)))) { // Fehler! MessageBox(NULL, "Fehler bei der Direct3D-Initialisierung!", "Fehler", MB_OK | MB_ICONEXCLAMATION); CleanUp(); return 1; } // Den Texturmanager initialisieren if(tbTextureManager::Instance().Init()) { MessageBox(tbDirect3D::Instance().GetWindow(), "Fehler beim Initialisieren des Texturmanagers!", "Fehler", MB_OK | MB_ICONEXCLAMATION); CleanUp(); return 1; } // Sky-Box initialisieren if(InitSkyBox()) TB_WARNING("Sky-Box konnte nicht erstellt werden!"); // Das Stadtmodell laden g_pCityModel = new tbModel; if(g_pCityModel->Init("City.tbm@Data\\City.zip", "", "@Data\\City.zip")) { MessageBox(tbDirect3D::Instance().GetWindow(), "Fehler beim Laden der Stadtmodelldatei!", "Fehler", MB_OK | MB_ICONEXCLAMATION); CleanUp(); return 1; } // Das Raumschiffmodell laden g_pShipModel = new tbModel; if(g_pShipModel->Init("tbm.tbm", "", "")) { MessageBox(tbDirect3D::Instance().GetWindow(), "Fehler beim Laden der Schiffmodelldatei!", "Fehler", MB_OK | MB_ICONEXCLAMATION); CleanUp(); return 1; } // Kamera initialisieren g_vCameraPos = tbVector3(0.0f, 0.0f, 0.0f); g_fCameraRot = 0.0f; g_fCameraUpDown = 0.0f; // Nachrichtenschleife betreten if(tbDoMessageLoop(MoveProc, RenderProc)) { MessageBox(tbDirect3D::Instance().GetWindow(), "Fehler beim Zeichnen!", "Fehler", MB_OK | MB_ICONEXCLAMATION); CleanUp(); return 1; } // Aufräumen CleanUp(); return 0; } // ****************************************************************** |
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Vison« (11.05.2010, 21:39)
Werbeanzeige