]> git.lizzy.rs Git - dragonfireclient.git/blob - src/camera.cpp
Create a separate scene manager for the wielded tool. This fixes the glitchyness...
[dragonfireclient.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.03 * bobvec;
193                 rel_cam_target += 0.045 * bobvec;
194                 rel_cam_up.rotateXYBy(0.03 * 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
253         // Render distance feedback loop
254         updateViewingRange(frametime);
255
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))
264         {
265                 // Start animation
266                 m_view_bobbing_state = 1;
267                 m_view_bobbing_speed = MYMIN(speed.getLength(), 40);
268         }
269         else if (m_view_bobbing_state == 1)
270         {
271                 // Stop animation
272                 m_view_bobbing_state = 2;
273                 m_view_bobbing_speed = 60;
274         }
275 }
276
277 void Camera::updateViewingRange(f32 frametime_in)
278 {
279         if (m_draw_control.range_all)
280                 return;
281
282         m_added_frametime += frametime_in;
283         m_added_frames += 1;
284
285         // Actually this counter kind of sucks because frametime is busytime
286         m_frametime_counter -= frametime_in;
287         if (m_frametime_counter > 0)
288                 return;
289         m_frametime_counter = 0.2;
290
291         /*dstream<<__FUNCTION_NAME
292                         <<": Collected "<<m_added_frames<<" frames, total of "
293                         <<m_added_frametime<<"s."<<std::endl;
294
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
299                         <<std::endl;*/
300
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;
305
306         f32 block_draw_ratio = 1.0;
307         if (m_draw_control.blocks_would_have_drawn != 0)
308         {
309                 block_draw_ratio = (f32)m_draw_control.blocks_drawn
310                         / (f32)m_draw_control.blocks_would_have_drawn;
311         }
312
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;
316
317         m_added_frametime = 0.0;
318         m_added_frames = 0;
319
320         f32 wanted_frametime_change = m_wanted_frametime - frametime;
321         //dstream<<"wanted_frametime_change="<<wanted_frametime_change<<std::endl;
322
323         // If needed frametime change is small, just return
324         if (fabs(wanted_frametime_change) < m_wanted_frametime*0.4)
325         {
326                 //dstream<<"ignoring small wanted_frametime_change"<<std::endl;
327                 return;
328         }
329
330         f32 range = m_draw_control.wanted_range;
331         f32 new_range = range;
332
333         f32 d_range = range - m_range_old;
334         f32 d_frametime = frametime - m_frametime_old;
335         if (d_range != 0)
336         {
337                 m_time_per_range = d_frametime / d_range;
338         }
339
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)
351         {
352                 m_time_per_range = min_time_per_range;
353                 //dstream<<"m_time_per_range="<<m_time_per_range<<" (min)"<<std::endl;
354         }
355         else
356         {
357                 //dstream<<"m_time_per_range="<<m_time_per_range<<std::endl;
358         }
359
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;
366
367         // If needed range change is very small, just return
368         if(fabs(wanted_range_change) < 0.001)
369         {
370                 //dstream<<"ignoring small wanted_range_change"<<std::endl;
371                 return;
372         }
373
374         new_range += wanted_range_change;
375         
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;*/
381
382         m_draw_control.wanted_range = new_range;
383
384         m_range_old = new_range;
385         m_frametime_old = frametime;
386 }
387
388 void Camera::updateSettings()
389 {
390         m_viewing_range_min = g_settings.getS16("viewing_range_nodes_min");
391         m_viewing_range_min = MYMAX(5.0, m_viewing_range_min);
392
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);
395
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;
400
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;
404 }
405
406 void Camera::wield(const InventoryItem* item)
407 {
408         if (item != NULL)
409         {
410                 bool isCube = false;
411
412                 // Try to make a MaterialItem cube.
413                 if (std::string(item->getName()) == "MaterialItem")
414                 {
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)
419                         {
420                                 m_wieldnode->setCube(content_features(content).tiles);
421                                 m_wieldnode->setScale(v3f(30));
422                                 isCube = true;
423                         }
424                 }
425
426                 // If that failed, make an extruded sprite.
427                 if (!isCube)
428                 {
429                         m_wieldnode->setSprite(item->getImageRaw());
430                         m_wieldnode->setScale(v3f(40));
431                 }
432
433                 m_wieldnode->setVisible(true);
434         }
435         else
436         {
437                 // Bare hands
438                 m_wieldnode->setVisible(false);
439         }
440 }
441
442 void Camera::setDigging(s32 button)
443 {
444         if (m_digging_button == -1)
445                 m_digging_button = button;
446 }
447
448 void Camera::drawWieldedTool()
449 {
450         m_wieldmgr->getVideoDriver()->clearZBuffer();
451
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();
458 }
459
460
461 ExtrudedSpriteSceneNode::ExtrudedSpriteSceneNode(
462         scene::ISceneNode* parent,
463         scene::ISceneManager* mgr,
464         s32 id,
465         const v3f& position,
466         const v3f& rotation,
467         const v3f& scale
468 ):
469         ISceneNode(parent, mgr, id, position, rotation, scale)
470 {
471         m_meshnode = mgr->addMeshSceneNode(NULL, this, -1, v3f(0,0,0), v3f(0,0,0), v3f(1,1,1), true);
472         m_thickness = 0.1;
473         m_cubemesh = NULL;
474         m_is_cube = false;
475 }
476
477 ExtrudedSpriteSceneNode::~ExtrudedSpriteSceneNode()
478 {
479         removeChild(m_meshnode);
480         if (m_cubemesh)
481                 m_cubemesh->drop();
482 }
483
484 void ExtrudedSpriteSceneNode::setSprite(video::ITexture* texture)
485 {
486         if (texture == NULL)
487         {
488                 m_meshnode->setVisible(false);
489                 return;
490         }
491
492         io::path name = getExtrudedName(texture);
493         scene::IMeshCache* cache = SceneManager->getMeshCache();
494         scene::IAnimatedMesh* mesh = cache->getMeshByName(name);
495         if (mesh != NULL)
496         {
497                 // Extruded texture has been found in cache.
498                 m_meshnode->setMesh(mesh);
499         }
500         else
501         {
502                 // Texture was not yet extruded, do it now and save in cache
503                 mesh = extrude(texture);
504                 if (mesh == NULL)
505                 {
506                         dstream << "Warning: failed to extrude sprite" << std::endl;
507                         m_meshnode->setVisible(false);
508                         return;
509                 }
510                 cache->addMesh(name, mesh);
511                 m_meshnode->setMesh(mesh);
512                 mesh->drop();
513         }
514
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);
521         m_is_cube = false;
522 }
523
524 void ExtrudedSpriteSceneNode::setCube(const TileSpec tiles[6])
525 {
526         if (m_cubemesh == NULL)
527                 m_cubemesh = createCubeMesh();
528
529         m_meshnode->setMesh(m_cubemesh);
530         m_meshnode->setScale(v3f(1));
531         for (int i = 0; i < 6; ++i)
532         {
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;
537
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);
546         }
547         m_meshnode->setVisible(true);
548         m_is_cube = true;
549 }
550
551 void ExtrudedSpriteSceneNode::removeSpriteFromCache(video::ITexture* texture)
552 {
553         scene::IMeshCache* cache = SceneManager->getMeshCache();
554         scene::IAnimatedMesh* mesh = cache->getMeshByName(getExtrudedName(texture));
555         if (mesh != NULL)
556                 cache->removeMesh(mesh);
557 }
558
559 void ExtrudedSpriteSceneNode::setSpriteThickness(f32 thickness)
560 {
561         m_thickness = thickness;
562         if (!m_is_cube)
563                 m_meshnode->setScale(v3f(1, 1, thickness));
564 }
565
566 const core::aabbox3d<f32>& ExtrudedSpriteSceneNode::getBoundingBox() const
567 {
568         return m_meshnode->getBoundingBox();
569 }
570
571 void ExtrudedSpriteSceneNode::OnRegisterSceneNode()
572 {
573         if (IsVisible)
574                 SceneManager->registerNodeForRendering(this);
575         ISceneNode::OnRegisterSceneNode();
576 }
577
578 void ExtrudedSpriteSceneNode::render()
579 {
580         // do nothing
581 }
582
583 io::path ExtrudedSpriteSceneNode::getExtrudedName(video::ITexture* texture)
584 {
585         io::path path = texture->getName();
586         path.append("/[extruded]");
587         return path;
588 }
589
590 scene::IAnimatedMesh* ExtrudedSpriteSceneNode::extrudeARGB(u32 width, u32 height, u8* data)
591 {
592         const s32 argb_wstep = 4 * width;
593         const s32 alpha_threshold = 1;
594
595         scene::IMeshBuffer* buf = new scene::SMeshBuffer();
596         video::SColor c(255,255,255,255);
597
598         // Front and back
599         {
600                 video::S3DVertex vertices[8] =
601                 {
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),
610                 };
611                 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
612                 buf->append(vertices, 8, indices, 12);
613         }
614
615         // "Interior"
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)
620         {
621                 u8* scanline = solidity + y * wstep;
622                 if (y == 0 || y == height + 1)
623                 {
624                         for (u32 x = 0; x < width + 2; ++x)
625                                 scanline[x] = 0;
626                 }
627                 else
628                 {
629                         scanline[0] = 0;
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;
634                 }
635         }
636
637         // without this, there would be occasional "holes" in the mesh
638         f32 eps = 0.01;
639
640         for (u32 y = 0; y <= height; ++y)
641         {
642                 u8* scanline = solidity + y * wstep + 1;
643                 for (u32 x = 0; x <= width; ++x)
644                 {
645                         if (scanline[x] && !scanline[x + wstep])
646                         {
647                                 u32 xx = x + 1;
648                                 while (scanline[xx] && !scanline[xx + wstep])
649                                         ++xx;
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] =
657                                 {
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),
662                                 };
663                                 u16 indices[6] = {0,1,2,2,3,0};
664                                 buf->append(vertices, 4, indices, 6);
665                                 x = xx - 1;
666                         }
667                         if (!scanline[x] && scanline[x + wstep])
668                         {
669                                 u32 xx = x + 1;
670                                 while (!scanline[xx] && scanline[xx + wstep])
671                                         ++xx;
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] =
679                                 {
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),
684                                 };
685                                 u16 indices[6] = {0,1,2,2,3,0};
686                                 buf->append(vertices, 4, indices, 6);
687                                 x = xx - 1;
688                         }
689                 }
690         }
691
692         for (u32 x = 0; x <= width; ++x)
693         {
694                 u8* scancol = solidity + x + wstep;
695                 for (u32 y = 0; y <= height; ++y)
696                 {
697                         if (scancol[y * wstep] && !scancol[y * wstep + 1])
698                         {
699                                 u32 yy = y + 1;
700                                 while (scancol[yy * wstep] && !scancol[yy * wstep + 1])
701                                         ++yy;
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] =
709                                 {
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),
714                                 };
715                                 u16 indices[6] = {0,1,2,2,3,0};
716                                 buf->append(vertices, 4, indices, 6);
717                                 y = yy - 1;
718                         }
719                         if (!scancol[y * wstep] && scancol[y * wstep + 1])
720                         {
721                                 u32 yy = y + 1;
722                                 while (!scancol[yy * wstep] && scancol[yy * wstep + 1])
723                                         ++yy;
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] =
731                                 {
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),
736                                 };
737                                 u16 indices[6] = {0,1,2,2,3,0};
738                                 buf->append(vertices, 4, indices, 6);
739                                 y = yy - 1;
740                         }
741                 }
742         }
743
744         // Add to mesh
745         scene::SMesh* mesh = new scene::SMesh();
746         buf->recalculateBoundingBox();
747         mesh->addMeshBuffer(buf);
748         buf->drop();
749         mesh->recalculateBoundingBox();
750         scene::SAnimatedMesh* anim_mesh = new scene::SAnimatedMesh(mesh);
751         mesh->drop();
752         return anim_mesh;
753 }
754
755 scene::IAnimatedMesh* ExtrudedSpriteSceneNode::extrude(video::ITexture* texture)
756 {
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)
761         {
762                 // Texture is in the correct color format, we can pass it
763                 // to extrudeARGB right away.
764                 void* data = texture->lock(true);
765                 if (data == NULL)
766                         return NULL;
767                 mesh = extrudeARGB(size.Width, size.Height, (u8*) data);
768                 texture->unlock();
769         }
770         else
771         {
772                 video::IVideoDriver* driver = SceneManager->getVideoDriver();
773
774                 video::IImage* img1 = driver->createImageFromData(format, size, texture->lock(true));
775                 if (img1 == NULL)
776                         return NULL;
777
778                 // img1 is in the texture's color format, convert to 8-bit ARGB
779                 video::IImage* img2 = driver->createImage(video::ECF_A8R8G8B8, size);
780                 if (img2 != NULL)
781                 {
782                         img1->copyTo(img2);
783                         img1->drop();
784
785                         mesh = extrudeARGB(size.Width, size.Height, (u8*) img2->lock());
786                         img2->unlock();
787                         img2->drop();
788                 }
789                 img1->drop();
790         }
791         return mesh;
792 }
793
794 scene::IMesh* ExtrudedSpriteSceneNode::createCubeMesh()
795 {
796         video::SColor c(255,255,255,255);
797         video::S3DVertex vertices[24] =
798         {
799                 // Up
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),
804                 // Down
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),
809                 // Right
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),
814                 // Left
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),
819                 // Back
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),
824                 // Front
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),
829         };
830
831         u16 indices[6] = {0,1,2,2,3,0};
832
833         scene::SMesh* mesh = new scene::SMesh();
834         for (u32 i=0; i<6; ++i)
835         {
836                 scene::IMeshBuffer* buf = new scene::SMeshBuffer();
837                 buf->append(vertices + 4 * i, 4, indices, 6);
838                 buf->recalculateBoundingBox();
839                 mesh->addMeshBuffer(buf);
840                 buf->drop();
841         }
842         mesh->recalculateBoundingBox();
843         return mesh;
844 }