3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "main.h" // for g_settings
29 Camera::Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control):
38 m_draw_control(draw_control),
39 m_viewing_range_min(5.0),
40 m_viewing_range_max(5.0),
42 m_camera_position(0,0,0),
43 m_camera_direction(0,0,0),
49 m_wanted_frametime(0.0),
54 m_frametime_counter(0),
55 m_time_per_range(30. / 50), // a sane default of 30ms per 50 nodes of range
57 m_view_bobbing_anim(0),
58 m_view_bobbing_state(0),
59 m_view_bobbing_speed(0),
64 //dstream<<__FUNCTION_NAME<<std::endl;
66 // note: making the camera node a child of the player node
67 // would lead to unexpected behaviour, so we don't do that.
68 m_playernode = smgr->addEmptySceneNode(smgr->getRootSceneNode());
69 m_headnode = smgr->addEmptySceneNode(m_playernode);
70 m_cameranode = smgr->addCameraSceneNode(smgr->getRootSceneNode());
71 m_cameranode->bindTargetAndRotation(true);
73 // This needs to be in its own scene manager. It is drawn after
74 // all other 3D scene nodes and before the GUI.
75 m_wieldmgr = smgr->createNewSceneManager();
76 m_wieldmgr->addCameraSceneNode();
77 m_wieldnode = new ExtrudedSpriteSceneNode(m_wieldmgr->getRootSceneNode(), m_wieldmgr);
88 bool Camera::successfullyCreated(std::wstring& error_message)
90 if (m_playernode == NULL)
92 error_message = L"Failed to create the player scene node";
95 if (m_headnode == NULL)
97 error_message = L"Failed to create the head scene node";
100 if (m_cameranode == NULL)
102 error_message = L"Failed to create the camera scene node";
105 if (m_wieldmgr == NULL)
107 error_message = L"Failed to create the wielded item scene manager";
110 if (m_wieldnode == NULL)
112 error_message = L"Failed to create the wielded item scene node";
118 // Returns the fractional part of x
119 inline f32 my_modf(f32 x)
122 return modf(x, &dummy);
125 void Camera::step(f32 dtime)
127 if (m_view_bobbing_state != 0)
129 f32 offset = dtime * m_view_bobbing_speed * 0.035;
130 if (m_view_bobbing_state == 2)
132 // Animation is getting turned off
133 if (m_view_bobbing_anim < 0.5)
134 m_view_bobbing_anim -= offset;
136 m_view_bobbing_anim += offset;
137 if (m_view_bobbing_anim <= 0 || m_view_bobbing_anim >= 1)
139 m_view_bobbing_anim = 0;
140 m_view_bobbing_state = 0;
145 m_view_bobbing_anim = my_modf(m_view_bobbing_anim + offset);
149 if (m_digging_button != -1)
151 f32 offset = dtime * 3.5;
152 m_digging_anim += offset;
153 if (m_digging_anim >= 1)
156 m_digging_button = -1;
161 void Camera::update(LocalPlayer* player, f32 frametime, v2u32 screensize)
163 // Set player node transformation
164 m_playernode->setPosition(player->getPosition());
165 m_playernode->setRotation(v3f(0, -1 * player->getYaw(), 0));
166 m_playernode->updateAbsolutePosition();
168 // Set head node transformation
169 m_headnode->setPosition(player->getEyeOffset());
170 m_headnode->setRotation(v3f(player->getPitch(), 0, 0));
171 m_headnode->updateAbsolutePosition();
173 // Compute relative camera position and target
174 v3f rel_cam_pos = v3f(0,0,0);
175 v3f rel_cam_target = v3f(0,0,1);
176 v3f rel_cam_up = v3f(0,1,0);
178 if (m_view_bobbing_anim != 0)
180 f32 bobfrac = my_modf(m_view_bobbing_anim * 2);
181 f32 bobdir = (m_view_bobbing_anim < 0.5) ? 1.0 : -1.0;
185 f32 bobtmp = sin(pow(bobfrac, bobknob) * PI);
188 bobdir * sin(bobfrac * PI),
189 0.8 * bobtmp * bobtmp,
192 rel_cam_pos += 0.03 * bobvec;
193 rel_cam_target += 0.045 * bobvec;
194 rel_cam_up.rotateXYBy(0.03 * bobdir * bobtmp * PI);
196 f32 angle_deg = 1 * bobdir * sin(bobfrac * PI);
197 f32 angle_rad = angle_deg * PI / 180;
201 r * (cos(angle_rad) - 1),
204 //rel_cam_target += off;
205 rel_cam_up.rotateXYBy(angle_deg);
210 // Compute absolute camera position and target
211 m_headnode->getAbsoluteTransformation().transformVect(m_camera_position, rel_cam_pos);
212 m_headnode->getAbsoluteTransformation().rotateVect(m_camera_direction, rel_cam_target - rel_cam_pos);
215 m_headnode->getAbsoluteTransformation().rotateVect(abs_cam_up, rel_cam_up);
217 // Set camera node transformation
218 m_cameranode->setPosition(m_camera_position);
219 m_cameranode->setUpVector(abs_cam_up);
220 // *100.0 helps in large map coordinates
221 m_cameranode->setTarget(m_camera_position + 100 * m_camera_direction);
223 // FOV and and aspect ratio
224 m_aspect = (f32)screensize.X / (f32) screensize.Y;
225 m_fov_x = 2 * atan(0.5 * m_aspect * tan(m_fov_y));
226 m_cameranode->setAspectRatio(m_aspect);
227 m_cameranode->setFOV(m_fov_y);
228 // Just so big a value that everything rendered is visible
229 // Some more allowance that m_viewing_range_max * BS because of active objects etc.
230 m_cameranode->setFarValue(m_viewing_range_max * BS * 10);
232 // Position the wielded item
233 v3f wield_position = v3f(45, -35, 65);
234 v3f wield_rotation = v3f(90, -90, -90);
235 if (m_digging_button != -1)
237 f32 digfrac = m_digging_anim;
238 wield_position.X -= 30 * sin(pow(digfrac, 0.8f) * PI);
239 wield_position.Y += 15 * sin(digfrac * 2 * PI);
240 wield_position.Z += 5 * digfrac;
242 // Euler angles are PURE EVIL, so why not use quaternions?
243 core::quaternion quat_begin(wield_rotation * core::DEGTORAD);
244 core::quaternion quat_end(v3f(90, 20, -130) * core::DEGTORAD);
245 core::quaternion quat_slerp;
246 quat_slerp.slerp(quat_begin, quat_end, sin(digfrac * PI));
247 quat_slerp.toEuler(wield_rotation);
248 wield_rotation *= core::RADTODEG;
250 m_wieldnode->setPosition(wield_position);
251 m_wieldnode->setRotation(wield_rotation);
253 // Render distance feedback loop
254 updateViewingRange(frametime);
256 // If the player seems to be walking on solid ground,
257 // view bobbing is enabled and free_move is off,
258 // start (or continue) the view bobbing animation.
259 v3f speed = player->getSpeed();
260 if ((hypot(speed.X, speed.Z) > BS) &&
261 (player->touching_ground) &&
262 (g_settings.getBool("view_bobbing") == true) &&
263 (g_settings.getBool("free_move") == false))
266 m_view_bobbing_state = 1;
267 m_view_bobbing_speed = MYMIN(speed.getLength(), 40);
269 else if (m_view_bobbing_state == 1)
272 m_view_bobbing_state = 2;
273 m_view_bobbing_speed = 60;
277 void Camera::updateViewingRange(f32 frametime_in)
279 if (m_draw_control.range_all)
282 m_added_frametime += frametime_in;
285 // Actually this counter kind of sucks because frametime is busytime
286 m_frametime_counter -= frametime_in;
287 if (m_frametime_counter > 0)
289 m_frametime_counter = 0.2;
291 /*dstream<<__FUNCTION_NAME
292 <<": Collected "<<m_added_frames<<" frames, total of "
293 <<m_added_frametime<<"s."<<std::endl;
295 dstream<<"m_draw_control.blocks_drawn="
296 <<m_draw_control.blocks_drawn
297 <<", m_draw_control.blocks_would_have_drawn="
298 <<m_draw_control.blocks_would_have_drawn
301 m_draw_control.wanted_min_range = m_viewing_range_min;
302 m_draw_control.wanted_max_blocks = (1.5*m_draw_control.blocks_would_have_drawn)+1;
303 if (m_draw_control.wanted_max_blocks < 10)
304 m_draw_control.wanted_max_blocks = 10;
306 f32 block_draw_ratio = 1.0;
307 if (m_draw_control.blocks_would_have_drawn != 0)
309 block_draw_ratio = (f32)m_draw_control.blocks_drawn
310 / (f32)m_draw_control.blocks_would_have_drawn;
313 // Calculate the average frametime in the case that all wanted
314 // blocks had been drawn
315 f32 frametime = m_added_frametime / m_added_frames / block_draw_ratio;
317 m_added_frametime = 0.0;
320 f32 wanted_frametime_change = m_wanted_frametime - frametime;
321 //dstream<<"wanted_frametime_change="<<wanted_frametime_change<<std::endl;
323 // If needed frametime change is small, just return
324 if (fabs(wanted_frametime_change) < m_wanted_frametime*0.4)
326 //dstream<<"ignoring small wanted_frametime_change"<<std::endl;
330 f32 range = m_draw_control.wanted_range;
331 f32 new_range = range;
333 f32 d_range = range - m_range_old;
334 f32 d_frametime = frametime - m_frametime_old;
337 m_time_per_range = d_frametime / d_range;
340 // The minimum allowed calculated frametime-range derivative:
341 // Practically this sets the maximum speed of changing the range.
342 // The lower this value, the higher the maximum changing speed.
343 // A low value here results in wobbly range (0.001)
344 // A high value here results in slow changing range (0.0025)
345 // SUGG: This could be dynamically adjusted so that when
346 // the camera is turning, this is lower
347 //f32 min_time_per_range = 0.0015;
348 f32 min_time_per_range = 0.0010;
349 //f32 min_time_per_range = 0.05 / range;
350 if(m_time_per_range < min_time_per_range)
352 m_time_per_range = min_time_per_range;
353 //dstream<<"m_time_per_range="<<m_time_per_range<<" (min)"<<std::endl;
357 //dstream<<"m_time_per_range="<<m_time_per_range<<std::endl;
360 f32 wanted_range_change = wanted_frametime_change / m_time_per_range;
361 // Dampen the change a bit to kill oscillations
362 //wanted_range_change *= 0.9;
363 //wanted_range_change *= 0.75;
364 wanted_range_change *= 0.5;
365 //dstream<<"wanted_range_change="<<wanted_range_change<<std::endl;
367 // If needed range change is very small, just return
368 if(fabs(wanted_range_change) < 0.001)
370 //dstream<<"ignoring small wanted_range_change"<<std::endl;
374 new_range += wanted_range_change;
376 //f32 new_range_unclamped = new_range;
377 new_range = MYMAX(new_range, m_viewing_range_min);
378 new_range = MYMIN(new_range, m_viewing_range_max);
379 /*dstream<<"new_range="<<new_range_unclamped
380 <<", clamped to "<<new_range<<std::endl;*/
382 m_draw_control.wanted_range = new_range;
384 m_range_old = new_range;
385 m_frametime_old = frametime;
388 void Camera::updateSettings()
390 m_viewing_range_min = g_settings.getS16("viewing_range_nodes_min");
391 m_viewing_range_min = MYMAX(5.0, m_viewing_range_min);
393 m_viewing_range_max = g_settings.getS16("viewing_range_nodes_max");
394 m_viewing_range_max = MYMAX(m_viewing_range_min, m_viewing_range_max);
396 f32 fov_degrees = g_settings.getFloat("fov");
397 fov_degrees = MYMAX(fov_degrees, 10.0);
398 fov_degrees = MYMIN(fov_degrees, 170.0);
399 m_fov_y = fov_degrees * PI / 180.0;
401 f32 wanted_fps = g_settings.getFloat("wanted_fps");
402 wanted_fps = MYMAX(wanted_fps, 1.0);
403 m_wanted_frametime = 1.0 / wanted_fps;
406 void Camera::wield(const InventoryItem* item)
412 // Try to make a MaterialItem cube.
413 if (std::string(item->getName()) == "MaterialItem")
415 // A block-type material
416 MaterialItem* mat_item = (MaterialItem*) item;
417 content_t content = mat_item->getMaterial();
418 if (content_features(content).solidness || content_features(content).visual_solidness)
420 m_wieldnode->setCube(content_features(content).tiles);
421 m_wieldnode->setScale(v3f(30));
426 // If that failed, make an extruded sprite.
429 m_wieldnode->setSprite(item->getImageRaw());
430 m_wieldnode->setScale(v3f(40));
433 m_wieldnode->setVisible(true);
438 m_wieldnode->setVisible(false);
442 void Camera::setDigging(s32 button)
444 if (m_digging_button == -1)
445 m_digging_button = button;
448 void Camera::drawWieldedTool()
450 m_wieldmgr->getVideoDriver()->clearZBuffer();
452 scene::ICameraSceneNode* cam = m_wieldmgr->getActiveCamera();
453 cam->setAspectRatio(m_cameranode->getAspectRatio());
454 cam->setFOV(m_cameranode->getFOV());
455 cam->setNearValue(0.1);
456 cam->setFarValue(100);
457 m_wieldmgr->drawAll();
461 ExtrudedSpriteSceneNode::ExtrudedSpriteSceneNode(
462 scene::ISceneNode* parent,
463 scene::ISceneManager* mgr,
469 ISceneNode(parent, mgr, id, position, rotation, scale)
471 m_meshnode = mgr->addMeshSceneNode(NULL, this, -1, v3f(0,0,0), v3f(0,0,0), v3f(1,1,1), true);
477 ExtrudedSpriteSceneNode::~ExtrudedSpriteSceneNode()
479 removeChild(m_meshnode);
484 void ExtrudedSpriteSceneNode::setSprite(video::ITexture* texture)
488 m_meshnode->setVisible(false);
492 io::path name = getExtrudedName(texture);
493 scene::IMeshCache* cache = SceneManager->getMeshCache();
494 scene::IAnimatedMesh* mesh = cache->getMeshByName(name);
497 // Extruded texture has been found in cache.
498 m_meshnode->setMesh(mesh);
502 // Texture was not yet extruded, do it now and save in cache
503 mesh = extrude(texture);
506 dstream << "Warning: failed to extrude sprite" << std::endl;
507 m_meshnode->setVisible(false);
510 cache->addMesh(name, mesh);
511 m_meshnode->setMesh(mesh);
515 m_meshnode->setScale(v3f(1, 1, m_thickness));
516 m_meshnode->getMaterial(0).setTexture(0, texture);
517 m_meshnode->getMaterial(0).setFlag(video::EMF_LIGHTING, false);
518 m_meshnode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, false);
519 m_meshnode->getMaterial(0).MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
520 m_meshnode->setVisible(true);
524 void ExtrudedSpriteSceneNode::setCube(const TileSpec tiles[6])
526 if (m_cubemesh == NULL)
527 m_cubemesh = createCubeMesh();
529 m_meshnode->setMesh(m_cubemesh);
530 m_meshnode->setScale(v3f(1));
531 for (int i = 0; i < 6; ++i)
533 // Get the tile texture and atlas transformation
534 video::ITexture* atlas = tiles[i].texture.atlas;
535 v2f pos = tiles[i].texture.pos;
536 v2f size = tiles[i].texture.size;
538 // Set material flags and texture
539 video::SMaterial& material = m_meshnode->getMaterial(i);
540 material.setFlag(video::EMF_LIGHTING, false);
541 material.setFlag(video::EMF_BILINEAR_FILTER, false);
542 tiles[i].applyMaterialOptions(material);
543 material.setTexture(0, atlas);
544 material.getTextureMatrix(0).setTextureTranslate(pos.X, pos.Y);
545 material.getTextureMatrix(0).setTextureScale(size.X, size.Y);
547 m_meshnode->setVisible(true);
551 void ExtrudedSpriteSceneNode::removeSpriteFromCache(video::ITexture* texture)
553 scene::IMeshCache* cache = SceneManager->getMeshCache();
554 scene::IAnimatedMesh* mesh = cache->getMeshByName(getExtrudedName(texture));
556 cache->removeMesh(mesh);
559 void ExtrudedSpriteSceneNode::setSpriteThickness(f32 thickness)
561 m_thickness = thickness;
563 m_meshnode->setScale(v3f(1, 1, thickness));
566 const core::aabbox3d<f32>& ExtrudedSpriteSceneNode::getBoundingBox() const
568 return m_meshnode->getBoundingBox();
571 void ExtrudedSpriteSceneNode::OnRegisterSceneNode()
574 SceneManager->registerNodeForRendering(this);
575 ISceneNode::OnRegisterSceneNode();
578 void ExtrudedSpriteSceneNode::render()
583 io::path ExtrudedSpriteSceneNode::getExtrudedName(video::ITexture* texture)
585 io::path path = texture->getName();
586 path.append("/[extruded]");
590 scene::IAnimatedMesh* ExtrudedSpriteSceneNode::extrudeARGB(u32 width, u32 height, u8* data)
592 const s32 argb_wstep = 4 * width;
593 const s32 alpha_threshold = 1;
595 scene::IMeshBuffer* buf = new scene::SMeshBuffer();
596 video::SColor c(255,255,255,255);
600 video::S3DVertex vertices[8] =
602 video::S3DVertex(-0.5,-0.5,-0.5, 0,0,-1, c, 0,1),
603 video::S3DVertex(-0.5,+0.5,-0.5, 0,0,-1, c, 0,0),
604 video::S3DVertex(+0.5,+0.5,-0.5, 0,0,-1, c, 1,0),
605 video::S3DVertex(+0.5,-0.5,-0.5, 0,0,-1, c, 1,1),
606 video::S3DVertex(+0.5,-0.5,+0.5, 0,0,+1, c, 1,1),
607 video::S3DVertex(+0.5,+0.5,+0.5, 0,0,+1, c, 1,0),
608 video::S3DVertex(-0.5,+0.5,+0.5, 0,0,+1, c, 0,0),
609 video::S3DVertex(-0.5,-0.5,+0.5, 0,0,+1, c, 0,1),
611 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
612 buf->append(vertices, 8, indices, 12);
616 // (add faces where a solid pixel is next to a transparent one)
617 u8* solidity = new u8[(width+2) * (height+2)];
618 u32 wstep = width + 2;
619 for (u32 y = 0; y < height + 2; ++y)
621 u8* scanline = solidity + y * wstep;
622 if (y == 0 || y == height + 1)
624 for (u32 x = 0; x < width + 2; ++x)
630 u8* argb_scanline = data + (y - 1) * argb_wstep;
631 for (u32 x = 0; x < width; ++x)
632 scanline[x+1] = (argb_scanline[x*4+3] >= alpha_threshold);
633 scanline[width + 1] = 0;
637 // without this, there would be occasional "holes" in the mesh
640 for (u32 y = 0; y <= height; ++y)
642 u8* scanline = solidity + y * wstep + 1;
643 for (u32 x = 0; x <= width; ++x)
645 if (scanline[x] && !scanline[x + wstep])
648 while (scanline[xx] && !scanline[xx + wstep])
650 f32 vx1 = (x - eps) / (f32) width - 0.5;
651 f32 vx2 = (xx + eps) / (f32) width - 0.5;
652 f32 vy = 0.5 - (y - eps) / (f32) height;
653 f32 tx1 = x / (f32) width;
654 f32 tx2 = xx / (f32) width;
655 f32 ty = (y - 0.5) / (f32) height;
656 video::S3DVertex vertices[8] =
658 video::S3DVertex(vx1,vy,-0.5, 0,-1,0, c, tx1,ty),
659 video::S3DVertex(vx2,vy,-0.5, 0,-1,0, c, tx2,ty),
660 video::S3DVertex(vx2,vy,+0.5, 0,-1,0, c, tx2,ty),
661 video::S3DVertex(vx1,vy,+0.5, 0,-1,0, c, tx1,ty),
663 u16 indices[6] = {0,1,2,2,3,0};
664 buf->append(vertices, 4, indices, 6);
667 if (!scanline[x] && scanline[x + wstep])
670 while (!scanline[xx] && scanline[xx + wstep])
672 f32 vx1 = (x - eps) / (f32) width - 0.5;
673 f32 vx2 = (xx + eps) / (f32) width - 0.5;
674 f32 vy = 0.5 - (y + eps) / (f32) height;
675 f32 tx1 = x / (f32) width;
676 f32 tx2 = xx / (f32) width;
677 f32 ty = (y + 0.5) / (f32) height;
678 video::S3DVertex vertices[8] =
680 video::S3DVertex(vx1,vy,-0.5, 0,1,0, c, tx1,ty),
681 video::S3DVertex(vx1,vy,+0.5, 0,1,0, c, tx1,ty),
682 video::S3DVertex(vx2,vy,+0.5, 0,1,0, c, tx2,ty),
683 video::S3DVertex(vx2,vy,-0.5, 0,1,0, c, tx2,ty),
685 u16 indices[6] = {0,1,2,2,3,0};
686 buf->append(vertices, 4, indices, 6);
692 for (u32 x = 0; x <= width; ++x)
694 u8* scancol = solidity + x + wstep;
695 for (u32 y = 0; y <= height; ++y)
697 if (scancol[y * wstep] && !scancol[y * wstep + 1])
700 while (scancol[yy * wstep] && !scancol[yy * wstep + 1])
702 f32 vx = (x - eps) / (f32) width - 0.5;
703 f32 vy1 = 0.5 - (y - eps) / (f32) height;
704 f32 vy2 = 0.5 - (yy + eps) / (f32) height;
705 f32 tx = (x - 0.5) / (f32) width;
706 f32 ty1 = y / (f32) height;
707 f32 ty2 = yy / (f32) height;
708 video::S3DVertex vertices[8] =
710 video::S3DVertex(vx,vy1,-0.5, 1,0,0, c, tx,ty1),
711 video::S3DVertex(vx,vy1,+0.5, 1,0,0, c, tx,ty1),
712 video::S3DVertex(vx,vy2,+0.5, 1,0,0, c, tx,ty2),
713 video::S3DVertex(vx,vy2,-0.5, 1,0,0, c, tx,ty2),
715 u16 indices[6] = {0,1,2,2,3,0};
716 buf->append(vertices, 4, indices, 6);
719 if (!scancol[y * wstep] && scancol[y * wstep + 1])
722 while (!scancol[yy * wstep] && scancol[yy * wstep + 1])
724 f32 vx = (x + eps) / (f32) width - 0.5;
725 f32 vy1 = 0.5 - (y - eps) / (f32) height;
726 f32 vy2 = 0.5 - (yy + eps) / (f32) height;
727 f32 tx = (x + 0.5) / (f32) width;
728 f32 ty1 = y / (f32) height;
729 f32 ty2 = yy / (f32) height;
730 video::S3DVertex vertices[8] =
732 video::S3DVertex(vx,vy1,-0.5, -1,0,0, c, tx,ty1),
733 video::S3DVertex(vx,vy2,-0.5, -1,0,0, c, tx,ty2),
734 video::S3DVertex(vx,vy2,+0.5, -1,0,0, c, tx,ty2),
735 video::S3DVertex(vx,vy1,+0.5, -1,0,0, c, tx,ty1),
737 u16 indices[6] = {0,1,2,2,3,0};
738 buf->append(vertices, 4, indices, 6);
745 scene::SMesh* mesh = new scene::SMesh();
746 buf->recalculateBoundingBox();
747 mesh->addMeshBuffer(buf);
749 mesh->recalculateBoundingBox();
750 scene::SAnimatedMesh* anim_mesh = new scene::SAnimatedMesh(mesh);
755 scene::IAnimatedMesh* ExtrudedSpriteSceneNode::extrude(video::ITexture* texture)
757 scene::IAnimatedMesh* mesh = NULL;
758 core::dimension2d<u32> size = texture->getSize();
759 video::ECOLOR_FORMAT format = texture->getColorFormat();
760 if (format == video::ECF_A8R8G8B8)
762 // Texture is in the correct color format, we can pass it
763 // to extrudeARGB right away.
764 void* data = texture->lock(true);
767 mesh = extrudeARGB(size.Width, size.Height, (u8*) data);
772 video::IVideoDriver* driver = SceneManager->getVideoDriver();
774 video::IImage* img1 = driver->createImageFromData(format, size, texture->lock(true));
778 // img1 is in the texture's color format, convert to 8-bit ARGB
779 video::IImage* img2 = driver->createImage(video::ECF_A8R8G8B8, size);
785 mesh = extrudeARGB(size.Width, size.Height, (u8*) img2->lock());
794 scene::IMesh* ExtrudedSpriteSceneNode::createCubeMesh()
796 video::SColor c(255,255,255,255);
797 video::S3DVertex vertices[24] =
800 video::S3DVertex(-0.5,+0.5,-0.5, 0,1,0, c, 0,1),
801 video::S3DVertex(-0.5,+0.5,+0.5, 0,1,0, c, 0,0),
802 video::S3DVertex(+0.5,+0.5,+0.5, 0,1,0, c, 1,0),
803 video::S3DVertex(+0.5,+0.5,-0.5, 0,1,0, c, 1,1),
805 video::S3DVertex(-0.5,-0.5,-0.5, 0,-1,0, c, 0,0),
806 video::S3DVertex(+0.5,-0.5,-0.5, 0,-1,0, c, 1,0),
807 video::S3DVertex(+0.5,-0.5,+0.5, 0,-1,0, c, 1,1),
808 video::S3DVertex(-0.5,-0.5,+0.5, 0,-1,0, c, 0,1),
810 video::S3DVertex(+0.5,-0.5,-0.5, 1,0,0, c, 0,1),
811 video::S3DVertex(+0.5,+0.5,-0.5, 1,0,0, c, 0,0),
812 video::S3DVertex(+0.5,+0.5,+0.5, 1,0,0, c, 1,0),
813 video::S3DVertex(+0.5,-0.5,+0.5, 1,0,0, c, 1,1),
815 video::S3DVertex(-0.5,-0.5,-0.5, -1,0,0, c, 1,1),
816 video::S3DVertex(-0.5,-0.5,+0.5, -1,0,0, c, 0,1),
817 video::S3DVertex(-0.5,+0.5,+0.5, -1,0,0, c, 0,0),
818 video::S3DVertex(-0.5,+0.5,-0.5, -1,0,0, c, 1,0),
820 video::S3DVertex(-0.5,-0.5,+0.5, 0,0,1, c, 1,1),
821 video::S3DVertex(+0.5,-0.5,+0.5, 0,0,1, c, 0,1),
822 video::S3DVertex(+0.5,+0.5,+0.5, 0,0,1, c, 0,0),
823 video::S3DVertex(-0.5,+0.5,+0.5, 0,0,1, c, 1,0),
825 video::S3DVertex(-0.5,-0.5,-0.5, 0,0,-1, c, 0,1),
826 video::S3DVertex(-0.5,+0.5,-0.5, 0,0,-1, c, 0,0),
827 video::S3DVertex(+0.5,+0.5,-0.5, 0,0,-1, c, 1,0),
828 video::S3DVertex(+0.5,-0.5,-0.5, 0,0,-1, c, 1,1),
831 u16 indices[6] = {0,1,2,2,3,0};
833 scene::SMesh* mesh = new scene::SMesh();
834 for (u32 i=0; i<6; ++i)
836 scene::IMeshBuffer* buf = new scene::SMeshBuffer();
837 buf->append(vertices + 4 * i, 4, indices, 6);
838 buf->recalculateBoundingBox();
839 mesh->addMeshBuffer(buf);
842 mesh->recalculateBoundingBox();