Du bist nicht angemeldet.

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

1

05.03.2007, 12:48

Frage zum Nebel

Hallo,

also hab mir das kapitel mit dem nebel jetzt zu gemüte geführt und hab da jetzt ein problem.

hab den nebel eben in das programm mit den würfeln (vertex/indexbuffer) eingebaut, aber irgendwie funktioniert das nicht so,wie ich eigentlich dachte:D

das problem ist,dass die würfel,die eigentlich im nebel verschwinden sollten weiß gezeichnet werden: also weiße farbe und keine textur(vermutlich vom weißton überdeckt/ausgelöscht).

warum das aber so ist weiß ich nicht und auch beim beispielprogramm von david bin ich nicht dahintergekommen,was er anders gemacht hätte bei den schildern als ich bei den würfeln.

bitte also um hilfe :) hier der code

wichtig ist eigentlich nur die render und movefunktion, den rest kann man sich sparen.

edit:

achja frage 2 war, die nebelfarbe abhängig von der geschwindigkeit der kamera zu machen. funktioniert das so, wie ich es gemacht habe oder ist das kompletter schwachsinn? :) v.a. versteh ich ned ganz warum sich die nebelfarbe ändern sollte mit der geschwindigkeit? tut sie doch im realen leben auch nicht?

mfg
Simon

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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// VIbuffer mit Nebel.cpp

// author : Simon Klausner

// version : 0.1

// purpose : use vertex and index buffer for DrawPrimitive

//                   integrate fog


#define D3D_DEBUG_INFO

#include <windows.h>
#include <TriBase.h>
#include <d3dx9.h>
#include <I:\\Spiele Programmierung\\Beispiele\\Allgemeines\\InitWindow.h>
#include <I:\\Spiele Programmierung\\Beispiele\\Allgemeines\\Direct3DEnum.h>
#include <I:\\Spiele Programmierung\\Beispiele\\Allgemeines\\InitDirect3D.h>
#include <I:\\Spiele Programmierung\\Beispiele\\Allgemeines\\Allgemeines.h>



// Globals

struct SVertex                                  // structure for Vertex

{
    tbVector3   vPosition;                  // Position

    DWORD   dwColor;                                // Color

    tbVector2   vTexture;                       // Texturecoordinates

    static const DWORD  dwFVF;      // Vertexformat

};
const DWORD SVertex::dwFVF = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1;
const int   g_iNumCubes = 2048;                                         // Number of Cubes

float   g_fTime = 0.0f;                                                         // Timecounter

SDirect3DParameters g_Direct3DParameters;                   // D3D Parameters

PDIRECT3DTEXTURE9   g_pTexture = NULL;                          // Texture

PDIRECT3DVERTEXBUFFER9 g_pVertexBuffer = NULL;      // Vertex-Buffer

PDIRECT3DINDEXBUFFER9 g_pIndexBuffer = NULL;            // Index-Buffer

tbVector3   g_vCameraPosition;                                          // Position of Cam

float   g_fCameraAngle = 0.0f;                                          // Angle of Cam

float   g_fFOV = TB_DEG_TO_RAD(90.0f);                          // Field of View

tbColor g_FogColor = (0.0f, 0.0f, 0.3f);                // TriBase Fogcolor

DWORD g_dwFogColor = (DWORD)(g_FogColor);                   // Fogcolor

float g_fColorchange = 0.3f;



// function : Render

