[10] | 1 | // %flair:license{
|
---|
[15] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[10] | 4 | // %flair:license}
|
---|
[8] | 5 | // created: 2014/03/07
|
---|
| 6 | // filename: SimuCameraGL.cpp
|
---|
| 7 | //
|
---|
| 8 | // author: Guillaume Sanahuja
|
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 10 | // pbo code from Song Ho Ahn (song.ahn@gmail.com)
|
---|
| 11 | //
|
---|
| 12 | // version: $Id: $
|
---|
| 13 | //
|
---|
| 14 | // purpose: Class for a simulation camera
|
---|
| 15 | //
|
---|
| 16 | //
|
---|
| 17 | /*********************************************************************/
|
---|
| 18 | #ifdef GL
|
---|
| 19 |
|
---|
| 20 | #include "SimuCameraGL.h"
|
---|
| 21 | #include "Model.h"
|
---|
| 22 | #include <SharedMem.h>
|
---|
| 23 | #include <TabWidget.h>
|
---|
| 24 | #include <Tab.h>
|
---|
| 25 | #include <DoubleSpinBox.h>
|
---|
| 26 | #include <Vector3DSpinBox.h>
|
---|
| 27 | #include <Gui.h>
|
---|
| 28 | #include <Euler.h>
|
---|
| 29 |
|
---|
| 30 | #include <ISceneManager.h>
|
---|
| 31 | #include <ICameraSceneNode.h>
|
---|
| 32 | #include <IVideoDriver.h>
|
---|
| 33 |
|
---|
| 34 | #define PBO_COUNT 2
|
---|
| 35 |
|
---|
| 36 | using namespace irr;
|
---|
| 37 | using namespace irr::scene;
|
---|
| 38 | using namespace irr::core;
|
---|
| 39 | using namespace flair::core;
|
---|
| 40 | using namespace flair::gui;
|
---|
| 41 | using namespace flair::simulator;
|
---|
| 42 |
|
---|
[15] | 43 | namespace flair {
|
---|
| 44 | namespace sensor {
|
---|
[8] | 45 |
|
---|
[15] | 46 | SimuCameraGL::SimuCameraGL(const Model *parent, std::string name, int width,
|
---|
| 47 | int height, int x, int y, int dev_id)
|
---|
| 48 | : SimuCamera(parent, name, width, height, 3, dev_id), SensorGL(parent) {
|
---|
| 49 | smgr = getGui()->getSceneManager();
|
---|
| 50 | camera = smgr->addCameraSceneNode();
|
---|
| 51 | camera->addAnimator(this);
|
---|
| 52 | camera->setAspectRatio(4.0f / 3.0f); // on force a cause du view port
|
---|
[8] | 53 |
|
---|
[15] | 54 | this->width = width;
|
---|
| 55 | this->height = height;
|
---|
| 56 | this->x = x;
|
---|
| 57 | this->y = y;
|
---|
[8] | 58 |
|
---|
[15] | 59 | index = 0;
|
---|
[8] | 60 |
|
---|
[15] | 61 | buffer = (char *)malloc(width * height * 3);
|
---|
[8] | 62 |
|
---|
[15] | 63 | // user interface
|
---|
| 64 | Tab *setup_tab = new Tab(parent->GetTabWidget(), name);
|
---|
| 65 | position = new Vector3DSpinBox(setup_tab->NewRow(), "position", -2, 2, .01);
|
---|
| 66 | direction = new Vector3DSpinBox(setup_tab->NewRow(), "direction", -2, 2, .01);
|
---|
| 67 | up = new Vector3DSpinBox(setup_tab->NewRow(), "up", -2, 2, .01);
|
---|
| 68 | fov = new DoubleSpinBox(setup_tab->NewRow(), "fov:", 0, 180, 5);
|
---|
[8] | 69 |
|
---|
[15] | 70 | if (strcmp((char *)glGetString(GL_VENDOR),
|
---|
| 71 | "Intel Open Source Technology Center") == 0) {
|
---|
| 72 | Thread::Warn("disabling cameras output for Intel card (bug with PBO)\n");
|
---|
| 73 | disable_output = true;
|
---|
| 74 | } else {
|
---|
| 75 | disable_output = false;
|
---|
| 76 | }
|
---|
[8] | 77 |
|
---|
[15] | 78 | if (isGlExtensionSupported("GL_ARB_pixel_buffer_object")) {
|
---|
| 79 | use_pbo = true;
|
---|
| 80 | // create 2 pixel buffer objects, you need to delete them when program
|
---|
| 81 | // exits.
|
---|
| 82 | // glBufferDataARB with NULL pointer reserves only memory space.
|
---|
| 83 | pboIds = (GLuint *)malloc(PBO_COUNT * sizeof(GLuint));
|
---|
| 84 | glGenBuffersARB(PBO_COUNT, pboIds);
|
---|
| 85 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[0]);
|
---|
| 86 | glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, width * height * 3, 0,
|
---|
| 87 | GL_STREAM_READ_ARB);
|
---|
| 88 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[1]);
|
---|
| 89 | glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, width * height * 3, 0,
|
---|
| 90 | GL_STREAM_READ_ARB);
|
---|
[8] | 91 |
|
---|
[15] | 92 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
|
---|
| 93 | } else {
|
---|
| 94 | use_pbo = false;
|
---|
| 95 | Thread::Warn("GL_ARB_pixel_buffer_object is not suppoorted\n");
|
---|
| 96 | }
|
---|
| 97 | if (isGlExtensionSupported("GL_PACK_INVERT_MESA")) {
|
---|
| 98 | invert_pixel = false;
|
---|
| 99 | Thread::Warn("GL_PACK_INVERT_MESA is suppoorted\n");
|
---|
| 100 | } else {
|
---|
| 101 | invert_pixel = true;
|
---|
| 102 | }
|
---|
[8] | 103 | }
|
---|
| 104 |
|
---|
[15] | 105 | SimuCameraGL::~SimuCameraGL() {
|
---|
| 106 | free(buffer);
|
---|
[8] | 107 |
|
---|
[15] | 108 | if (use_pbo) {
|
---|
| 109 | glDeleteBuffersARB(PBO_COUNT, pboIds);
|
---|
| 110 | free(pboIds);
|
---|
| 111 | }
|
---|
[8] | 112 | }
|
---|
| 113 |
|
---|
[15] | 114 | void SimuCameraGL::setNearValue(float zn) { camera->setNearValue(zn); }
|
---|
[8] | 115 |
|
---|
[15] | 116 | void SimuCameraGL::setFarValue(float zf) { camera->setFarValue(zf); }
|
---|
[8] | 117 |
|
---|
[15] | 118 | void SimuCameraGL::UpdateFrom(const io_data *data) {
|
---|
| 119 | if (noGui() == false && data == NULL) {
|
---|
| 120 | smgr->setActiveCamera(camera);
|
---|
| 121 | smgr->getVideoDriver()->setViewPort(rect<s32>(x, y, x + width, y + height));
|
---|
| 122 | smgr->drawAll();
|
---|
| 123 | // use_pbo=false;
|
---|
| 124 | getImage();
|
---|
| 125 | }
|
---|
[8] | 126 | }
|
---|
| 127 |
|
---|
[15] | 128 | void SimuCameraGL::getImage(void) {
|
---|
| 129 | if (disable_output)
|
---|
| 130 | return;
|
---|
| 131 | // convert from irrlicht top left origin to gl bottom left origin
|
---|
| 132 | int y = smgr->getVideoDriver()->getScreenSize().Height - height - this->y;
|
---|
[8] | 133 |
|
---|
[15] | 134 | // We want to read the front buffer to get the latest render finished.
|
---|
| 135 | Time a = GetTime();
|
---|
| 136 | glReadBuffer(GL_FRONT);
|
---|
| 137 | if (use_pbo) // with PBO
|
---|
| 138 | {
|
---|
| 139 | // increment current index first then get the next index
|
---|
| 140 | // "index" is used to read pixels from a framebuffer to a PBO
|
---|
| 141 | // "nextIndex" is used to process pixels in the other PBO
|
---|
| 142 | index = (index + 1) % PBO_COUNT;
|
---|
| 143 | int nextIndex = (index + 1) % PBO_COUNT;
|
---|
[8] | 144 |
|
---|
[15] | 145 | // copy pixels from framebuffer to PBO
|
---|
| 146 | // Use offset instead of pointer.
|
---|
| 147 | // OpenGL should perform asynch DMA transfer, so glReadPixels() will return
|
---|
| 148 | // immediately.
|
---|
| 149 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[index]);
|
---|
| 150 | glReadPixels(x, y, width, height, GL_BGR, GL_UNSIGNED_BYTE, 0);
|
---|
[8] | 151 |
|
---|
[15] | 152 | // map the PBO that contain framebuffer pixels before processing it
|
---|
| 153 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[nextIndex]);
|
---|
| 154 | GLubyte *src = (GLubyte *)glMapBufferARB(
|
---|
| 155 | GL_PIXEL_PACK_BUFFER_ARB, GL_READ_WRITE_ARB); // GL_READ_ONLY_ARB);
|
---|
| 156 | if (src) {
|
---|
| 157 | putImage((char *)src);
|
---|
| 158 | glUnmapBufferARB(
|
---|
| 159 | GL_PIXEL_PACK_BUFFER_ARB); // release pointer to the mapped buffer
|
---|
[8] | 160 | }
|
---|
[15] | 161 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
|
---|
| 162 | } else {
|
---|
| 163 | glReadPixels(x, y, width, height, GL_BGR, GL_UNSIGNED_BYTE, buffer);
|
---|
| 164 | putImage(buffer);
|
---|
| 165 | }
|
---|
[8] | 166 |
|
---|
[15] | 167 | glReadBuffer(GL_BACK);
|
---|
| 168 | Time b = GetTime();
|
---|
| 169 | Time c = b - a;
|
---|
| 170 | // printf("%s %f\n",Thread::ObjectName().c_str(),(float)(c/1000000.));
|
---|
[8] | 171 | }
|
---|
| 172 |
|
---|
[15] | 173 | void SimuCameraGL::putImage(char *buf) {
|
---|
| 174 | if (invert_pixel == true) {
|
---|
| 175 | // opengl images are horizontally flipped, so we have to fix that here.
|
---|
| 176 | const s32 pitch = width * 3;
|
---|
| 177 | char *pixels = buf;
|
---|
| 178 | char *p2 = pixels + (height - 1) * pitch;
|
---|
| 179 | char *tmpBuffer = new char[pitch];
|
---|
| 180 | for (int i = 0; i < height; i += 2) {
|
---|
| 181 | memcpy(tmpBuffer, pixels, pitch);
|
---|
| 182 | memcpy(pixels, p2, pitch);
|
---|
| 183 | memcpy(p2, tmpBuffer, pitch);
|
---|
| 184 | pixels += pitch;
|
---|
| 185 | p2 -= pitch;
|
---|
[8] | 186 | }
|
---|
[15] | 187 | delete[] tmpBuffer;
|
---|
| 188 | }
|
---|
[8] | 189 |
|
---|
[15] | 190 | shmem->Write(buf, width * height * 3);
|
---|
[8] | 191 | }
|
---|
| 192 |
|
---|
[15] | 193 | void SimuCameraGL::animateNode(ISceneNode *node, u32 timeMs) {
|
---|
| 194 | ICameraSceneNode *camera = static_cast<ICameraSceneNode *>(node);
|
---|
[8] | 195 |
|
---|
[15] | 196 | matrix4 m;
|
---|
| 197 | m.setRotationDegrees(Node()->getRotation());
|
---|
[8] | 198 |
|
---|
[15] | 199 | // transform forward vector of camera
|
---|
| 200 | vector3df frv = ToIrrlichtCoordinates(direction->Value());
|
---|
| 201 | m.transformVect(frv);
|
---|
[8] | 202 |
|
---|
[15] | 203 | // transform upvector of camera
|
---|
| 204 | vector3df upv = ToIrrlichtCoordinates(up->Value());
|
---|
| 205 | m.transformVect(upv);
|
---|
[8] | 206 |
|
---|
[15] | 207 | // transform camera offset (thanks to Zeuss for finding it was missing)
|
---|
| 208 | vector3df offset = ToIrrlichtCoordinates(position->Value());
|
---|
| 209 | m.transformVect(offset);
|
---|
[8] | 210 |
|
---|
[15] | 211 | // set camera
|
---|
| 212 | camera->setPosition(Node()->getPosition() +
|
---|
| 213 | offset); // position camera in front of the ship
|
---|
| 214 | camera->setUpVector(upv); // set up vector of camera >> Zeuss - tested with
|
---|
| 215 | // +node->getPostion() and it didnt work, but this
|
---|
| 216 | // works fine.
|
---|
| 217 | camera->setTarget(Node()->getPosition() + offset + frv); // set target of
|
---|
| 218 | // camera (look at
|
---|
| 219 | // point) >> Zeuss -
|
---|
| 220 | // Dont forget to add
|
---|
| 221 | // the node positiob
|
---|
[8] | 222 |
|
---|
[15] | 223 | camera->setFOV(Euler::ToRadian(fov->Value()));
|
---|
[8] | 224 | }
|
---|
| 225 |
|
---|
[15] | 226 | ISceneNodeAnimator *SimuCameraGL::createClone(ISceneNode *node,
|
---|
| 227 | ISceneManager *newManager) {
|
---|
| 228 | return NULL;
|
---|
[8] | 229 | }
|
---|
| 230 |
|
---|
| 231 | } // end namespace sensor
|
---|
| 232 | } // end namespace flair
|
---|
| 233 | #endif
|
---|