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

Pak-X

Frischling

  • »Pak-X« ist der Autor dieses Themas

Beiträge: 4

Wohnort: Lüneburg

Beruf: Schüler

  • Private Nachricht senden

1

02.04.2006, 18:27

C++ 2D ArrayProblem

Ich habe ein Problem mit C++ und 2 dimensionalen Array.

Also das war ich Programmiere soll ein Renderer werden. In 2 Jahren vieleicht einmal :P
Nun eigentlich mache ich das ganze nur, um zu üben und um C++ besser zu verstehen. An sich stecken noch keine wirklichen Berechnungen drinne.

Mein Problem ist, dass er die 2 Dimensionalen Array in der Funktion GetPixelRBG mit zufallszahlen füllen soll. Dazu Randomized er sich eine beliebige Pixelposition, woraus sich dann auch die beiden Angaben für die Dimensionen ergeben. Wenn ich nun 10 ( Hauptschleife wird nur 10 mal ausgeführt) mit zufallszahlen belegen will, dann belelegt er in einem durchgang z.b nicht nur [2,3] sondern dann auch [3,3] [4,3] [5,3] , usw also alle werte in der ersten Dimension. Ich hoffe ihr versteht was ich meine.

Ich könnt ja einmal den code kompilieren. Dann 60 mal 60 oder so eingeben und dann mal schauen was er dann unten so ausgibt.

Ich habe schon sehr viel rumprobiert, aber irgentwie bekomme ich das Problem nicht weg.

Quellcode

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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#include <iostream>
#include <windows.h>
#include <stdio.h>

#pragma comment (lib, "winmm.lib")

using namespace std;







// Resolution
int x_res = 0;
int y_res = 0;

// QualitySettings

int AA_Quality=10;

float CommaDivisor;
int CommaRange;

// CameraSettings
float CameraArc = 90;
float CameraArc_x = 0;
float CameraArc_y = 0;
// Compute the Comma position for the CamRay Angles
float CommaPos_x;
float CommaPos_y;

int pixelrate = 0;

// At first we need the class Pixel
// Declaration of the Classgroup pPixel 

class Pixel
{
public:

    // variables of the pixel
    float   Red;        // The red of the pixel
    float   Blue;       // The blue of the pixel    
    float   Green;      // The green of the pixel
    float   Alpha;      // The alpha of the pixel
    int     computed;   // how often is the pixel computed  

    void    PixelTrace(int &x_res, int &y_res);     //Function to get Pixeldata


    // Function to compute the pixel


    Pixel::Pixel()  // Construct the Pixel
    {
        Red         = 0;
        Blue        = 0;
        Green       = 0;
        Alpha       = 0;
        computed    = 1;
    }

    
};

////////////////
////////////////

class CamRay
{
public:

    // variables of the CamRay
    float   Red;        // The red of the pixel
    float   Blue;       // The blue of the pixel    
    float   Green;      // The green of the pixel
    float   Alpha;      // The alpha of the pixel
    float   Angle_y_start;
    float   Angle_x_start;
    float   Angle_y;
    float   Angle_x;
    float   y_pos;
    float   x_pos;
    float   z_pos;





    // Function to compute the pixel


    CamRay::CamRay()    
    {
        Red                 = 0;
        Blue                = 0;
        Green               = 0;
        Alpha               = 0;
        Angle_y_start       = 0;
        Angle_x_start       = 0;
        Angle_y             = 0;
        Angle_x             = 0;
        y_pos               = 0;
        x_pos               = 0;
        z_pos               = 0;

        //cout << "CamRay done"<<endl;
    }


        

void GenCamRay();           // Create new CamRay
    
};


class LightRay
{
public:

    // variables of the CamRay
    float   Red;        // The red of the pixel
    float   Blue;       // The blue of the pixel    
    float   Green;      // The green of the pixel
    float   Alpha;      // The alpha of the pixel
    float   Angle_y_start;
    float   Angle_x_start;
    float   Angle_y;
    float   Angle_x;
    float   y_pos;
    float   x_pos;
    float   z_pos;





    // Function to compute the pixel


    LightRay::LightRay()    
    {
        Red                 = 0;
        Blue                = 0;
        Green               = 0;
        Alpha               = 0;
        Angle_y             = 0;
        Angle_x             = 0;
        y_pos               = 0;
        x_pos               = 0;
        z_pos               = 0;
    }




void GenCamRay();           // Create new CamRay
    
};