tbResult Render(float fNumSecsPassed)
{
    // Variables

    HRESULT hResult;
    float   fAspect;
    tbMatrix mCamera;
    tbMatrix mProjection;


    // Clear Picture and Z-Buffer

    if(FAILED(hResult = g_pD3DDevice->Clear(0,
                                                                                    NULL,
                                                                                    D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
                                                                                    g_dwFogColor,
                                                                          1.0f,
                                                                            0)))
    {
        // Error

        TB_ERROR_DIRECTX("g_pD3DDevice->Clear", hResult, TB_STOP);
    }

    // Begin Scene

    g_pD3DDevice->BeginScene();

    // Fog on

    g_pD3DDevice->SetRenderState(D3DRS_FOGENABLE, TRUE);
    // Vertexfog

    g_pD3DDevice->SetRenderState(D3DRS_FOGVERTEXMODE, D3DFOG_LINEAR);
    g_pD3DDevice->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_NONE);
    // Fogcolor

    g_pD3DDevice->SetRenderState(D3DRS_FOGCOLOR, g_dwFogColor);
    // Fogstart and end

    float fFogStart = 60.0f;
    g_pD3DDevice->SetRenderState(D3DRS_FOGSTART, *((DWORD*)(&fFogStart)));
    float fFogEnd = 200.0f;
    g_pD3DDevice->SetRenderState(D3DRS_FOGEND, *((DWORD*)(&fFogEnd)));
    // use real distance of vertex for fog calculating

    g_pD3DDevice->SetRenderState(D3DRS_RANGEFOGENABLE, TRUE);

    // Create and Set Cameramatrix

    mCamera = tbMatrixCamera(g_vCameraPosition,
                                                     g_vCameraPosition + tbVector3(sinf(g_fCameraAngle),
                                                                                                                0.0f,
                                                                                                                cosf(g_fCameraAngle)),
                                                     tbVector3(0.0f, 1.0f, 0.0f));
    g_pD3DDevice->SetTransform(D3DTS_VIEW, (D3DMATRIX*)(&mCamera));

    // Picturepropotion

    fAspect = (float)(g_Direct3DParameters.VideoMode.Width)
              / (float)(g_Direct3DParameters.VideoMode.Height);

    // Create and Set Projectionmatrix

    mProjection = tbMatrixProjection(g_fFOV,
                                                                     fAspect,
                                                                     0.1f,
                                                                     250.0f);
    g_pD3DDevice->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)(&mProjection));

    // Draw all cubes

    // Set Vertex- and Indexbuffer for datasource

    g_pD3DDevice->SetStreamSource(0, g_pVertexBuffer, 0, sizeof(SVertex));
    g_pD3DDevice->SetIndices(g_pIndexBuffer);

    // Draw

    hResult = g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
                                                                                             0,
                                                                                             0,
                                                                                             g_iNumCubes * 8,
                                                                                             0,
                                                                                             g_iNumCubes * 12);
    if(FAILED(hResult))
    {
        // Error

        TB_ERROR_DIRECTX("g_pD3DDevice->DrawIndexedPrimitive", hResult, TB_STOP);
    }

    // End Scene

    g_pD3DDevice->EndScene();

    // Present Scene

    g_pD3DDevice->Present(NULL, NULL, NULL, NULL);

    return TB_OK;
}   // end function : Render




// function : Move

tbResult Move(float fNumSecsPassed)
{
    // Variables

    tbVector3 vCameraDirection;

    // Increase Timecounter

    g_fTime += fNumSecsPassed;
    g_FogColor = (0.0f, 0.0f, 0.3f+0.1f);


    if(GetAsyncKeyState(VK_LEFT)) g_fCameraAngle -= TB_DEG_TO_RAD(45.0f) * fNumSecsPassed;
    if(GetAsyncKeyState(VK_RIGHT)) g_fCameraAngle += TB_DEG_TO_RAD(45.0f) * fNumSecsPassed;
    vCameraDirection = tbVector3(sinf(g_fCameraAngle), 0.0f, cosf(g_fCameraAngle));

    if(GetAsyncKeyState(VK_UP)) 
    {
        g_fColorchange += 0.00005f;
        g_vCameraPosition += vCameraDirection * 10.0f * fNumSecsPassed;
        // Fogcolor

        g_FogColor = (0.0f, 0.0f, g_fColorchange);
        g_dwFogColor = (DWORD)(g_FogColor);
    }
    if(GetAsyncKeyState(VK_DOWN)) 
    {
        g_fColorchange -= 0.00005f;
        g_vCameraPosition -= vCameraDirection * 10.0f * fNumSecsPassed;
        // Fogcolor

        g_FogColor = (0.0f, 0.0f, g_fColorchange);
        g_dwFogColor = (DWORD)(g_FogColor);
    }

    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;

    // Field of View != 180° %% 0°

    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;
}   // end function : Move




// function : Exit Scene

tbResult ExitScene()
{
    // Remove Texture

    g_pD3DDevice->SetTexture(0, NULL);
    TB_SAFE_RELEASE(g_pTexture);

    // Remove Vertex- and Indexbuffer

    g_pD3DDevice->SetStreamSource(0, NULL, 0, 0);
    g_pD3DDevice->SetIndices(NULL);
    TB_SAFE_RELEASE(g_pVertexBuffer);
    TB_SAFE_RELEASE(g_pIndexBuffer);

    return TB_OK;
}   // end function : Exit Scene




