3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
23 #include "main.h" // for g_settings
28 #include <SAnimatedMesh.h>
30 #include "nodedef.h" // For wield visualization
32 Camera::Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control):
41 m_draw_control(draw_control),
42 m_viewing_range_min(5.0),
43 m_viewing_range_max(5.0),
45 m_camera_position(0,0,0),
46 m_camera_direction(0,0,0),
52 m_wanted_frametime(0.0),
57 m_frametime_counter(0),
58 m_time_per_range(30. / 50), // a sane default of 30ms per 50 nodes of range
60 m_view_bobbing_anim(0),
61 m_view_bobbing_state(0),
62 m_view_bobbing_speed(0),
67 //dstream<<__FUNCTION_NAME<<std::endl;
69 // note: making the camera node a child of the player node
70 // would lead to unexpected behaviour, so we don't do that.
71 m_playernode = smgr->addEmptySceneNode(smgr->getRootSceneNode());
72 m_headnode = smgr->addEmptySceneNode(m_playernode);
73 m_cameranode = smgr->addCameraSceneNode(smgr->getRootSceneNode());
74 m_cameranode->bindTargetAndRotation(true);
76 // This needs to be in its own scene manager. It is drawn after
77 // all other 3D scene nodes and before the GUI.
78 m_wieldmgr = smgr->createNewSceneManager();
79 m_wieldmgr->addCameraSceneNode();
80 m_wieldnode = new ExtrudedSpriteSceneNode(m_wieldmgr->getRootSceneNode(), m_wieldmgr);
91 bool Camera::successfullyCreated(std::wstring& error_message)
93 if (m_playernode == NULL)
95 error_message = L"Failed to create the player scene node";
98 if (m_headnode == NULL)
100 error_message = L"Failed to create the head scene node";
103 if (m_cameranode == NULL)
105 error_message = L"Failed to create the camera scene node";
108 if (m_wieldmgr == NULL)
110 error_message = L"Failed to create the wielded item scene manager";
113 if (m_wieldnode == NULL)
115 error_message = L"Failed to create the wielded item scene node";
121 // Returns the fractional part of x
122 inline f32 my_modf(f32 x)
125 return modf(x, &dummy);
128 void Camera::step(f32 dtime)
130 if (m_view_bobbing_state != 0)
132 //f32 offset = dtime * m_view_bobbing_speed * 0.035;
133 f32 offset = dtime * m_view_bobbing_speed * 0.030;
134 if (m_view_bobbing_state == 2)
137 // Animation is getting turned off
138 if (m_view_bobbing_anim < 0.5)
139 m_view_bobbing_anim -= offset;
141 m_view_bobbing_anim += offset;
142 if (m_view_bobbing_anim <= 0 || m_view_bobbing_anim >= 1)
144 m_view_bobbing_anim = 0;
145 m_view_bobbing_state = 0;
149 // Animation is getting turned off
150 if(m_view_bobbing_anim < 0.25){
151 m_view_bobbing_anim -= offset;
152 } else if(m_view_bobbing_anim > 0.75){
153 m_view_bobbing_anim += offset;
154 } if(m_view_bobbing_anim < 0.5){
155 m_view_bobbing_anim += offset;
156 if(m_view_bobbing_anim > 0.5)
157 m_view_bobbing_anim = 0.5;
159 m_view_bobbing_anim -= offset;
160 if(m_view_bobbing_anim < 0.5)
161 m_view_bobbing_anim = 0.5;
163 if(m_view_bobbing_anim <= 0 || m_view_bobbing_anim >= 1 ||
164 fabs(m_view_bobbing_anim - 0.5) < 0.01)
166 m_view_bobbing_anim = 0;
167 m_view_bobbing_state = 0;
173 m_view_bobbing_anim = my_modf(m_view_bobbing_anim + offset);
177 if (m_digging_button != -1)
179 f32 offset = dtime * 3.5;
180 m_digging_anim += offset;
181 if (m_digging_anim >= 1)
184 m_digging_button = -1;
189 void Camera::update(LocalPlayer* player, f32 frametime, v2u32 screensize)
191 // Set player node transformation
192 m_playernode->setPosition(player->getPosition());
193 m_playernode->setRotation(v3f(0, -1 * player->getYaw(), 0));
194 m_playernode->updateAbsolutePosition();
196 // Set head node transformation
197 m_headnode->setPosition(player->getEyeOffset());
198 m_headnode->setRotation(v3f(player->getPitch(), 0, 0));
199 m_headnode->updateAbsolutePosition();
201 // Compute relative camera position and target
202 v3f rel_cam_pos = v3f(0,0,0);
203 v3f rel_cam_target = v3f(0,0,1);
204 v3f rel_cam_up = v3f(0,1,0);
206 if (m_view_bobbing_anim != 0)
208 f32 bobfrac = my_modf(m_view_bobbing_anim * 2);
209 f32 bobdir = (m_view_bobbing_anim < 0.5) ? 1.0 : -1.0;
213 f32 bobtmp = sin(pow(bobfrac, bobknob) * PI);
214 //f32 bobtmp2 = cos(pow(bobfrac, bobknob) * PI);
217 0.3 * bobdir * sin(bobfrac * PI),
218 -0.28 * bobtmp * bobtmp,
221 //rel_cam_pos += 0.2 * bobvec;
222 //rel_cam_target += 0.03 * bobvec;
223 //rel_cam_up.rotateXYBy(0.02 * bobdir * bobtmp * PI);
225 f *= g_settings->getFloat("view_bobbing_amount");
226 rel_cam_pos += bobvec * f;
227 //rel_cam_target += 0.995 * bobvec * f;
228 rel_cam_target += bobvec * f;
229 rel_cam_target.Z -= 0.005 * bobvec.Z * f;
230 //rel_cam_target.X -= 0.005 * bobvec.X * f;
231 //rel_cam_target.Y -= 0.005 * bobvec.Y * f;
232 rel_cam_up.rotateXYBy(-0.03 * bobdir * bobtmp * PI * f);
234 f32 angle_deg = 1 * bobdir * sin(bobfrac * PI);
235 f32 angle_rad = angle_deg * PI / 180;
239 r * (cos(angle_rad) - 1),
242 //rel_cam_target += off;
243 rel_cam_up.rotateXYBy(angle_deg);
248 // Compute absolute camera position and target
249 m_headnode->getAbsoluteTransformation().transformVect(m_camera_position, rel_cam_pos);
250 m_headnode->getAbsoluteTransformation().rotateVect(m_camera_direction, rel_cam_target - rel_cam_pos);
253 m_headnode->getAbsoluteTransformation().rotateVect(abs_cam_up, rel_cam_up);
255 // Set camera node transformation
256 m_cameranode->setPosition(m_camera_position);
257 m_cameranode->setUpVector(abs_cam_up);
258 // *100.0 helps in large map coordinates
259 m_cameranode->setTarget(m_camera_position + 100 * m_camera_direction);
261 // FOV and and aspect ratio
262 m_aspect = (f32)screensize.X / (f32) screensize.Y;
263 m_fov_x = 2 * atan(0.5 * m_aspect * tan(m_fov_y));
264 m_cameranode->setAspectRatio(m_aspect);
265 m_cameranode->setFOV(m_fov_y);
266 // Just so big a value that everything rendered is visible
267 // Some more allowance that m_viewing_range_max * BS because of active objects etc.
268 m_cameranode->setFarValue(m_viewing_range_max * BS * 10);
270 // Position the wielded item
271 v3f wield_position = v3f(45, -35, 65);
272 v3f wield_rotation = v3f(-100, 110, -100);
273 if (m_digging_button != -1)
275 f32 digfrac = m_digging_anim;
276 wield_position.X -= 30 * sin(pow(digfrac, 0.8f) * PI);
277 wield_position.Y += 15 * sin(digfrac * 2 * PI);
278 wield_position.Z += 5 * digfrac;
280 // Euler angles are PURE EVIL, so why not use quaternions?
281 core::quaternion quat_begin(wield_rotation * core::DEGTORAD);
282 core::quaternion quat_end(v3f(90, -10, -130) * core::DEGTORAD);
283 core::quaternion quat_slerp;
284 quat_slerp.slerp(quat_begin, quat_end, sin(digfrac * PI));
285 quat_slerp.toEuler(wield_rotation);
286 wield_rotation *= core::RADTODEG;
289 f32 bobfrac = my_modf(m_view_bobbing_anim);
290 wield_position.X -= sin(bobfrac*PI*2.0) * 3.0;
291 wield_position.Y += sin(my_modf(bobfrac*2.0)*PI) * 3.0;
293 m_wieldnode->setPosition(wield_position);
294 m_wieldnode->setRotation(wield_rotation);
295 m_wieldnode->updateLight(player->light);
297 // Render distance feedback loop
298 updateViewingRange(frametime);
300 // If the player seems to be walking on solid ground,
301 // view bobbing is enabled and free_move is off,
302 // start (or continue) the view bobbing animation.
303 v3f speed = player->getSpeed();
304 if ((hypot(speed.X, speed.Z) > BS) &&
305 (player->touching_ground) &&
306 (g_settings->getBool("view_bobbing") == true) &&
307 (g_settings->getBool("free_move") == false))
310 m_view_bobbing_state = 1;
311 m_view_bobbing_speed = MYMIN(speed.getLength(), 40);
313 else if (m_view_bobbing_state == 1)
316 m_view_bobbing_state = 2;
317 m_view_bobbing_speed = 60;
321 void Camera::updateViewingRange(f32 frametime_in)
323 if (m_draw_control.range_all)
326 m_added_frametime += frametime_in;
329 // Actually this counter kind of sucks because frametime is busytime
330 m_frametime_counter -= frametime_in;
331 if (m_frametime_counter > 0)
333 m_frametime_counter = 0.2;
335 /*dstream<<__FUNCTION_NAME
336 <<": Collected "<<m_added_frames<<" frames, total of "
337 <<m_added_frametime<<"s."<<std::endl;
339 dstream<<"m_draw_control.blocks_drawn="
340 <<m_draw_control.blocks_drawn
341 <<", m_draw_control.blocks_would_have_drawn="
342 <<m_draw_control.blocks_would_have_drawn
345 m_draw_control.wanted_min_range = m_viewing_range_min;
346 m_draw_control.wanted_max_blocks = (2.0*m_draw_control.blocks_would_have_drawn)+1;
347 if (m_draw_control.wanted_max_blocks < 10)
348 m_draw_control.wanted_max_blocks = 10;
350 f32 block_draw_ratio = 1.0;
351 if (m_draw_control.blocks_would_have_drawn != 0)
353 block_draw_ratio = (f32)m_draw_control.blocks_drawn
354 / (f32)m_draw_control.blocks_would_have_drawn;
357 // Calculate the average frametime in the case that all wanted
358 // blocks had been drawn
359 f32 frametime = m_added_frametime / m_added_frames / block_draw_ratio;
361 m_added_frametime = 0.0;
364 f32 wanted_frametime_change = m_wanted_frametime - frametime;
365 //dstream<<"wanted_frametime_change="<<wanted_frametime_change<<std::endl;
367 // If needed frametime change is small, just return
368 // This value was 0.4 for many months until 2011-10-18 by c55;
369 // Let's see how this works out.
370 if (fabs(wanted_frametime_change) < m_wanted_frametime*0.33)
372 //dstream<<"ignoring small wanted_frametime_change"<<std::endl;
376 f32 range = m_draw_control.wanted_range;
377 f32 new_range = range;
379 f32 d_range = range - m_range_old;
380 f32 d_frametime = frametime - m_frametime_old;
383 m_time_per_range = d_frametime / d_range;
386 // The minimum allowed calculated frametime-range derivative:
387 // Practically this sets the maximum speed of changing the range.
388 // The lower this value, the higher the maximum changing speed.
389 // A low value here results in wobbly range (0.001)
390 // A high value here results in slow changing range (0.0025)
391 // SUGG: This could be dynamically adjusted so that when
392 // the camera is turning, this is lower
393 //f32 min_time_per_range = 0.0015;
394 f32 min_time_per_range = 0.0010;
395 //f32 min_time_per_range = 0.05 / range;
396 if(m_time_per_range < min_time_per_range)
398 m_time_per_range = min_time_per_range;
399 //dstream<<"m_time_per_range="<<m_time_per_range<<" (min)"<<std::endl;
403 //dstream<<"m_time_per_range="<<m_time_per_range<<std::endl;
406 f32 wanted_range_change = wanted_frametime_change / m_time_per_range;
407 // Dampen the change a bit to kill oscillations
408 //wanted_range_change *= 0.9;
409 //wanted_range_change *= 0.75;
410 wanted_range_change *= 0.5;
411 //dstream<<"wanted_range_change="<<wanted_range_change<<std::endl;
413 // If needed range change is very small, just return
414 if(fabs(wanted_range_change) < 0.001)
416 //dstream<<"ignoring small wanted_range_change"<<std::endl;
420 new_range += wanted_range_change;
422 //f32 new_range_unclamped = new_range;
423 new_range = MYMAX(new_range, m_viewing_range_min);
424 new_range = MYMIN(new_range, m_viewing_range_max);
425 /*dstream<<"new_range="<<new_range_unclamped
426 <<", clamped to "<<new_range<<std::endl;*/
428 m_draw_control.wanted_range = new_range;
430 m_range_old = new_range;
431 m_frametime_old = frametime;
434 void Camera::updateSettings()
436 m_viewing_range_min = g_settings->getS16("viewing_range_nodes_min");
437 m_viewing_range_min = MYMAX(5.0, m_viewing_range_min);
439 m_viewing_range_max = g_settings->getS16("viewing_range_nodes_max");
440 m_viewing_range_max = MYMAX(m_viewing_range_min, m_viewing_range_max);
442 f32 fov_degrees = g_settings->getFloat("fov");
443 fov_degrees = MYMAX(fov_degrees, 10.0);
444 fov_degrees = MYMIN(fov_degrees, 170.0);
445 m_fov_y = fov_degrees * PI / 180.0;
447 f32 wanted_fps = g_settings->getFloat("wanted_fps");
448 wanted_fps = MYMAX(wanted_fps, 1.0);
449 m_wanted_frametime = 1.0 / wanted_fps;
452 void Camera::wield(const InventoryItem* item, IGameDef *gamedef)
454 //ITextureSource *tsrc = gamedef->tsrc();
455 INodeDefManager *ndef = gamedef->ndef();
461 // Try to make a MaterialItem cube.
462 if (std::string(item->getName()) == "MaterialItem")
464 // A block-type material
465 MaterialItem* mat_item = (MaterialItem*) item;
466 content_t content = mat_item->getMaterial();
467 switch(ndef->get(content).drawtype){
470 case NDT_FLOWINGLIQUID:
473 case NDT_ALLFACES_OPTIONAL:
474 m_wieldnode->setCube(ndef->get(content).tiles);
475 m_wieldnode->setScale(v3f(30));
483 // If that failed, make an extruded sprite.
486 m_wieldnode->setSprite(item->getImageRaw());
487 m_wieldnode->setScale(v3f(40));
490 m_wieldnode->setVisible(true);
495 m_wieldnode->setVisible(false);
499 void Camera::setDigging(s32 button)
501 if (m_digging_button == -1)
502 m_digging_button = button;
505 void Camera::drawWieldedTool()
507 m_wieldmgr->getVideoDriver()->clearZBuffer();
509 scene::ICameraSceneNode* cam = m_wieldmgr->getActiveCamera();
510 cam->setAspectRatio(m_cameranode->getAspectRatio());
511 cam->setFOV(m_cameranode->getFOV());
512 cam->setNearValue(0.1);
513 cam->setFarValue(100);
514 m_wieldmgr->drawAll();
518 ExtrudedSpriteSceneNode::ExtrudedSpriteSceneNode(
519 scene::ISceneNode* parent,
520 scene::ISceneManager* mgr,
526 ISceneNode(parent, mgr, id, position, rotation, scale)
528 m_meshnode = mgr->addMeshSceneNode(NULL, this, -1, v3f(0,0,0), v3f(0,0,0), v3f(1,1,1), true);
535 ExtrudedSpriteSceneNode::~ExtrudedSpriteSceneNode()
537 removeChild(m_meshnode);
542 void ExtrudedSpriteSceneNode::setSprite(video::ITexture* texture)
546 m_meshnode->setVisible(false);
550 io::path name = getExtrudedName(texture);
551 scene::IMeshCache* cache = SceneManager->getMeshCache();
552 scene::IAnimatedMesh* mesh = cache->getMeshByName(name);
555 // Extruded texture has been found in cache.
556 m_meshnode->setMesh(mesh);
560 // Texture was not yet extruded, do it now and save in cache
561 mesh = extrude(texture);
564 dstream << "Warning: failed to extrude sprite" << std::endl;
565 m_meshnode->setVisible(false);
568 cache->addMesh(name, mesh);
569 m_meshnode->setMesh(mesh);
573 m_meshnode->setScale(v3f(1, 1, m_thickness));
574 m_meshnode->getMaterial(0).setTexture(0, texture);
575 m_meshnode->getMaterial(0).setFlag(video::EMF_LIGHTING, false);
576 m_meshnode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, false);
577 m_meshnode->getMaterial(0).MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
578 m_meshnode->setVisible(true);
580 updateLight(m_light);
583 void ExtrudedSpriteSceneNode::setCube(const TileSpec tiles[6])
585 if (m_cubemesh == NULL)
586 m_cubemesh = createCubeMesh();
588 m_meshnode->setMesh(m_cubemesh);
589 m_meshnode->setScale(v3f(1));
590 for (int i = 0; i < 6; ++i)
592 // Get the tile texture and atlas transformation
593 video::ITexture* atlas = tiles[i].texture.atlas;
594 v2f pos = tiles[i].texture.pos;
595 v2f size = tiles[i].texture.size;
597 // Set material flags and texture
598 video::SMaterial& material = m_meshnode->getMaterial(i);
599 material.setFlag(video::EMF_LIGHTING, false);
600 material.setFlag(video::EMF_BILINEAR_FILTER, false);
601 tiles[i].applyMaterialOptions(material);
602 material.setTexture(0, atlas);
603 material.getTextureMatrix(0).setTextureTranslate(pos.X, pos.Y);
604 material.getTextureMatrix(0).setTextureScale(size.X, size.Y);
606 m_meshnode->setVisible(true);
608 updateLight(m_light);
611 void ExtrudedSpriteSceneNode::updateLight(u8 light)
615 u8 li = decode_light(light);
616 // Set brightness one lower than incoming light
618 video::SColor color(255,li,li,li);
619 setMeshVerticesColor(m_meshnode->getMesh(), color);
622 void ExtrudedSpriteSceneNode::removeSpriteFromCache(video::ITexture* texture)
624 scene::IMeshCache* cache = SceneManager->getMeshCache();
625 scene::IAnimatedMesh* mesh = cache->getMeshByName(getExtrudedName(texture));
627 cache->removeMesh(mesh);
630 void ExtrudedSpriteSceneNode::setSpriteThickness(f32 thickness)
632 m_thickness = thickness;
634 m_meshnode->setScale(v3f(1, 1, thickness));
637 const core::aabbox3d<f32>& ExtrudedSpriteSceneNode::getBoundingBox() const
639 return m_meshnode->getBoundingBox();
642 void ExtrudedSpriteSceneNode::OnRegisterSceneNode()
645 SceneManager->registerNodeForRendering(this);
646 ISceneNode::OnRegisterSceneNode();
649 void ExtrudedSpriteSceneNode::render()
654 io::path ExtrudedSpriteSceneNode::getExtrudedName(video::ITexture* texture)
656 io::path path = texture->getName();
657 path.append("/[extruded]");
661 scene::IAnimatedMesh* ExtrudedSpriteSceneNode::extrudeARGB(u32 width, u32 height, u8* data)
663 const s32 argb_wstep = 4 * width;
664 const s32 alpha_threshold = 1;
666 scene::IMeshBuffer* buf = new scene::SMeshBuffer();
667 video::SColor c(255,255,255,255);
671 video::S3DVertex vertices[8] =
673 video::S3DVertex(-0.5,-0.5,-0.5, 0,0,-1, c, 0,1),
674 video::S3DVertex(-0.5,+0.5,-0.5, 0,0,-1, c, 0,0),
675 video::S3DVertex(+0.5,+0.5,-0.5, 0,0,-1, c, 1,0),
676 video::S3DVertex(+0.5,-0.5,-0.5, 0,0,-1, c, 1,1),
677 video::S3DVertex(+0.5,-0.5,+0.5, 0,0,+1, c, 1,1),
678 video::S3DVertex(+0.5,+0.5,+0.5, 0,0,+1, c, 1,0),
679 video::S3DVertex(-0.5,+0.5,+0.5, 0,0,+1, c, 0,0),
680 video::S3DVertex(-0.5,-0.5,+0.5, 0,0,+1, c, 0,1),
682 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
683 buf->append(vertices, 8, indices, 12);
687 // (add faces where a solid pixel is next to a transparent one)
688 u8* solidity = new u8[(width+2) * (height+2)];
689 u32 wstep = width + 2;
690 for (u32 y = 0; y < height + 2; ++y)
692 u8* scanline = solidity + y * wstep;
693 if (y == 0 || y == height + 1)
695 for (u32 x = 0; x < width + 2; ++x)
701 u8* argb_scanline = data + (y - 1) * argb_wstep;
702 for (u32 x = 0; x < width; ++x)
703 scanline[x+1] = (argb_scanline[x*4+3] >= alpha_threshold);
704 scanline[width + 1] = 0;
708 // without this, there would be occasional "holes" in the mesh
711 for (u32 y = 0; y <= height; ++y)
713 u8* scanline = solidity + y * wstep + 1;
714 for (u32 x = 0; x <= width; ++x)
716 if (scanline[x] && !scanline[x + wstep])
719 while (scanline[xx] && !scanline[xx + wstep])
721 f32 vx1 = (x - eps) / (f32) width - 0.5;
722 f32 vx2 = (xx + eps) / (f32) width - 0.5;
723 f32 vy = 0.5 - (y - eps) / (f32) height;
724 f32 tx1 = x / (f32) width;
725 f32 tx2 = xx / (f32) width;
726 f32 ty = (y - 0.5) / (f32) height;
727 video::S3DVertex vertices[8] =
729 video::S3DVertex(vx1,vy,-0.5, 0,-1,0, c, tx1,ty),
730 video::S3DVertex(vx2,vy,-0.5, 0,-1,0, c, tx2,ty),
731 video::S3DVertex(vx2,vy,+0.5, 0,-1,0, c, tx2,ty),
732 video::S3DVertex(vx1,vy,+0.5, 0,-1,0, c, tx1,ty),
734 u16 indices[6] = {0,1,2,2,3,0};
735 buf->append(vertices, 4, indices, 6);
738 if (!scanline[x] && scanline[x + wstep])
741 while (!scanline[xx] && scanline[xx + wstep])
743 f32 vx1 = (x - eps) / (f32) width - 0.5;
744 f32 vx2 = (xx + eps) / (f32) width - 0.5;
745 f32 vy = 0.5 - (y + eps) / (f32) height;
746 f32 tx1 = x / (f32) width;
747 f32 tx2 = xx / (f32) width;
748 f32 ty = (y + 0.5) / (f32) height;
749 video::S3DVertex vertices[8] =
751 video::S3DVertex(vx1,vy,-0.5, 0,1,0, c, tx1,ty),
752 video::S3DVertex(vx1,vy,+0.5, 0,1,0, c, tx1,ty),
753 video::S3DVertex(vx2,vy,+0.5, 0,1,0, c, tx2,ty),
754 video::S3DVertex(vx2,vy,-0.5, 0,1,0, c, tx2,ty),
756 u16 indices[6] = {0,1,2,2,3,0};
757 buf->append(vertices, 4, indices, 6);
763 for (u32 x = 0; x <= width; ++x)
765 u8* scancol = solidity + x + wstep;
766 for (u32 y = 0; y <= height; ++y)
768 if (scancol[y * wstep] && !scancol[y * wstep + 1])
771 while (scancol[yy * wstep] && !scancol[yy * wstep + 1])
773 f32 vx = (x - eps) / (f32) width - 0.5;
774 f32 vy1 = 0.5 - (y - eps) / (f32) height;
775 f32 vy2 = 0.5 - (yy + eps) / (f32) height;
776 f32 tx = (x - 0.5) / (f32) width;
777 f32 ty1 = y / (f32) height;
778 f32 ty2 = yy / (f32) height;
779 video::S3DVertex vertices[8] =
781 video::S3DVertex(vx,vy1,-0.5, 1,0,0, c, tx,ty1),
782 video::S3DVertex(vx,vy1,+0.5, 1,0,0, c, tx,ty1),
783 video::S3DVertex(vx,vy2,+0.5, 1,0,0, c, tx,ty2),
784 video::S3DVertex(vx,vy2,-0.5, 1,0,0, c, tx,ty2),
786 u16 indices[6] = {0,1,2,2,3,0};
787 buf->append(vertices, 4, indices, 6);
790 if (!scancol[y * wstep] && scancol[y * wstep + 1])
793 while (!scancol[yy * wstep] && scancol[yy * wstep + 1])
795 f32 vx = (x + eps) / (f32) width - 0.5;
796 f32 vy1 = 0.5 - (y - eps) / (f32) height;
797 f32 vy2 = 0.5 - (yy + eps) / (f32) height;
798 f32 tx = (x + 0.5) / (f32) width;
799 f32 ty1 = y / (f32) height;
800 f32 ty2 = yy / (f32) height;
801 video::S3DVertex vertices[8] =
803 video::S3DVertex(vx,vy1,-0.5, -1,0,0, c, tx,ty1),
804 video::S3DVertex(vx,vy2,-0.5, -1,0,0, c, tx,ty2),
805 video::S3DVertex(vx,vy2,+0.5, -1,0,0, c, tx,ty2),
806 video::S3DVertex(vx,vy1,+0.5, -1,0,0, c, tx,ty1),
808 u16 indices[6] = {0,1,2,2,3,0};
809 buf->append(vertices, 4, indices, 6);
816 scene::SMesh* mesh = new scene::SMesh();
817 buf->recalculateBoundingBox();
818 mesh->addMeshBuffer(buf);
820 mesh->recalculateBoundingBox();
821 scene::SAnimatedMesh* anim_mesh = new scene::SAnimatedMesh(mesh);
826 scene::IAnimatedMesh* ExtrudedSpriteSceneNode::extrude(video::ITexture* texture)
828 scene::IAnimatedMesh* mesh = NULL;
829 core::dimension2d<u32> size = texture->getSize();
830 video::ECOLOR_FORMAT format = texture->getColorFormat();
831 if (format == video::ECF_A8R8G8B8)
833 // Texture is in the correct color format, we can pass it
834 // to extrudeARGB right away.
835 void* data = texture->lock(true);
838 mesh = extrudeARGB(size.Width, size.Height, (u8*) data);
843 video::IVideoDriver* driver = SceneManager->getVideoDriver();
845 video::IImage* img1 = driver->createImageFromData(format, size, texture->lock(true));
849 // img1 is in the texture's color format, convert to 8-bit ARGB
850 video::IImage* img2 = driver->createImage(video::ECF_A8R8G8B8, size);
856 mesh = extrudeARGB(size.Width, size.Height, (u8*) img2->lock());
865 scene::IMesh* ExtrudedSpriteSceneNode::createCubeMesh()
867 video::SColor c(255,255,255,255);
868 video::S3DVertex vertices[24] =
871 video::S3DVertex(-0.5,+0.5,-0.5, 0,1,0, c, 0,1),
872 video::S3DVertex(-0.5,+0.5,+0.5, 0,1,0, c, 0,0),
873 video::S3DVertex(+0.5,+0.5,+0.5, 0,1,0, c, 1,0),
874 video::S3DVertex(+0.5,+0.5,-0.5, 0,1,0, c, 1,1),
876 video::S3DVertex(-0.5,-0.5,-0.5, 0,-1,0, c, 0,0),
877 video::S3DVertex(+0.5,-0.5,-0.5, 0,-1,0, c, 1,0),
878 video::S3DVertex(+0.5,-0.5,+0.5, 0,-1,0, c, 1,1),
879 video::S3DVertex(-0.5,-0.5,+0.5, 0,-1,0, c, 0,1),
881 video::S3DVertex(+0.5,-0.5,-0.5, 1,0,0, c, 0,1),
882 video::S3DVertex(+0.5,+0.5,-0.5, 1,0,0, c, 0,0),
883 video::S3DVertex(+0.5,+0.5,+0.5, 1,0,0, c, 1,0),
884 video::S3DVertex(+0.5,-0.5,+0.5, 1,0,0, c, 1,1),
886 video::S3DVertex(-0.5,-0.5,-0.5, -1,0,0, c, 1,1),
887 video::S3DVertex(-0.5,-0.5,+0.5, -1,0,0, c, 0,1),
888 video::S3DVertex(-0.5,+0.5,+0.5, -1,0,0, c, 0,0),
889 video::S3DVertex(-0.5,+0.5,-0.5, -1,0,0, c, 1,0),
891 video::S3DVertex(-0.5,-0.5,+0.5, 0,0,1, c, 1,1),
892 video::S3DVertex(+0.5,-0.5,+0.5, 0,0,1, c, 0,1),
893 video::S3DVertex(+0.5,+0.5,+0.5, 0,0,1, c, 0,0),
894 video::S3DVertex(-0.5,+0.5,+0.5, 0,0,1, c, 1,0),
896 video::S3DVertex(-0.5,-0.5,-0.5, 0,0,-1, c, 0,1),
897 video::S3DVertex(-0.5,+0.5,-0.5, 0,0,-1, c, 0,0),
898 video::S3DVertex(+0.5,+0.5,-0.5, 0,0,-1, c, 1,0),
899 video::S3DVertex(+0.5,-0.5,-0.5, 0,0,-1, c, 1,1),
902 u16 indices[6] = {0,1,2,2,3,0};
904 scene::SMesh* mesh = new scene::SMesh();
905 for (u32 i=0; i<6; ++i)
907 scene::IMeshBuffer* buf = new scene::SMeshBuffer();
908 buf->append(vertices + 4 * i, 4, indices, 6);
909 buf->recalculateBoundingBox();
910 mesh->addMeshBuffer(buf);
913 mesh->recalculateBoundingBox();