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 |
#include "Direct3D.h" #include <stdio.h> #include <time.h> #include <math.h> #include <time.h> #include <mmsystem.h> CInitDirect3D Direct3D; struct SVertex { tbVector3 vPosition; DWORD dwColor; tbVector2 vTexture; }; struct STriangle { tbVector3 vPosition; tbVector3 vVelocity; tbVector3 vRotation; tbVector3 vRotVelocity; float fSzie; SVertex aVertex[3]; }; HRESULT hr; float g_fTime = 0.0f; SVertex g_aTriangleVertex[3]; const int g_iNumCubes = 2048; tbVector3 g_vCameraPosition; float g_vCameraAngle; float g_fFOV; STriangle g_aTriangle[g_iNumCubes]; tbResult Render(float fNumSecsPassed) { HRESULT hResult; tbMatrix mTranslation; tbMatrix mWorld; hResult = Direct3D.m_lpD3DDevice->TestCooperativeLevel(); tbMatrix mCamera = tbMatrixCamera(g_vCameraPosition, g_vCameraPosition + tbVector3(sinf(g_vCameraAngle), 0.0f, cosf(g_vCameraAngle)), tbVector3(0.0f, 1.0f, 0.0f)); Direct3D.m_lpD3DDevice->SetTransform(D3DTS_VIEW, (D3DMATRIX*)(&mCamera)); float fAspect = (float)(Direct3D.window_width()) / (float)(Direct3D.window_height()); tbMatrix mProjection = tbMatrixProjection(g_fFOV, fAspect, 0.1f, 250.0f); Direct3D.m_lpD3DDevice->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)(&mProjection)); Direct3D.BeginScene(); Direct3D.m_lpD3DDevice->SetStreamSource(0, Direct3D.m_pVertexBuffer, 0, sizeof(SVertex)); Direct3D.m_lpD3DDevice->SetIndices(Direct3D.m_pIndexBuffer); hResult = Direct3D.m_lpD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, g_iNumCubes * 8, 0, g_iNumCubes * 12); Direct3D.EndScene(); return TB_OK; } tbResult InitScene() { HRESULT hResult; Direct3D.CreateFont(); if(FAILED(hResult = Direct3D.m_lpD3DDevice->SetFVF(D3DFVF_DIFFUSE | D3DFVF_XYZ))) { MessageBox(NULL, "Fehler beim setzten des Vertexformats", "Fehler", MB_OK | MB_ICONERROR); TB_ERROR_DIRECTX("Direct3D.m_lpD3DDevice->SetFVF", hResult, TB_ERROR); } Direct3D.m_lpD3DDevice->SetRenderState(D3DRS_LIGHTING, false); Direct3D.m_lpD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW ); Direct3D.m_lpD3DDevice->SetRenderState(D3DRS_DITHERENABLE, true); float fAspect = (float)(Direct3D.window_width()) / (float)(Direct3D.window_height()); tbMatrix mProjection = tbMatrixProjection(TB_DEG_TO_RAD(90.0f), fAspect, 0.1f, 100.0f); Direct3D.m_lpD3DDevice->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)(&mProjection)); if(FAILED(hResult = D3DXCreateTextureFromFileEx(Direct3D.m_lpD3DDevice, "Texture.bmp", D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT, 0, NULL, NULL, &Direct3D.m_pTexture))) { MessageBox(NULL, "Textur wurde nicht gefunden", "Fehler", MB_OK | MB_ICONERROR); TB_ERROR_DIRECTX("D3DXCreateTextureFromFileEx", hResult, TB_ERROR); } Direct3D.m_lpD3DDevice->SetTexture(0, Direct3D.m_pTexture); if((hResult = Direct3D.m_lpD3DDevice->CreateVertexBuffer(g_iNumCubes * 8 * sizeof(SVertex), 0, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1, D3DPOOL_MANAGED, &Direct3D.m_pVertexBuffer, NULL) != D3D_OK)) { MessageBox(NULL, "Fehler beim Erstellen des Vertex-Buffers", "Fehler", MB_OK | MB_ICONERROR); TB_ERROR_DIRECTX("Direct3D.m_lpD3DDevice->CreateVertexBuffer", hResult, TB_ERROR); tbExit(); } if(hResult = Direct3D.m_lpD3DDevice->CreateIndexBuffer(g_iNumCubes * 36 * 2 , 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &Direct3D.m_pIndexBuffer, NULL) != D3D_OK) { MessageBox(NULL, "Fehler beim Erstellen des Index-Buffers", "Fehler", MB_OK | MB_ICONERROR); TB_ERROR_DIRECTX("Direct3D.m_lpD3DDevice->CreateIndexBuffer", hResult, TB_ERROR); tbExit(); } SVertex* pVertices; WORD* pwIndices; Direct3D.m_pVertexBuffer->Lock(0, 0, (void**)(&pVertices), D3DLOCK_NOSYSLOCK); Direct3D.m_pIndexBuffer->Lock(0, 0, (void**)(&pwIndices), D3DLOCK_NOSYSLOCK); for(int iCube = 0; iCube < g_iNumCubes; iCube++) { tbVector3 vCube = tbVector3Random() * tbFloatRandom(50.0f, 250.0f); int iStartVertex = iCube * 8; int iStartIndex = iCube * 36; pVertices[iStartVertex + 0].vPosition = vCube + tbVector3(-1.0f, 1.0f, -1.0f); pVertices[iStartVertex + 1].vPosition = vCube + tbVector3(-1.0f, 1.0f, 1.0f); pVertices[iStartVertex + 2].vPosition = vCube + tbVector3( 1.0f, 1.0f, 1.0f); pVertices[iStartVertex + 3].vPosition = vCube + tbVector3( 1.0f, 1.0f, -1.0f); pVertices[iStartVertex + 4].vPosition = vCube + tbVector3(-1.0f,-1.0f, -1.0f); pVertices[iStartVertex + 5].vPosition = vCube + tbVector3(-1.0f,-1.0f, 1.0f); pVertices[iStartVertex + 6].vPosition = vCube + tbVector3( 1.0f,-1.0f, 1.0f); pVertices[iStartVertex + 7].vPosition = vCube + tbVector3( 1.0f,-1.0f, -1.0f); for(int iVertex = iStartVertex; iVertex < iStartVertex + 8; iVertex++) { pVertices[iVertex].dwColor = tbColorRandom(1.0f) * 2.0f; pVertices[iVertex].vTexture = tbVector2Random(); } int aiIndex[36] = {0, 3, 7, 0, 7, 4, 2, 1, 5, 2, 5, 6, 1, 0, 4, 1, 4, 5, 3, 2, 6, 3, 6, 7, 0, 1, 2, 0, 2, 3, 6, 5, 4, 6, 4, 7}; for(int iIndex = 0; iIndex < 36; iIndex++) { pwIndices[iStartIndex + iIndex] = (WORD)(aiIndex[iIndex] + iStartVertex); } } Direct3D.m_pVertexBuffer->Unlock(); Direct3D.m_pIndexBuffer->Unlock(); return TB_OK; } tbResult Move(float fNumSecsPassed) { g_fTime += fNumSecsPassed; if(GetAsyncKeyState(VK_SPACE)) fNumSecsPassed = 0.0f; if(GetAsyncKeyState(VK_LEFT)) g_vCameraAngle -= TB_DEG_TO_RAD(45.0f) * fNumSecsPassed; if(GetAsyncKeyState(VK_RIGHT)) g_vCameraAngle += TB_DEG_TO_RAD(45.0f) * fNumSecsPassed; tbVector3 vCameraDirection = tbVector3(sinf(g_vCameraAngle), 0.0f, cosf(g_vCameraAngle)); if(GetAsyncKeyState(VK_UP)) g_vCameraPosition += vCameraDirection * 10.0f * fNumSecsPassed; if(GetAsyncKeyState(VK_DOWN)) g_vCameraPosition -= vCameraDirection * 10.0f * fNumSecsPassed; if(g_fFOV >= TB_DEG_TO_RAD(180.0f)) g_fFOV = TB_DEG_TO_RAD(179.9f); if(g_fFOV <= TB_DEG_TO_RAD(0.0f)) g_fFOV = TB_DEG_TO_RAD(0.1f); return TB_OK; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { tbInit(); InitWindow(Direct3D.window_width(), Direct3D.window_height(), "Fenster", LoadIcon(NULL, IDI_WINLOGO)); if(Direct3D.InitDirect3D(g_hWindow, hr) == false) { tbExit(); return TB_OK; } InitScene(); tbDoMessageLoop(Render, Move); Direct3D.CleanUp(); tbExit(); return 0; } |
Werbeanzeige