| 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 > demo2.cpp |
| 12 | /**************************************************************************\ |
| 13 | * Copyright (c) Kongsberg Oil & Gas Technologies AS |
| 14 | * All rights reserved. |
| 15 | * |
| 16 | * Redistribution and use in source and binary forms, with or without |
| 17 | * modification, are permitted provided that the following conditions are |
| 18 | * met: |
| 19 | * |
| 20 | * Redistributions of source code must retain the above copyright notice, |
| 21 | * this list of conditions and the following disclaimer. |
| 22 | * |
| 23 | * Redistributions in binary form must reproduce the above copyright |
| 24 | * notice, this list of conditions and the following disclaimer in the |
| 25 | * documentation and/or other materials provided with the distribution. |
| 26 | * |
| 27 | * Neither the name of the copyright holder nor the names of its |
| 28 | * contributors may be used to endorse or promote products derived from |
| 29 | * this software without specific prior written permission. |
| 30 | * |
| 31 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 32 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 33 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 34 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 35 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 36 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 37 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 38 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 39 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 40 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 41 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 42 | \**************************************************************************/ |
| 43 | |
| 44 | // ************************************************************************* |
| 45 | |
| 46 | // glxiv.cpp -- example demonstrating Coin (or Open Inventor) bound |
| 47 | // directly into GLX/X11. Useful e.g. as a starting point if one wants |
| 48 | // to make an X11 application that doesn't depend on SoXt. |
| 49 | // |
| 50 | |
| 51 | // If you have Coin properly installed, you should be able to build by |
| 52 | // simply doing: |
| 53 | // |
| 54 | // $ coin-config --build glxiv glxiv.cpp |
| 55 | // |
| 56 | |
| 57 | // ************************************************************************* |
| 58 | |
| 59 | // FIXME: note that there are some limitations to this example: |
| 60 | // |
| 61 | // * The most important one is that events are not translated from |
| 62 | // native X11 events to Coin events (to be sent to the scene |
| 63 | // graph). This means that for instance draggers and manipulators in |
| 64 | // the scene graph will not respond to attempts at interaction. |
| 65 | // |
| 66 | // * The sensor queue processing is just a hack. Don't use this |
| 67 | // in production code. |
| 68 | // |
| 69 | // 20031113 mortene. |
| 70 | |
| 71 | // ************************************************************************* |
| 72 | |
| 73 | #include <stdlib.h> |
| 74 | #include <stdio.h> |
| 75 | #include <string.h> |
| 76 | #include <X11/Xlib.h> |
| 77 | #include <X11/keysym.h> |
| 78 | #include <GL/gl.h> |
| 79 | #include <GL/glx.h> |
| 80 | |
| 81 | #include <Inventor/SbTime.h> |
| 82 | #include <Inventor/SoDB.h> |
| 83 | #include <Inventor/SoInteraction.h> |
| 84 | #include <Inventor/SoSceneManager.h> |
| 85 | #include <Inventor/nodekits/SoNodeKit.h> |
| 86 | #include <Inventor/nodes/SoDirectionalLight.h> |
| 87 | #include <Inventor/nodes/SoPerspectiveCamera.h> |
| 88 | #include <Inventor/nodes/SoRotor.h> |
| 89 | #include <Inventor/nodes/SoSeparator.h> |
| 90 | #include <Inventor/nodes/SoText3.h> |
| 91 | |
| 92 | // ************************************************************************* |
| 93 | |
| 94 | typedef struct { |
| 95 | Display * display; |
| 96 | Window window; |
| 97 | GLXContext context; |
| 98 | |
| 99 | SoSceneManager * scenemanager; |
| 100 | } WindowData; |
| 101 | |
| 102 | // ************************************************************************* |
| 103 | |
| 104 | SbTime * starttime = NULL; |
| 105 | unsigned int rendercounter = 0; |
| 106 | static SoSeparator * root = NULL; |
| 107 | |
| 108 | static void |
| 109 | draw_scene(void * userdata, SoSceneManager * scenemanager) |
| 110 | { |
| 111 | if (starttime->getValue() == 0) { *starttime = SbTime::getTimeOfDay(); } |
| 112 | |
| 113 | // FIXME: should set near and far planes properly before |
| 114 | // rendering. 20031113 mortene. |
| 115 | |
| 116 | scenemanager->render(); |
| 117 | |
| 118 | WindowData * win = (WindowData *)userdata; |
| 119 | glXSwapBuffers(win->display, win->window); |
| 120 | |
| 121 | rendercounter++; |
| 122 | SbTime currenttime = SbTime::getTimeOfDay(); |
| 123 | SbTime interval = currenttime - *starttime; |
| 124 | if (interval > 1.0) { |
| 125 | (void)fprintf(stdout, "fps %f\n", rendercounter / interval.getValue()); |
| 126 | *starttime = currenttime; |
| 127 | rendercounter = 0; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // ************************************************************************* |
| 132 | |
| 133 | static void |
| 134 | make_glx_window(WindowData * win, |
| 135 | int x, int y, unsigned int width, unsigned int height) |
| 136 | { |
| 137 | int attrib[] = { |
| 138 | GLX_RGBA, |
| 139 | GLX_RED_SIZE, 1, |
| 140 | GLX_GREEN_SIZE, 1, |
| 141 | GLX_BLUE_SIZE, 1, |
| 142 | GLX_DEPTH_SIZE, 1, |
| 143 | GLX_DOUBLEBUFFER, |
| 144 | None |
| 145 | }; |
| 146 | |
| 147 | |
| 148 | const int scrnum = DefaultScreen(win->display); |
| 149 | const Window rootwindow = RootWindow(win->display, scrnum); |
| 150 | |
| 151 | XVisualInfo * visinfo = glXChooseVisual(win->display, scrnum, attrib); |
| 152 | if (!visinfo) { |
| 153 | (void)fprintf(stderr, "Error: couldn't get an RGB, double-buffered visual.\n"); |
| 154 | exit(1); |
| 155 | } |
| 156 | |
| 157 | XSetWindowAttributes attr; |
| 158 | attr.background_pixel = 0; |
| 159 | attr.border_pixel = 0; |
| 160 | attr.colormap = XCreateColormap(win->display, rootwindow, visinfo->visual, AllocNone); |
| 161 | attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; |
| 162 | unsigned long mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; |
| 163 | |
| 164 | win->window = XCreateWindow(win->display, rootwindow, 0, 0, width, height, |
| 165 | 0, visinfo->depth, InputOutput, |
| 166 | visinfo->visual, mask, &attr); |
| 167 | |
| 168 | { |
| 169 | XSizeHints sizehints; |
| 170 | sizehints.x = x; |
| 171 | sizehints.y = y; |
| 172 | sizehints.width = width; |
| 173 | sizehints.height = height; |
| 174 | sizehints.flags = USSize | USPosition; |
| 175 | XSetNormalHints(win->display, win->window, &sizehints); |
| 176 | const char * name = "Coin in GLX"; |
| 177 | XSetStandardProperties(win->display, win->window, name, name, |
| 178 | None, (char **)NULL, 0, &sizehints); |
| 179 | } |
| 180 | |
| 181 | win->context = glXCreateContext(win->display, visinfo, NULL, True); |
| 182 | if (!win->context) { |
| 183 | (void)fprintf(stderr, "Error: glXCreateContext() failed.\n"); |
| 184 | exit(1); |
| 185 | } |
| 186 | |
| 187 | XFree(visinfo); |
| 188 | } |
| 189 | |
| 190 | // ************************************************************************* |
| 191 | |
| 192 | static void |
| 193 | event_loop(WindowData * win) |
| 194 | { |
| 195 | while (1) { |
| 196 | static long mask = StructureNotifyMask | ExposureMask | KeyPressMask; |
| 197 | XEvent event; |
| 198 | while (XCheckWindowEvent(win->display, win->window, mask, &event)) { |
| 199 | if (event.xany.window == win->window) { |
| 200 | switch (event.type) { |
| 201 | |
| 202 | case Expose: |
| 203 | draw_scene(win, win->scenemanager); |
| 204 | break; |
| 205 | |
| 206 | case ConfigureNotify: |
| 207 | { |
| 208 | const int w = event.xconfigure.width; |
| 209 | const int h = event.xconfigure.height; |
| 210 | |
| 211 | win->scenemanager->setWindowSize(SbVec2s(w, h)); |
| 212 | win->scenemanager->setSize(SbVec2s(w, h)); |
| 213 | win->scenemanager->setViewportRegion(SbViewportRegion(w, h)); |
| 214 | win->scenemanager->scheduleRedraw(); |
| 215 | } |
| 216 | break; |
| 217 | |
| 218 | case KeyPress: |
| 219 | { |
| 220 | char buffer[1] = ""; |
| 221 | (void)XLookupString(&event.xkey, buffer, sizeof(buffer), NULL, NULL); |
| 222 | if (buffer[0] == /* Esc: */ 27) { return; } |
| 223 | } |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // FIXME: should do this properly б la SoXt, to avoid using ~100% |
| 230 | // CPU. 20031113 mortene. |
| 231 | SoDB::getSensorManager()->processTimerQueue(); |
| 232 | SoDB::getSensorManager()->processDelayQueue(TRUE); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // ************************************************************************* |
| 237 | |
| 238 | static void |
| 239 | make_coin_scenegraph(WindowData * win) |
| 240 | { |
| 241 | root = new SoSeparator; |
| 242 | root->ref(); |
| 243 | |
| 244 | SoPerspectiveCamera * camera = new SoPerspectiveCamera; |
| 245 | root->addChild(camera); |
| 246 | |
| 247 | SoDirectionalLight * light = new SoDirectionalLight; |
| 248 | light->direction.setValue(-0.5, -0.5, 1); |
| 249 | root->addChild(light); |
| 250 | |
| 251 | SoDirectionalLight * l = new SoDirectionalLight; |
| 252 | l->direction = - light->direction.getValue(); |
| 253 | root->addChild(l); |
| 254 | |
| 255 | SoRotor * rotor = new SoRotor; |
| 256 | rotor->rotation.setValue(SbVec3f(0, 1, 0), 0.1f); |
| 257 | rotor->speed = 0.2; |
| 258 | root->addChild(rotor); |
| 259 | |
| 260 | SoText3 * text3 = new SoText3; |
| 261 | text3->string = "Coin 3D"; |
| 262 | text3->justification = SoText3::CENTER; |
| 263 | text3->parts = SoText3::ALL; |
| 264 | root->addChild(text3); |
| 265 | |
| 266 | win->scenemanager = new SoSceneManager; |
| 267 | win->scenemanager->setRenderCallback(draw_scene, win); |
| 268 | win->scenemanager->activate(); |
| 269 | win->scenemanager->setSceneGraph(root); |
| 270 | |
| 271 | camera->viewAll(root, win->scenemanager->getViewportRegion()); |
| 272 | } |
| 273 | |
| 274 | // ************************************************************************* |
| 275 | |
| 276 | int |
| 277 | main(int argc, char *argv[]) |
| 278 | { |
| 279 | // Initialize Coin. |
| 280 | SoDB::init(); |
| 281 | SoDB::setRealTimeInterval(1/120.0); |
| 282 | SoNodeKit::init(); |
| 283 | SoInteraction::init(); |
| 284 | |
| 285 | Display * dpy = XOpenDisplay(NULL); |
| 286 | if (!dpy) { |
| 287 | (void)fprintf(stderr, "Error: couldn't open default display.\n"); |
| 288 | exit(1); |
| 289 | } |
| 290 | |
| 291 | WindowData win = { dpy, 0, NULL, NULL }; |
| 292 | make_glx_window(&win, 100, 100, 400, 400); |
| 293 | make_coin_scenegraph(&win); |
| 294 | |
| 295 | XMapWindow(win.display, win.window); |
| 296 | glXMakeCurrent(win.display, win.window, win.context); |
| 297 | |
| 298 | glEnable(GL_DEPTH_TEST); |
| 299 | glEnable(GL_LIGHTING); |
| 300 | |
| 301 | starttime = new SbTime(0.0); |
| 302 | |
| 303 | //event_loop(&win); |
| 304 | |
| 305 | delete starttime; |
| 306 | |
| 307 | XDestroyWindow(win.display, win.window); |
| 308 | glXDestroyContext(win.display, win.context); |
| 309 | XCloseDisplay(win.display); |
| 310 | |
| 311 | win.scenemanager->setSceneGraph(NULL); |
| 312 | root->unref(); |
| 313 | SoDB::finish(); |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | // ************************************************************************* |
| 318 | |
| 319 | |
| 320 | EOF |
| 321 | cat <<EOF > CMakeLists.txt |
| 322 | cmake_minimum_required(VERSION 3.7) |
| 323 | PROJECT( demo2 ) |
| 324 | |
| 325 | find_package(Coin REQUIRED CONFIG) |
| 326 | include_directories(\${Coin_INCLUDE_DIR}) |
| 327 | add_definitions(\${Coin_DEFINES}) |
| 328 | |
| 329 | find_package(X11 REQUIRED) |
| 330 | include_directories(\${X11_INCLUDE_DIR}) |
| 331 | |
| 332 | set(OpenGL_GL_PREFERENCE "LEGACY") |
| 333 | find_package(OpenGL REQUIRED) |
| 334 | include_directories(\${OPENGL_INCLUDE_DIR}) |
| 335 | |
| 336 | add_executable(demo2 demo2.cpp) |
| 337 | target_link_libraries (demo2 \${Coin_LIBRARIES} \${OPENGL_LIBRARIES} \${X11_LIBRARIES}) |
| 338 | |
| 339 | EOF |
| 340 | |
| 341 | cmake . |
| 342 | make |
| 343 | [ -x demo2 ] |
| 344 | echo "build: OK" |
| 345 | touch a |
| 346 | xvfb-run -a -s "-screen 0 640x480x24" --auth-file=a ./demo2 && echo run:OK || echo run:Failed |