// function : Exit Application

tbResult ExitApplication()
{
    // Exit Scene

    ExitScene();

    // Exit D3D and close window

    ExitDirect3D();
    ExitWindow();

    // Exit Engine

    tbExit();

    return TB_OK;
}   // end function : Exit Application




// function : Init Scene

tbResult InitScene()
{
    // Variables

    HRESULT hResult;
    SVertex* pVertices;
    unsigned short* pusIndices;
    tbVector3 vCubePosition;
    int iStartVertex;
    int iStartIndex;


    // Set Vertexformat

    if(FAILED(hResult = g_pD3DDevice->SetFVF(SVertex::dwFVF)))
    {
        // Error

        TB_ERROR_DIRECTX("g_pD3DDevice->SetFVF", hResult, TB_ERROR);
    }

  // Lightning and Culling off

    g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
    g_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
    // Dithering on

    g_pD3DDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE);

    // linear Texturefilter with linear MIP-mapping

    g_pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
    g_pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
    g_pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);

    // Load Texture

    if(FAILED(hResult = D3DXCreateTextureFromFileEx(g_pD3DDevice,
                                                                                                "Texture.bmp",
                                                                                                    D3DX_DEFAULT,
                                                                                                    D3DX_DEFAULT,
                                                                                                    D3DX_DEFAULT,
                                                                                                    0,
                                                                                                    D3DFMT_UNKNOWN,
                                                                                                    D3DPOOL_MANAGED,
                                                                                                    D3DX_FILTER_NONE,
                                                                                                    D3DX_DEFAULT,
                                                                                                    0,
                                                                                                    NULL,
                                                                                                    NULL,
                                                                                                    &g_pTexture)))
    {
        // Error

        TB_ERROR_DIRECTX("D3DXCreateTextureFromFileEx", hResult, TB_ERROR);
    }

    // Set Texture

    g_pD3DDevice->SetTexture(0, g_pTexture);


    // Create Vertexbuffer

    if(FAILED(hResult = g_pD3DDevice->CreateVertexBuffer(g_iNumCubes * 8 * sizeof(SVertex),
                                                                                                             0,
                                                                                                             SVertex::dwFVF,
                                                                                                             D3DPOOL_MANAGED,
                                                                                                             &g_pVertexBuffer,
                                                                                                             NULL)))
    {
        // Error

        TB_ERROR_DIRECTX("g_pD3DDevice->CreateVertexBuffer", hResult, TB_ERROR);
    }

    // Create Indexbuffer 16bit

    if(FAILED(hResult = g_pD3DDevice->CreateIndexBuffer(g_iNumCubes * 36 * 2,
                                                                                                            0,
                                                                                                            D3DFMT_INDEX16,
                                                                                                            D3DPOOL_MANAGED,
                                                                                                            &g_pIndexBuffer,
                                                                                                            NULL)))
    {
        // Error

        TB_ERROR_DIRECTX("g_pD3DDevice->CreateIndexBuffer", hResult, TB_ERROR);
    }

    // Lock Vertex and Indexbuffer

    g_pVertexBuffer->Lock(0, 0, (void**)(&pVertices), D3DLOCK_NOSYSLOCK);
    g_pIndexBuffer->Lock(0, 0, (void**)(&pusIndices), D3DLOCK_NOSYSLOCK);

    // Create Cubes

    for(int iCube = 0; iCube < g_iNumCubes; iCube++)
    {
        // Position = random

        vCubePosition = tbVector3Random() * tbFloatRandom(20.0f, 250.0f);

        // Set Startvertex and Startindex

        iStartVertex = iCube * 8;
        iStartIndex = iCube * 36;

        // Vertexposition

        pVertices[iStartVertex + 0].vPosition = vCubePosition + tbVector3(-1.0f,  1.0f, -1.0f);
        pVertices[iStartVertex + 1].vPosition = vCubePosition + tbVector3(-1.0f,  1.0f,  1.0f);
        pVertices[iStartVertex + 2].vPosition = vCubePosition + tbVector3( 1.0f,  1.0f,  1.0f);
        pVertices[iStartVertex + 3].vPosition = vCubePosition + tbVector3( 1.0f,  1.0f, -1.0f);
        pVertices[iStartVertex + 4].vPosition = vCubePosition + tbVector3(-1.0f, -1.0f, -1.0f);
        pVertices[iStartVertex + 5].vPosition = vCubePosition + tbVector3(-1.0f, -1.0f,  1.0f);
        pVertices[iStartVertex + 6].vPosition = vCubePosition + tbVector3( 1.0f, -1.0f,  1.0f);
        pVertices[iStartVertex + 7].vPosition = vCubePosition + tbVector3( 1.0f, -1.0f, -1.0f);

        for(int iVertex = iStartVertex; iVertex < iStartVertex + 8; iVertex++)
        {
            // Color = random

            pVertices[iVertex].dwColor = tbColorRandom(1.0f) * 2.0f;
            // // Texturecoordinates = random

            pVertices[iVertex].vTexture = tbVector2Random();
        }

        // Set Index

        int aiIndex[36] = {0, 3, 7,   0, 7, 4,  // Front

                                             2, 1, 5,   2, 5, 6,    // Back

                                             1, 0, 4,   1, 4, 5,    // Left

                                         3, 2, 6,   3, 6, 7,    // Right

                                         0, 1, 2,   0, 2, 3,    // Top

                                         6, 5, 4,   6, 4, 7};   // Bottom


        // Assign Index to Index Buffer

        for(int iIndex = 0; iIndex < 36; iIndex++)
        {
            pusIndices[iStartIndex + iIndex] = aiIndex[iIndex] + iStartVertex;
        }
    }

    // Unlock Vertex- and Indexbuffer

    g_pVertexBuffer->Unlock();
    g_pIndexBuffer->Unlock();

    return TB_OK;
}   // end function : Init Scene