////////////////
////////////////

class Camera
{
public:

    // variables of the CamRay
    float   Angle_y;        
    float   Angle_x;    
    float   Angle_z;    
    float   y_pos;
    float   x_pos;
    float   z_pos;








    Camera::Camera()    
    {

        Angle_z             = 0;
        Angle_y             = 0;
        Angle_x             = 0;
        y_pos               = 0;
        x_pos               = 0;
        z_pos               = 0;
    }

    
};


////////////////
////////////////

//  Variables 






// Prototypes of the funktions
//  Pixelfunktions


    void    GetResolution       (int &x_res, int &y_res);
    void    GetPixelAverage     (int &x_res, int &y_res);
    void    PixelGenerate       (int &x_res, int &y_res);
    
    void    GetCameraArc        (int &x_res, int &y_res, float &CameraArc_x , float &CameraArc_y , float &CameraArc);
    
    void    MainProcess         ();
    //////
    void    GenCamRay           ();
    void    GenLightRay         ();
    void    DeleteRays          ();
    //////
    void    GetPixelPos         ();
    int     CompPixelPos        (int res, float CameraArc_xy, float PixelAngel_xy );
    int     CheckPixelPos       (int res, int PixelPos);
    int     PixelPos_x;
    int     PixelPos_y;
    //////

    void    GetCommaRange       (int &Commarange , float &CommaDivisor);
    //////
    void    GetPixelRBG         ();
    float   CompPixelRBG        (float PixelColor, float RayColor);
    //////
    //////
    void    Anzeiger            ();


// Generate all Classes
Pixel       *pPixel     = NULL;
CamRay      *pCamRay    = NULL;
LightRay    *pLightRay  = NULL; 





/////////////////////////////////////////////////////////////////////
// MainFunction
int main()
{

    // Function GetReslution to get the resolution


    GetResolution(x_res, y_res);

    // Get the Camera X and Y Arc

    GetCameraArc(x_res, y_res, CameraArc_x , CameraArc_y , CameraArc);

    //  Generate Instances of the Pixel with ResolutionData

    PixelGenerate(x_res, y_res);

    // Initialize the Randomizer

    srand (timeGetTime ());

    // Get the Comma Range, because we dont render to anything without seeing it.

    GetCommaRange(CommaRange, CommaDivisor);

    //  Activate the MainProcess Iteration the Calculate the whole image

    MainProcess();



    getchar();
    getchar();

    return 0;

}








///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////

//Funktions

void MainProcess()  //This is the Main processing routine. Starts when all things are initialised 
{
    int kp = 0;
    do                      // Do while because rendering never ends. Or you press Excape
    {
        int kp2 = 0;
        do
        {
        GenCamRay();   // Camera generates a random Ray to get Pixeldata
        GenLightRay();
        GetPixelPos();
        GetPixelRBG();
        DeleteRays();
        kp2++;
        } while ( kp2 < 10 );

        kp++;
    



    } while ( kp < 1);
    Anzeiger();
    cout << "Finished !!!"<< endl;
}


///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////


void GetResolution(int &x_res,int &y_res)           // Get the resolution of the Picture (Only for the first tests)
{
    cout << "Picture resolution width         :"; 
    cin >> x_res;
    cout << "Picture resolution high          :"; 
    cin >> y_res;

    cout << "Resolution                       :" << x_res <<"*"<< y_res << endl;
}


    // Generate instance



///////////////////////////////////////////////////////////////////////



void PixelGenerate(int &x_res, int &y_res)      //  Generate all Pixels for the picture
{
    pPixel = new Pixel[x_res,y_res];
    cout << "Memory used for Pixels           :" << sizeof (pPixel) * x_res * y_res / 1024  << " kbyte" << endl;

} 



///////////////////////////////////////////////////////////////////////



// Function to get the Y and X angle of the Camera
void GetCameraArc(int &x_res, int &y_res, float &CameraArc_x , float &CameraArc_y , float &CameraArc)
{
if (y_res > x_res)
    {
    CameraArc_x = CameraArc / y_res * x_res;
    CameraArc_y = CameraArc;
    }
if (y_res < x_res)
    {
    CameraArc_y = CameraArc / x_res * y_res;
    CameraArc_x = CameraArc;
    }
if (y_res == x_res)
    {
    CameraArc_y = CameraArc;
    CameraArc_x = CameraArc;
    }
    
    cout << "CameraArc_x                      :" << CameraArc_x << endl;
    cout << "CameraArc_y                      :" << CameraArc_y << endl;

}

