Simplistic wielded tool lighting, added setMeshVerticesColor to utility.h and refacto...
[oweals/minetest.git] / src / camera.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "camera.h"
21 #include "debug.h"
22 #include "client.h"
23 #include "main.h" // for g_settings
24 #include "map.h"
25 #include "player.h"
26 #include "tile.h"
27 #include <cmath>
28
29 Camera::Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control):
30         m_smgr(smgr),
31         m_playernode(NULL),
32         m_headnode(NULL),
33         m_cameranode(NULL),
34
35         m_wieldmgr(NULL),
36         m_wieldnode(NULL),
37
38         m_draw_control(draw_control),
39         m_viewing_range_min(5.0),
40         m_viewing_range_max(5.0),
41
42         m_camera_position(0,0,0),
43         m_camera_direction(0,0,0),
44
45         m_aspect(1.0),
46         m_fov_x(1.0),
47         m_fov_y(1.0),
48
49         m_wanted_frametime(0.0),
50         m_added_frametime(0),
51         m_added_frames(0),
52         m_range_old(0),
53         m_frametime_old(0),
54         m_frametime_counter(0),
55         m_time_per_range(30. / 50), // a sane default of 30ms per 50 nodes of range
56
57         m_view_bobbing_anim(0),
58         m_view_bobbing_state(0),
59         m_view_bobbing_speed(0),
60
61         m_digging_anim(0),
62         m_digging_button(-1)
63 {
64         //dstream<<__FUNCTION_NAME<<std::endl;
65
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);
72
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);
78
79         updateSettings();
80 }
81
82 Camera::~Camera()
83 {
84         m_wieldmgr->drop();
85         m_wieldnode->drop();
86 }
87
88 bool Camera::successfullyCreated(std::wstring& error_message)
89 {
90         if (m_playernode == NULL)
91         {
92                 error_message = L"Failed to create the player scene node";
93                 return false;
94         }
95         if (m_headnode == NULL)
96         {
97                 error_message = L"Failed to create the head scene node";
98                 return false;
99         }
100         if (m_cameranode == NULL)
101         {
102                 error_message = L"Failed to create the camera scene node";
103                 return false;
104         }
105         if (m_wieldmgr == NULL)
106         {
107                 error_message = L"Failed to create the wielded item scene manager";
108                 return false;
109         }
110         if (m_wieldnode == NULL)
111         {
112                 error_message = L"Failed to create the wielded item scene node";
113                 return false;
114         }
115         return true;
116 }
117
118 // Returns the fractional part of x
119 inline f32 my_modf(f32 x)
120 {
121         double dummy;
122         return modf(x, &dummy);
123 }
124
125 void Camera::step(f32 dtime)
126 {
127         if (m_view_bobbing_state != 0)
128         {
129                 f32 offset = dtime * m_view_bobbing_speed * 0.035;
130                 if (m_view_bobbing_state == 2)
131                 {
132                         // Animation is getting turned off
133                         if (m_view_bobbing_anim < 0.5)
134                                 m_view_bobbing_anim -= offset;
135                         else
136                                 m_view_bobbing_anim += offset;
137                         if (m_view_bobbing_anim <= 0 || m_view_bobbing_anim >= 1)
138                         {
139                                 m_view_bobbing_anim = 0;
140                                 m_view_bobbing_state = 0;
141                         }
142                 }
143                 else
144                 {
145                         m_view_bobbing_anim = my_modf(m_view_bobbing_anim + offset);
146                 }
147         }
148
149         if (m_digging_button != -1)
150         {
151                 f32 offset = dtime * 3.5;
152                 m_digging_anim += offset;
153                 if (m_digging_anim >= 1)
154                 {
155                         m_digging_anim = 0;
156                         m_digging_button = -1;
157                 }
158         }
159 }
160
161 void Camera::update(LocalPlayer* player, f32 frametime, v2u32 screensize)
162 {
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();
167
168         // Set head node transformation
169         m_headnode->setPosition(player->getEyeOffset());
170         m_headnode->setRotation(v3f(player->getPitch(), 0, 0));
171         m_headnode->updateAbsolutePosition();
172
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);
177
178         if (m_view_bobbing_anim != 0)
179         {
180                 f32 bobfrac = my_modf(m_view_bobbing_anim * 2);
181                 f32 bobdir = (m_view_bobbing_anim < 0.5) ? 1.0 : -1.0;
182
183                 #if 1
184                 f32 bobknob = 1.2;
185                 f32 bobtmp = sin(pow(bobfrac, bobknob) * PI);
186
187                 v3f bobvec = v3f(
188                         bobdir * sin(bobfrac * PI),
189                         0.8 * bobtmp * bobtmp,
190                         0.);
191
192                 rel_cam_pos += 0.02 * bobvec;
193                 rel_cam_target += 0.03 * bobvec;
194                 rel_cam_up.rotateXYBy(0.02 * bobdir * bobtmp * PI);
195                 #else
196                 f32 angle_deg = 1 * bobdir * sin(bobfrac * PI);
197                 f32 angle_rad = angle_deg * PI / 180;
198                 f32 r = 0.05;
199                 v3f off = v3f(
200                         r * sin(angle_rad),
201                         r * (cos(angle_rad) - 1),
202                         0);
203                 rel_cam_pos += off;
204                 //rel_cam_target += off;
205                 rel_cam_up.rotateXYBy(angle_deg);
206                 #endif
207
208         }
209
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);
213
214         v3f abs_cam_up;
215         m_headnode->getAbsoluteTransformation().rotateVect(abs_cam_up, rel_cam_up);
216
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);
222
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);
231
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)
236         {
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;
241
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;
249         }
250         m_wieldnode->setPosition(wield_position);
251         m_wieldnode->setRotation(wield_rotation);
252         m_wieldnode->updateLight(player->light);
253
254         // Render distance feedback loop
255         updateViewingRange(frametime);
256
257         // If the player seems to be walking on solid ground,
258         // view bobbing is enabled and free_move is off,
259         // start (or continue) the view bobbing animation.
260         v3f speed = player->getSpeed();
261         if ((hypot(speed.X, speed.Z) > BS) &&
262                 (player->touching_ground) &&
263                 (g_settings.getBool("view_bobbing") == true) &&
264                 (g_settings.getBool("free_move") == false))
265         {
266                 // Start animation
267                 m_view_bobbing_state = 1;
268                 m_view_bobbing_speed = MYMIN(speed.getLength(), 40);
269         }
270         else if (m_view_bobbing_state == 1)
271         {
272                 // Stop animation
273                 m_view_bobbing_state = 2;
274                 m_view_bobbing_speed = 60;
275         }
276 }
277
278 void Camera::updateViewingRange(f32 frametime_in)
279 {
280         if (m_draw_control.range_all)
281                 return;
282
283         m_added_frametime += frametime_in;
284         m_added_frames += 1;
285
286         // Actually this counter kind of sucks because frametime is busytime
287         m_frametime_counter -= frametime_in;
288         if (m_frametime_counter > 0)
289                 return;
290         m_frametime_counter = 0.2;
291
292         /*dstream<<__FUNCTION_NAME
293                         <<": Collected "<<m_added_frames<<" frames, total of "
294                         <<m_added_frametime<<"s."<<std::endl;
295
296         dstream<<"m_draw_control.blocks_drawn="
297                         <<m_draw_control.blocks_drawn
298                         <<", m_draw_control.blocks_would_have_drawn="
299                         <<m_draw_control.blocks_would_have_drawn
300                         <<std::endl;*/
301
302         m_draw_control.wanted_min_range = m_viewing_range_min;
303         m_draw_control.wanted_max_blocks = (1.5*m_draw_control.blocks_would_have_drawn)+1;
304         if (m_draw_control.wanted_max_blocks < 10)
305                 m_draw_control.wanted_max_blocks = 10;
306
307         f32 block_draw_ratio = 1.0;
308         if (m_draw_control.blocks_would_have_drawn != 0)
309         {
310                 block_draw_ratio = (f32)m_draw_control.blocks_drawn
311                         / (f32)m_draw_control.blocks_would_have_drawn;
312         }
313
314         // Calculate the average frametime in the case that all wanted
315         // blocks had been drawn
316         f32 frametime = m_added_frametime / m_added_frames / block_draw_ratio;
317
318         m_added_frametime = 0.0;
319         m_added_frames = 0;
320
321         f32 wanted_frametime_change = m_wanted_frametime - frametime;
322         //dstream<<"wanted_frametime_change="<<wanted_frametime_change<<std::endl;
323
324         // If needed frametime change is small, just return
325         if (fabs(wanted_frametime_change) < m_wanted_frametime*0.4)
326         {
327                 //dstream<<"ignoring small wanted_frametime_change"<<std::endl;
328                 return;
329         }
330
331         f32 range = m_draw_control.wanted_range;
332         f32 new_range = range;
333
334         f32 d_range = range - m_range_old;
335         f32 d_frametime = frametime - m_frametime_old;
336         if (d_range != 0)
337         {
338                 m_time_per_range = d_frametime / d_range;
339         }
340
341         // The minimum allowed calculated frametime-range derivative:
342         // Practically this sets the maximum speed of changing the range.
343         // The lower this value, the higher the maximum changing speed.
344         // A low value here results in wobbly range (0.001)
345         // A high value here results in slow changing range (0.0025)
346         // SUGG: This could be dynamically adjusted so that when
347         //       the camera is turning, this is lower
348         //f32 min_time_per_range = 0.0015;
349         f32 min_time_per_range = 0.0010;
350         //f32 min_time_per_range = 0.05 / range;
351         if(m_time_per_range < min_time_per_range)
352         {
353                 m_time_per_range = min_time_per_range;
354                 //dstream<<"m_time_per_range="<<m_time_per_range<<" (min)"<<std::endl;
355         }
356         else
357         {
358                 //dstream<<"m_time_per_range="<<m_time_per_range<<std::endl;
359         }
360
361         f32 wanted_range_change = wanted_frametime_change / m_time_per_range;
362         // Dampen the change a bit to kill oscillations
363         //wanted_range_change *= 0.9;
364         //wanted_range_change *= 0.75;
365         wanted_range_change *= 0.5;
366         //dstream<<"wanted_range_change="<<wanted_range_change<<std::endl;
367
368         // If needed range change is very small, just return
369         if(fabs(wanted_range_change) < 0.001)
370         {
371                 //dstream<<"ignoring small wanted_range_change"<<std::endl;
372                 return;
373         }
374
375         new_range += wanted_range_change;
376         
377         //f32 new_range_unclamped = new_range;
378         new_range = MYMAX(new_range, m_viewing_range_min);
379         new_range = MYMIN(new_range, m_viewing_range_max);
380         /*dstream<<"new_range="<<new_range_unclamped
381                         <<", clamped to "<<new_range<<std::endl;*/
382
383         m_draw_control.wanted_range = new_range;
384
385         m_range_old = new_range;
386         m_frametime_old = frametime;
387 }
388
389 void Camera::updateSettings()
390 {
391         m_viewing_range_min = g_settings.getS16("viewing_range_nodes_min");
392         m_viewing_range_min = MYMAX(5.0, m_viewing_range_min);
393
394         m_viewing_range_max = g_settings.getS16("viewing_range_nodes_max");
395         m_viewing_range_max = MYMAX(m_viewing_range_min, m_viewing_range_max);
396
397         f32 fov_degrees = g_settings.getFloat("fov");
398         fov_degrees = MYMAX(fov_degrees, 10.0);
399         fov_degrees = MYMIN(fov_degrees, 170.0);
400         m_fov_y = fov_degrees * PI / 180.0;
401
402         f32 wanted_fps = g_settings.getFloat("wanted_fps");
403         wanted_fps = MYMAX(wanted_fps, 1.0);
404         m_wanted_frametime = 1.0 / wanted_fps;
405 }
406
407 void Camera::wield(const InventoryItem* item)
408 {
409         if (item != NULL)
410         {
411                 bool isCube = false;
412
413                 // Try to make a MaterialItem cube.
414                 if (std::string(item->getName()) == "MaterialItem")
415                 {
416                         // A block-type material
417                         MaterialItem* mat_item = (MaterialItem*) item;
418                         content_t content = mat_item->getMaterial();
419                         if (content_features(content).solidness || content_features(content).visual_solidness)
420                         {
421                                 m_wieldnode->setCube(content_features(content).tiles);
422                                 m_wieldnode->setScale(v3f(30));
423                                 isCube = true;
424                         }
425                 }
426
427                 // If that failed, make an extruded sprite.
428                 if (!isCube)
429                 {
430                         m_wieldnode->setSprite(item->getImageRaw());
431                         m_wieldnode->setScale(v3f(40));
432                 }
433
434                 m_wieldnode->setVisible(true);
435         }
436         else
437         {
438                 // Bare hands
439                 m_wieldnode->setVisible(false);
440         }
441 }
442
443 void Camera::setDigging(s32 button)
444 {
445         if (m_digging_button == -1)
446                 m_digging_button = button;
447 }
448
449 void Camera::drawWieldedTool()
450 {
451         m_wieldmgr->getVideoDriver()->clearZBuffer();
452
453         scene::ICameraSceneNode* cam = m_wieldmgr->getActiveCamera();
454         cam->setAspectRatio(m_cameranode->getAspectRatio());
455         cam->setFOV(m_cameranode->getFOV());
456         cam->setNearValue(0.1);
457         cam->setFarValue(100);
458         m_wieldmgr->drawAll();
459 }
460
461
462 ExtrudedSpriteSceneNode::ExtrudedSpriteSceneNode(
463         scene::ISceneNode* parent,
464         scene::ISceneManager* mgr,
465         s32 id,
466         const v3f& position,
467         const v3f& rotation,
468         const v3f& scale
469 ):
470         ISceneNode(parent, mgr, id, position, rotation, scale)
471 {
472         m_meshnode = mgr->addMeshSceneNode(NULL, this, -1, v3f(0,0,0), v3f(0,0,0), v3f(1,1,1), true);
473         m_thickness = 0.1;
474         m_cubemesh = NULL;
475         m_is_cube = false;
476         m_light = LIGHT_MAX;
477 }
478
479 ExtrudedSpriteSceneNode::~ExtrudedSpriteSceneNode()
480 {
481         removeChild(m_meshnode);
482         if (m_cubemesh)
483                 m_cubemesh->drop();
484 }
485
486 void ExtrudedSpriteSceneNode::setSprite(video::ITexture* texture)
487 {
488         if (texture == NULL)
489         {
490                 m_meshnode->setVisible(false);
491                 return;
492         }
493
494         io::path name = getExtrudedName(texture);
495         scene::IMeshCache* cache = SceneManager->getMeshCache();
496         scene::IAnimatedMesh* mesh = cache->getMeshByName(name);
497         if (mesh != NULL)
498         {
499                 // Extruded texture has been found in cache.
500                 m_meshnode->setMesh(mesh);
501         }
502         else
503         {
504                 // Texture was not yet extruded, do it now and save in cache
505                 mesh = extrude(texture);
506                 if (mesh == NULL)
507                 {
508                         dstream << "Warning: failed to extrude sprite" << std::endl;
509                         m_meshnode->setVisible(false);
510                         return;
511                 }
512                 cache->addMesh(name, mesh);
513                 m_meshnode->setMesh(mesh);
514                 mesh->drop();
515         }
516
517         m_meshnode->setScale(v3f(1, 1, m_thickness));
518         m_meshnode->getMaterial(0).setTexture(0, texture);
519         m_meshnode->getMaterial(0).setFlag(video::EMF_LIGHTING, false);
520         m_meshnode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, false);
521         m_meshnode->getMaterial(0).MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
522         m_meshnode->setVisible(true);
523         m_is_cube = false;
524         updateLight(m_light);
525 }
526
527 void ExtrudedSpriteSceneNode::setCube(const TileSpec tiles[6])
528 {
529         if (m_cubemesh == NULL)
530                 m_cubemesh = createCubeMesh();
531
532         m_meshnode->setMesh(m_cubemesh);
533         m_meshnode->setScale(v3f(1));
534         for (int i = 0; i < 6; ++i)
535         {
536                 // Get the tile texture and atlas transformation
537                 video::ITexture* atlas = tiles[i].texture.atlas;
538                 v2f pos = tiles[i].texture.pos;
539                 v2f size = tiles[i].texture.size;
540
541                 // Set material flags and texture
542                 video::SMaterial& material = m_meshnode->getMaterial(i);
543                 material.setFlag(video::EMF_LIGHTING, false);
544                 material.setFlag(video::EMF_BILINEAR_FILTER, false);
545                 tiles[i].applyMaterialOptions(material);
546                 material.setTexture(0, atlas);
547                 material.getTextureMatrix(0).setTextureTranslate(pos.X, pos.Y);
548                 material.getTextureMatrix(0).setTextureScale(size.X, size.Y);
549         }
550         m_meshnode->setVisible(true);
551         m_is_cube = true;
552         updateLight(m_light);
553 }
554
555 void ExtrudedSpriteSceneNode::updateLight(u8 light)
556 {
557         m_light = light;
558
559         u8 li = decode_light(light);
560         video::SColor color(255,li,li,li);
561         setMeshVerticesColor(m_meshnode->getMesh(), color);
562 }
563
564 void ExtrudedSpriteSceneNode::removeSpriteFromCache(video::ITexture* texture)
565 {
566         scene::IMeshCache* cache = SceneManager->getMeshCache();
567         scene::IAnimatedMesh* mesh = cache->getMeshByName(getExtrudedName(texture));
568         if (mesh != NULL)
569                 cache->removeMesh(mesh);
570 }
571
572 void ExtrudedSpriteSceneNode::setSpriteThickness(f32 thickness)
573 {
574         m_thickness = thickness;
575         if (!m_is_cube)
576                 m_meshnode->setScale(v3f(1, 1, thickness));
577 }
578
579 const core::aabbox3d<f32>& ExtrudedSpriteSceneNode::getBoundingBox() const
580 {
581         return m_meshnode->getBoundingBox();
582 }
583
584 void ExtrudedSpriteSceneNode::OnRegisterSceneNode()
585 {
586         if (IsVisible)
587                 SceneManager->registerNodeForRendering(this);
588         ISceneNode::OnRegisterSceneNode();
589 }
590
591 void ExtrudedSpriteSceneNode::render()
592 {
593         // do nothing
594 }
595
596 io::path ExtrudedSpriteSceneNode::getExtrudedName(video::ITexture* texture)
597 {
598         io::path path = texture->getName();
599         path.append("/[extruded]");
600         return path;
601 }
602
603 scene::IAnimatedMesh* ExtrudedSpriteSceneNode::extrudeARGB(u32 width, u32 height, u8* data)
604 {
605         const s32 argb_wstep = 4 * width;
606         const s32 alpha_threshold = 1;
607
608         scene::IMeshBuffer* buf = new scene::SMeshBuffer();
609         video::SColor c(255,255,255,255);
610
611         // Front and back
612         {
613                 video::S3DVertex vertices[8] =
614                 {
615                         video::S3DVertex(-0.5,-0.5,-0.5, 0,0,-1, c, 0,1),
616                         video::S3DVertex(-0.5,+0.5,-0.5, 0,0,-1, c, 0,0),
617                         video::S3DVertex(+0.5,+0.5,-0.5, 0,0,-1, c, 1,0),
618                         video::S3DVertex(+0.5,-0.5,-0.5, 0,0,-1, c, 1,1),
619                         video::S3DVertex(+0.5,-0.5,+0.5, 0,0,+1, c, 1,1),
620                         video::S3DVertex(+0.5,+0.5,+0.5, 0,0,+1, c, 1,0),
621                         video::S3DVertex(-0.5,+0.5,+0.5, 0,0,+1, c, 0,0),
622                         video::S3DVertex(-0.5,-0.5,+0.5, 0,0,+1, c, 0,1),
623                 };
624                 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
625                 buf->append(vertices, 8, indices, 12);
626         }
627
628         // "Interior"
629         // (add faces where a solid pixel is next to a transparent one)
630         u8* solidity = new u8[(width+2) * (height+2)];
631         u32 wstep = width + 2;
632         for (u32 y = 0; y < height + 2; ++y)
633         {
634                 u8* scanline = solidity + y * wstep;
635                 if (y == 0 || y == height + 1)
636                 {
637                         for (u32 x = 0; x < width + 2; ++x)
638                                 scanline[x] = 0;
639                 }
640                 else
641                 {
642                         scanline[0] = 0;
643                         u8* argb_scanline = data + (y - 1) * argb_wstep;
644                         for (u32 x = 0; x < width; ++x)
645                                 scanline[x+1] = (argb_scanline[x*4+3] >= alpha_threshold);
646                         scanline[width + 1] = 0;
647                 }
648         }
649
650         // without this, there would be occasional "holes" in the mesh
651         f32 eps = 0.01;
652
653         for (u32 y = 0; y <= height; ++y)
654         {
655                 u8* scanline = solidity + y * wstep + 1;
656                 for (u32 x = 0; x <= width; ++x)
657                 {
658                         if (scanline[x] && !scanline[x + wstep])
659                         {
660                                 u32 xx = x + 1;
661                                 while (scanline[xx] && !scanline[xx + wstep])
662                                         ++xx;
663                                 f32 vx1 = (x - eps) / (f32) width - 0.5;
664                                 f32 vx2 = (xx + eps) / (f32) width - 0.5;
665                                 f32 vy = 0.5 - (y - eps) / (f32) height;
666                                 f32 tx1 = x / (f32) width;
667                                 f32 tx2 = xx / (f32) width;
668                                 f32 ty = (y - 0.5) / (f32) height;
669                                 video::S3DVertex vertices[8] =
670                                 {
671                                         video::S3DVertex(vx1,vy,-0.5, 0,-1,0, c, tx1,ty),
672                                         video::S3DVertex(vx2,vy,-0.5, 0,-1,0, c, tx2,ty),
673                                         video::S3DVertex(vx2,vy,+0.5, 0,-1,0, c, tx2,ty),
674                                         video::S3DVertex(vx1,vy,+0.5, 0,-1,0, c, tx1,ty),
675                                 };
676                                 u16 indices[6] = {0,1,2,2,3,0};
677                                 buf->append(vertices, 4, indices, 6);
678                                 x = xx - 1;
679                         }
680                         if (!scanline[x] && scanline[x + wstep])
681                         {
682                                 u32 xx = x + 1;
683                                 while (!scanline[xx] && scanline[xx + wstep])
684                                         ++xx;
685                                 f32 vx1 = (x - eps) / (f32) width - 0.5;
686                                 f32 vx2 = (xx + eps) / (f32) width - 0.5;
687                                 f32 vy = 0.5 - (y + eps) / (f32) height;
688                                 f32 tx1 = x / (f32) width;
689                                 f32 tx2 = xx / (f32) width;
690                                 f32 ty = (y + 0.5) / (f32) height;
691                                 video::S3DVertex vertices[8] =
692                                 {
693                                         video::S3DVertex(vx1,vy,-0.5, 0,1,0, c, tx1,ty),
694                                         video::S3DVertex(vx1,vy,+0.5, 0,1,0, c, tx1,ty),
695                                         video::S3DVertex(vx2,vy,+0.5, 0,1,0, c, tx2,ty),
696                                         video::S3DVertex(vx2,vy,-0.5, 0,1,0, c, tx2,ty),
697                                 };
698                                 u16 indices[6] = {0,1,2,2,3,0};
699                                 buf->append(vertices, 4, indices, 6);
700                                 x = xx - 1;
701                         }
702                 }
703         }
704
705         for (u32 x = 0; x <= width; ++x)
706         {
707                 u8* scancol = solidity + x + wstep;
708                 for (u32 y = 0; y <= height; ++y)
709                 {
710                         if (scancol[y * wstep] && !scancol[y * wstep + 1])
711                         {
712                                 u32 yy = y + 1;
713                                 while (scancol[yy * wstep] && !scancol[yy * wstep + 1])
714                                         ++yy;
715                                 f32 vx = (x - eps) / (f32) width - 0.5;
716                                 f32 vy1 = 0.5 - (y - eps) / (f32) height;
717                                 f32 vy2 = 0.5 - (yy + eps) / (f32) height;
718                                 f32 tx = (x - 0.5) / (f32) width;
719                                 f32 ty1 = y / (f32) height;
720                                 f32 ty2 = yy / (f32) height;
721                                 video::S3DVertex vertices[8] =
722                                 {
723                                         video::S3DVertex(vx,vy1,-0.5, 1,0,0, c, tx,ty1),
724                                         video::S3DVertex(vx,vy1,+0.5, 1,0,0, c, tx,ty1),
725                                         video::S3DVertex(vx,vy2,+0.5, 1,0,0, c, tx,ty2),
726                                         video::S3DVertex(vx,vy2,-0.5, 1,0,0, c, tx,ty2),
727                                 };
728                                 u16 indices[6] = {0,1,2,2,3,0};
729                                 buf->append(vertices, 4, indices, 6);
730                                 y = yy - 1;
731                         }
732                         if (!scancol[y * wstep] && scancol[y * wstep + 1])
733                         {
734                                 u32 yy = y + 1;
735                                 while (!scancol[yy * wstep] && scancol[yy * wstep + 1])
736                                         ++yy;
737                                 f32 vx = (x + eps) / (f32) width - 0.5;
738                                 f32 vy1 = 0.5 - (y - eps) / (f32) height;
739                                 f32 vy2 = 0.5 - (yy + eps) / (f32) height;
740                                 f32 tx = (x + 0.5) / (f32) width;
741                                 f32 ty1 = y / (f32) height;
742                                 f32 ty2 = yy / (f32) height;
743                                 video::S3DVertex vertices[8] =
744                                 {
745                                         video::S3DVertex(vx,vy1,-0.5, -1,0,0, c, tx,ty1),
746                                         video::S3DVertex(vx,vy2,-0.5, -1,0,0, c, tx,ty2),
747                                         video::S3DVertex(vx,vy2,+0.5, -1,0,0, c, tx,ty2),
748                                         video::S3DVertex(vx,vy1,+0.5, -1,0,0, c, tx,ty1),
749                                 };
750                                 u16 indices[6] = {0,1,2,2,3,0};
751                                 buf->append(vertices, 4, indices, 6);
752                                 y = yy - 1;
753                         }
754                 }
755         }
756
757         // Add to mesh
758         scene::SMesh* mesh = new scene::SMesh();
759         buf->recalculateBoundingBox();
760         mesh->addMeshBuffer(buf);
761         buf->drop();
762         mesh->recalculateBoundingBox();
763         scene::SAnimatedMesh* anim_mesh = new scene::SAnimatedMesh(mesh);
764         mesh->drop();
765         return anim_mesh;
766 }
767
768 scene::IAnimatedMesh* ExtrudedSpriteSceneNode::extrude(video::ITexture* texture)
769 {
770         scene::IAnimatedMesh* mesh = NULL;
771         core::dimension2d<u32> size = texture->getSize();
772         video::ECOLOR_FORMAT format = texture->getColorFormat();
773         if (format == video::ECF_A8R8G8B8)
774         {
775                 // Texture is in the correct color format, we can pass it
776                 // to extrudeARGB right away.
777                 void* data = texture->lock(true);
778                 if (data == NULL)
779                         return NULL;
780                 mesh = extrudeARGB(size.Width, size.Height, (u8*) data);
781                 texture->unlock();
782         }
783         else
784         {
785                 video::IVideoDriver* driver = SceneManager->getVideoDriver();
786
787                 video::IImage* img1 = driver->createImageFromData(format, size, texture->lock(true));
788                 if (img1 == NULL)
789                         return NULL;
790
791                 // img1 is in the texture's color format, convert to 8-bit ARGB
792                 video::IImage* img2 = driver->createImage(video::ECF_A8R8G8B8, size);
793                 if (img2 != NULL)
794                 {
795                         img1->copyTo(img2);
796                         img1->drop();
797
798                         mesh = extrudeARGB(size.Width, size.Height, (u8*) img2->lock());
799                         img2->unlock();
800                         img2->drop();
801                 }
802                 img1->drop();
803         }
804         return mesh;
805 }
806
807 scene::IMesh* ExtrudedSpriteSceneNode::createCubeMesh()
808 {
809         video::SColor c(255,255,255,255);
810         video::S3DVertex vertices[24] =
811         {
812                 // Up
813                 video::S3DVertex(-0.5,+0.5,-0.5, 0,1,0, c, 0,1),
814                 video::S3DVertex(-0.5,+0.5,+0.5, 0,1,0, c, 0,0),
815                 video::S3DVertex(+0.5,+0.5,+0.5, 0,1,0, c, 1,0),
816                 video::S3DVertex(+0.5,+0.5,-0.5, 0,1,0, c, 1,1),
817                 // Down
818                 video::S3DVertex(-0.5,-0.5,-0.5, 0,-1,0, c, 0,0),
819                 video::S3DVertex(+0.5,-0.5,-0.5, 0,-1,0, c, 1,0),
820                 video::S3DVertex(+0.5,-0.5,+0.5, 0,-1,0, c, 1,1),
821                 video::S3DVertex(-0.5,-0.5,+0.5, 0,-1,0, c, 0,1),
822                 // Right
823                 video::S3DVertex(+0.5,-0.5,-0.5, 1,0,0, c, 0,1),
824                 video::S3DVertex(+0.5,+0.5,-0.5, 1,0,0, c, 0,0),
825                 video::S3DVertex(+0.5,+0.5,+0.5, 1,0,0, c, 1,0),
826                 video::S3DVertex(+0.5,-0.5,+0.5, 1,0,0, c, 1,1),
827                 // Left
828                 video::S3DVertex(-0.5,-0.5,-0.5, -1,0,0, c, 1,1),
829                 video::S3DVertex(-0.5,-0.5,+0.5, -1,0,0, c, 0,1),
830                 video::S3DVertex(-0.5,+0.5,+0.5, -1,0,0, c, 0,0),
831                 video::S3DVertex(-0.5,+0.5,-0.5, -1,0,0, c, 1,0),
832                 // Back
833                 video::S3DVertex(-0.5,-0.5,+0.5, 0,0,1, c, 1,1),
834                 video::S3DVertex(+0.5,-0.5,+0.5, 0,0,1, c, 0,1),
835                 video::S3DVertex(+0.5,+0.5,+0.5, 0,0,1, c, 0,0),
836                 video::S3DVertex(-0.5,+0.5,+0.5, 0,0,1, c, 1,0),
837                 // Front
838                 video::S3DVertex(-0.5,-0.5,-0.5, 0,0,-1, c, 0,1),
839                 video::S3DVertex(-0.5,+0.5,-0.5, 0,0,-1, c, 0,0),
840                 video::S3DVertex(+0.5,+0.5,-0.5, 0,0,-1, c, 1,0),
841                 video::S3DVertex(+0.5,-0.5,-0.5, 0,0,-1, c, 1,1),
842         };
843
844         u16 indices[6] = {0,1,2,2,3,0};
845
846         scene::SMesh* mesh = new scene::SMesh();
847         for (u32 i=0; i<6; ++i)
848         {
849                 scene::IMeshBuffer* buf = new scene::SMeshBuffer();
850                 buf->append(vertices + 4 * i, 4, indices, 6);
851                 buf->recalculateBoundingBox();
852                 mesh->addMeshBuffer(buf);
853                 buf->drop();
854         }
855         mesh->recalculateBoundingBox();
856         return mesh;
857 }