| [8] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| [151] | 7 | |
|---|
| 8 | |
|---|
| [8] | 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| [151] | 13 | |
|---|
| [8] | 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | #define LIB3DS_EXPORT |
|---|
| [1563] | 23 | #include "camera.h" |
|---|
| 24 | #include "chunk.h" |
|---|
| 25 | #include "readwrite.h" |
|---|
| [8] | 26 | #include <stdlib.h> |
|---|
| 27 | #include <math.h> |
|---|
| 28 | #include <string.h> |
|---|
| [1563] | 29 | #include "config.h" |
|---|
| [8] | 30 | #ifdef WITH_DMALLOC |
|---|
| 31 | #include <dmalloc.h> |
|---|
| 32 | #endif |
|---|
| 33 | |
|---|
| [151] | 34 | |
|---|
| [8] | 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| [151] | 41 | |
|---|
| [8] | 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | Lib3dsCamera* |
|---|
| 46 | lib3ds_camera_new(const char *name) |
|---|
| 47 | { |
|---|
| [151] | 48 | Lib3dsCamera *camera; |
|---|
| [8] | 49 | |
|---|
| [151] | 50 | ASSERT(name); |
|---|
| 51 | ASSERT(strlen(name)<64); |
|---|
| 52 | |
|---|
| 53 | camera=(Lib3dsCamera*)calloc(sizeof(Lib3dsCamera), 1); |
|---|
| 54 | if (!camera) { |
|---|
| 55 | return(0); |
|---|
| 56 | } |
|---|
| 57 | strcpy(camera->name, name); |
|---|
| 58 | camera->fov=45.0f; |
|---|
| 59 | return(camera); |
|---|
| 60 | } |
|---|
| [8] | 61 | |
|---|
| [151] | 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | void |
|---|
| 67 | lib3ds_camera_free(Lib3dsCamera *camera) |
|---|
| 68 | { |
|---|
| 69 | memset(camera, 0, sizeof(Lib3dsCamera)); |
|---|
| 70 | free(camera); |
|---|
| [8] | 71 | } |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | void |
|---|
| [151] | 78 | lib3ds_camera_dump(Lib3dsCamera *camera) |
|---|
| [8] | 79 | { |
|---|
| [151] | 80 | ASSERT(camera); |
|---|
| 81 | printf(" name: %s\n", camera->name); |
|---|
| 82 | printf(" position: (%f, %f, %f)\n", |
|---|
| 83 | camera->position[0], camera->position[1], camera->position[2]); |
|---|
| 84 | printf(" target (%f, %f, %f)\n", |
|---|
| 85 | camera->target[0], camera->target[1], camera->target[2]); |
|---|
| 86 | printf(" roll: %f\n", camera->roll); |
|---|
| 87 | printf(" fov: %f\n", camera->fov); |
|---|
| 88 | printf(" see_cone: %s\n", camera->see_cone ? "yes" : "no"); |
|---|
| 89 | printf(" near_range: %f\n", camera->near_range); |
|---|
| 90 | printf(" far_range: %f\n", camera->near_range); |
|---|
| 91 | printf("\n"); |
|---|
| [8] | 92 | } |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | Lib3dsBool |
|---|
| [10076] | 99 | lib3ds_camera_read(Lib3dsCamera *camera, iostream *strm) |
|---|
| [8] | 100 | { |
|---|
| [151] | 101 | Lib3dsChunk c; |
|---|
| 102 | Lib3dsWord chunk; |
|---|
| [8] | 103 | |
|---|
| [10076] | 104 | if (!lib3ds_chunk_read_start(&c, LIB3DS_N_CAMERA, strm)) { |
|---|
| [151] | 105 | return(LIB3DS_FALSE); |
|---|
| 106 | } |
|---|
| 107 | { |
|---|
| 108 | int i; |
|---|
| 109 | for (i=0; i<3; ++i) { |
|---|
| [10076] | 110 | camera->position[i]=lib3ds_float_read(strm); |
|---|
| [8] | 111 | } |
|---|
| [151] | 112 | for (i=0; i<3; ++i) { |
|---|
| [10076] | 113 | camera->target[i]=lib3ds_float_read(strm); |
|---|
| [8] | 114 | } |
|---|
| [151] | 115 | } |
|---|
| [10076] | 116 | camera->roll=lib3ds_float_read(strm); |
|---|
| [151] | 117 | { |
|---|
| 118 | float s; |
|---|
| [10076] | 119 | s=lib3ds_float_read(strm); |
|---|
| [151] | 120 | if (fabs(s)<LIB3DS_EPSILON) { |
|---|
| 121 | camera->fov=45.0; |
|---|
| 122 | } |
|---|
| 123 | else { |
|---|
| 124 | camera->fov=2400.0f/s; |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| [10076] | 127 | lib3ds_chunk_read_tell(&c, strm); |
|---|
| [151] | 128 | |
|---|
| [10076] | 129 | while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { |
|---|
| [151] | 130 | switch (chunk) { |
|---|
| 131 | case LIB3DS_CAM_SEE_CONE: |
|---|
| [8] | 132 | { |
|---|
| [151] | 133 | camera->see_cone=LIB3DS_TRUE; |
|---|
| [8] | 134 | } |
|---|
| [151] | 135 | break; |
|---|
| 136 | case LIB3DS_CAM_RANGES: |
|---|
| [8] | 137 | { |
|---|
| [10076] | 138 | camera->near_range=lib3ds_float_read(strm); |
|---|
| 139 | camera->far_range=lib3ds_float_read(strm); |
|---|
| [8] | 140 | } |
|---|
| [151] | 141 | break; |
|---|
| 142 | default: |
|---|
| 143 | lib3ds_chunk_unknown(chunk); |
|---|
| [8] | 144 | } |
|---|
| [151] | 145 | } |
|---|
| 146 | |
|---|
| [10076] | 147 | lib3ds_chunk_read_end(&c, strm); |
|---|
| [151] | 148 | return(LIB3DS_TRUE); |
|---|
| [8] | 149 | } |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | Lib3dsBool |
|---|
| [10076] | 156 | lib3ds_camera_write(Lib3dsCamera *camera, iostream *strm) |
|---|
| [8] | 157 | { |
|---|
| [151] | 158 | Lib3dsChunk c; |
|---|
| [8] | 159 | |
|---|
| [151] | 160 | c.chunk=LIB3DS_N_CAMERA; |
|---|
| [10076] | 161 | if (!lib3ds_chunk_write_start(&c,strm)) { |
|---|
| [151] | 162 | return(LIB3DS_FALSE); |
|---|
| 163 | } |
|---|
| [8] | 164 | |
|---|
| [10076] | 165 | lib3ds_vector_write(camera->position, strm); |
|---|
| 166 | lib3ds_vector_write(camera->target, strm); |
|---|
| 167 | lib3ds_float_write(camera->roll, strm); |
|---|
| [151] | 168 | if (fabs(camera->fov)<LIB3DS_EPSILON) { |
|---|
| [10076] | 169 | lib3ds_float_write(2400.0f/45.0f, strm); |
|---|
| [151] | 170 | } |
|---|
| 171 | else { |
|---|
| [10076] | 172 | lib3ds_float_write(2400.0f/camera->fov, strm); |
|---|
| [151] | 173 | } |
|---|
| [8] | 174 | |
|---|
| [151] | 175 | if (camera->see_cone) { |
|---|
| 176 | Lib3dsChunk c; |
|---|
| 177 | c.chunk=LIB3DS_CAM_SEE_CONE; |
|---|
| 178 | c.size=6; |
|---|
| [10076] | 179 | lib3ds_chunk_write(&c, strm); |
|---|
| [151] | 180 | } |
|---|
| 181 | { |
|---|
| 182 | Lib3dsChunk c; |
|---|
| 183 | c.chunk=LIB3DS_CAM_RANGES; |
|---|
| 184 | c.size=14; |
|---|
| [10076] | 185 | lib3ds_chunk_write(&c, strm); |
|---|
| 186 | lib3ds_float_write(camera->near_range, strm); |
|---|
| 187 | lib3ds_float_write(camera->far_range, strm); |
|---|
| [151] | 188 | } |
|---|
| [8] | 189 | |
|---|
| [10076] | 190 | if (!lib3ds_chunk_write_end(&c,strm)) { |
|---|
| [151] | 191 | return(LIB3DS_FALSE); |
|---|
| 192 | } |
|---|
| 193 | return(LIB3DS_TRUE); |
|---|
| [8] | 194 | } |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|