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