void AnimatingScene::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // Create the Octree component to the scene so that drawable objects can be rendered. Use default volume // (-1000, -1000, -1000) to (1000, 1000, 1000) scene_->CreateComponent<Octree>(); // Create a Zone component into a child scene node. The Zone controls ambient lighting and fog settings. Like the Octree, // it also defines its volume with a bounding box, but can be rotated (so it does not need to be aligned to the world X, Y // and Z axes.) Drawable objects "pick up" the zone they belong to and use it when rendering; several zones can exist Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); // Set same volume as the Octree, set a close bluish fog and some ambient light zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.05f, 0.1f, 0.15f)); zone->SetFogColor(Color(0.1f, 0.2f, 0.3f)); zone->SetFogStart(10.0f); zone->SetFogEnd(100.0f); // Create randomly positioned and oriented box StaticModels in the scene const unsigned NUM_OBJECTS = 2000; for (unsigned i = 0; i < NUM_OBJECTS; ++i) { Node* boxNode = scene_->CreateChild("Box"); boxNode->SetPosition(Vector3(Random(200.0f) - 100.0f, Random(200.0f) - 100.0f, Random(200.0f) - 100.0f)); // Orient using random pitch, yaw and roll Euler angles boxNode->SetRotation(Quaternion(Random(360.0f), Random(360.0f), Random(360.0f))); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); // Add our custom Rotator component which will rotate the scene node each frame, when the scene sends its update event. // The Rotator component derives from the base class LogicComponent, which has convenience functionality to subscribe // to the various update events, and forward them to virtual functions that can be implemented by subclasses. This way // writing logic/update components in C++ becomes similar to scripting. // Now we simply set same rotation speed for all objects Rotator* rotator = boxNode->CreateComponent<Rotator>(); rotator->SetRotationSpeed(Vector3(10.0f, 20.0f, 30.0f)); } // Create the camera. Let the starting position be at the world origin. As the fog limits maximum visible distance, we can // bring the far clip plane closer for more effective culling of distant objects cameraNode_ = scene_->CreateChild("Camera"); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(100.0f); // Create a point light to the camera scene node Light* light = cameraNode_->CreateComponent<Light>(); light->SetLightType(LIGHT_POINT); light->SetRange(30.0f); }
void Base::draw() { glPushMatrix(); glTranslatef(x,0.0f,0.0f); // Draw a rectangle. glColor3f(0.58f,0.29f,0.0f); glBegin(GL_QUADS); glVertex2f(-1*(width/2),-15); glVertex2f(width/2,-15); glVertex2f(width/2,15); glVertex2f(-1*(width/2),15); glEnd(); // Draw wheels. glPushMatrix(); glTranslatef(-1.0f*(width/2.0f), 15.0f, 0.0f); wheels[0]->draw(); glPopMatrix(); glPushMatrix(); glTranslatef((width/2.0f), 15.0f, 0.0f); wheels[1]->draw(); glPopMatrix(); // Draw attachments. for(int i = 0; i < 3; i++) { if(attachments[i]) { glPushMatrix(); glTranslatef( (-1.0f*(width/2.0f)+(i*(width/3.0f))), -25.0f, 0.0f ); attachments[i]->draw(); glPopMatrix(); glPushMatrix(); glTranslatef( (-1.0f*(width/2.0f)+(i*(width/3.0f))), -15.0f, 0.0f ); Rotator *r = new Rotator(); r->draw(); glPopMatrix(); } } glPopMatrix(); }
void RenderToTexture::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); { // Create the scene which will be rendered to a texture rttScene_ = new Scene(context_); // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000) rttScene_->CreateComponent<Octree>(); // Create a Zone for ambient light & fog control Node* zoneNode = rttScene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); // Set same volume as the Octree, set a close bluish fog and some ambient light zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.05f, 0.1f, 0.15f)); zone->SetFogColor(Color(0.1f, 0.2f, 0.3f)); zone->SetFogStart(10.0f); zone->SetFogEnd(100.0f); // Create randomly positioned and oriented box StaticModels in the scene const unsigned NUM_OBJECTS = 2000; for (unsigned i = 0; i < NUM_OBJECTS; ++i) { Node* boxNode = rttScene_->CreateChild("Box"); boxNode->SetPosition(Vector3(Random(200.0f) - 100.0f, Random(200.0f) - 100.0f, Random(200.0f) - 100.0f)); // Orient using random pitch, yaw and roll Euler angles boxNode->SetRotation(Quaternion(Random(360.0f), Random(360.0f), Random(360.0f))); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); // Add our custom Rotator component which will rotate the scene node each frame, when the scene sends its update event. // Simply set same rotation speed for all objects Rotator* rotator = boxNode->CreateComponent<Rotator>(); rotator->SetRotationSpeed(Vector3(10.0f, 20.0f, 30.0f)); } // Create a camera for the render-to-texture scene. Simply leave it at the world origin and let it observe the scene rttCameraNode_ = rttScene_->CreateChild("Camera"); Camera* camera = rttCameraNode_->CreateComponent<Camera>(); camera->SetFarClip(100.0f); // Create a point light to the camera scene node Light* light = rttCameraNode_->CreateComponent<Light>(); light->SetLightType(LIGHT_POINT); light->SetRange(30.0f); } { // Create the scene in which we move around scene_ = new Scene(context_); // Create octree, use also default volume (-1000, -1000, -1000) to (1000, 1000, 1000) scene_->CreateComponent<Octree>(); // Create a Zone component for ambient lighting & fog control Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.1f, 0.1f, 0.1f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.0f); // Create a directional light without shadows Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.5f, -1.0f, 0.5f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetColor(Color(0.2f, 0.2f, 0.2f)); light->SetSpecularIntensity(1.0f); // Create a "floor" consisting of several tiles for (int y = -5; y <= 5; ++y) { for (int x = -5; x <= 5; ++x) { Node* floorNode = scene_->CreateChild("FloorTile"); floorNode->SetPosition(Vector3(x * 20.5f, -0.5f, y * 20.5f)); floorNode->SetScale(Vector3(20.0f, 1.0f, 20.f)); StaticModel* floorObject = floorNode->CreateComponent<StaticModel>(); floorObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); floorObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); } } // Create a "screen" like object for viewing the second scene. Construct it from two StaticModels, a box for the frame // and a plane for the actual view { Node* boxNode = scene_->CreateChild("ScreenBox"); boxNode->SetPosition(Vector3(0.0f, 10.0f, 0.0f)); boxNode->SetScale(Vector3(21.0f, 16.0f, 0.5f)); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); Node* screenNode = scene_->CreateChild("Screen"); screenNode->SetPosition(Vector3(0.0f, 10.0f, -0.27f)); screenNode->SetRotation(Quaternion(-90.0f, 0.0f, 0.0f)); screenNode->SetScale(Vector3(20.0f, 0.0f, 15.0f)); StaticModel* screenObject = screenNode->CreateComponent<StaticModel>(); screenObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl")); // Create a renderable texture (1024x768, RGB format), enable bilinear filtering on it SharedPtr<Texture2D> renderTexture(new Texture2D(context_)); renderTexture->SetSize(1024, 768, Graphics::GetRGBFormat(), TEXTURE_RENDERTARGET); renderTexture->SetFilterMode(FILTER_BILINEAR); // Create a new material from scratch, use the diffuse unlit technique, assign the render texture // as its diffuse texture, then assign the material to the screen plane object SharedPtr<Material> renderMaterial(new Material(context_)); renderMaterial->SetTechnique(0, cache->GetResource<Technique>("Techniques/DiffUnlit.xml")); renderMaterial->SetTexture(TU_DIFFUSE, renderTexture); screenObject->SetMaterial(renderMaterial); // Get the texture's RenderSurface object (exists when the texture has been created in rendertarget mode) // and define the viewport for rendering the second scene, similarly as how backbuffer viewports are defined // to the Renderer subsystem. By default the texture viewport will be updated when the texture is visible // in the main view RenderSurface* surface = renderTexture->GetRenderSurface(); SharedPtr<Viewport> rttViewport(new Viewport(context_, rttScene_, rttCameraNode_->GetComponent<Camera>())); surface->SetViewport(0, rttViewport); } // Create the camera which we will move around. Limit far clip distance to match the fog cameraNode_ = scene_->CreateChild("Camera"); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(300.0f); // Set an initial position for the camera scene node above the plane cameraNode_->SetPosition(Vector3(0.0f, 7.0f, -30.0f)); } }
Vector2D::Vector2D(const Rotator& rotator) { float rotation = rotator.getValue(); this->x = cos(rotation); this->y = sin(rotation); }
QmlView::QmlView(QUrl source, QWidget *parent, MafwRegistryAdapter *mafwRegistry ) : QMainWindow(parent), ui(new Ui::QmlView), mafwRegistry(mafwRegistry), mafwRenderer(mafwRegistry->renderer()) { ui->setupUi(this); ui->declarativeView->setSource(source); ui->declarativeView->setResizeMode(QDeclarativeView::SizeRootObjectToView); setAttribute(Qt::WA_DeleteOnClose); setAttribute(Qt::WA_Maemo5StackedWindow); setAttribute(Qt::WA_Maemo5NonComposited); QGLWidget *glWidget = new QGLWidget(this); ui->declarativeView->setViewport(glWidget); positionTimer = new QTimer(this); positionTimer->setInterval(1000); fmtx = new FMTXInterface(this); Rotator *rotator = Rotator::acquire(); savedPolicy = rotator->policy(); rotator->setPolicy(Rotator::Landscape); rootObject = dynamic_cast<QObject*>(ui->declarativeView->rootObject()); rootObject->setParent(this); connect(rootObject, SIGNAL(quitButtonClicked()), this, SLOT(close())); connect(rootObject, SIGNAL(prevButtonClicked()), mafwRenderer, SLOT(previous())); connect(rootObject, SIGNAL(playButtonClicked()), this, SLOT(onPlayClicked())); connect(rootObject, SIGNAL(nextButtonClicked()), mafwRenderer, SLOT(next())); connect(rootObject, SIGNAL(fmtxButtonClicked()), this, SLOT(onFmtxClicked())); connect(rootObject, SIGNAL(sliderValueChanged(int)), this, SLOT(onSliderValueChanged(int))); connect(rootObject, SIGNAL(playlistItemSelected(int)), this, SLOT(onPlaylistItemChanged(int))); connect(this, SIGNAL(titleChanged(QVariant)), rootObject, SLOT(setSongTitle(QVariant))); connect(this, SIGNAL(albumChanged(QVariant)), rootObject, SLOT(setSongAlbum(QVariant))); connect(this, SIGNAL(artistChanged(QVariant)), rootObject, SLOT(setSongArtist(QVariant))); connect(this, SIGNAL(albumArtChanged(QVariant)), rootObject, SLOT(setAlbumArt(QVariant))); connect(this, SIGNAL(durationTextChanged(QVariant)), rootObject, SLOT(setPosition(QVariant))); connect(this, SIGNAL(positionChanged(QVariant)), rootObject, SLOT(setSliderValue(QVariant))); connect(this, SIGNAL(durationChanged(QVariant)), rootObject, SLOT(setSliderMaximum(QVariant))); connect(this, SIGNAL(stateIconChanged(QVariant)), rootObject, SLOT(setPlayButtonIcon(QVariant))); connect(this, SIGNAL(rowChanged(QVariant)), rootObject, SLOT(onRowChanged(QVariant))); connect(this, SIGNAL(fmtxStateChanged(QVariant)), rootObject, SLOT(onFmtxStateChanged(QVariant))); connect(this, SIGNAL(playlistItemAppended(QVariant,QVariant,QVariant)), rootObject, SLOT(appendPlaylistItem(QVariant,QVariant,QVariant))); connect(this, SIGNAL(playlistItemInserted(QVariant,QVariant,QVariant,QVariant)), rootObject, SLOT(insertPlaylistItem(QVariant,QVariant,QVariant,QVariant))); connect(this, SIGNAL(playlistItemSet(QVariant,QVariant,QVariant,QVariant)), rootObject, SLOT(setPlaylistItem(QVariant,QVariant,QVariant,QVariant))); connect(this, SIGNAL(playlistItemRemoved(QVariant)), rootObject, SLOT(removePlaylistItem(QVariant))); connect(this, SIGNAL(playlistCleared()), rootObject, SLOT(clearPlaylist())); connect(mafwRenderer, SIGNAL(stateChanged(MafwPlayState)), this, SLOT(onStateChanged(MafwPlayState))); connect(mafwRenderer, SIGNAL(positionReceived(int,QString)), this, SLOT(onPositionChanged(int))); connect(mafwRenderer, SIGNAL(statusReceived(MafwPlaylist*,uint,MafwPlayState,QString,QString)), this, SLOT(onStatusReceived(MafwPlaylist*,uint,MafwPlayState))); connect(positionTimer, SIGNAL(timeout()), mafwRenderer, SLOT(getPosition())); connect(fmtx, SIGNAL(propertyChanged()), this, SLOT(onFmtxChanged())); onFmtxChanged(); positionTimer->start(); quint32 disable = {0}; Atom winPortraitModeSupportAtom = XInternAtom(QX11Info::display(), "_HILDON_PORTRAIT_MODE_SUPPORT", false); XChangeProperty(QX11Info::display(), winId(), winPortraitModeSupportAtom, XA_CARDINAL, 32, PropModeReplace, (uchar*) &disable, 1); this->setDNDAtom(true); mafwRenderer->getStatus(); mafwRenderer->getPosition(); }
bool SceneLoader::Load(Scene::Scene &scene, LogicSystem &logic, const String &file) { String filePath = directory + file; LOG_INFO("Loading scene '%'...", filePath); std::ifstream f(filePath.c_str(), std::ifstream::binary); if (!f.is_open()) { LOG_ERROR("Couldn't open file."); return false; } std::map<uint, Scene::Node *> node_by_id; struct Node { uint type; uint id; uint parent; float mat[3][4]; }; const uint node_size = 60; assert(sizeof(Node) == node_size); struct Object { uint cast_shadows; char mesh_name[32]; }; const uint object_size = 36; assert(sizeof(Object) == object_size); struct Light { uint type; float radius; float cutoff; float color[3]; float energy; }; const uint light_size = 28; assert(sizeof(Light) == light_size); union { char as_char[4]; uint count; }; f.read(as_char, 4); for (uint i = 0; i < count; i++) { union { char as_char[node_size]; Node as_node; }; Scene::Node *node = 0; f.read(as_char, node_size); if (as_node.type == 0) // DUMMY { node = scene.CreateDummy(); } else if (as_node.type == 1 || as_node.type == 3) // OBJECT or SKY { union { char as_char[object_size]; Object as_object; }; Scene::Object *ob = 0; f.read(as_char, object_size); if (as_node.type == 1) ob = scene.CreateObject(); else ob = scene.CreateSky(); ob->SetCastShadows(as_object.cast_shadows); ob->SetModel(modelCache->Get(String(as_object.mesh_name) + ".obj")); node = ob; } else if (as_node.type == 2) // LIGHT { union { char as_char[light_size]; Light as_light; }; Scene::Light *light = 0; f.read(as_char, light_size); if (as_light.type == 0) // DIRECTIONAL light = scene.CreateDirLight(); else if (as_light.type == 1) // SPOT light = scene.CreateSpotLight(); else if (as_light.type == 2) // POINT light = scene.CreatePointLight(); else { LOG_ERROR("Invalid light type."); return false; } light->SetRadius(as_light.radius); light->SetCutoff(as_light.cutoff); light->SetColor(Vector3(as_light.color[0], as_light.color[1], as_light.color[2])); light->SetEnergy(as_light.energy); node = light; float m[3][3]; for (int i = 0; i < 3; i++) { m[i][0] = as_node.mat[i][0]; m[i][1] = as_node.mat[i][2]; m[i][2] = -as_node.mat[i][1]; } for (int i = 0; i < 3; i++) { as_node.mat[i][0] = m[i][0]; as_node.mat[i][1] = m[i][1]; as_node.mat[i][2] = m[i][2]; } } else { LOG_ERROR("Invalid node type."); return false; } node_by_id[as_node.id] = node; if (as_node.parent) node->SetParent(node_by_id[as_node.parent]); float (*m)[4] = as_node.mat; float scale_x = Vector3(m[0][0], m[1][0], m[2][0]).Length(); float scale_y = Vector3(m[0][1], m[1][1], m[2][1]).Length(); float scale_z = Vector3(m[0][2], m[1][2], m[2][2]).Length(); Matrix3 rot; for (int i = 0; i < 3; i++) { rot.mat[0][i] = m[i][0] / scale_x; rot.mat[1][i] = m[i][1] / scale_y; rot.mat[2][i] = m[i][2] / scale_z; } node->SetPosition(Vector3(m[0][3], m[1][3], m[2][3])); node->SetRotation(rot); node->SetScale(Vector3(scale_x, scale_y, scale_z)); } struct Prop { uint node; uint type; float value; }; const uint prop_size = 12; assert(sizeof(Prop) == prop_size); f.read(as_char, 4); for (uint i = 0; i < count; i++) { union { char as_char[prop_size]; Prop prop; }; f.read(as_char, prop_size); Scene::Node *node = node_by_id[prop.node]; if (prop.type == 0) { Rotator *rot = logic.CreateRotator(); rot->SetNode(node); rot->SetAxis(Vector3(1.0f, 0.0f, 0.0f)); rot->SetAngularVelocity(prop.value); } else if (prop.type == 1) { Rotator *rot = logic.CreateRotator(); rot->SetNode(node); rot->SetAxis(Vector3(0.0f, 1.0f, 0.0f)); rot->SetAngularVelocity(prop.value); } else if (prop.type == 2) { Rotator *rot = logic.CreateRotator(); rot->SetNode(node); rot->SetAxis(Vector3(0.0f, 0.0f, 1.0f)); rot->SetAngularVelocity(prop.value); } } f.close(); return true; }