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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
#include <TriBase.h> #include "Resource.h" #define SKY_BOX_VERTEX_FVF (D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE3(0)) #define handle tbDirect3D::GetWindow() struct SSkyBoxVertex { tbVector3 vPosition; tbVector3 vTexture; }; // ****************************************************************** // Globale Variablen tbConfig g_Config; tbVertexBuffer* g_pSkyBoxVB = NULL; tbIndexBuffer* g_pSkyBoxIB = NULL; PDIRECT3DCUBETEXTURE9 g_pSkyBoxTexture = NULL; tbEffect* g_pSkyBoxEffect = NULL; tbModel* g_pCityModel = NULL; tbModel* g_pShipModel = NULL; tbVector3 g_vCameraPos; float g_fCameraRot; float g_fCameraUpDown; float g_fTime = 0.0f; LPDIRECTINPUT8 g_pDInput = NULL; LPDIRECTINPUTDEVICE8 g_pMouse = NULL; DIMOUSESTATE2 MouseState; tbResult InitMouse(HWND hDlg) { if(FAILED(g_pDInput->CreateDevice(GUID_SysMouse, &g_pMouse, NULL))) return TB_ERROR; if(FAILED(g_pMouse->SetDataFormat(&c_dfDIMouse2))) return TB_ERROR; if(FAILED(g_pMouse->SetCooperativeLevel(hDlg, DISCL_EXCLUSIVE | DISCL_FOREGROUND))) return TB_ERROR; return TB_OK; } int UpdateMouseState(HWND hDlg) { HRESULT r; r = g_pMouse->Poll(); if(r == DIERR_INPUTLOST) { g_pMouse->Acquire(); } g_pMouse->GetDeviceState(sizeof(DIMOUSESTATE2), &MouseState); tbWriteToLog("%d,%d,%d",MouseState.lX,MouseState.lY, MouseState.lZ); if(MouseState.rgbButtons[0] & 0x80) { tbWriteToLog("%f",MouseState.rgbButtons[0]); } return TB_OK; } tbResult MoveProc(float fNumSecsPassed) { tbVector3 vCameraDir; g_fTime += fNumSecsPassed; vCameraDir = tbVector3(sinf(g_fCameraRot) * cosf(g_fCameraUpDown), sinf(g_fCameraUpDown), cosf(g_fCameraRot) * cosf(g_fCameraUpDown)); UpdateMouseState(tbDirect3D::GetWindow()); 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 * 15.0f * fNumSecsPassed; if(GetAsyncKeyState(VK_SUBTRACT)) g_vCameraPos -= vCameraDir * 15.0f * fNumSecsPassed; return TB_OK; } // ****************************************************************** // Die Render-Funktion tbResult RenderProc(float fNumSecsPassed) { float fAspect; tbMatrix mProjection; tbMatrix mCamera; tbMatrix mWorld; tbVector3 vCameraDir; D3DLIGHT9 Light; int iNumPasses; // Z-Buffer leeren und die Szene beginnen tbDirect3D::GetDevice()->Clear(0, NULL, D3DCLEAR_ZBUFFER, 0, 1.0f, 0); tbDirect3D::GetDevice()->BeginScene(); // Projektionsmatrix erstellen und einsetzen fAspect = (float)(g_Config.Direct3D.VideoMode.Width) / (float)(g_Config.Direct3D.VideoMode.Height); mProjection = tbMatrixProjection(TB_DEG_TO_RAD(60.0f), fAspect, 1.0f, 300.0f); tbDirect3D::GetDevice()->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)(&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); tbDirect3D::GetDevice()->SetTransform(D3DTS_VIEW, (D3DMATRIX*)(&mCamera)); // ------------------------------------------------------------------ // Die Sky-Box-Textur setzen tbDirect3D::SetTexture(0, g_pSkyBoxTexture); // Weltmatrix erstellen, die die Sky-Box zur Kamera hin schiebt mWorld = tbMatrixTranslation(g_vCameraPos); tbDirect3D::GetDevice()->SetTransform(D3DTS_WORLD, (D3DMATRIX*)(&mWorld)); // Vertex- und Index-Buffer aktivieren und das Vertexformat setzen tbDirect3D::GetDevice()->SetStreamSource(0, g_pSkyBoxVB->GetVB(), 0, sizeof(SSkyBoxVertex)); tbDirect3D::GetDevice()->SetIndices(g_pSkyBoxIB->GetIB()); tbDirect3D::SetFVF(SKY_BOX_VERTEX_FVF); // Zeichnen! iNumPasses = g_pSkyBoxEffect->Begin(); for(int iPass = 0; iPass < iNumPasses; iPass++) { g_pSkyBoxEffect->Pass(iPass); tbDirect3D::GetDevice()->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; tbDirect3D::GetDevice()->SetLight(0, &Light); tbDirect3D::GetDevice()->LightEnable(0, TRUE); // Linearer schwarzer Nebel! tbDirect3D::SetRS(D3DRS_FOGENABLE, TRUE); tbDirect3D::SetRS(D3DRS_FOGVERTEXMODE, D3DFOG_LINEAR); tbDirect3D::SetRS(D3DRS_RANGEFOGENABLE, TRUE); tbDirect3D::SetRS(D3DRS_FOGCOLOR, 0); tbDirect3D::SetRSF(D3DRS_FOGSTART, 100.0f); tbDirect3D::SetRSF(D3DRS_FOGEND, 350.0f); // Weltmatrix zurücksetzen tbDirect3D::GetDevice()->SetTransform(D3DTS_WORLD, (D3DMATRIX*)(&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(g_fTime * 0.5f) * tbMatrixTranslation(tbVector3(100.0f, 100.0f, 0.0f)) * tbMatrixRotationY(g_fTime * -0.5f); tbDirect3D::GetDevice()->SetTransform(D3DTS_WORLD, (D3DMATRIX*)(&mWorld)); // Rendern g_pShipModel->Render(); // ------------------------------------------------------------------ // Kein Nebel mehr! tbDirect3D::SetRS(D3DRS_FOGENABLE, FALSE); // ------------------------------------------------------------------ // Szene beenden und darstellen tbDirect3D::GetDevice()->EndScene(); tbDirect3D::GetDevice()->Present(NULL, NULL, NULL, NULL); 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), SKY_BOX_VERTEX_FVF, 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::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::Exit(); tbTextureManager::Exit(); // Die TriBase-Engine herunterfahren tbExit(); return TB_OK; } // ****************************************************************** // Windows-Hauptfunktion int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char* pcCommandLine, int iShowCommand) { int r; // TriBase-Engine initialisieren tbInit(); // Konfiguration abfragen 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::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; } if(FAILED(DirectInput8Create(hInstance, // Instanz 0x0800, // Version IID_IDirectInput8, // Schnittstellenversion (void**)(&g_pDInput), // Doppelzeiger auf Objekt NULL))) // nicht wichtig { MessageBox(NULL, "IDirectInput8-Objekt konnte nicht erstellt werden!", "Fehler", MB_OK | MB_ICONEXCLAMATION); return 1; } if(InitMouse(tbDirect3D::GetWindow())) { MessageBox(NULL, "Maus konnte nicht initialisiert werden!", "Fehler", MB_OK | MB_ICONEXCLAMATION); } UpdateMouseState(tbDirect3D::GetWindow()); // Den Texturmanager initialisieren if(tbTextureManager::Init()) { MessageBox(tbDirect3D::GetWindow(), "Fehler beim Initialisieren des Texturmanagers!", "Fehler", MB_OK | MB_ICONEXCLAMATION); CleanUp(); return 1; } // Sky-Box initialisieren if(InitSkyBox()) { MessageBox(tbDirect3D::GetWindow(), "Fehler beim Erstellen der Sky-Box!", "Fehler", MB_OK | MB_ICONEXCLAMATION); CleanUp(); return 1; } // Das Stadtmodell laden g_pCityModel = new tbModel; if(g_pCityModel->Init("City.tbm@Data\\City.zip", "", "@Data\\City.zip")) { MessageBox(tbDirect3D::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("Ship.tbm@Data\\Ship.zip", "", "@Data\\Ship.zip")) { MessageBox(tbDirect3D::GetWindow(), "Fehler beim Laden der Schiffmodelldatei!", "Fehler", MB_OK | MB_ICONEXCLAMATION); CleanUp(); return 1; } // Kamera initialisieren g_vCameraPos = tbVector3(0.0f, 10.0f, 0.0f); g_fCameraRot = 0.0f; g_fCameraUpDown = 0.0f; // Nachrichtenschleife betreten if(tbDoMessageLoop(MoveProc, RenderProc)) { MessageBox(tbDirect3D::GetWindow(), "Fehler beim Zeichnen!", "Fehler", MB_OK | MB_ICONEXCLAMATION); CleanUp(); return 1; } // Aufräumen CleanUp(); return 0; } |
Anonymous
unregistriert
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 |
// Update view coordinates m_XPos += XMove; m_ZPos += ZMove; // Position camera and rotate based on mouse position m_Camera.Move(m_XPos + XMove, 0.0f, m_ZPos + ZMove); m_Camera.RotateRel((float)m_Mouse.GetYDelta() / 500.0f, (float)m_Mouse.GetXDelta() / 500.0f, 0.0f); |