close Warning: Can't use blame annotator:
svn blame failed on trunk/lib/FlairSimulator/src/SimuCameraGL.cpp: 200029 - Couldn't perform atomic initialization

source: flair-src/trunk/lib/FlairSimulator/src/SimuCameraGL.cpp@ 146

Last change on this file since 146 was 146, checked in by Bayard Gildas, 7 years ago

OK

File size: 7.5 KB
RevLine 
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
36using namespace irr;
37using namespace irr::scene;
38using namespace irr::core;
39using namespace flair::core;
40using namespace flair::gui;
41using namespace flair::simulator;
42
43namespace flair {
44namespace sensor {
45
46SimuCameraGL::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 buffer = (char *)malloc(width * height * 3);
62
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);
69
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 Thread::Warn("camera output for Intel card may have bugs with PBO\n");
74 //disable_output = true;
75 disable_output = false;
76 } else {
77 disable_output = false;
78 }
79
80 if (isGlExtensionSupported("GL_ARB_pixel_buffer_object")) {
81 use_pbo = true;
82 // create 2 pixel buffer objects, you need to delete them when program
83 // exits.
84 // glBufferDataARB with NULL pointer reserves only memory space.
85 pboIds = (GLuint *)malloc(PBO_COUNT * sizeof(GLuint));
86 glGenBuffersARB(PBO_COUNT, pboIds);
87 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[0]);
88 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, width * height * 3, 0,
89 GL_STREAM_READ_ARB);
90 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[1]);
91 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, width * height * 3, 0,
92 GL_STREAM_READ_ARB);
93
94 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
95 } else {
96 use_pbo = false;
97 Thread::Warn("GL_ARB_pixel_buffer_object is not suppoorted\n");
98 }
99 if (isGlExtensionSupported("GL_PACK_INVERT_MESA")) {
100 invert_pixel = false;
101 Thread::Warn("GL_PACK_INVERT_MESA is suppoorted\n");
102 } else {
103 invert_pixel = true;
104 }
105}
106
107SimuCameraGL::~SimuCameraGL() {
108 free(buffer);
109
110 if (use_pbo) {
111 glDeleteBuffersARB(PBO_COUNT, pboIds);
112 free(pboIds);
113 }
114}
115
116void SimuCameraGL::setNearValue(float zn) { camera->setNearValue(zn); }
117
118void SimuCameraGL::setFarValue(float zf) { camera->setFarValue(zf); }
119
120void SimuCameraGL::UpdateFrom(const io_data *data) {
121 if (noGui() == false && data == NULL) {
122 smgr->setActiveCamera(camera);
123 smgr->getVideoDriver()->setViewPort(rect<s32>(x, y, x + width, y + height));
124 smgr->drawAll();
125 // use_pbo=false;
126 getImage();
127 }
128}
129
130void SimuCameraGL::getImage(void) {
131 if (disable_output)
132 return;
133 // convert from irrlicht top left origin to gl bottom left origin
134 int y = smgr->getVideoDriver()->getScreenSize().Height - height - this->y;
135
136 // We want to read the front buffer to get the latest render finished.
137 Time a = GetTime();
138 glReadBuffer(GL_FRONT);
139 if (use_pbo) // with PBO
140 {
141 // increment current index first then get the next index
142 // "index" is used to read pixels from a framebuffer to a PBO
143 // "nextIndex" is used to process pixels in the other PBO
144 index = (index + 1) % PBO_COUNT;
145 int nextIndex = (index + 1) % PBO_COUNT;
146
147 // copy pixels from framebuffer to PBO
148 // Use offset instead of pointer.
149 // OpenGL should perform asynch DMA transfer, so glReadPixels() will return
150 // immediately.
151 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[index]);
152 glReadPixels(x, y, width, height, GL_BGR, GL_UNSIGNED_BYTE, 0);
153
154 // map the PBO that contain framebuffer pixels before processing it
155 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[nextIndex]);
156 GLubyte *src = (GLubyte *)glMapBufferARB(
157 GL_PIXEL_PACK_BUFFER_ARB, GL_READ_WRITE_ARB); // GL_READ_ONLY_ARB);
158 if (src) {
159 putImage((char *)src);
160 glUnmapBufferARB(
161 GL_PIXEL_PACK_BUFFER_ARB); // release pointer to the mapped buffer
162 }
163 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
164 } else {
165 glReadPixels(x, y, width, height, GL_BGR, GL_UNSIGNED_BYTE, buffer);
166 putImage(buffer);
167 }
168
169 glReadBuffer(GL_BACK);
170 Time b = GetTime();
171 Time c = b - a;
172 // printf("%s %f\n",Thread::ObjectName().c_str(),(float)(c/1000000.));
173}
174
175void SimuCameraGL::putImage(char *buf) {
176 if (invert_pixel == true) {
177 // opengl images are horizontally flipped, so we have to fix that here.
178 const s32 pitch = width * 3;
179 char *pixels = buf;
180 char *p2 = pixels + (height - 1) * pitch;
181 char *tmpBuffer = new char[pitch];
182 for (int i = 0; i < height; i += 2) {
183 memcpy(tmpBuffer, pixels, pitch);
184 memcpy(pixels, p2, pitch);
185 memcpy(p2, tmpBuffer, pitch);
186 pixels += pitch;
187 p2 -= pitch;
188 }
189 delete[] tmpBuffer;
190 }
191
192 shmem->Write(buf, width * height * 3);
193Printf("écriture faite!\n");
194}
195
196void SimuCameraGL::animateNode(ISceneNode *node, u32 timeMs) {
197 ICameraSceneNode *camera = static_cast<ICameraSceneNode *>(node);
198
199 matrix4 m;
200 m.setRotationDegrees(Node()->getRotation());
201
202 // transform forward vector of camera
203 vector3df frv = ToIrrlichtCoordinates(direction->Value());
204 m.transformVect(frv);
205
206 // transform upvector of camera
207 vector3df upv = ToIrrlichtCoordinates(up->Value());
208 m.transformVect(upv);
209
210 // transform camera offset (thanks to Zeuss for finding it was missing)
211 vector3df offset = ToIrrlichtCoordinates(position->Value());
212 m.transformVect(offset);
213
214 // set camera
215 camera->setPosition(Node()->getPosition() +
216 offset); // position camera in front of the ship
217 camera->setUpVector(upv); // set up vector of camera >> Zeuss - tested with
218 // +node->getPostion() and it didnt work, but this
219 // works fine.
220 camera->setTarget(Node()->getPosition() + offset + frv); // set target of
221 // camera (look at
222 // point) >> Zeuss -
223 // Dont forget to add
224 // the node positiob
225
226 camera->setFOV(Euler::ToRadian(fov->Value()));
227}
228
229ISceneNodeAnimator *SimuCameraGL::createClone(ISceneNode *node,
230 ISceneManager *newManager) {
231 return NULL;
232}
233
234} // end namespace sensor
235} // end namespace flair
236#endif
Note: See TracBrowser for help on using the repository browser.