BRL-CAD
osg.cpp
Go to the documentation of this file.
1 /* O S G . C P P
2  * BRL-CAD
3  *
4  * Copyright (c) 2011-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @addtogroup libged */
21 /** @{ */
22 /** @file libged/osg.cpp
23  *
24  * An interface to OSG.
25  *
26  */
27 /** @} */
28 
29 #include "common.h"
30 
31 #include "ged.h"
32 #include "solid.h"
33 #include <assert.h>
34 
35 #include <osg/Geode>
36 #include <osg/Geometry>
37 #include <osg/RenderInfo>
38 #include <osgViewer/Viewer>
39 #include <osgUtil/Optimizer>
40 #include <osgDB/ReadFile>
41 #include <osgDB/WriteFile>
42 
43 
45 void ged_osgLoadScene(struct bu_list *hdlp, void *osgData);
47 
48 
49 struct osg_stuff {
50  osg::ref_ptr<osgViewer::Viewer> viewer;
51  double left, right, bottom, top, near, far;
53 };
54 
55 HIDDEN void
56 _osgLoadHiddenSolid(osg::Geode *geode, struct solid *sp)
57 {
58  register struct bn_vlist *vp = (struct bn_vlist *)&sp->s_vlist;
59  osg::Vec3dArray* vertices;
60 }
61 
62 
63 HIDDEN void
64 _osgLoadSolid(osg::Geode *geode, osg::Geometry *geom, osg::Vec3dArray *vertices, osg::Vec3dArray *normals, struct solid *sp)
65 {
66  struct bn_vlist *tvp;
67  int first;
68  register struct bn_vlist *vp = (struct bn_vlist *)&sp->s_vlist;
69  int begin;
70  int nverts;
71 
72  bu_log("ged_osgLoadSolid: enter\n");
73 
74 
75 
76  /* Viewing region is from -1.0 to +1.0 */
77  begin = 0;
78  nverts = 0;
79  first = 1;
80  for (BU_LIST_FOR(tvp, bn_vlist, &vp->l)) {
81  int i;
82  int nused = tvp->nused;
83  int *cmd = tvp->cmd;
84  point_t *pt = tvp->pt;
85  for (i = 0; i < nused; i++, cmd++, pt++) {
86  switch (*cmd) {
87  case BN_VLIST_LINE_MOVE:
88  /* Move, start line */
89  if (first == 0) {
90  geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP,begin,nverts));
91  //geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP,0,vertices->size()));
92 
93  bu_log("Add linestrip: begin - %d, nverts - %d\n", begin, nverts);
94 
95  // add the points geometry to the geode.
96  //geode->addDrawable(geom);
97  } else
98  first = 0;
99 
100  vertices->push_back(osg::Vec3d((*pt)[X], (*pt)[Y], (*pt)[Z]));
101  normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
102  begin += nverts;
103  nverts = 1;
104  //bu_log("ged_osgLoadSolid: loaded point - (%lf %lf %lf)\n", (*pt)[X], (*pt)[Y], (*pt)[Z]);
105  break;
106  case BN_VLIST_POLY_START:
107  normals->push_back(osg::Vec3d((*pt)[X], (*pt)[Y], (*pt)[Z]));
108  begin += nverts;
109  nverts = 0;
110 
111  break;
112  case BN_VLIST_LINE_DRAW:
113  case BN_VLIST_POLY_MOVE:
114  case BN_VLIST_POLY_DRAW:
115  vertices->push_back(osg::Vec3d((*pt)[X], (*pt)[Y], (*pt)[Z]));
116  ++nverts;
117 
118  //bu_log("ged_osgLoadSolid: loaded point - (%lf %lf %lf)\n", (*pt)[X], (*pt)[Y], (*pt)[Z]);
119  break;
120  case BN_VLIST_POLY_END:
121  //vertices->push_back(osg::Vec3d((*pt)[X], (*pt)[Y], (*pt)[Z]));
122  //++nverts;
123  geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,begin,nverts));
124  first = 1;
125 
126  bu_log("Add polygon: begin - %d, nverts - %d\n", begin, nverts);
127 
128  break;
130  break;
131  }
132  }
133  }
134 
135  if (first == 0) {
136  geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP,begin,nverts));
137  //geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP,0,vertices->size()));
138 
139  bu_log("Add linestrip: begin - %d, nverts - %d\n", begin, nverts);
140 
141  // add the points geometry to the geode.
142  //geode->addDrawable(geom);
143  }
144 
145  bu_log("ged_osgLoadSolid: leave\n");
146 }
147 
148 
149 void
150 ged_osgLoadScene(struct bu_list *hdlp, void *osgData)
151 {
152  register struct display_list *gdlp;
153  register struct display_list *next_gdlp;
154  struct solid *sp;
155  struct osg_stuff *osp = (struct osg_stuff *)osgData;
156 
157  bu_log("ged_osgLoadScene: part B\n");
158  osg::Group* root = new osg::Group();
159 
160  // create the Geode (Geometry Node) to contain all our osg::Geometry objects.
161  osg::Geode* geode = new osg::Geode();
162 
163  bu_log("before: max frame rate - %lf\n", osp->viewer->getRunMaxFrameRate());
164  bu_log("ged_osgLoadScene: enter\n");
165  gdlp = BU_LIST_NEXT(display_list, hdlp);
166  while (BU_LIST_NOT_HEAD(gdlp, hdlp)) {
167  next_gdlp = BU_LIST_PNEXT(display_list, gdlp);
168 
169  FOR_ALL_SOLIDS(sp, &gdlp->dl_headSolid) {
170  if (sp->s_hiddenLine) {
171  _osgLoadHiddenSolid(geode, sp);
172  } else {
173  osg::Geometry* geom = new osg::Geometry();
174  osg::Vec3dArray* vertices = new osg::Vec3dArray;
175  osg::Vec3dArray* normals = new osg::Vec3dArray;
176  _osgLoadSolid(geode, geom, vertices, normals, sp);
177  geom->setVertexArray(vertices);
178  geom->setNormalArray(normals);
179  geom->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
180  //osg::RenderInfo ri(osp->viewer->getCamera()->getGraphicsContext()->getState(), osp->viewer->getCamera()->getView());
181  //geom->compileGLObjects(ri);
182  geom->setUseDisplayList(true);
183  geode->addDrawable(geom);
184  }
185  }
186 
187  gdlp = next_gdlp;
188  }
189 
190  root->addChild(geode);
191  osp->viewer->setSceneData(root);
192  bu_log("after: max frame rate - %lf\n", osp->viewer->getRunMaxFrameRate());
193 
194 
195  bu_log("ged_osgLoadScene: loaded geode\n");
196 }
197 
198 
199 /*
200  * Local Variables:
201  * tab-width: 8
202  * mode: C
203  * indent-tabs-mode: t
204  * c-file-style: "stroustrup"
205  * End:
206  * ex: shiftwidth=4 tabstop=8
207  */
#define BU_LIST_FOR(p, structure, hp)
Definition: list.h:365
size_t nused
elements 0..nused active
Definition: vlist.h:73
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
struct bu_list l
magic, forw, back
Definition: vlist.h:72
struct bu_list dl_headSolid
head of solid list for this object
Definition: bview.h:49
double top
Definition: osg.cpp:51
Definition: list.h:118
int prev_pflag
Definition: osg.cpp:52
int cmd[BN_VLIST_CHUNK]
VL_CMD_*.
Definition: vlist.h:74
double far
Definition: osg.cpp:51
void ged_osgLoadScene(struct bu_list *hdlp, void *osgData)
Definition: osg.cpp:150
Header file for the BRL-CAD common definitions.
HIDDEN void _osgLoadSolid(osg::Geode *geode, osg::Geometry *geom, osg::Vec3dArray *vertices, osg::Vec3dArray *normals, struct solid *sp)
Definition: osg.cpp:64
#define HIDDEN
Definition: common.h:86
#define BN_VLIST_POLY_MOVE
move to first poly vertex
Definition: vlist.h:85
Definition: color.c:49
double left
Definition: osg.cpp:51
#define __BEGIN_DECLS
Definition: common.h:73
#define BN_VLIST_POLY_START
pt[] has surface normal
Definition: vlist.h:84
#define BN_VLIST_LINE_MOVE
Definition: vlist.h:82
#define BN_VLIST_LINE_DRAW
Definition: vlist.h:83
#define BU_LIST_PNEXT(structure, p)
Definition: list.h:422
#define BN_VLIST_POLY_DRAW
subsequent poly vertex
Definition: vlist.h:86
double near
Definition: osg.cpp:51
double bottom
Definition: osg.cpp:51
Definition: vlist.h:71
#define BN_VLIST_POLY_VERTNORM
per-vertex normal, for interpolation
Definition: vlist.h:88
osg::ref_ptr< osgViewer::Viewer > viewer
Definition: osg.cpp:50
#define BN_VLIST_POLY_END
last vert (repeats 1st), draw poly
Definition: vlist.h:87
double right
Definition: osg.cpp:51
Definition: color.c:51
#define __END_DECLS
Definition: common.h:74
HIDDEN void _osgLoadHiddenSolid(osg::Geode *geode, struct solid *sp)
Definition: osg.cpp:56
point_t pt[BN_VLIST_CHUNK]
associated 3-point/vect
Definition: vlist.h:75
#define BU_LIST_NEXT(structure, hp)
Definition: list.h:316
#define BU_LIST_NOT_HEAD(p, hp)
Definition: list.h:324
Definition: color.c:50