///////////////////////////////////////////////////////////////////////
// Get the Right-Comma-Range because the renderer dont have to render for example "CamAngle = 20.134213" when you have only 500 Pixels.... better "20.13"
void GetCommaRange(int &Commarange , float &CommaDivisor)
{

    // Get the Comma Range
    CommaRange = AA_Quality * ((x_res + y_res) / 2) / 20; 
                        
    // Get the Comma divisor



    if (CommaRange > 100)
    {
        CommaDivisor = 1000;
    }

    if (CommaRange > 1000)
    {
        CommaDivisor = 10000;
    }

    if (CommaRange > 10000)
    {
        CommaDivisor = 100000;
    }

    if (CommaRange > 100000)
    {   
        CommaDivisor = 1000000;
    }
    if (CommaRange < 100)
    {
        CommaDivisor = 10;
    }
}




///////////////////////////////////////////////////////////////////////










///////////////////////////////////////////////////////////////////////
// FUNCTIONS FOR THE MAINPROCESS
///////////////////////////////////////////////////////////////////////

// First we have to generate and declare a CamRay

void GenCamRay()          // Generate new cam ray
{
    
    pCamRay = new CamRay[1];  // Make new Camray
    pCamRay[1].Angle_x =    (rand()%static_cast<int>(CameraArc_x)) + (rand()%static_cast<int>(CommaRange))/CommaDivisor;
    pCamRay[1].Angle_y =    (rand()%static_cast<int>(CameraArc_y)) + (rand()%static_cast<int>(CommaRange))/CommaDivisor;

}

void DeleteRays()
{
     delete pCamRay;
     pCamRay = NULL;
}


void GenLightRay()
{
    pLightRay = new LightRay[1];
}

///////////////////////////////////////////////////////////////////////

void GetPixelPos()
{
    PixelPos_y      = CompPixelPos(y_res, CameraArc_y, pCamRay[1].Angle_y);
    PixelPos_x      = CompPixelPos(x_res, CameraArc_x, pCamRay[1].Angle_x);

    // If Mistake ...

    PixelPos_y = CheckPixelPos  (y_res, PixelPos_y);
    PixelPos_x = CheckPixelPos  (x_res, PixelPos_x);


//  cout << "CamAngleX: " << pCamRay[1].Angle_x << " CamAngleY: " << pCamRay[1].Angle_y << " PixelPosX: "<< PixelPos_x << " PixelPosY: " << PixelPos_y << endl; 


}

int CompPixelPos(int res, float CameraArc_xy, float PixelAngel_xy )
{
    int PixelPos = res / CameraArc_xy * PixelAngel_xy;

    return PixelPos;
}

int CheckPixelPos(int res, int PixelPos)
{
    if (PixelPos > res)
        return res;
        else
        return PixelPos;
}

///////////////////////////////////////////////////////////////////////
// Compute the Final PixelColor ( Not used at the moment )

void GetPixelRBG()
{
    


    pPixel[PixelPos_x,PixelPos_y].Red   = CompPixelRBG  (pPixel[PixelPos_x,PixelPos_y].Red,     pLightRay[1].Red);
    pPixel[PixelPos_x,PixelPos_y].Blue  = CompPixelRBG  (pPixel[PixelPos_x,PixelPos_y].Blue,    pLightRay[1].Blue);
    pPixel[PixelPos_x,PixelPos_y].Green = CompPixelRBG  (pPixel[PixelPos_x,PixelPos_y].Green,   pLightRay[1].Green);

    cout << " Y " << PixelPos_y << " X " << PixelPos_x << endl;
}

float   CompPixelRBG (float PixelColor, float RayColor)
{
    PixelColor =  (rand()%10)   ;


    return PixelColor;
}





////////////////////////////////////

