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
Anonymous
unregistriert
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 |
struct SVertex { tbVector3 vPosition; // Position des Vertex DWORD dwColor; // Farbe des Vertex static const DWORD dwFVF; // Vertexformat (statisch) }; tbResult InitScene() { HRESULT hResult; tbMatrix mProjection; float fAspect; tbColor VertexColor; // Beleuchtung und Culling ausschalten, Dithering aktivieren 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)(g_Direct3DParameters.VideoMode.Width) / (float)(g_Direct3DParameters.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 // Projektionsmatrix einsetzen g_pD3DDevice->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)(&mProjection)); // Vertexformat setzen if(FAILED(hResult = g_pD3DDevice->SetFVF(SVertex::dwFVF))) TB_ERROR_DIRECTX("g_pD3DDevice->SetFVF", hResult, TB_ERROR); //vertex + index buffer hResult=g_pD3DDevice->CreateVertexBuffer(8*sizeof(SVertex), 0, D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1, D3DPOOL_MANAGED, &pVertexBuffer, NULL); if(FAILED(hResult)) TB_ERROR_DIRECTX("CreateVertexBuffer", hResult, TB_STOP); hResult=g_pD3DDevice->CreateIndexBuffer(36*2, 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &pIndexBuffer, NULL); if(FAILED(hResult)) TB_ERROR_DIRECTX("CreateIndexBuffer", hResult, TB_STOP); SVertex* pVertizes; //daten gesperrter vert buffer WORD* pwIndices; //ind buffer pVertexBuffer->Lock(0,0,(void**)(&pVertizes), D3DLOCK_NOSYSLOCK); pIndexBuffer->Lock(0,0,(void**)(&pwIndices), D3DLOCK_NOSYSLOCK); pVertizes[0].vPosition=tbVector3(-1.0f,1.0f,1.0f); pVertizes[1].vPosition=tbVector3(-1.0f,1.0f,-1.0f); pVertizes[2].vPosition=tbVector3(1.0f,1.0f,-1.0f); pVertizes[3].vPosition=tbVector3(1.0f,1.0f,1.0f); pVertizes[4].vPosition=tbVector3(-1.0f,-1.0f,1.0f); pVertizes[5].vPosition=tbVector3(-1.0f,-1.0f,-1.0f); pVertizes[6].vPosition=tbVector3(1.0f,-1.0f,-1.0f); pVertizes[7].vPosition=tbVector3(1.0f,-1.0f,1.0f); 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 }; VertexColor.r = 255;VertexColor.g = 255;VertexColor.b = 255; for(int i=0;i<8;++i) pVertizes[i].dwColor=(DWORD)(VertexColor); for(i=0;i<36;++i) pwIndices[i]=(DWORD)aiIndex[i]; pIndexBuffer->Unlock(); pVertexBuffer->Unlock(); return TB_OK; } // ****************************************************************** // Render-Funktion tbResult Render(float fNumSecsPassed) { HRESULT hResult; 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, 0), 1.0f, 0))) { // Fehler beim Leeren! TB_ERROR_DIRECTX("g_pD3DDevice->Clear", hResult, TB_STOP); } // Szene beginnen g_pD3DDevice->BeginScene(); //Cube zeichnen mTranslation = tbMatrixTranslation(tbVector3(0.0f, 0.0f, 0.0f)); mWorld=mTranslation; g_pD3DDevice->SetTransform(D3DTS_WORLD, (D3DMATRIX*)(&mWorld)); g_pD3DDevice->SetStreamSource(0,pVertexBuffer, 0, sizeof(SVertex)); g_pD3DDevice->SetIndices(pIndexBuffer); hResult=g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12); if(FAILED(hResult)) TB_ERROR_DIRECTX("drawindexedprimitive", hResult, TB_STOP); g_pD3DDevice->EndScene(); // Der große Moment: den Bildpuffer sichtbar machen g_pD3DDevice->Present(NULL, NULL, NULL, NULL); return TB_OK; } |
Anonymous
unregistriert
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 |
// Würfel-Struktur struct SCube { tbVector3 vPosition; // Position tbVector3 vVelocity; // Bewegung (Richtung und Geschwindigkeit) tbVector3 vRotation; // Rotationszustand tbVector3 vRotVelocity; // Rotationsbewegung float fSize; // Größe SVertex aVertex[8]; // Vertizes }; tbResult InitScene() { HRESULT hResult; SCube* pCubes; unsigned short* pusIndices; // ... // // Vertex-Buffer und Index-Buffer erstellen // Vertex- und Index-Buffer komplett sperren g_pVertexBuffer->Lock(0, 0, (void**)(&pCubes), D3DLOCK_NOSYSLOCK); g_pIndexBuffer->Lock(0, 0, (void**)(&pusIndices), D3DLOCK_NOSYSLOCK); // Nun gehen wir jeden einzelnen Würfel durch for(int iCube = 0; iCube < g_iNumCubes; iCube++) { // Zufällige Position für diesen Würfel erzeugen pCubes[iCube].vPosition = tbVector3Random() * tbFloatRandom(20.0f, 250.0f); // Die Bewegung ist zufällig. pCubes[iCube].vVelocity = tbVector3Random() * tbFloatRandom(0.1f, 5.0f); // Rotation zurücksetzen pCubes[iCube].vRotation = tbVector3(0.0f, 0.0f, 0.0f); // Rotationsgeschwindigkeit ist zufällig auf allen drei Achsen pCubes[iCube].vRotVelocity.x = tbFloatRandom(-1.0f, 1.0f); pCubes[iCube].vRotVelocity.y = tbFloatRandom(-1.0f, 1.0f); pCubes[iCube].vRotVelocity.z = tbFloatRandom(-1.0f, 1.0f); // Größe zufällig zwischen 1 und 5 festlegen pCubes[iCube].fSize = tbFloatRandom(1.0f, 5.0f); // Vertizes für diesen Würfel generieren. Erst die Positionsangaben. pCubes[iCube].aVertex[0].vPosition = pCubes[iCube].vPosition + tbVector3(-1.0f, 1.0f, -1.0f); pCubes[iCube].aVertex[1].vPosition = pCubes[iCube].vPosition + tbVector3(-1.0f, 1.0f, 1.0f); pCubes[iCube].aVertex[2].vPosition = pCubes[iCube].vPosition + tbVector3( 1.0f, 1.0f, 1.0f); pCubes[iCube].aVertex[3].vPosition = pCubes[iCube].vPosition + tbVector3( 1.0f, 1.0f, -1.0f); pCubes[iCube].aVertex[4].vPosition = pCubes[iCube].vPosition + tbVector3(-1.0f, -1.0f, -1.0f); pCubes[iCube].aVertex[5].vPosition = pCubes[iCube].vPosition + tbVector3(-1.0f, -1.0f, 1.0f); pCubes[iCube].aVertex[6].vPosition = pCubes[iCube].vPosition + tbVector3( 1.0f, -1.0f, 1.0f); pCubes[iCube].aVertex[7].vPosition = pCubes[iCube].vPosition + tbVector3( 1.0f, -1.0f, -1.0f); for(int iVertex = 0; iVertex < 8; iVertex++) { // Zufallsfarbe erzeugen (Alpha = 1) pCubes[iCube].aVertex[iVertex].dwColor = tbColorRandom(1.0f) * 2.0f; // Texturkoordinaten generieren pCubes[iCube].aVertex[iVertex].vTexture = tbVector2Random(); } // Nun die Indizes eintragen. Jeweils drei von ihnen ergeben ein Dreieck. int aiIndex[36] = {0, 3, 7, 0, 7, 4, // Vorderseite 2, 1, 5, 2, 5, 6, // Hinterseite 1, 0, 4, 1, 4, 5, // Linke Seite 3, 2, 6, 3, 6, 7, // Rechte Seite 0, 1, 2, 0, 2, 3, // Oberseite 6, 5, 4, 6, 4, 7}; // Unterseite // Die 36 Indizes in den Index-Buffer übertragen for(int iIndex = 0; iIndex < 36; iIndex++) { // Index eintragen pusIndices[(36 * iCube) + iIndex] = aiIndex[iIndex] + (8 * iCube); } } // Vertex- und Index-Buffer wieder entsperren g_pVertexBuffer->Unlock(); g_pIndexBuffer->Unlock(); return TB_OK; } // Render-Funktion tbResult Render(float fNumSecsPassed) { // ... // // Vertex- und den Index-Buffer als Datenquelle aktivieren g_pD3DDevice->SetStreamSource(0, g_pVertexBuffer, 0, sizeof(SCube)); g_pD3DDevice->SetIndices(g_pIndexBuffer); // Zeichnen! hResult = g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, // Dreiecksliste 0, // Basisvertexindex 0, // Der kleinste Index g_iNumCubes * 8, // Diff. zw. größtem u. kleinstem Index 0, // Von Anfang an zeichnen g_iNumCubes * 12); // 12 Dreiecke pro Würfel // ... // } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 |
// Würfel-Struktur struct SCube { tbVector3 vPosition; // Position tbVector3 vVelocity; // Bewegung (Richtung und Geschwindigkeit) tbVector3 vRotation; // Rotationszustand tbVector3 vRotVelocity; // Rotationsbewegung float fSize; // Größe SVertex aVertex[8]; // Vertizes }; |
Werbeanzeige