| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
#include <OpenSG/OSGGLUT.h> |
|---|
| 12 |
#include <OpenSG/OSGConfig.h> |
|---|
| 13 |
#include <OpenSG/OSGSimpleGeometry.h> |
|---|
| 14 |
#include <OpenSG/OSGGLUTWindow.h> |
|---|
| 15 |
#include <OpenSG/OSGSimpleSceneManager.h> |
|---|
| 16 |
#include <OpenSG/OSGBaseFunctions.h> |
|---|
| 17 |
#include <OpenSG/OSGTransform.h> |
|---|
| 18 |
#include <OpenSG/OSGGroup.h> |
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
#include <OpenSG/OSGSimpleMaterial.h> |
|---|
| 24 |
#include <OpenSG/OSGSimpleTexturedMaterial.h> |
|---|
| 25 |
#include <OpenSG/OSGImage.h> |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
OSG_USING_NAMESPACE |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
TransformGlobalRefPtr cyltrans, tortrans; |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
SimpleSceneManager *mgr; |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
int setupGLUT(int *argc, char *argv[]); |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
void display(void) |
|---|
| 43 |
{ |
|---|
| 44 |
|
|---|
| 45 |
Matrix m; |
|---|
| 46 |
Real32 t = glutGet(GLUT_ELAPSED_TIME ); |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
m.setTransform(Vec3f(0, 0, osgSin(t / 1000.f) * 1.5), |
|---|
| 50 |
Quaternion( Vec3f (1, 0, 0), t / 500.f)); |
|---|
| 51 |
|
|---|
| 52 |
cyltrans->setMatrix(m); |
|---|
| 53 |
|
|---|
| 54 |
m.setTransform(Vec3f(osgSin(t / 1000.f), 0, 0), |
|---|
| 55 |
Quaternion( Vec3f (0, 0, 1), t / 1000.f)); |
|---|
| 56 |
|
|---|
| 57 |
tortrans->setMatrix(m); |
|---|
| 58 |
|
|---|
| 59 |
commitChanges(); |
|---|
| 60 |
|
|---|
| 61 |
mgr->redraw(); |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
int main(int argc, char **argv) |
|---|
| 67 |
{ |
|---|
| 68 |
|
|---|
| 69 |
osgInit(argc,argv); |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
int winid = setupGLUT(&argc, argv); |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
GLUTWindowGlobalRefPtr gwin= GLUTWindow::create(); |
|---|
| 76 |
gwin->setGlutId(winid); |
|---|
| 77 |
gwin->init(); |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
NodeGlobalRefPtr scene = Node::create(); |
|---|
| 87 |
GroupGlobalRefPtr g = Group::create(); |
|---|
| 88 |
|
|---|
| 89 |
scene->setCore(g); |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
NodeGlobalRefPtr cyl = Node::create(); |
|---|
| 93 |
GeometryGlobalRefPtr cylgeo = makeCylinderGeo( 1.4, .3, 8, true, true, true ); |
|---|
| 94 |
|
|---|
| 95 |
cyl->setCore(cylgeo); |
|---|
| 96 |
|
|---|
| 97 |
cyltrans = Transform::create(); |
|---|
| 98 |
|
|---|
| 99 |
NodeGlobalRefPtr cyltransnode = Node::create(); |
|---|
| 100 |
cyltransnode->setCore (cyltrans); |
|---|
| 101 |
cyltransnode->addChild(cyl ); |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
scene->addChild(cyltransnode); |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
NodeGlobalRefPtr torus = Node::create(); |
|---|
| 108 |
GeometryGlobalRefPtr torusgeo = makeTorusGeo( .2, 1, 8, 12 ); |
|---|
| 109 |
|
|---|
| 110 |
torus->setCore(torusgeo); |
|---|
| 111 |
|
|---|
| 112 |
tortrans = Transform::create(); |
|---|
| 113 |
|
|---|
| 114 |
NodeGlobalRefPtr tortransnode = Node::create(); |
|---|
| 115 |
tortransnode->setCore (tortrans); |
|---|
| 116 |
tortransnode->addChild(torus ); |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
scene->addChild(tortransnode); |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
SimpleMaterialGlobalRefPtr m1 = SimpleMaterial::create(); |
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
m1->setAmbient (Color3f(0.2,0.2,0.2)); |
|---|
| 145 |
m1->setDiffuse (Color3f(0.8,0.5,0.2)); |
|---|
| 146 |
m1->setEmission (Color3f(0.0,0.0,0.0)); |
|---|
| 147 |
m1->setSpecular (Color3f(1.0,1.0,1.0)); |
|---|
| 148 |
m1->setShininess (10); |
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
m1->setTransparency (0); |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
m1->setColorMaterial(GL_NONE); |
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
cylgeo->setMaterial(m1); |
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
ImageGlobalRefPtr image = Image::create(); |
|---|
| 201 |
|
|---|
| 202 |
if(argc > 1) |
|---|
| 203 |
{ |
|---|
| 204 |
image->read(argv[1]); |
|---|
| 205 |
} |
|---|
| 206 |
else |
|---|
| 207 |
{ |
|---|
| 208 |
UChar8 data[] = { 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, |
|---|
| 209 |
0x80, 0x00, 0x00, 0xff, 0xff, 0xff }; |
|---|
| 210 |
|
|---|
| 211 |
image->set( Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, data ); |
|---|
| 212 |
} |
|---|
| 213 |
|
|---|
| 214 |
SimpleTexturedMaterialGlobalRefPtr m2 = SimpleTexturedMaterial::create(); |
|---|
| 215 |
|
|---|
| 216 |
m2->setAmbient (Color3f(0.3,0.3,0.3)); |
|---|
| 217 |
m2->setDiffuse (Color3f(0.2,0.8,0.8)); |
|---|
| 218 |
m2->setEmission (Color3f(0.0,0.0,0.0)); |
|---|
| 219 |
m2->setSpecular (Color3f(1.0,1.0,1.0)); |
|---|
| 220 |
m2->setShininess (20); |
|---|
| 221 |
m2->setTransparency (0); |
|---|
| 222 |
m2->setColorMaterial(GL_NONE); |
|---|
| 223 |
|
|---|
| 224 |
m2->setImage (image); |
|---|
| 225 |
m2->setMinFilter (GL_LINEAR_MIPMAP_LINEAR); |
|---|
| 226 |
m2->setMagFilter (GL_NEAREST); |
|---|
| 227 |
m2->setEnvMode (GL_MODULATE); |
|---|
| 228 |
m2->setEnvMap (false); |
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
torusgeo->setMaterial(m2); |
|---|
| 232 |
|
|---|
| 233 |
commitChanges(); |
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
mgr = new SimpleSceneManager; |
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
mgr->setWindow(gwin ); |
|---|
| 240 |
mgr->setRoot (scene); |
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
mgr->showAll(); |
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
glutMainLoop(); |
|---|
| 247 |
|
|---|
| 248 |
return 0; |
|---|
| 249 |
} |
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
void reshape(int w, int h) |
|---|
| 257 |
{ |
|---|
| 258 |
mgr->resize(w, h); |
|---|
| 259 |
glutPostRedisplay(); |
|---|
| 260 |
} |
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
void mouse(int button, int state, int x, int y) |
|---|
| 264 |
{ |
|---|
| 265 |
if (state) |
|---|
| 266 |
mgr->mouseButtonRelease(button, x, y); |
|---|
| 267 |
else |
|---|
| 268 |
mgr->mouseButtonPress(button, x, y); |
|---|
| 269 |
|
|---|
| 270 |
glutPostRedisplay(); |
|---|
| 271 |
} |
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
void motion(int x, int y) |
|---|
| 275 |
{ |
|---|
| 276 |
mgr->mouseMove(x, y); |
|---|
| 277 |
glutPostRedisplay(); |
|---|
| 278 |
} |
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
void keyboard(unsigned char k, int x, int y) |
|---|
| 282 |
{ |
|---|
| 283 |
switch(k) |
|---|
| 284 |
{ |
|---|
| 285 |
case 27: |
|---|
| 286 |
{ |
|---|
| 287 |
delete mgr; |
|---|
| 288 |
|
|---|
| 289 |
OSG::osgExit(); |
|---|
| 290 |
exit(0); |
|---|
| 291 |
} |
|---|
| 292 |
break; |
|---|
| 293 |
|
|---|
| 294 |
case 's': |
|---|
| 295 |
{ |
|---|
| 296 |
mgr->setStatistics(!mgr->getStatistics()); |
|---|
| 297 |
} |
|---|
| 298 |
break; |
|---|
| 299 |
} |
|---|
| 300 |
} |
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
int setupGLUT(int *argc, char *argv[]) |
|---|
| 304 |
{ |
|---|
| 305 |
glutInit(argc, argv); |
|---|
| 306 |
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); |
|---|
| 307 |
|
|---|
| 308 |
int winid = glutCreateWindow("OpenSG"); |
|---|
| 309 |
|
|---|
| 310 |
glutReshapeFunc(reshape); |
|---|
| 311 |
glutDisplayFunc(display); |
|---|
| 312 |
glutMouseFunc(mouse); |
|---|
| 313 |
glutMotionFunc(motion); |
|---|
| 314 |
glutKeyboardFunc(keyboard); |
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
glutIdleFunc(display); |
|---|
| 318 |
|
|---|
| 319 |
return winid; |
|---|
| 320 |
} |
|---|