| 1 | #!/bin/sh |
| 2 | # autopkgtest check |
| 3 | # (C) 2014 Anton Gladky |
| 4 | # (C) 2020 Leopold Palomo-Avellaneda |
| 5 | |
| 6 | set -e |
| 7 | |
| 8 | WORKDIR=$(mktemp -d) |
| 9 | trap "rm -rf $WORKDIR" 0 INT QUIT ABRT PIPE TERM |
| 10 | cd $WORKDIR |
| 11 | cat <<EOF > demo1.cpp |
| 12 | /* Simple example that demonstrates how to generate a texture based on |
| 13 | * a rendered scene, using offscreen rendering. Based on glutiv.cpp |
| 14 | * by Morten Eriksen and 09.2.Texture.cpp.in from the Inventor Mentor. |
| 15 | * |
| 16 | * Note: This example uses GLUT, so you do not need to have any of the |
| 17 | * SoGUI libraries installed. If you have a working Coin installation, |
| 18 | * you should be able to build the example as follows: |
| 19 | * |
| 20 | * UNIX: |
| 21 | * coin-config --build glut_tex glut_tex.cpp -lglut3 |
| 22 | * |
| 23 | * Windows: |
| 24 | * coin-config --build glut_tex glut_tex.cpp -lglut32 |
| 25 | * |
| 26 | * Mac OS X: |
| 27 | * |
| 28 | * export LDFLAGS="-framework GLUT" |
| 29 | * coin-config --build glut_tex glut_tex.cpp |
| 30 | */ |
| 31 | |
| 32 | #include <Inventor/SoDB.h> |
| 33 | #include <Inventor/SoInput.h> |
| 34 | #include <Inventor/SoSceneManager.h> |
| 35 | #include <Inventor/SoOffscreenRenderer.h> |
| 36 | #include <Inventor/nodes/SoCube.h> |
| 37 | #include <Inventor/nodes/SoTexture2.h> |
| 38 | #include <Inventor/nodes/SoDirectionalLight.h> |
| 39 | #include <Inventor/nodes/SoSeparator.h> |
| 40 | #include <Inventor/nodes/SoMaterial.h> |
| 41 | #include <Inventor/nodes/SoPerspectiveCamera.h> |
| 42 | #include <Inventor/nodes/SoRotationXYZ.h> |
| 43 | |
| 44 | #ifdef __APPLE__ |
| 45 | #include <GLUT/glut.h> |
| 46 | #else |
| 47 | #include <GL/glut.h> |
| 48 | #endif |
| 49 | |
| 50 | // ---------------------------------------------------------------------- |
| 51 | |
| 52 | SoSceneManager * scenemanager; |
| 53 | int glutwin; |
| 54 | |
| 55 | static char red_cone_iv[] = { |
| 56 | "#Inventor V2.1 ascii\n\n" |
| 57 | "Separator {\n" |
| 58 | " BaseColor { rgb 0.8 0 0 }\n" |
| 59 | " Rotation { rotation 1 1 0 1.57 }\n" |
| 60 | " Cone { }\n" |
| 61 | "}\n" |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | // ---------------------------------------------------------------------- |
| 66 | |
| 67 | // Redraw on scenegraph changes. |
| 68 | void |
| 69 | redraw_cb(void * user, SoSceneManager * manager) |
| 70 | { |
| 71 | glutSetWindow(glutwin); |
| 72 | glEnable(GL_DEPTH_TEST); |
| 73 | glEnable(GL_LIGHTING); |
| 74 | scenemanager->render(); |
| 75 | glutSwapBuffers(); |
| 76 | } |
| 77 | |
| 78 | // Redraw on expose events. |
| 79 | void |
| 80 | expose_cb(void) |
| 81 | { |
| 82 | glEnable(GL_DEPTH_TEST); |
| 83 | glEnable(GL_LIGHTING); |
| 84 | scenemanager->render(); |
| 85 | glutSwapBuffers(); |
| 86 | } |
| 87 | |
| 88 | // Reconfigure on changes to window dimensions. |
| 89 | void |
| 90 | reshape_cb(int w, int h) |
| 91 | { |
| 92 | int idx = glutGetWindow(); |
| 93 | |
| 94 | scenemanager->setWindowSize(SbVec2s(w, h)); |
| 95 | scenemanager->setSize(SbVec2s(w, h)); |
| 96 | scenemanager->setViewportRegion(SbViewportRegion(w, h)); |
| 97 | scenemanager->scheduleRedraw(); |
| 98 | } |
| 99 | |
| 100 | // Process the internal Coin queues when idle. Necessary to get the |
| 101 | // animation to work. |
| 102 | void |
| 103 | idle_cb(void) |
| 104 | { |
| 105 | SoDB::getSensorManager()->processTimerQueue(); |
| 106 | SoDB::getSensorManager()->processDelayQueue(TRUE); |
| 107 | } |
| 108 | |
| 109 | // ---------------------------------------------------------------------- |
| 110 | |
| 111 | SbBool |
| 112 | generateTextureMap (SoNode *root, SoTexture2 *texture, |
| 113 | short textureWidth, short textureHeight) |
| 114 | { |
| 115 | SbViewportRegion myViewport(textureWidth, textureHeight); |
| 116 | SoOffscreenRenderer::Components comp = SoOffscreenRenderer::RGB; |
| 117 | |
| 118 | // Render the scene |
| 119 | SoOffscreenRenderer *myRenderer = new SoOffscreenRenderer(myViewport); |
| 120 | myRenderer->setComponents(comp); |
| 121 | myRenderer->setBackgroundColor(SbColor(0.8, 0.8, 0.0)); |
| 122 | if (!myRenderer->render(root)) { |
| 123 | delete myRenderer; |
| 124 | return FALSE; |
| 125 | } |
| 126 | |
| 127 | // Generate the texture |
| 128 | texture->image.setValue(SbVec2s(textureWidth, textureHeight), comp, |
| 129 | myRenderer->getBuffer()); |
| 130 | delete myRenderer; |
| 131 | return TRUE; |
| 132 | } |
| 133 | |
| 134 | SoSeparator * |
| 135 | createScenegraph(void) |
| 136 | { |
| 137 | SoSeparator * texroot = new SoSeparator; |
| 138 | texroot->ref(); |
| 139 | SoInput in; |
| 140 | in.setBuffer(red_cone_iv, strlen(red_cone_iv)); |
| 141 | |
| 142 | SoSeparator * result = SoDB::readAll(&in); |
| 143 | if (result == NULL) { exit(1); } |
| 144 | |
| 145 | SoPerspectiveCamera *myCamera = new SoPerspectiveCamera; |
| 146 | SoRotationXYZ *rot = new SoRotationXYZ; |
| 147 | rot->axis = SoRotationXYZ::X; |
| 148 | rot->angle = M_PI_2; |
| 149 | myCamera->position.setValue(SbVec3f(-0.2, -0.2, 2.0)); |
| 150 | myCamera->scaleHeight(0.4); |
| 151 | texroot->addChild(myCamera); |
| 152 | texroot->addChild(new SoDirectionalLight); |
| 153 | texroot->addChild(rot); |
| 154 | texroot->addChild(result); |
| 155 | myCamera->viewAll(texroot, SbViewportRegion()); |
| 156 | |
| 157 | // Generate the texture map |
| 158 | SoTexture2 *texture = new SoTexture2; |
| 159 | texture->ref(); |
| 160 | if (generateTextureMap(texroot, texture, 128, 128)) |
| 161 | printf ("Successfully generated texture map\n"); |
| 162 | else |
| 163 | printf ("Could not generate texture map\n"); |
| 164 | texroot->unref(); |
| 165 | |
| 166 | // Make a scene with a cube and apply the texture to it |
| 167 | SoSeparator * root = new SoSeparator; |
| 168 | root->addChild(texture); |
| 169 | root->addChild(new SoCube); |
| 170 | return root; |
| 171 | } |
| 172 | |
| 173 | // ---------------------------------------------------------------------- |
| 174 | |
| 175 | #ifdef _WIN32 |
| 176 | |
| 177 | #include <windows.h> |
| 178 | #include <winbase.h> |
| 179 | |
| 180 | int |
| 181 | WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, |
| 182 | int nCmdShow) |
| 183 | { |
| 184 | #else // UNIX |
| 185 | |
| 186 | int |
| 187 | main(int argc, char ** argv) |
| 188 | { |
| 189 | |
| 190 | #endif |
| 191 | |
| 192 | // initialize Coin and glut libraries |
| 193 | SoDB::init(); |
| 194 | |
| 195 | #ifdef _WIN32 |
| 196 | int argc = 1; |
| 197 | char * argv[] = { "glutiv.exe", (char *) NULL }; |
| 198 | glutInit(&argc, argv); |
| 199 | #else |
| 200 | glutInit(&argc, argv); |
| 201 | #endif |
| 202 | |
| 203 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); |
| 204 | |
| 205 | SoSeparator * root; |
| 206 | root = new SoSeparator; |
| 207 | root->ref(); |
| 208 | SoPerspectiveCamera * camera = new SoPerspectiveCamera; |
| 209 | root->addChild(camera); |
| 210 | root->addChild(new SoDirectionalLight); |
| 211 | root->addChild(createScenegraph()); |
| 212 | |
| 213 | scenemanager = new SoSceneManager; |
| 214 | scenemanager->setRenderCallback(redraw_cb, (void *)1); |
| 215 | scenemanager->setBackgroundColor(SbColor(0.2f, 0.2f, 0.2f)); |
| 216 | scenemanager->activate(); |
| 217 | camera->viewAll(root, scenemanager->getViewportRegion()); |
| 218 | scenemanager->setSceneGraph(root); |
| 219 | |
| 220 | glutInitWindowSize(512, 400); |
| 221 | SbString title("Offscreen Rendering"); |
| 222 | glutwin = glutCreateWindow(title.getString()); |
| 223 | glutDisplayFunc(expose_cb); |
| 224 | glutReshapeFunc(reshape_cb); |
| 225 | |
| 226 | // start main loop processing (with an idle callback) |
| 227 | |
| 228 | glutIdleFunc(idle_cb); |
| 229 | //glutMainLoop(); |
| 230 | |
| 231 | root->unref(); |
| 232 | delete scenemanager; |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | EOF |
| 237 | |
| 238 | g++ -o demo1 demo1.cpp -lGL -lGLU -lglut -lCoin -lX11 |
| 239 | echo "build: OK" |
| 240 | #[ -x demo1 ] |
| 241 | #touch a |
| 242 | #xvfb-run -s "-screen 0 640x480x24" --auth-file=a ./demo1 && echo run:OK || echo run:Failed |