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