root/OpenSceneGraph/trunk/src/osgPlugins/3ds/viewport.cpp @ 10076

Revision 10076, 11.9 kB (checked in by robert, 6 years ago)

From Neil Hughes, converted across to use istream for reading data from file to enable reading .3ds files over http (use OSG's libcurl plugin).

From Robert Osfield, ammendments of the above to better support reading of files from local directories.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[8]1/*
2 * The 3D Studio File Format Library
3 * Copyright (C) 1996-2001 by J.E. Hoffmann <je-h@gmx.net>
4 * All rights reserved.
5 *
6 * This program is  free  software;  you can redistribute it and/or modify it
[151]7 * under the terms of the  GNU Lesser General Public License  as published by
8 * the  Free Software Foundation;  either version 2.1 of the License,  or (at
[8]9 * your option) any later version.
10 *
11 * This  program  is  distributed in  the  hope that it will  be useful,  but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
[151]13 * or  FITNESS FOR A  PARTICULAR PURPOSE.  See the  GNU Lesser General Public 
[8]14 * License for more details.
15 *
16 * You should  have received  a copy of the GNU Lesser General Public License
17 * along with  this program;  if not, write to the  Free Software Foundation,
18 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * $Id$
21 */
22#define LIB3DS_EXPORT
[1563]23#include "viewport.h"
24#include "chunk.h"
25#include "readwrite.h"
[8]26#include <stdlib.h>
27#include <string.h>
28
[151]29
[8]30/*!
31 * \defgroup viewport Viewport and default view settings
32 *
33 * \author J.E. Hoffmann <je-h@gmx.net>
34 */
35
[151]36
[8]37/*!
[151]38 * \ingroup viewport
[8]39 */
40Lib3dsBool
[10076]41lib3ds_viewport_read(Lib3dsViewport *viewport, iostream *strm)
[8]42{
[151]43  Lib3dsChunk c;
44  Lib3dsWord chunk;
[8]45
[10076]46  if (!lib3ds_chunk_read_start(&c, 0, strm)) {
[151]47    return(LIB3DS_FALSE);
48  }
49 
50  switch (c.chunk) {
51    case LIB3DS_VIEWPORT_LAYOUT:
52      {
53        int cur=0;
[10076]54        viewport->layout.style=lib3ds_word_read(strm);
55        viewport->layout.active=lib3ds_intw_read(strm);
56        lib3ds_intw_read(strm);
57        viewport->layout.swap=lib3ds_intw_read(strm);
58        lib3ds_intw_read(strm);
59        viewport->layout.swap_prior=lib3ds_intw_read(strm);
60        viewport->layout.swap_view=lib3ds_intw_read(strm);
61        lib3ds_chunk_read_tell(&c, strm);
62        while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) {
[151]63          switch (chunk) {
64            case LIB3DS_VIEWPORT_SIZE:
65              {
[10076]66                viewport->layout.position[0]=lib3ds_word_read(strm);
67                viewport->layout.position[1]=lib3ds_word_read(strm);
68                viewport->layout.size[0]=lib3ds_word_read(strm);
69                viewport->layout.size[1]=lib3ds_word_read(strm);
[151]70              }
71              break;
72            case LIB3DS_VIEWPORT_DATA_3:
73              {
74                lib3ds_viewport_set_views(viewport,cur+1);
[10076]75                lib3ds_intw_read(strm);
76                viewport->layout.viewL[cur].axis_lock=lib3ds_word_read(strm);
77                viewport->layout.viewL[cur].position[0]=lib3ds_intw_read(strm);
78                viewport->layout.viewL[cur].position[1]=lib3ds_intw_read(strm);
79                viewport->layout.viewL[cur].size[0]=lib3ds_intw_read(strm);
80                viewport->layout.viewL[cur].size[1]=lib3ds_intw_read(strm);
81                viewport->layout.viewL[cur].type=lib3ds_word_read(strm);
82                viewport->layout.viewL[cur].zoom=lib3ds_float_read(strm);
83                lib3ds_vector_read(viewport->layout.viewL[cur].center,strm);
84                viewport->layout.viewL[cur].horiz_angle=lib3ds_float_read(strm);
85                viewport->layout.viewL[cur].vert_angle=lib3ds_float_read(strm);
86                strm->read(viewport->layout.viewL[cur].camera,11);
[151]87                ++cur;
88              }
89              break;
90            case LIB3DS_VIEWPORT_DATA:
91              /* 3DS R2 & R3 chunk
92                 unsupported */
93              break;
94            default:
95              lib3ds_chunk_unknown(chunk);
96          }
[8]97        }
[151]98      }
99      break;
100    case LIB3DS_DEFAULT_VIEW:
101      {
102        memset(&viewport->default_view,0,sizeof(Lib3dsDefaultView));
[10076]103        while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) {
[151]104          switch (chunk) {
105            case LIB3DS_VIEW_TOP:
106              {
107                viewport->default_view.type=LIB3DS_VIEW_TYPE_TOP;
[10076]108                lib3ds_vector_read(viewport->default_view.position,strm);
109                viewport->default_view.width=lib3ds_float_read(strm);
[151]110              }
111              break;
112            case LIB3DS_VIEW_BOTTOM:
113              {
114                viewport->default_view.type=LIB3DS_VIEW_TYPE_BOTTOM;
[10076]115                lib3ds_vector_read(viewport->default_view.position,strm);
116                viewport->default_view.width=lib3ds_float_read(strm);
[151]117              }
118              break;
119            case LIB3DS_VIEW_LEFT:
120              {
121                viewport->default_view.type=LIB3DS_VIEW_TYPE_LEFT;
[10076]122                lib3ds_vector_read(viewport->default_view.position,strm);
123                viewport->default_view.width=lib3ds_float_read(strm);
[151]124              }
125              break;
126            case LIB3DS_VIEW_RIGHT:
127              {
128                viewport->default_view.type=LIB3DS_VIEW_TYPE_RIGHT;
[10076]129                lib3ds_vector_read(viewport->default_view.position,strm);
130                viewport->default_view.width=lib3ds_float_read(strm);
[151]131              }
132              break;
133            case LIB3DS_VIEW_FRONT:
134              {
135                viewport->default_view.type=LIB3DS_VIEW_TYPE_FRONT;
[10076]136                lib3ds_vector_read(viewport->default_view.position,strm);
137                viewport->default_view.width=lib3ds_float_read(strm);
[151]138              }
139              break;
140            case LIB3DS_VIEW_BACK:
141              {
142                viewport->default_view.type=LIB3DS_VIEW_TYPE_BACK;
[10076]143                lib3ds_vector_read(viewport->default_view.position,strm);
144                viewport->default_view.width=lib3ds_float_read(strm);
[151]145              }
146              break;
147            case LIB3DS_VIEW_USER:
148              {
149                viewport->default_view.type=LIB3DS_VIEW_TYPE_USER;
[10076]150                lib3ds_vector_read(viewport->default_view.position,strm);
151                viewport->default_view.width=lib3ds_float_read(strm);
152                viewport->default_view.horiz_angle=lib3ds_float_read(strm);
153                viewport->default_view.vert_angle=lib3ds_float_read(strm);
154                viewport->default_view.roll_angle=lib3ds_float_read(strm);
[151]155              }
156              break;
157            case LIB3DS_VIEW_CAMERA:
158              {
159                viewport->default_view.type=LIB3DS_VIEW_TYPE_CAMERA;
[10076]160                strm->read(viewport->default_view.camera,11);
[151]161              }
162              break;
163            default:
164              lib3ds_chunk_unknown(chunk);
165          }
[8]166        }
[151]167      }
168      break;
169  }
170
[10076]171  lib3ds_chunk_read_end(&c, strm);
[151]172  return(LIB3DS_TRUE);
[8]173}
174
175
176/*!
[151]177 * \ingroup viewport
[8]178 */
179void
180lib3ds_viewport_set_views(Lib3dsViewport *viewport, Lib3dsDword views)
181{
[151]182  ASSERT(viewport);
183  if (viewport->layout.views) {
184    if (views) {
185      viewport->layout.views=views;
186      viewport->layout.viewL=(Lib3dsView*)realloc(viewport->layout.viewL, sizeof(Lib3dsView)*views);
[8]187    }
[151]188    else {
189      free(viewport->layout.viewL);
190      viewport->layout.views=0;
191      viewport->layout.viewL=0;
[8]192    }
[151]193  }
194  else {
195    viewport->layout.views=views;
[6188]196    viewport->layout.viewL= views ? (Lib3dsView*)calloc(sizeof(Lib3dsView),views) : 0;
[151]197  }
[8]198}
199
200
201/*!
[151]202 * \ingroup viewport
[8]203 */
204Lib3dsBool
[10076]205lib3ds_viewport_write(Lib3dsViewport *viewport, iostream *strm)
[8]206{
[151]207  if (viewport->layout.views) {
208    Lib3dsChunk c;
209    unsigned i;
210
211    c.chunk=LIB3DS_VIEWPORT_LAYOUT;
[10076]212    if (!lib3ds_chunk_write_start(&c,strm)) {
[151]213      return(LIB3DS_FALSE);
214    }
215
[10076]216    lib3ds_word_write(viewport->layout.style,strm);
217    lib3ds_intw_write(viewport->layout.active,strm);
218    lib3ds_intw_write(0,strm);
219    lib3ds_intw_write(viewport->layout.swap,strm);
220    lib3ds_intw_write(0,strm);
221    lib3ds_intw_write(viewport->layout.swap_prior,strm);
222    lib3ds_intw_write(viewport->layout.swap_view,strm);
[151]223   
[8]224    {
[151]225      Lib3dsChunk c;
226      c.chunk=LIB3DS_VIEWPORT_SIZE;
227      c.size=14;
[10076]228      lib3ds_chunk_write(&c,strm);
229      lib3ds_intw_write(viewport->layout.position[0],strm);
230      lib3ds_intw_write(viewport->layout.position[1],strm);
231      lib3ds_intw_write(viewport->layout.size[0],strm);
232      lib3ds_intw_write(viewport->layout.size[1],strm);
[151]233    }
[8]234
[151]235    for (i=0; i<viewport->layout.views; ++i) {
236      Lib3dsChunk c;
237      c.chunk=LIB3DS_VIEWPORT_DATA_3;
238      c.size=55;
[10076]239      lib3ds_chunk_write(&c,strm);
[8]240
[10076]241      lib3ds_intw_write(0,strm);
242      lib3ds_word_write(viewport->layout.viewL[i].axis_lock,strm);
243      lib3ds_intw_write(viewport->layout.viewL[i].position[0],strm);
244      lib3ds_intw_write(viewport->layout.viewL[i].position[1],strm);
245      lib3ds_intw_write(viewport->layout.viewL[i].size[0],strm);
246      lib3ds_intw_write(viewport->layout.viewL[i].size[1],strm);
247      lib3ds_word_write(viewport->layout.viewL[i].type,strm);
248      lib3ds_float_write(viewport->layout.viewL[i].zoom,strm);
249      lib3ds_vector_write(viewport->layout.viewL[i].center,strm);
250      lib3ds_float_write(viewport->layout.viewL[i].horiz_angle,strm);
251      lib3ds_float_write(viewport->layout.viewL[i].vert_angle,strm);
252      strm->write(viewport->layout.viewL[i].camera,11);
[151]253    }
[8]254
[10076]255    if (!lib3ds_chunk_write_end(&c,strm)) {
[151]256      return(LIB3DS_FALSE);
257    }
258  }
259
260  if (viewport->default_view.type) {
261    Lib3dsChunk c;
262
263    c.chunk=LIB3DS_DEFAULT_VIEW;
[10076]264    if (!lib3ds_chunk_write_start(&c,strm)) {
[151]265      return(LIB3DS_FALSE);
266    }
267
268    switch (viewport->default_view.type) {
269      case LIB3DS_VIEW_TYPE_TOP:
[8]270        {
[151]271          Lib3dsChunk c;
272          c.chunk=LIB3DS_VIEW_TOP;
273          c.size=22;
[10076]274          lib3ds_chunk_write(&c,strm);
275          lib3ds_vector_write(viewport->default_view.position,strm);
276          lib3ds_float_write(viewport->default_view.width,strm);
[8]277        }
[151]278        break;
279      case LIB3DS_VIEW_TYPE_BOTTOM:
[8]280        {
[151]281          Lib3dsChunk c;
282          c.chunk=LIB3DS_VIEW_BOTTOM;
283          c.size=22;
[10076]284          lib3ds_chunk_write(&c,strm);
285          lib3ds_vector_write(viewport->default_view.position,strm);
286          lib3ds_float_write(viewport->default_view.width,strm);
[8]287        }
[151]288        break;
289      case LIB3DS_VIEW_TYPE_LEFT:
[8]290        {
[151]291          Lib3dsChunk c;
292          c.chunk=LIB3DS_VIEW_LEFT;
293          c.size=22;
[10076]294          lib3ds_chunk_write(&c,strm);
295          lib3ds_vector_write(viewport->default_view.position,strm);
296          lib3ds_float_write(viewport->default_view.width,strm);
[8]297        }
[151]298        break;
299      case LIB3DS_VIEW_TYPE_RIGHT:
[8]300        {
[151]301          Lib3dsChunk c;
302          c.chunk=LIB3DS_VIEW_RIGHT;
303          c.size=22;
[10076]304          lib3ds_chunk_write(&c,strm);
305          lib3ds_vector_write(viewport->default_view.position,strm);
306          lib3ds_float_write(viewport->default_view.width,strm);
[8]307        }
[151]308        break;
309      case LIB3DS_VIEW_TYPE_FRONT:
[8]310        {
[151]311          Lib3dsChunk c;
312          c.chunk=LIB3DS_VIEW_FRONT;
313          c.size=22;
[10076]314          lib3ds_chunk_write(&c,strm);
315          lib3ds_vector_write(viewport->default_view.position,strm);
316          lib3ds_float_write(viewport->default_view.width,strm);
[8]317        }
[151]318        break;
319      case LIB3DS_VIEW_TYPE_BACK:
[8]320        {
[151]321          Lib3dsChunk c;
322          c.chunk=LIB3DS_VIEW_BACK;
323          c.size=22;
[10076]324          lib3ds_chunk_write(&c,strm);
325          lib3ds_vector_write(viewport->default_view.position,strm);
326          lib3ds_float_write(viewport->default_view.width,strm);
[8]327        }
[151]328        break;
329      case LIB3DS_VIEW_TYPE_USER:
330        {
331          Lib3dsChunk c;
332          c.chunk=LIB3DS_VIEW_USER;
333          c.size=34;
[10076]334          lib3ds_chunk_write(&c,strm);
335          lib3ds_vector_write(viewport->default_view.position,strm);
336          lib3ds_float_write(viewport->default_view.width,strm);
337          lib3ds_float_write(viewport->default_view.horiz_angle,strm);
338          lib3ds_float_write(viewport->default_view.vert_angle,strm);
339          lib3ds_float_write(viewport->default_view.roll_angle,strm);
[151]340        }
341        break;
342      case LIB3DS_VIEW_TYPE_CAMERA:
343        {
344          Lib3dsChunk c;
345          c.chunk=LIB3DS_VIEW_CAMERA;
346          c.size=17;
[10076]347          lib3ds_chunk_write(&c,strm);
348          strm->write(viewport->default_view.camera,11); // NAH Potential issue here
[151]349        }
350        break;
[8]351    }
[151]352
[10076]353    if (!lib3ds_chunk_write_end(&c,strm)) {
[151]354      return(LIB3DS_FALSE);
355    }
356  }
357  return(LIB3DS_TRUE);
[8]358}
359
360
361/*!
362
363\typedef Lib3dsViewport
364  \ingroup viewport
365  \sa _Lib3dsViewport
366
367*/
Note: See TracBrowser for help on using the browser.