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

Last change on this file since 228 was 224, checked in by Sanahuja Guillaume, 7 years ago

maj for armv5te

File size: 7.7 KB
RevLine 
[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
36using namespace irr;
37using namespace irr::scene;
38using namespace irr::core;
39using namespace flair::core;
40using namespace flair::gui;
41using namespace flair::simulator;
42
[15]43namespace flair {
44namespace sensor {
[8]45
[15]46SimuCameraGL::SimuCameraGL(const Model *parent, std::string name, int width,
[158]47 int height, int x, int y, uint32_t modelId,uint32_t deviceId)
48 : SimuCamera(parent, name, width, height, 3, modelId,deviceId), SensorGL(parent) {
[15]49 smgr = getGui()->getSceneManager();
50 camera = smgr->addCameraSceneNode();
51 camera->addAnimator(this);
[158]52 camera->setAspectRatio((float)width /(float)height); // 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 // 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);
[8]67
[15]68 if (strcmp((char *)glGetString(GL_VENDOR),
69 "Intel Open Source Technology Center") == 0) {
[132]70 //Thread::Warn("disabling cameras output for Intel card (bug with PBO)\n");
[224]71 Warn("camera output for Intel card may have bugs with PBO\n");
[132]72 //disable_output = true;
73 disable_output = false;
[15]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]);
[151]86 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, width * height * 3+sizeof(Time), 0,
[15]87 GL_STREAM_READ_ARB);
88 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[1]);
[151]89 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, width * height * 3+sizeof(Time), 0,
[15]90 GL_STREAM_READ_ARB);
[8]91
[15]92 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
93 } else {
94 use_pbo = false;
[151]95 buffer = (char *)malloc(width * height * 3+sizeof(Time));
[224]96 Warn("GL_ARB_pixel_buffer_object is not suppoorted\n");
[15]97 }
98 if (isGlExtensionSupported("GL_PACK_INVERT_MESA")) {
99 invert_pixel = false;
[224]100 Warn("GL_PACK_INVERT_MESA is supported\n");
[15]101 } else {
102 invert_pixel = true;
103 }
[8]104}
105
[15]106SimuCameraGL::~SimuCameraGL() {
107 if (use_pbo) {
108 glDeleteBuffersARB(PBO_COUNT, pboIds);
109 free(pboIds);
[151]110 } else {
111 free(buffer);
[15]112 }
[162]113 camera->removeAnimator(this);
114 camera->drop();
[8]115}
116
[15]117void SimuCameraGL::setNearValue(float zn) { camera->setNearValue(zn); }
[8]118
[15]119void SimuCameraGL::setFarValue(float zf) { camera->setFarValue(zf); }
[8]120
[15]121void SimuCameraGL::UpdateFrom(const io_data *data) {
122 if (noGui() == false && data == NULL) {
123 smgr->setActiveCamera(camera);
124 smgr->getVideoDriver()->setViewPort(rect<s32>(x, y, x + width, y + height));
125 smgr->drawAll();
126 getImage();
127 }
[8]128}
129
[15]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;
[8]135
[15]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;
[8]146
[15]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);
[151]153 Time time=GetTime();
[15]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) {
[151]159 putImage((char *)src,time);
[15]160 glUnmapBufferARB(
161 GL_PIXEL_PACK_BUFFER_ARB); // release pointer to the mapped buffer
[8]162 }
[15]163 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
164 } else {
165 glReadPixels(x, y, width, height, GL_BGR, GL_UNSIGNED_BYTE, buffer);
[151]166 putImage(buffer,GetTime());
[15]167 }
[8]168
[15]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.));
[8]173}
174
[151]175void SimuCameraGL::putImage(char *buf,Time imageTime) {
[15]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;
[8]188 }
[15]189 delete[] tmpBuffer;
190 }
[151]191 memcpy(buf+width * height * 3,&imageTime,sizeof(Time));
192 shmem->Write(buf, width * height * 3+sizeof(Time));
[8]193}
194
[15]195void SimuCameraGL::animateNode(ISceneNode *node, u32 timeMs) {
196 ICameraSceneNode *camera = static_cast<ICameraSceneNode *>(node);
[8]197
[15]198 matrix4 m;
199 m.setRotationDegrees(Node()->getRotation());
[8]200
[15]201 // transform forward vector of camera
202 vector3df frv = ToIrrlichtCoordinates(direction->Value());
203 m.transformVect(frv);
[8]204
[15]205 // transform upvector of camera
206 vector3df upv = ToIrrlichtCoordinates(up->Value());
207 m.transformVect(upv);
[8]208
[15]209 // transform camera offset (thanks to Zeuss for finding it was missing)
210 vector3df offset = ToIrrlichtCoordinates(position->Value());
211 m.transformVect(offset);
[8]212
[15]213 // set camera
214 camera->setPosition(Node()->getPosition() +
215 offset); // position camera in front of the ship
216 camera->setUpVector(upv); // set up vector of camera >> Zeuss - tested with
217 // +node->getPostion() and it didnt work, but this
218 // works fine.
219 camera->setTarget(Node()->getPosition() + offset + frv); // set target of
220 // camera (look at
221 // point) >> Zeuss -
222 // Dont forget to add
223 // the node positiob
[8]224
[15]225 camera->setFOV(Euler::ToRadian(fov->Value()));
[8]226}
227
[15]228ISceneNodeAnimator *SimuCameraGL::createClone(ISceneNode *node,
229 ISceneManager *newManager) {
230 return NULL;
[8]231}
232
233} // end namespace sensor
234} // end namespace flair
235#endif
Note: See TracBrowser for help on using the repository browser.