tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
12
ZUGRIFFE
745
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    BloodOrange BloodOrange ist offline Grünschnabel
    Registriert seit
    Nov 2008
    Beiträge
    4
    Einen schünen guten tag und hallo Forum.

    Nunja wie die Topic schon sagt suche ich einen weg wie ich zur Laufzeit eines Programmes den Quellcode ändern kann.

    Vorgeschichte:
    Ich möchte mir ein kleines game in OpenGL / GLUT basteln.
    Da ich ein fauler Mensch bin und die Modell mit Blender mach und mit einen Exporter in OpenGL Code umwandle, damit ich diesen Code, der in einer Extra Datei ist, irgendwie in den Quellcode "importieren" kann.
    Das man das auch Hardcode Technisch machen kann, sprich code kopieren und in Quellcode einfügen, weis ich.
    Doch müsste das ja auch irgendwie anders gehen, siehe second life und ihr "user generated content".

    Hoffe das soweit alles ist was ihr an informationen braucht

    Mfg
    Austen
     

  2. #2
    RenderWilli RenderWilli ist offline Mitglied Silber
    Registriert seit
    Oct 2008
    Beiträge
    69
    Ist es nicht besser, die Objekte extern zu speichern und dann zur Laufzeit einfach zu laden?
    Wenn man die Daten zur Laufzeit in das Programm schreiben will, wirds schwierig.
     

  3. #3
    BloodOrange BloodOrange ist offline Grünschnabel
    Registriert seit
    Nov 2008
    Beiträge
    4
    Ich fragte ja nach verschiedenen varianten
    Ich kenn das halt von OpenGL so das alle Objekte in der "draw function" eingebunden werden, dargestellt werden.
    Nun hätte ich gedachte, das man das durch bearbeiten des codes erreicht,
    aber wenn du mehr informationen zum laden von Objekten hast bin ich ganz Ohr
     

  4. #4
    3Cyb3r 3Cyb3r ist offline Mitglied Brokat
    Registriert seit
    May 2008
    Ort
    Baunatal (Hessen)
    Beiträge
    413
    inwiefern Objekt? was für eins(was für eine Datei)?
     
    Bei der Geburt ist ein Mensch so unfertig, wie ein an einem schönen blauen Montag in Taiwan hergestellter Computer vor der Programmierung.
    - Wolfgang Körner

  5. #5
    RenderWilli RenderWilli ist offline Mitglied Silber
    Registriert seit
    Oct 2008
    Beiträge
    69
    Du könntest die 3D-Daten als Obj-Datei speichern und diese in deinem Programm wieder einladen. Du musst dir nur Methoden schreiben, die das korrekt machen und gleichzeitig die Texturen laden und zuweisen können. Das wäre schon alles.
    Damit bräuchtest du dein Programm nicht neu zu erstellen und kannst die externen Daten nach belieben verändern.

    Schau dir vielleicht mal Ogre an, eventuell kannst du damit alles machen.
    Geändert von RenderWilli (07.11.08 um 20:29 Uhr)
     

  6. #6
    BloodOrange BloodOrange ist offline Grünschnabel
    Registriert seit
    Nov 2008
    Beiträge
    4
    Ok danke erstmal, ich werde mir mal das OGRE ansehen.
    Falls denn doch noch was fehlen sollte melde ich mich wieder

    3Cyb3r: also das ist ein export aus Blender, in der datei steht code in klartext, und wenn ich den gesamten text daraus kopieren würde und in meinen quellcode einfügen würde, hätte ich die figur auf den bildschirm

    RenderWilli: was da jetzt ein kleines prob wäre ... ich wüsste nicht wie man das mit der obj datei machen könnte, bzw was für methoden du dir da vorstellst

    Naja, aber wie gesagt, danke erstmal für die hilfe

    Mfg
    Austen
     

  7. #7
    3Cyb3r 3Cyb3r ist offline Mitglied Brokat
    Registriert seit
    May 2008
    Ort
    Baunatal (Hessen)
    Beiträge
    413
    eine Frage noch geth auch c++?
     
    Bei der Geburt ist ein Mensch so unfertig, wie ein an einem schönen blauen Montag in Taiwan hergestellter Computer vor der Programmierung.
    - Wolfgang Körner

  8. #8
    3Cyb3r 3Cyb3r ist offline Mitglied Brokat
    Registriert seit
    May 2008
    Ort
    Baunatal (Hessen)
    Beiträge
    413
    du könntest das aus Blender ja auch als z.b. md2 modell speichern und dir hierfür eine Moddel-Loader schreiben. Ach und ein andere Möglichkeit wäre per Polymorphismus während der Lauzeit zu unterscheiden welches Objekt du haben möchtest(virtuelle Funktion, vererbung).
     
    Bei der Geburt ist ein Mensch so unfertig, wie ein an einem schönen blauen Montag in Taiwan hergestellter Computer vor der Programmierung.
    - Wolfgang Körner

  9. #9
    3Cyb3r 3Cyb3r ist offline Mitglied Brokat
    Registriert seit
    May 2008
    Ort
    Baunatal (Hessen)
    Beiträge
    413
    www.morrowland.com hier kannst du dir Code downloaden wo es eine 3ds-Loader und ms3d-Loader kannst du dir ja mal anschauen.
    Code :
    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
    
    /* Permission is hereby granted, free of charge, to any person obtaining a copy
     * of this software and associated documentation files (the "Software"), to deal
     * in the Software without restriction, including without limitation the rights
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     * copies of the Software, and to permit persons to whom the Software is
     * furnished to do so, subject to the following conditions:
     * 
     * The above notice and this permission notice shall be included in all copies
     * or substantial portions of the Software.
     * 
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     * SOFTWARE.
     */
     
     
     
    #ifndef MD2_MODEL_H_INCLUDED
    #define MD2_MODEL_H_INCLUDED
     
    #include <vector>
     
    #ifdef __APPLE__
    #include <OpenGL/OpenGL.h>
    #include <GLUT/glut.h>
    #else
    #include <GL/glut.h>
    #endif
     
    #include "vec3f.h"
     
    struct MD2Vertex {
        Vec3f pos;
        Vec3f normal;
    };
     
    struct MD2Frame {
        char name[16];
        MD2Vertex* vertices;
    };
     
    struct MD2TexCoord {
        float texCoordX;
        float texCoordY;
    };
     
    struct MD2Triangle {
        int vertices[3];  //The indices of the vertices in this triangle
        int texCoords[3]; //The indices of the texture coordinates of the triangle
    };
     
    class MD2Model {
        private:
            MD2Frame* frames;
            int numFrames;
            MD2TexCoord* texCoords;
            MD2Triangle* triangles;
            int numTriangles;
            std::vector<GLuint> textureIds;
            
            int startFrame; //The first frame of the current animation
            int endFrame;   //The last frame of the current animation
            
            MD2Model();
        public:
            ~MD2Model();
            
            //Switches to the given animation
            void setAnimation(const char* name);
            /* Draws the state of the animated model at the specified time in the
             * animation.  A time of i, integer i, indicates the beginning of the
             * animation, and a time of i + 0.5 indicates halfway through the
             * animation.  textureNum is the index of the texture that is used when
             * drawing the model.
             */
            void draw(int textureNum, float time);
            
            //Loads an MD2Model from the specified file, loading texture images from
            //the indicated files.  Returns NULL if there was an error loading it.
            static MD2Model* load(const char* filename,
                                  std::vector<const char*> textureFilenames);
    };
     
     
     
     
     
     
     
     
     
     
    #endif

    Code :
    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
    
    /* Permission is hereby granted, free of charge, to any person obtaining a copy
     * of this software and associated documentation files (the "Software"), to deal
     * in the Software without restriction, including without limitation the rights
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     * copies of the Software, and to permit persons to whom the Software is
     * furnished to do so, subject to the following conditions:
     * 
     * The above notice and this permission notice shall be included in all copies
     * or substantial portions of the Software.
     * 
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     * SOFTWARE.
     */
     
     
     
    #include <fstream>
    #include <vector>
     
    #include "imageloader.h"
    #include "md2model.h"
     
    using namespace std;
     
    namespace {
        //Normals used in the MD2 file format
        float NORMALS[486] =
            {-0.525731f,  0.000000f,  0.850651f,
             -0.442863f,  0.238856f,  0.864188f,
             -0.295242f,  0.000000f,  0.955423f,
             -0.309017f,  0.500000f,  0.809017f,
             -0.162460f,  0.262866f,  0.951056f,
              0.000000f,  0.000000f,  1.000000f,
              0.000000f,  0.850651f,  0.525731f,
             -0.147621f,  0.716567f,  0.681718f,
              0.147621f,  0.716567f,  0.681718f,
              0.000000f,  0.525731f,  0.850651f,
              0.309017f,  0.500000f,  0.809017f,
              0.525731f,  0.000000f,  0.850651f,
              0.295242f,  0.000000f,  0.955423f,
              0.442863f,  0.238856f,  0.864188f,
              0.162460f,  0.262866f,  0.951056f,
             -0.681718f,  0.147621f,  0.716567f,
             -0.809017f,  0.309017f,  0.500000f,
             -0.587785f,  0.425325f,  0.688191f,
             -0.850651f,  0.525731f,  0.000000f,
             -0.864188f,  0.442863f,  0.238856f,
             -0.716567f,  0.681718f,  0.147621f,
             -0.688191f,  0.587785f,  0.425325f,
             -0.500000f,  0.809017f,  0.309017f,
             -0.238856f,  0.864188f,  0.442863f,
             -0.425325f,  0.688191f,  0.587785f,
             -0.716567f,  0.681718f, -0.147621f,
             -0.500000f,  0.809017f, -0.309017f,
             -0.525731f,  0.850651f,  0.000000f,
              0.000000f,  0.850651f, -0.525731f,
             -0.238856f,  0.864188f, -0.442863f,
              0.000000f,  0.955423f, -0.295242f,
             -0.262866f,  0.951056f, -0.162460f,
              0.000000f,  1.000000f,  0.000000f,
              0.000000f,  0.955423f,  0.295242f,
             -0.262866f,  0.951056f,  0.162460f,
              0.238856f,  0.864188f,  0.442863f,
              0.262866f,  0.951056f,  0.162460f,
              0.500000f,  0.809017f,  0.309017f,
              0.238856f,  0.864188f, -0.442863f,
              0.262866f,  0.951056f, -0.162460f,
              0.500000f,  0.809017f, -0.309017f,
              0.850651f,  0.525731f,  0.000000f,
              0.716567f,  0.681718f,  0.147621f,
              0.716567f,  0.681718f, -0.147621f,
              0.525731f,  0.850651f,  0.000000f,
              0.425325f,  0.688191f,  0.587785f,
              0.864188f,  0.442863f,  0.238856f,
              0.688191f,  0.587785f,  0.425325f,
              0.809017f,  0.309017f,  0.500000f,
              0.681718f,  0.147621f,  0.716567f,
              0.587785f,  0.425325f,  0.688191f,
              0.955423f,  0.295242f,  0.000000f,
              1.000000f,  0.000000f,  0.000000f,
              0.951056f,  0.162460f,  0.262866f,
              0.850651f, -0.525731f,  0.000000f,
              0.955423f, -0.295242f,  0.000000f,
              0.864188f, -0.442863f,  0.238856f,
              0.951056f, -0.162460f,  0.262866f,
              0.809017f, -0.309017f,  0.500000f,
              0.681718f, -0.147621f,  0.716567f,
              0.850651f,  0.000000f,  0.525731f,
              0.864188f,  0.442863f, -0.238856f,
              0.809017f,  0.309017f, -0.500000f,
              0.951056f,  0.162460f, -0.262866f,
              0.525731f,  0.000000f, -0.850651f,
              0.681718f,  0.147621f, -0.716567f,
              0.681718f, -0.147621f, -0.716567f,
              0.850651f,  0.000000f, -0.525731f,
              0.809017f, -0.309017f, -0.500000f,
              0.864188f, -0.442863f, -0.238856f,
              0.951056f, -0.162460f, -0.262866f,
              0.147621f,  0.716567f, -0.681718f,
              0.309017f,  0.500000f, -0.809017f,
              0.425325f,  0.688191f, -0.587785f,
              0.442863f,  0.238856f, -0.864188f,
              0.587785f,  0.425325f, -0.688191f,
              0.688191f,  0.587785f, -0.425325f,
             -0.147621f,  0.716567f, -0.681718f,
             -0.309017f,  0.500000f, -0.809017f,
              0.000000f,  0.525731f, -0.850651f,
             -0.525731f,  0.000000f, -0.850651f,
             -0.442863f,  0.238856f, -0.864188f,
             -0.295242f,  0.000000f, -0.955423f,
             -0.162460f,  0.262866f, -0.951056f,
              0.000000f,  0.000000f, -1.000000f,
              0.295242f,  0.000000f, -0.955423f,
              0.162460f,  0.262866f, -0.951056f,
             -0.442863f, -0.238856f, -0.864188f,
             -0.309017f, -0.500000f, -0.809017f,
             -0.162460f, -0.262866f, -0.951056f,
              0.000000f, -0.850651f, -0.525731f,
             -0.147621f, -0.716567f, -0.681718f,
              0.147621f, -0.716567f, -0.681718f,
              0.000000f, -0.525731f, -0.850651f,
              0.309017f, -0.500000f, -0.809017f,
              0.442863f, -0.238856f, -0.864188f,
              0.162460f, -0.262866f, -0.951056f,
              0.238856f, -0.864188f, -0.442863f,
              0.500000f, -0.809017f, -0.309017f,
              0.425325f, -0.688191f, -0.587785f,
              0.716567f, -0.681718f, -0.147621f,
              0.688191f, -0.587785f, -0.425325f,
              0.587785f, -0.425325f, -0.688191f,
              0.000000f, -0.955423f, -0.295242f,
              0.000000f, -1.000000f,  0.000000f,
              0.262866f, -0.951056f, -0.162460f,
              0.000000f, -0.850651f,  0.525731f,
              0.000000f, -0.955423f,  0.295242f,
              0.238856f, -0.864188f,  0.442863f,
              0.262866f, -0.951056f,  0.162460f,
              0.500000f, -0.809017f,  0.309017f,
              0.716567f, -0.681718f,  0.147621f,
              0.525731f, -0.850651f,  0.000000f,
             -0.238856f, -0.864188f, -0.442863f,
             -0.500000f, -0.809017f, -0.309017f,
             -0.262866f, -0.951056f, -0.162460f,
             -0.850651f, -0.525731f,  0.000000f,
             -0.716567f, -0.681718f, -0.147621f,
             -0.716567f, -0.681718f,  0.147621f,
             -0.525731f, -0.850651f,  0.000000f,
             -0.500000f, -0.809017f,  0.309017f,
             -0.238856f, -0.864188f,  0.442863f,
             -0.262866f, -0.951056f,  0.162460f,
             -0.864188f, -0.442863f,  0.238856f,
             -0.809017f, -0.309017f,  0.500000f,
             -0.688191f, -0.587785f,  0.425325f,
             -0.681718f, -0.147621f,  0.716567f,
             -0.442863f, -0.238856f,  0.864188f,
             -0.587785f, -0.425325f,  0.688191f,
             -0.309017f, -0.500000f,  0.809017f,
             -0.147621f, -0.716567f,  0.681718f,
             -0.425325f, -0.688191f,  0.587785f,
             -0.162460f, -0.262866f,  0.951056f,
              0.442863f, -0.238856f,  0.864188f,
              0.162460f, -0.262866f,  0.951056f,
              0.309017f, -0.500000f,  0.809017f,
              0.147621f, -0.716567f,  0.681718f,
              0.000000f, -0.525731f,  0.850651f,
              0.425325f, -0.688191f,  0.587785f,
              0.587785f, -0.425325f,  0.688191f,
              0.688191f, -0.587785f,  0.425325f,
             -0.955423f,  0.295242f,  0.000000f,
             -0.951056f,  0.162460f,  0.262866f,
             -1.000000f,  0.000000f,  0.000000f,
             -0.850651f,  0.000000f,  0.525731f,
             -0.955423f, -0.295242f,  0.000000f,
             -0.951056f, -0.162460f,  0.262866f,
             -0.864188f,  0.442863f, -0.238856f,
             -0.951056f,  0.162460f, -0.262866f,
             -0.809017f,  0.309017f, -0.500000f,
             -0.864188f, -0.442863f, -0.238856f,
             -0.951056f, -0.162460f, -0.262866f,
             -0.809017f, -0.309017f, -0.500000f,
             -0.681718f,  0.147621f, -0.716567f,
             -0.681718f, -0.147621f, -0.716567f,
             -0.850651f,  0.000000f, -0.525731f,
             -0.688191f,  0.587785f, -0.425325f,
             -0.587785f,  0.425325f, -0.688191f,
             -0.425325f,  0.688191f, -0.587785f,
             -0.425325f, -0.688191f, -0.587785f,
             -0.587785f, -0.425325f, -0.688191f,
             -0.688191f, -0.587785f, -0.425325f};
        
        //Returns whether the system is little-endian
        bool littleEndian() {
            //The short value 1 has bytes (1, 0) in little-endian and (0, 1) in
            //big-endian
            short s = 1;
            return (((char*)&s)[0]) == 1;
        }
        
        //Converts a four-character array to an integer, using little-endian form
        int toInt(const char* bytes) {
            return (int)(((unsigned char)bytes[3] << 24) |
                         ((unsigned char)bytes[2] << 16) |
                         ((unsigned char)bytes[1] << 8) |
                         (unsigned char)bytes[0]);
        }
        
        //Converts a two-character array to a short, using little-endian form
        short toShort(const char* bytes) {
            return (short)(((unsigned char)bytes[1] << 8) |
                           (unsigned char)bytes[0]);
        }
        
        //Converts a two-character array to an unsigned short, using little-endian
        //form
        unsigned short toUShort(const char* bytes) {
            return (unsigned short)(((unsigned char)bytes[1] << 8) |
                                    (unsigned char)bytes[0]);
        }
        
        //Converts a four-character array to a float, using little-endian form
        float toFloat(const char* bytes) {
            float f;
            if (littleEndian()) {
                ((char*)&f)[0] = bytes[0];
                ((char*)&f)[1] = bytes[1];
                ((char*)&f)[2] = bytes[2];
                ((char*)&f)[3] = bytes[3];
            }
            else {
                ((char*)&f)[0] = bytes[3];
                ((char*)&f)[1] = bytes[2];
                ((char*)&f)[2] = bytes[1];
                ((char*)&f)[3] = bytes[0];
            }
            return f;
        }
        
        //Reads the next four bytes as an integer, using little-endian form
        int readInt(ifstream &input) {
            char buffer[4];
            input.read(buffer, 4);
            return toInt(buffer);
        }
        
        //Reads the next two bytes as a short, using little-endian form
        short readShort(ifstream &input) {
            char buffer[2];
            input.read(buffer, 2);
            return toShort(buffer);
        }
        
        //Reads the next two bytes as an unsigned short, using little-endian form
        unsigned short readUShort(ifstream &input) {
            char buffer[2];
            input.read(buffer, 2);
            return toUShort(buffer);
        }
        
        //Reads the next four bytes as a float, using little-endian form
        float readFloat(ifstream &input) {
            char buffer[4];
            input.read(buffer, 4);
            return toFloat(buffer);
        }
        
        //Calls readFloat three times and returns the results as a Vec3f object
        Vec3f readVec3f(ifstream &input) {
            float x = readFloat(input);
            float y = readFloat(input);
            float z = readFloat(input);
            return Vec3f(x, y, z);
        }
        
        //Makes the image into a texture, and returns the id of the texture
        GLuint loadTexture(Image *image) {
            GLuint textureId;
            glGenTextures(1, &textureId);
            glBindTexture(GL_TEXTURE_2D, textureId);
            glTexImage2D(GL_TEXTURE_2D,
                         0,
                         GL_RGB,
                         image->width, image->height,
                         0,
                         GL_RGB,
                         GL_UNSIGNED_BYTE,
                         image->pixels);
            return textureId;
        }
    }
     
    MD2Model::~MD2Model() {
        if (frames != NULL) {
            for(int i = 0; i < numFrames; i++) {
                delete[] frames[i].vertices;
            }
            delete[] frames;
        }
        
        if (texCoords != NULL) {
            delete[] texCoords;
        }
        if (triangles != NULL) {
            delete[] triangles;
        }
    }
     
    MD2Model::MD2Model() {
        frames = NULL;
        texCoords = NULL;
        triangles = NULL;
    }
     
    //Loads the MD2 model
    MD2Model* MD2Model::load(const char* filename,
                             vector<const char*> textureFilenames) {
        ifstream input;
        input.open(filename, istream::binary);
        
        char buffer[64];
        input.read(buffer, 4); //Should be "IPD2", if this is an MD2 file
        if (buffer[0] != 'I' || buffer[1] != 'D' ||
            buffer[2] != 'P' || buffer[3] != '2') {
            return NULL;
        }
        if (readInt(input) != 8) { //The version number
            return NULL;
        }
        
        int textureWidth = readInt(input);   //The width of the textures
        int textureHeight = readInt(input);  //The height of the textures
        readInt(input);                      //The number of bytes per frame
        int numTextures = readInt(input);    //The number of textures
        if (numTextures != 1) {
            return NULL;
        }
        int numVertices = readInt(input);    //The number of vertices
        int numTexCoords = readInt(input);   //The number of texture coordinates
        int numTriangles = readInt(input);   //The number of triangles
        readInt(input);                      //The number of OpenGL commands
        int numFrames = readInt(input);      //The number of frames
        
        //Offsets (number of bytes after the beginning of the file to the beginning
        //of where certain data appear)
        readInt(input);                      //The offset to the textures
        int texCoordOffset = readInt(input); //The offset to the texture coordinates
        int triangleOffset = readInt(input); //The offset to the triangles
        int frameOffset = readInt(input);    //The offset to the frames
        readInt(input);                      //The offset to the OpenGL commands
        readInt(input);                      //The offset to the end of the file
        
        //Load the textures (ignore the texture suggested by the MD2 file)
        MD2Model* model = new MD2Model();
        for(unsigned int i = 0; i < textureFilenames.size(); i++) {
            const char* f = textureFilenames[i];
            if (strlen(f) < 5 ||
                strcmp(f + strlen(f) - 4, ".bmp") != 0) {
                delete model;
                return NULL;
            }
            Image* image = loadBMP(f);
            GLuint textureId = loadTexture(image);
            delete image;
            model->textureIds.push_back(textureId);
        }
        
        //Load the texture coordinates
        input.seekg(texCoordOffset, ios_base::beg);
        model->texCoords = new MD2TexCoord[numTexCoords];
        for(int i = 0; i < numTexCoords; i++) {
            MD2TexCoord* texCoord = model->texCoords + i;
            texCoord->texCoordX = (float)readShort(input) / textureWidth;
            texCoord->texCoordY = 1 - (float)readShort(input) / textureHeight;
        }
        
        //Load the triangles
        input.seekg(triangleOffset, ios_base::beg);
        model->triangles = new MD2Triangle[numTriangles];
        model->numTriangles = numTriangles;
        for(int i = 0; i < numTriangles; i++) {
            MD2Triangle* triangle = model->triangles + i;
            for(int j = 0; j < 3; j++) {
                triangle->vertices[j] = readUShort(input);
            }
            for(int j = 0; j < 3; j++) {
                triangle->texCoords[j] = readUShort(input);
            }
        }
        
        //Load the frames
        input.seekg(frameOffset, ios_base::beg);
        model->frames = new MD2Frame[numFrames];
        model->numFrames = numFrames;
        for(int i = 0; i < numFrames; i++) {
            MD2Frame* frame = model->frames + i;
            frame->vertices = new MD2Vertex[numVertices];
            Vec3f scale = readVec3f(input);
            Vec3f translation = readVec3f(input);
            input.read(frame->name, 16);
            
            for(int j = 0; j < numVertices; j++) {
                MD2Vertex* vertex = frame->vertices + j;
                input.read(buffer, 3);
                Vec3f v((unsigned char)buffer[0],
                        (unsigned char)buffer[1],
                        (unsigned char)buffer[2]);
                vertex->pos = translation + Vec3f(scale[0] * v[0],
                                                  scale[1] * v[1],
                                                  scale[2] * v[2]);
                input.read(buffer, 1);
                int normalIndex = (int)((unsigned char)buffer[0]);
                vertex->normal = Vec3f(NORMALS[3 * normalIndex],
                                       NORMALS[3 * normalIndex + 1],
                                       NORMALS[3 * normalIndex + 2]);
            }
        }
        
        model->startFrame = 0;
        model->endFrame = numFrames - 1;
        return model;
    }
     
    void MD2Model::setAnimation(const char* name) {
        /* The names of frames normally begin with the name of the animation in
         * which they are, e.g. "run", and are followed by a non-alphabetical
         * character.  Normally, they indicate their frame number in the animation,
         * e.g. "run_1", "run_2", etc.
         */
        bool found = false;
        for(int i = 0; i < numFrames; i++) {
            MD2Frame* frame = frames + i;
            if (strlen(frame->name) > strlen(name) &&
                strncmp(frame->name, name, strlen(name)) == 0 &&
                !isalpha(frame->name[strlen(name)])) {
                if (!found) {
                    found = true;
                    startFrame = i;
                }
                else {
                    endFrame = i;
                }
            }
            else if (found) {
                break;
            }
        }
    }
     
    void MD2Model::draw(int textureNum, float time) {
        if (time > -100000000 && time < 1000000000) {
            time -= (int)time;
            if (time < 0) {
                time += 1;
            }
        }
        else {
            time = 0;
        }
        
        glEnable(GL_TEXTURE_2D);
        //Use the appropriate texture
        glBindTexture(GL_TEXTURE_2D, textureIds[textureNum]);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        
        //Figure out the two frames between which we are interpolating
        int frameIndex1 = (int)(time * (endFrame - startFrame + 1)) + startFrame;
        if (frameIndex1 > endFrame) {
            frameIndex1 = startFrame;
        }
        
        int frameIndex2;
        if (frameIndex1 < endFrame) {
            frameIndex2 = frameIndex1 + 1;
        }
        else {
            frameIndex2 = startFrame;
        }
        
        MD2Frame* frame1 = frames + frameIndex1;
        MD2Frame* frame2 = frames + frameIndex2;
        
        //Figure out the fraction that we are between the two frames
        float frac =
            (time - (float)(frameIndex1 - startFrame) /
             (float)(endFrame - startFrame + 1)) * (endFrame - startFrame + 1);
        
        //Draw the model as an interpolation between the two frames
        glBegin(GL_TRIANGLES);
        for(int i = 0; i < numTriangles; i++) {
            MD2Triangle* triangle = triangles + i;
            for(int j = 2; j >= 0; j--) {
                MD2Vertex* v1 = frame1->vertices + triangle->vertices[j];
                MD2Vertex* v2 = frame2->vertices + triangle->vertices[j];
                Vec3f pos = v1->pos * (1 - frac) + v2->pos * frac;
                Vec3f normal = v1->normal * (1 - frac) + v2->normal * frac;
                if (normal[0] == 0 && normal[1] == 0 && normal[2] == 0) {
                    normal = Vec3f(0, 0, 1);
                }
                glNormal3f(normal[0], normal[1], normal[2]);
                
                MD2TexCoord* texCoord = texCoords + triangle->texCoords[j];
                glTexCoord2f(texCoord->texCoordX, texCoord->texCoordY);
                glVertex3f(pos[0], pos[1], pos[2]);
            }
        }
        glEnd();
    }
     
    Bei der Geburt ist ein Mensch so unfertig, wie ein an einem schönen blauen Montag in Taiwan hergestellter Computer vor der Programmierung.
    - Wolfgang Körner

  10. #10
    RenderWilli RenderWilli ist offline Mitglied Silber
    Registriert seit
    Oct 2008
    Beiträge
    69
    In der Obj-Datei stehen die Punkte als Klartext und das könnte man wie eine normale Textdatei einlesen und dann als OpenGl-Punkte nutzen.

    Mal so nebenbei, warum nutzt du nicht die Blender GameEngine, wenn schon alles in Blender aufbereitet ist?
    Geändert von RenderWilli (08.11.08 um 10:18 Uhr)
     

  11. #11
    BloodOrange BloodOrange ist offline Grünschnabel
    Registriert seit
    Nov 2008
    Beiträge
    4
    wahnsinn, danke
    mit soviel / schneller resonatz habe ich garnicht gerechnet big thx

    3Cyb3r:
    Danke für den bsp code, werde mich mal durcharbeiten,
    naja cpp habe ich noch nicht in angriff genommen, aber ich meien wäre ja mal ein anlass eine neue sprache zu lernen , bzw ein neues konzept ( Vererbung Poly...)

    RenderWilli:
    tjoa, daran habe ich eigentlich garnicht gedacht wenn ich erlich bin
    werde mir auch mal das zu gemühte führen

    In der Obj-Datei stehen die Punkte als Klartext und das könnte man wie eine normale Textdatei einlesen und dann als OpenGl-Punkte nutzen.
    das wäre ja genau das was ich im moment vorliegen habe.

    OT:
    wisst ihr zufällig ob die ganzen gegenstände und level in games alles per hand gemoddelt werden? also sprich das sich leute daran setzten und jeden baum und alles neu moddeln ?

    Mfg Austen
     

  12. #12
    3Cyb3r 3Cyb3r ist offline Mitglied Brokat
    Registriert seit
    May 2008
    Ort
    Baunatal (Hessen)
    Beiträge
    413
    ja ganz sicher
     
    Bei der Geburt ist ein Mensch so unfertig, wie ein an einem schönen blauen Montag in Taiwan hergestellter Computer vor der Programmierung.
    - Wolfgang Körner

  13. #13
    RenderWilli RenderWilli ist offline Mitglied Silber
    Registriert seit
    Oct 2008
    Beiträge
    69
    wisst ihr zufällig ob die ganzen gegenstände und level in games alles per hand gemoddelt werden? also sprich das sich leute daran setzten und jeden baum und alles neu moddeln ?
    Es gibt auch freie Modelle im Internet, die man nutzen kann. Um jedoch eventuelle Probleme mit Copyright/Urheberrecht ausschließen zu können, solltest du lieber alles selber machen. Dann ist man auf der sicheren Seite.

    In vielen Spielen wird SpeedTree für Bäume benutzt. Dazu benötigt man aber eine Lizenz. Die tollen Sachen kosten halt...
    Alternativ kann man einen Baum selber erstellen und den mit einer Methode verändern und als eine Art Duplikat/Instanz mehrfach darstellen und schon hat man einen Wald.
    Geändert von RenderWilli (08.11.08 um 10:17 Uhr)
     

Ähnliche Themen

  1. Laufzeit von java code
    Von natter im Forum Coders Talk
    Antworten: 1
    Letzter Beitrag: 23.03.10, 18:12
  2. Antworten: 2
    Letzter Beitrag: 26.11.09, 15:57
  3. Zur Laufzeit Code fest einzukompilieren
    Von Snowcrack im Forum .NET Café
    Antworten: 5
    Letzter Beitrag: 16.04.08, 15:19
  4. Code zur Laufzeit ändern
    Von RealHAZZARD im Forum Java
    Antworten: 6
    Letzter Beitrag: 19.07.07, 01:47
  5. Code während der Laufzeit erstellen!
    Von MichiStroi im Forum .NET Archiv
    Antworten: 5
    Letzter Beitrag: 15.06.05, 13:02

Stichworte