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