root/OpenSceneGraph/trunk/src/osgPlugins/3ds/lib3ds/lib3ds_util.c @ 10853

Revision 10853, 2.8 kB (checked in by robert, 5 years ago)

From Sukender,
"Here is our freshly baked 3DS reader/writer (named 'v0.5' to differentiate from previous one). Changes are against trunk rev. 10819.
Short changelog (from rev 10819):
- Added 3DS writer
- Sync'd with latest lib3DS
- Added options, especially "flattenMatrixTransforms" to get the "old" behaviour (else the reader correctly maps to OSG the transforms from the 3DS file).

What should be done:
- Check with pivot points, with and without "flattenMatrixTransforms" option.
- We ran tests on it, but we can never be 100% sure there is no bug. Testing from the community would of course be helpful."

RevLine 
[10853]1/*
2    Copyright (C) 1996-2008 by Jan Eric Kyprianidis <www.kyprianidis.com>
3    All rights reserved.
4   
5    This program is free  software: you can redistribute it and/or modify
6    it under the terms of the GNU Lesser General Public License as published
7    by the Free Software Foundation, either version 2.1 of the License, or
8    (at your option) any later version.
9
10    Thisprogram  is  distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    GNU Lesser General Public License for more details.
14   
15    You should  have received a copy of the GNU Lesser General Public License
16    along with  this program; If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "lib3ds_impl.h"
19
20
21void* lib3ds_util_realloc_array(void *ptr, int old_size, int new_size, int element_size) {
22    if (!ptr)
23        old_size = 0;
24    if (old_size != new_size) {
25        ptr = realloc(ptr, element_size * new_size);
26        if (old_size < new_size) {
27            memset((char*)ptr + element_size * old_size, 0, element_size * (new_size - old_size));
28        }
29    }
30    return ptr;
31}
32
33
34void lib3ds_util_reserve_array(void ***ptr, int *n, int *size, int new_size, int force, Lib3dsFreeFunc free_func) {
35    assert(ptr && n && size);
36    if ((*size < new_size) || force) {
37        if (force && free_func) {
38            int i;
39            for (i = new_size; i < *n; ++i) {
40                free_func((*ptr)[i]);
41                (*ptr)[i] = 0;
42            }
43        }
44        *ptr = (void**)realloc(*ptr, sizeof(void*) * new_size);
45        *size = new_size;
46        if (*n > new_size) {
47            *n = new_size;
48        }
49    }
50}
51
52
53void lib3ds_util_insert_array(void ***ptr, int *n, int *size, void *element, int index) {
54    int i;
55    assert(ptr && n && size && element);
56    i = ((index >= 0) && (index < *n)) ? index : *n;
57    if (i >= *size) {
58        int new_size = 2 * (*size);
59        #ifdef _DEBUG
60            if (new_size < 1) {
61                new_size = 1;
62            }
63        #else
64            if (new_size < 32) {
65                new_size = 32;
66            }
67        #endif
68        lib3ds_util_reserve_array(ptr, n, size, new_size, FALSE, NULL);
69    }
70    assert(*ptr);
71    if (i < *n) {
72        memmove(&(*ptr)[i+1], &(*ptr)[i], sizeof(void*) * (*n - i));
73    }
74    (*ptr)[i] = element;
75    *n = *n + 1;
76}
77
78
79void lib3ds_util_remove_array(void ***ptr, int *n, int index, Lib3dsFreeFunc free_func) {
80    assert(ptr && n);
81    if ((index >= 0) && (index < *n)) {
82        assert(*ptr);
83        free_func((*ptr)[index]);
84        if (index < *n - 1) {
85            memmove(&(*ptr)[index], &(*ptr)[index+1], sizeof(void*) * (*n - index - 1));
86        }
87        *n = *n - 1;
88    }
89}
Note: See TracBrowser for help on using the browser.