27 if(event.EventType == EET_KEY_INPUT_EVENT)
29 if(event.KeyInput.PressedDown)
31 switch(event.KeyInput.Key)
47 if(event.EventType == EET_MOUSE_INPUT_EVENT)
49 switch(event.MouseInput.Event)
51 case EMIE_LMOUSE_PRESSED_DOWN:
52 m_mouse.X =
event.MouseInput.X;
53 m_mouse.Y =
event.MouseInput.Y;
55 case EMIE_MOUSE_WHEEL:
57 if(event.MouseInput.Wheel == 1)
59 if (m_radius > 0.001){
65 else if(event.MouseInput.Wheel == -1)
71 case EMIE_MOUSE_MOVED:
72 if (event.MouseInput.isLeftPressed()){
73 s32 dx =
event.MouseInput.X - m_mouse.X;
74 s32 dy =
event.MouseInput.Y - m_mouse.Y;
77 if (m_tilt < -
M_PI/2) m_tilt = -
M_PI/2;
78 if (m_tilt >
M_PI/2) m_tilt =
M_PI/2;
80 m_mouse.X =
event.MouseInput.X;
81 m_mouse.Y =
event.MouseInput.Y;
93 vector3df target = m_camera->getTarget();
94 m_eye.X = target.X + m_radius*cos(m_tilt)*cos(m_pan);
95 m_eye.Y = target.Y + m_radius*cos(m_tilt)*sin(m_pan);
96 m_eye.Z = target.Z + m_radius*sin(m_tilt);
97 m_camera->setPosition(m_eye);
105 class GLlink :
public ISceneNode
108 GLlink(ISceneNode *i_parent, ISceneManager *i_mgr, s32 i_id,
109 const LinkInfo &i_li, BodyInfo_var i_binfo) :
110 ISceneNode(i_parent, i_mgr, i_id),
111 m_jointId(i_li.jointId){
112 setAutomaticCulling(scene::EAC_OFF);
115 setPosition(vector3df( i_li.translation[0],
116 -i_li.translation[1],
117 i_li.translation[2]));
125 setRotation(vector3df(-180/
M_PI*rpy[0],
129 m_axis << i_li.jointAxis[0], i_li.jointAxis[1], i_li.jointAxis[2];
131 ShapeInfoSequence_var sis = i_binfo->shapes();
132 AppearanceInfoSequence_var ais = i_binfo->appearances();
133 MaterialInfoSequence_var mis = i_binfo->materials();
134 TextureInfoSequence_var txs = i_binfo->textures();
135 const TransformedShapeIndexSequence& tsis = i_li.shapeIndices;
138 core::vector3df vertex;
139 core::vector3df normal;
141 for (
unsigned int l=0;
l<tsis.length();
l++){
142 SMesh* mesh =
new SMesh();
143 SMeshBuffer* meshBuffer =
new SMeshBuffer();
144 mesh->addMeshBuffer(meshBuffer);
147 const TransformedShapeIndex &tsi = tsis[
l];
148 short index = tsi.shapeIndex;
149 ShapeInfo& si = sis[index];
150 const float *vertices = si.vertices.get_buffer();
151 const LongSequence& triangles = si.triangles;
152 const AppearanceInfo& ai = ais[si.appearanceIndex];
153 const float *normals = ai.normals.get_buffer();
155 const LongSequence& normalIndices = ai.normalIndices;
157 const int numTriangles = triangles.length() / 3;
160 video::SColor color(0xffffffff);
161 if (ai.colors.length()){
166 }
else if (ai.materialIndex >= 0){
167 const MaterialInfo& mi = mis[ai.materialIndex];
169 0xff*mi.diffuseColor[0],
170 0xff*mi.diffuseColor[1],
171 0xff*mi.diffuseColor[2]);
173 std::cout <<
"no material" << std::endl;
177 SMeshBuffer* mb =
reinterpret_cast<SMeshBuffer*
>(mesh->getMeshBuffer(mesh->getMeshBufferCount()-1));
178 u32 vCount = mb->getVertexCount();
180 const DblArray12& tfm = tsi.transformMatrix;
182 for (
int i=0;
i<3;
i++){
183 for (
int j=0; j<4; j++){
184 cmat[j*4+
i] = tfm[
i*4+j];
187 cmat[3] = cmat[7] = cmat[11] = 0.0; cmat[15] = 1.0;
188 vector3df
pos = cmat.getTranslation();
190 vector3df rpy = cmat.getRotationDegrees();
193 vector3df scale = cmat.getScale();
195 const float *textureCoordinate = NULL;
196 if (ai.textureIndex >= 0){
197 textureCoordinate = ai.textureCoordinate.get_buffer();
203 for(
int j=0; j < numTriangles; ++j){
204 if (!ai.normalPerVertex){
206 if (normalIndices.length() == 0){
209 p = normalIndices[j]*3;
211 if ( normals != NULL ) {
212 normal.X = normals[p];
213 normal.Y = -normals[p+1];
214 normal.Z = normals[p+2];
221 for(
int k=0; k < 3; ++k){
222 long orgVertexIndex = si.triangles[j * 3 + k];
223 if (ai.normalPerVertex){
225 if (normalIndices.length()){
226 p = normalIndices[j*3+k]*3;
228 p = orgVertexIndex*3;
230 normal.X = normals[p];
231 normal.Y = -normals[p+1];
232 normal.Z = normals[p+2];
234 int p = orgVertexIndex * 3;
235 vertex.X = scale.X*vertices[p];
236 vertex.Y = -scale.Y*vertices[p+1];
237 vertex.Z = scale.Z*vertices[p+2];
240 if (textureCoordinate){
242 texc.X = textureCoordinate[ai.textureCoordIndices[j*3+k]*2];
243 texc.Y = textureCoordinate[ai.textureCoordIndices[j*3+k]*2+1];
246 mb->Vertices.push_back(video::S3DVertex(vertex,normal,color, texc));
248 mb->Indices.push_back(vCount);
249 mb->Indices.push_back(vCount+2);
250 mb->Indices.push_back(vCount+1);
253 mesh->getMeshBuffer(0)->recalculateBoundingBox();
256 SAnimatedMesh* pAM = 0;
257 if ( 0 != mesh->getMeshBufferCount() )
259 mesh->recalculateBoundingBox();
260 pAM =
new SAnimatedMesh();
261 pAM->Type = EAMT_OBJ;
263 pAM->recalculateBoundingBox();
268 vector3df noscale(1,1,1);
271 = i_mgr->addMeshSceneNode(mesh,
this, -1,
276 if (ai.textureIndex >= 0){
277 const TextureInfo& ti = txs[ai.textureIndex];
279 video::IVideoDriver* driver = i_mgr->getVideoDriver();
280 const char *
path = ti.url;
281 SMaterial& mat = node->getMaterial(0);
282 ITexture *texture = driver->getTexture(path);
283 mat.setTexture( 0, texture);
288 const SensorInfoSequence& sensors = i_li.sensors;
289 for (
unsigned int i=0;
i<sensors.length();
i++){
290 const SensorInfo& si = sensors[
i];
291 std::string
type(si.type);
292 if (type ==
"Vision"){
294 ISceneNode *camera = i_mgr->addEmptySceneNode(
this);
295 camera->setName(si.name);
296 camera->setPosition(vector3df( si.translation[0],
305 camera->setRotation(vector3df(-180/
M_PI*rpy[0],
308 m_cameraInfos.push_back(
new GLcamera(si, camera));
319 vector3df euler(-rpy[0], rpy[1], -rpy[2]);
324 for (
unsigned int i=0;
i<m_cameraInfos.size();
i++){
325 if (strcmp(i_name, m_cameraInfos[
i]->
name())==0)
return m_cameraInfos[
i];
336 GLbody::GLbody(ISceneNode *i_parent, ISceneManager *i_mgr, s32 i_id,
337 BodyInfo_var i_binfo) :
338 ISceneNode(i_parent, i_mgr, i_id){
339 setAutomaticCulling(scene::EAC_OFF);
341 LinkInfoSequence_var lis = i_binfo->links();
343 for (
unsigned int i=0;
i<lis->length();
i++){
345 new GLlink(i_mgr->getRootSceneNode(), i_mgr,
346 -1, lis[
i], i_binfo));
350 for (
unsigned int i=0;
i<m_links.size();
i++){
351 const LinkInfo &li = lis[
i];
352 if (li.parentIndex < 0) {
356 for (
unsigned int j=0; j<li.childIndices.length(); j++){
357 m_links[
i]->addChild(m_links[li.childIndices[j]]);
364 m_root->setPosition(vector3df(i_pos[0], -i_pos[1], i_pos[2]));
365 m_root->setRotation(vector3df(-180/
M_PI*i_rpy[0],
367 -180/
M_PI*i_rpy[2]));
368 for (
unsigned int i=0;
i<m_links.size();
i++){
369 int id = m_links[
i]->jointId();
371 m_links[
i]->setQ(i_angles[
id]);
378 for (
unsigned int i=0;
i<m_links.size();
i++){
379 GLcamera *ci = m_links[
i]->findCamera(i_name);
388 m_near = i_info.specValues[0];
389 m_far = i_info.specValues[1];
390 m_fovy = i_info.specValues[2];
391 m_width = i_info.specValues[4];
398 #if 0 // This doesn't work 400 i_camera->setNearValue(
m_near);
401 i_camera->setFarValue(
m_far);
402 i_camera->setFOV(m_fov);
425 i_camera->setProjectionMatrix(m);
431 ISceneNode *parent = i_node->getParent();
435 i_node->updateAbsolutePosition();
441 matrix4 mat =
m_node->getAbsoluteTransformation();
442 vector3df
pos = mat.getTranslation();
443 i_camera->setPosition(pos);
444 vector3df
view(pos.X-mat[8],pos.Y-mat[9],pos.Z-mat[10]);
445 vector3df up(-mat[4], -mat[5], -mat[6]);
446 i_camera->setTarget(view);
447 i_camera->setUpVector(up);
467 matrix4 mat =
m_node->getAbsoluteTransformation();
468 vector3df
pos = mat.getTranslation();
469 vector3df rpy = mat.getRotationDegrees();
471 o_T[ 0] =
R(0,0);o_T[ 4] =
R(0,1);o_T[ 8] =
R(0,2);o_T[12] = pos.X;
472 o_T[ 1] =
R(1,0);o_T[ 5] =
R(1,1);o_T[ 9] =
R(1,2);o_T[13] = -pos.Y;
473 o_T[ 2] =
R(2,0);o_T[ 6] =
R(2,1);o_T[10] =
R(2,2);o_T[14] = pos.Z;
474 o_T[ 3] = 0; o_T[ 7] = 0; o_T[11] = 0; ;o_T[15] = 1.0;
499 if (m_camera != m_defaultCamera) m_camera->updateCameraTransform(m_cnode);
500 m_device->getVideoDriver()->beginScene(
true,
true, SColor(255,100,101,140));
501 m_device->getSceneManager()->drawAll();
502 m_device->getVideoDriver()->endScene();
504 int fps = m_device->getVideoDriver()->getFPS();
505 int prims = m_device->getVideoDriver()->getPrimitiveCountDrawn();
507 swprintf(tmp, 1024, L
"Irrlicht (fps:%d) Triangles:%d", fps, prims);
508 m_device->setWindowCaption(tmp);
513 if (m_defaultCamera)
delete m_defaultCamera;
520 if (!m_scene) m_scene =
new GLscene;
527 createDevice( video::EDT_OPENGL, dimension2d<u32>(w, h), 32,
528 false,
false,
false, 0);
530 if (!m_device)
return false;
532 m_device->setWindowCaption(L
"Irrlicht");
534 ISceneManager* smgr = m_device->getSceneManager();
535 smgr->addLightSceneNode(0, vector3df(18,-12,6), SColorf(1.0, 1.0, 1.0), 30.0
f);
536 smgr->addLightSceneNode(0, vector3df(-18,12,6), SColorf(1.0, 1.0, 1.0), 30.0
f);
537 m_cnode = smgr->addCameraSceneNode();
539 m_cnode->setTarget(vector3df(0,0,0.7));
540 m_cnode->setUpVector(vector3df(0,0,1));
543 m_device->setEventReceiver(m_receiver);
544 m_defaultCamera =
new GLcamera(m_cnode);
545 setCamera(m_defaultCamera);
552 ISceneManager* smgr = m_device->getSceneManager();
553 return new GLbody(smgr->getRootSceneNode(), smgr, -1, i_binfo);
574 return m_device->getSceneManager();
579 return m_device->getVideoDriver();
double * getAbsTransform()
png_infop png_charp png_int_32 png_int_32 int * type
MyEventReceiver(ICameraSceneNode *i_camera, double i_radius=3.0)
void addBody(GLbody *i_body)
virtual const aabbox3d< f32 > & getBoundingBox() const
png_infop png_charpp name
void setCameraParameters(irr::scene::ICameraSceneNode *i_camera)
const std::string & name() const
OpenHRP::matrix33 Matrix33
irr::scene::ISceneNode * m_node
GLcamera * findCamera(const char *i_name)
Matrix33 rotFromRpy(const Vector3 &rpy)
static GLscene * getInstance()
void updateAbsoluteTransformation(ISceneNode *i_node)
HRP_UTIL_EXPORT void calcRodrigues(Matrix44 &out_R, const Vector3 &axis, double q)
GLcamera * findCamera(const char *i_name)
irr::scene::ISceneManager * getSceneManager()
void updateCameraTransform(irr::scene::ICameraSceneNode *i_camera)
GLcamera(int i_width, int i_height, double i_near, double i_far, double i_fovy, GLlink *i_link=NULL, int i_id=-1)
HRP_UTIL_EXPORT Vector3 rpyFromRot(const Matrix33 &m)
std::vector< GLcamera * > m_cameraInfos
void setPosture(const double *i_angles)
void setCamera(GLcamera *i_camera)
virtual bool OnEvent(const SEvent &event)
void addChild(GLlink *i_child)
irr::video::IVideoDriver * getVideoDriver()
ICameraSceneNode * m_camera
position2d< s32 > m_mouse
GLlink(ISceneNode *i_parent, ISceneManager *i_mgr, s32 i_id, const LinkInfo &i_li, BodyInfo_var i_binfo)