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