1 | // %flair:license{
|
---|
2 | // This file is part of the Flair framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
4 | // %flair:license}
|
---|
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 |
|
---|
43 | namespace flair {
|
---|
44 | namespace sensor {
|
---|
45 |
|
---|
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
|
---|
53 |
|
---|
54 | this->width = width;
|
---|
55 | this->height = height;
|
---|
56 | this->x = x;
|
---|
57 | this->y = y;
|
---|
58 |
|
---|
59 | index = 0;
|
---|
60 |
|
---|
61 | // user interface
|
---|
62 | Tab *setup_tab = new Tab(parent->GetTabWidget(), name);
|
---|
63 | position = new Vector3DSpinBox(setup_tab->NewRow(), "position", -2, 2, .01);
|
---|
64 | direction = new Vector3DSpinBox(setup_tab->NewRow(), "direction", -2, 2, .01);
|
---|
65 | up = new Vector3DSpinBox(setup_tab->NewRow(), "up", -2, 2, .01);
|
---|
66 | fov = new DoubleSpinBox(setup_tab->NewRow(), "fov:", 0, 180, 5);
|
---|
67 |
|
---|
68 | if (strcmp((char *)glGetString(GL_VENDOR),
|
---|
69 | "Intel Open Source Technology Center") == 0) {
|
---|
70 | //Thread::Warn("disabling cameras output for Intel card (bug with PBO)\n");
|
---|
71 | Thread::Warn("camera output for Intel card may have bugs with PBO\n");
|
---|
72 | //disable_output = true;
|
---|
73 | disable_output = false;
|
---|
74 | } else {
|
---|
75 | disable_output = false;
|
---|
76 | }
|
---|
77 |
|
---|
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+sizeof(Time), 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+sizeof(Time), 0,
|
---|
90 | GL_STREAM_READ_ARB);
|
---|
91 |
|
---|
92 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
|
---|
93 | } else {
|
---|
94 | use_pbo = false;
|
---|
95 | buffer = (char *)malloc(width * height * 3+sizeof(Time));
|
---|
96 | Thread::Warn("GL_ARB_pixel_buffer_object is not suppoorted\n");
|
---|
97 | }
|
---|
98 | if (isGlExtensionSupported("GL_PACK_INVERT_MESA")) {
|
---|
99 | invert_pixel = false;
|
---|
100 | Thread::Warn("GL_PACK_INVERT_MESA is suppoorted\n");
|
---|
101 | } else {
|
---|
102 | invert_pixel = true;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | SimuCameraGL::~SimuCameraGL() {
|
---|
107 | if (use_pbo) {
|
---|
108 | glDeleteBuffersARB(PBO_COUNT, pboIds);
|
---|
109 | free(pboIds);
|
---|
110 | } else {
|
---|
111 | free(buffer);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | void SimuCameraGL::setNearValue(float zn) { camera->setNearValue(zn); }
|
---|
116 |
|
---|
117 | void SimuCameraGL::setFarValue(float zf) { camera->setFarValue(zf); }
|
---|
118 |
|
---|
119 | void SimuCameraGL::UpdateFrom(const io_data *data) {
|
---|
120 | if (noGui() == false && data == NULL) {
|
---|
121 | smgr->setActiveCamera(camera);
|
---|
122 | smgr->getVideoDriver()->setViewPort(rect<s32>(x, y, x + width, y + height));
|
---|
123 | smgr->drawAll();
|
---|
124 | // use_pbo=false;
|
---|
125 | getImage();
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | void SimuCameraGL::getImage(void) {
|
---|
130 | if (disable_output)
|
---|
131 | return;
|
---|
132 | // convert from irrlicht top left origin to gl bottom left origin
|
---|
133 | int y = smgr->getVideoDriver()->getScreenSize().Height - height - this->y;
|
---|
134 |
|
---|
135 | // We want to read the front buffer to get the latest render finished.
|
---|
136 | Time a = GetTime();
|
---|
137 | glReadBuffer(GL_FRONT);
|
---|
138 | if (use_pbo) // with PBO
|
---|
139 | {
|
---|
140 | // increment current index first then get the next index
|
---|
141 | // "index" is used to read pixels from a framebuffer to a PBO
|
---|
142 | // "nextIndex" is used to process pixels in the other PBO
|
---|
143 | index = (index + 1) % PBO_COUNT;
|
---|
144 | int nextIndex = (index + 1) % PBO_COUNT;
|
---|
145 |
|
---|
146 | // copy pixels from framebuffer to PBO
|
---|
147 | // Use offset instead of pointer.
|
---|
148 | // OpenGL should perform asynch DMA transfer, so glReadPixels() will return
|
---|
149 | // immediately.
|
---|
150 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[index]);
|
---|
151 | glReadPixels(x, y, width, height, GL_BGR, GL_UNSIGNED_BYTE, 0);
|
---|
152 | Time time=GetTime();
|
---|
153 | // map the PBO that contain framebuffer pixels before processing it
|
---|
154 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[nextIndex]);
|
---|
155 | GLubyte *src = (GLubyte *)glMapBufferARB(
|
---|
156 | GL_PIXEL_PACK_BUFFER_ARB, GL_READ_WRITE_ARB); // GL_READ_ONLY_ARB);
|
---|
157 | if (src) {
|
---|
158 | putImage((char *)src,time);
|
---|
159 | glUnmapBufferARB(
|
---|
160 | GL_PIXEL_PACK_BUFFER_ARB); // release pointer to the mapped buffer
|
---|
161 | }
|
---|
162 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
|
---|
163 | } else {
|
---|
164 | glReadPixels(x, y, width, height, GL_BGR, GL_UNSIGNED_BYTE, buffer);
|
---|
165 | putImage(buffer,GetTime());
|
---|
166 | }
|
---|
167 |
|
---|
168 | glReadBuffer(GL_BACK);
|
---|
169 | Time b = GetTime();
|
---|
170 | Time c = b - a;
|
---|
171 | // printf("%s %f\n",Thread::ObjectName().c_str(),(float)(c/1000000.));
|
---|
172 | }
|
---|
173 |
|
---|
174 | void SimuCameraGL::putImage(char *buf,Time imageTime) {
|
---|
175 | if (invert_pixel == true) {
|
---|
176 | // opengl images are horizontally flipped, so we have to fix that here.
|
---|
177 | const s32 pitch = width * 3;
|
---|
178 | char *pixels = buf;
|
---|
179 | char *p2 = pixels + (height - 1) * pitch;
|
---|
180 | char *tmpBuffer = new char[pitch];
|
---|
181 | for (int i = 0; i < height; i += 2) {
|
---|
182 | memcpy(tmpBuffer, pixels, pitch);
|
---|
183 | memcpy(pixels, p2, pitch);
|
---|
184 | memcpy(p2, tmpBuffer, pitch);
|
---|
185 | pixels += pitch;
|
---|
186 | p2 -= pitch;
|
---|
187 | }
|
---|
188 | delete[] tmpBuffer;
|
---|
189 | }
|
---|
190 | memcpy(buf+width * height * 3,&imageTime,sizeof(Time));
|
---|
191 | shmem->Write(buf, width * height * 3+sizeof(Time));
|
---|
192 | }
|
---|
193 |
|
---|
194 | void SimuCameraGL::animateNode(ISceneNode *node, u32 timeMs) {
|
---|
195 | ICameraSceneNode *camera = static_cast<ICameraSceneNode *>(node);
|
---|
196 |
|
---|
197 | matrix4 m;
|
---|
198 | m.setRotationDegrees(Node()->getRotation());
|
---|
199 |
|
---|
200 | // transform forward vector of camera
|
---|
201 | vector3df frv = ToIrrlichtCoordinates(direction->Value());
|
---|
202 | m.transformVect(frv);
|
---|
203 |
|
---|
204 | // transform upvector of camera
|
---|
205 | vector3df upv = ToIrrlichtCoordinates(up->Value());
|
---|
206 | m.transformVect(upv);
|
---|
207 |
|
---|
208 | // transform camera offset (thanks to Zeuss for finding it was missing)
|
---|
209 | vector3df offset = ToIrrlichtCoordinates(position->Value());
|
---|
210 | m.transformVect(offset);
|
---|
211 |
|
---|
212 | // set camera
|
---|
213 | camera->setPosition(Node()->getPosition() +
|
---|
214 | offset); // position camera in front of the ship
|
---|
215 | camera->setUpVector(upv); // set up vector of camera >> Zeuss - tested with
|
---|
216 | // +node->getPostion() and it didnt work, but this
|
---|
217 | // works fine.
|
---|
218 | camera->setTarget(Node()->getPosition() + offset + frv); // set target of
|
---|
219 | // camera (look at
|
---|
220 | // point) >> Zeuss -
|
---|
221 | // Dont forget to add
|
---|
222 | // the node positiob
|
---|
223 |
|
---|
224 | camera->setFOV(Euler::ToRadian(fov->Value()));
|
---|
225 | }
|
---|
226 |
|
---|
227 | ISceneNodeAnimator *SimuCameraGL::createClone(ISceneNode *node,
|
---|
228 | ISceneManager *newManager) {
|
---|
229 | return NULL;
|
---|
230 | }
|
---|
231 |
|
---|
232 | } // end namespace sensor
|
---|
233 | } // end namespace flair
|
---|
234 | #endif
|
---|