// function : Init Application

tbResult InitApplication()
{
    // Variables

    tbResult Result;

    // Init Engine

    tbInit();

    // Get Direct3D Options

    Result = GetDirect3DParameters(&g_Direct3DParameters);
    if(Result == TB_ERROR)
    {
        // Error

        MessageBox(NULL, "Fehler beim Abzählen!", "Fehler",
                             MB_OK | MB_ICONEXCLAMATION);
        return TB_ERROR;
    }
    else if(Result == TB_CANCELED)
    {
        // Error

        return TB_CANCELED;
    }

    // Create Window

    if(InitWindow(g_Direct3DParameters.VideoMode.Width,
                                g_Direct3DParameters.VideoMode.Height,
                                "Vertex- und Index-Buffer",
                                LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1))))
    {
        // Error

        MessageBox(NULL, "Fehler beim Erstellen des Fensters!",
                            "Fehler", MB_OK | MB_ICONEXCLAMATION);
        return TB_ERROR;
    }

    // Initiate Direct3D

    if(InitDirect3D(&g_Direct3DParameters,
                    g_hWindow))
    {
        // Error

        MessageBox(g_hWindow, "Fehler beim Initialisieren von Direct3D!",
                             "Fehler", MB_OK | MB_ICONEXCLAMATION);
        ExitApplication();
        return TB_ERROR;
    }

    // Initiate Scene

    if(InitScene())
    {
        // Error

        MessageBox(g_hWindow, "Fehler beim Initialisieren der Szene!",
                            "Fehler", MB_OK | MB_ICONINFORMATION);
        ExitApplication();
        return TB_ERROR;
    }

    return TB_OK;
}   // end function : Init Application




// function : main

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char* pcCmdLine, int iShowCmd)
{
    // Variables

    tbResult Result;

    // Initiate Application

    Result = InitApplication();
    if(Result == TB_CANCELED) return 0;
    else if(Result == TB_ERROR)
    {
        MessageBox(NULL, "Fehler beim Initialisieren der Anwendung!",
                   "Fehler", MB_OK | MB_ICONEXCLAMATION);
        return 1;
    }

    // Messageloop

    tbDoMessageLoop(Render, Move);

    // Exit Application

    ExitApplication();

    return 0;
}   // end function : main

Chase

Alter Hase

Beiträge: 753

Wohnort: Nagaoka / Darmstadt / Düsseldorf

Beruf: fauler Studi

  • Private Nachricht senden

2

05.03.2007, 14:57

Du musst den Backbuffer mit der Nebelfarbe 'loeschen'
"Have you tried turning it off and on again?"

3

05.03.2007, 19:19

ah k ja das wars

danke :)


edit: achja zweite frage wär noch offen:

warum ändert sich die nebelfarbe mit der geschwindigkeit der kamera? oder soll das nur ne sinnlosübung sein?

Werbeanzeige