1
#include "lightSensor.h"
3
#include <osgDB/WriteFile>
7
LightSensor::LightSensor(dtCore::RefPtr<BodyBase> attachedTo, osg::Vec3 pos, osg::Vec3 orient, float flareAngle, const std::string &name):
8
CameraSensorBase(attachedTo, pos, orient, name, "LightSensor", RGB_CAMERA, true)
11
// light sensor actually consists of a grid of 30x30 pixels. These values
12
// will be added to get a more accurate sensor value.
13
this->minDistance = 1;
14
this->maxDistance = 100;
15
this->resolution = osg::Vec2(30,30);
16
this->cameraType = RGB_CAMERA;
19
if ((flareAngle>0) && (flareAngle != M_PI/4) ) {
21
this->frustumDown = tan(flareAngle/2) * this->minDistance;
22
this->frustumUp = tan(flareAngle/2) * this->minDistance;
23
this->frustumRight = tan(flareAngle/2) * this->minDistance;
24
this->frustumLeft = tan(flareAngle/2) * this->minDistance;
28
//this->init(attachedTo, pos, orient);
30
this->type = "LightSensor";
33
this->sensorReturnValueType = typeid(unsigned long*).name();
37
void LightSensor::getSensorValue(std::ostream& theStream) const {
43
// DO NOT USE THIS FUNCTION DIRECTLY, USE THE STREAM OPERATOR INSTEAD !!!
44
void LightSensor::getNewSensorValue() {
46
dynamic_cast<dtCore::Camera*>(sensor.get())->SetClearColor(osg::Vec4(255,255,255,255));
48
unsigned long lightValue = 0;
51
osg::ref_ptr<osg::Image> image = getRawData();
53
// add red, green and blue values for all pixels
54
for (unsigned int i=0; i<image->getImageSizeInBytes(); i++)
55
lightValue += image->data()[i];
58
this->value = (int) (lightValue / 2700);
62
void LightSensor::evaluate(ParameterReturnValue srv)
66
osg::Image* image = new osg::Image;
68
int x = static_cast<int>(this->camera->GetOSGCamera()->getViewport()->x());
69
int y = static_cast<int>(this->camera->GetOSGCamera()->getViewport()->y());
70
unsigned int width = static_cast<unsigned int>(this->camera->GetOSGCamera()->getViewport()->width());
71
unsigned int height = static_cast<unsigned int>(this->camera->GetOSGCamera()->getViewport()->height());
73
printf("%s: Viewport x: %d Viewport y: %d Höhe: %d, Breite: %d \n", this->camera->GetName().c_str(), x, y, height, width);
75
image->allocateImage(width, height, 1, GL_RGB, GL_UNSIGNED_BYTE);
76
image->readPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE);
80
// sensor value type check
81
if (srv.type != typeid(unsigned long*).name())
82
printf("Parameter type needs to be unsigned long*. Casting could lead to errors. \n");
84
// cast void* to unsigned long int*
85
unsigned long* data = reinterpret_cast<unsigned long*> (srv.pValue);
89
// return sensor value
91
//srv.pValue = &(this->value);
93
//note: this is a raw sensor value. it might be necessary to normalize the sensor value to fit in a given range.
98
LightSensor::~LightSensor()