//Anzeiger
void Anzeiger()
{
for     (int i = 1; i < y_res; i++)
{
    for ( int j = 1; j < x_res; j++)
    {
        cout << pPixel[i,j].Red;
    }
    cout << endl;
}

cout << pPixel[1,3].Red;
cout << pPixel[1,5].Red;
cout << pPixel[2,3].Red;
cout << pPixel[3,3].Red;
cout << pPixel[5,3].Red;
cout << pPixel[7,3].Red;

}


Ich hoffe jemand kann mir helfen. Und wenn das Problem wieder so billig ist wie mein letztes, dann beiß ich mir in den Allerwertesten :P

Anonymous

unregistriert

2

02.04.2006, 18:41

C-/C++-Quelltext

1
2
3
4
5
6
cout << pPixel[1,3].Red; 
cout << pPixel[1,5].Red; 
cout << pPixel[2,3].Red; 
cout << pPixel[3,3].Red; 
cout << pPixel[5,3].Red; 
cout << pPixel[7,3].Red;

Das ist jedenfalls kein C oder C+.

Pak-X

Frischling

  • »Pak-X« ist der Autor dieses Themas

Beiträge: 4

Wohnort: Lüneburg

Beruf: Schüler

  • Private Nachricht senden

3

02.04.2006, 18:47

Das habe ich nur eingegeben, um zu sehen ob der Fehler evtl. an der "Anzeigen"-Schleife liegt. Das gehört natürlich normalerweise nicht darein.
Ergibt auch sonst keinen sinn.

Anonymous

unregistriert

4

02.04.2006, 18:59

Pak-X
Der ergibt auch so keinen Sinn, da man so nicht auf 2D Arrays zugreift!

Phili

unregistriert

5

02.04.2006, 19:04

nicht

C-/C++-Quelltext

1
Pixel[1,2]; 


sondern

C-/C++-Quelltext

1
Pixel[1][2];

Pak-X

Frischling

  • »Pak-X« ist der Autor dieses Themas

Beiträge: 4

Wohnort: Lüneburg

Beruf: Schüler

  • Private Nachricht senden

6

02.04.2006, 19:14

Ja da bin ich auch schon drauf gekommen aber wenn ich das ClassArray erstellen will. beziehungsweise neue Instanzen je nach PixelAnzahl erstellen möchte, dann gibt er mir bei deiner variante eine Fehlermeldung aus.

C-/C++-Quelltext

1
2
3
4
5
6
void PixelGenerate(int &x_res, int &y_res)      //  Generate all Pixels for the picture

{
    pPixel = new Pixel[x_res,y_res];
    cout << "Memory used for Pixels           :" << sizeof (pPixel) * x_res * y_res / 1024  << " kbyte" << endl;

}


Der kann leider mit "...Pixel[x_res][y_res];" nichts anfangen. Ich weis auch nicht warum.

edit: Er meint dann immer, dass die Variablen ja nicht Konstant sind, aber wie zum Teufen mache ich aus einer Variablen eine Konstante ?

"const int x_res_const = x_res;" geht zum Beispiel ja auch nicht. Da ja x_res nicht Konstant ist.

Args zum Haareraufen

Phili

unregistriert

7

02.04.2006, 19:25

Zitat

edit: Er meint dann immer, dass die Variablen ja nicht Konstant sind, aber wie zum Teufen mache ich aus einer Variablen eine Konstante ?

"const int x_res_const = x_res;" geht zum Beispiel ja auch nicht. Da ja x_res nicht Konstant ist.

Args zum Haareraufen


ROFL

Anonymous

unregistriert

8

02.04.2006, 19:29

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
namespace ttl
{
    // 2D array

template<typename T> class array2d 
{ 
public: 
    array2d (void) : row_(0), col_(0), data_(NULL) 
    {}
    array2d (unsigned long row, unsigned long col) : row_(row), col_(col), data_(NULL)
    { data_ = new T[row_*col_]; } 
    array2d (const array2d& other) : row_(other.row_), col_(other.col_), data_(NULL)
    { for (unsigned long i=0; i<row_*col_; ++i) data_[i] = other.data_[i]; }
    ~array2d (void) 
    { delete [] data_; } 

    inline T* operator[] (unsigned long line) 
    { return (&data_[line * col_]); } 

private: 
    T*              data_; 
    unsigned long   row_;
    unsigned long   col_; 
};

} // Namespace: ttl

Dies mal möchte ich nicht den Assclown-Award verleihen

Werbeanzeige