void PMirror::initialize() { m_reflectionCamera = PNEW(PCamera("reflection", this)); m_reflectionCamera->setFixed(true); // Framebuffer PResourceManager *resMgr = scene()->context()->module<PResourceManager>("resource-manager"); const puint32 *rect = scene()->context()->rect(); m_reflectionFB = resMgr->createFrameBuffer(P_NULL, rect[2], rect[3], P_GLTEXTURE_FORMAT_RGBA8888, P_GLTEXTURE_FORMAT_DEPTH_COMPONENT16); m_reflectionFB->retain(); // Geometry and material setGeometry(PNEW(PGeometryPlane(resMgr))); setMaterial(PNEW(PMaterial("reflection.pmt", resMgr))); material()->parameter("texture") = m_reflectionFB->colorBuffer(); material()->parameter("inv-viewport") = pVector2(1.0f / (pfloat32)rect[2], 1.0f / (pfloat32)rect[3]); material()->parameter("color") = pVector3(0.0f, 0.0f, 1.0f); material()->parameter("blend") = 0.7f; // Render pass m_reflectionPass = PNEW(PRenderPass("reflection", scene())); }
MyScene::MyScene(PContext *context) : PScene("my-scene", context) { PRandom random(1001); const puint32 *rect = context->rect(); for (puint32 i = 0; i < NUM_LOGOS; ++i) { m_velocities[i][0] = (random.getFloat32() - 0.5f) * (pfloat32)rect[2] * 0.001f; m_velocities[i][1] = (random.getFloat32() - 0.5f) * (pfloat32)rect[3] * 0.001f; } // -------------------------------------------------------------- // Add camera // -------------------------------------------------------------- PCamera *camera = PNEW(PCamera("camera", this)); camera->setFixed(true); camera->transform().setLookAt(0.0f, 0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); camera->projection().window((pfloat32)rect[2], (pfloat32)rect[3]); setMainCamera(camera); PResourceManager* resMgr = context->module<PResourceManager>("resource-manager"); // -------------------------------------------------------------- // Scene objects // -------------------------------------------------------------- PMaterial *material = PNEW(PMaterial("texture.pmt", resMgr)); material->parameter("texture") = resMgr->getTexture("texture.png"); material->setTransparent(true); PAbstractGeometry *geometry = PNEW(PGeometryPlane(resMgr)); for (puint32 i = 0; i < NUM_LOGOS; ++i) { PString name = PString::fromUint(i); m_logos[i] = PNEW(PDrawable(name.c_str(), this)); m_logos[i]->setGeometry(geometry); m_logos[i]->setMaterial(material); m_logos[i]->transform().setTranslation(pVector3(0.0f, 0.0f, 0.0f)); m_logos[i]->transform().setScaling(pVector3(64.0f, 16.0f, 1.0f)); } setBackgroundColor(pColorRGBA(1.0f, 1.0f, 1.0f, 0.0f)); }
PDirectionalLight::PDirectionalLight(const pchar *name, PNode *node) : PAbstractLight(P_LIGHT_DIRECTIONAL, name, node) { P_OBJECT_INITIALIZE_PROPERTY(PAbstractLight); P_OBJECT_REGISTER_PROPERTY("direction", &m_direction); P_OBJECT_REGISTER_PROPERTY("castshadow", &m_castShadow); m_direction = pVector3(-1.0f, 0.0f, 0.0f); }
void MyScene::fire() { PSprite* sprite = m_sprites[m_nextSprite]; sprite->transform().setTranslation(pVector3(0, 0, 0)); pfloat32 theta = P_PI * 2.0f * m_random.getFloat32(); pfloat32 phi = 0.48f * P_PI; pfloat32 speed = m_random.getFloat32() * 5.0f + 10.0f; pfloat32 y = sinf(phi); pfloat32 x = cosf(phi) * sinf(theta); pfloat32 z = cosf(phi) * cosf(theta); m_velocity[m_nextSprite] = pVector3(x, y, z) * speed; m_nextSprite = (m_nextSprite + 1) % NUM_SPRITES; }
MyScene::MyScene(PContext *context) : PScene("my-scene", context) { m_nextSprite = 0; PResourceManager *resMgr = context->module<PResourceManager>("resource-manager"); const puint32 *rect = context->rect(); PCamera *camera = PNEW(PCamera("camera", this)); camera->setFixed(true); camera->transform().setLookAt(0.0f, 10.0f, 7.0f, 0.0f, 5.0f, 0.0f, 0.0f, 1.0f, 0.0f); camera->projection().perspective(60.0f, (pfloat32)rect[2] / (pfloat32)rect[3], 0.1f, 100.0f); setMainCamera(camera); m_plane = PNEW(PDrawable("plane", this)); m_plane->setGeometry(PNEW(PGeometryPlane(resMgr))); m_plane->setMaterial(PNEW(PMaterial("color.pmt", resMgr))); m_plane->material()->parameter("color") = P_COLOR_RED; PTexture* textures[1]; textures[0] = resMgr->getTexture("texture.png"); pchar name[] = "sprite0"; for (puint32 i = 0; i < NUM_SPRITES; ++i) { name[6]++; m_sprites[i] = PNEW(PSprite(name, this)); m_sprites[i]->setAnimationDuration(4, 2000); m_sprites[i]->setTextureAtlas(textures, 1, 2, 2, true); m_sprites[i]->transform().setScaling(pVector3(0.25f, 0.25f, 1.0f)); m_sprites[i]->transform().setTranslation(pVector3(-100000.0f, -100000.0f, -100000.0f)); } pfloat32 aspect = (pfloat32)rect[2] / (pfloat32)rect[3]; PSprite2D *legend = PNEW(PSprite2D("legend", this)); legend->transform().setScaling(pVector3(aspect * 0.25f, aspect * 0.0625f, 1.0f)); textures[0] = resMgr->getTexture("legend.png"); legend->setTextureAtlas(textures, 1, 1, 1, true); legend->setAnimationDuration(1, 10000); legend->transform().setTranslation(pVector3(0, 0.4f, -1.0f)); }
void PCamera::fromLight(const PDirectionalLight *light, const PBox *bbox) { PVector3 center = pVector3(0, 0, 0); PVector3 eye = light->direction() * 100.0f; // any point on the light ray is OK. PVector3 up; PVector3 lightSource = pVector3(-light->direction()[0], -light->direction()[1], -light->direction()[2]); if (lightSource[1] >= 0) { if (lightSource[1] < 0.99f) { up = pVector3(0, 1, 0); } else { up = pVector3(-1.0f, 0.0f, 0.0f); } } else { if (lightSource[1] > -0.99f) { up = pVector3(0, -1, 0); } else { up = pVector3(-1.0f, 0.0f, 0.0f); } } up.normalize(); // Generate the camera matrix m_localTransform.setLookAt(eye[0], eye[1], eye[2], center[0], center[1], center[2], up[0], up[1], up[2]); PBox bboxInCameraSpace(*bbox); bboxInCameraSpace.transform(cameraTransform()); // Generate the projection matrix m_projection.orthogonal(bboxInCameraSpace.min()[0], bboxInCameraSpace.max()[0], bboxInCameraSpace.min()[1], bboxInCameraSpace.max()[1], bboxInCameraSpace.min()[2] - 0.01f, bboxInCameraSpace.max()[2] + 0.01f); }
void PDirectionalLight::setDirection(pfloat32 x, pfloat32 y, pfloat32 z) { PVector3 v = pVector3(x, y, z); v.normalize(); m_direction = v; }
PVector3 PPropertyTransform::scaling() const { return pVector3(m_scalingx.toFloat(), m_scalingy.toFloat(), m_scalingz.toFloat()); }
PVector3 PPropertyTransform::rotation() const { return pVector3(m_rotationx.toFloat(), m_rotationy.toFloat(), m_rotationz.toFloat()); }
MyScene::MyScene(PContext *context) : PScene("my-scene", context) { PResourceManager *resMgr = context->module<PResourceManager>("resource-manager"); // -------------------------------------------------------------- // Add camera // -------------------------------------------------------------- const puint32 *rect = context->rect(); PCamera *camera = PNEW(PCamera("camera", this)); camera->setFixed(true); camera->transform().setLookAt(0.0f, 0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); camera->projection().orthogonal((pfloat32)rect[2] / (pfloat32)rect[3], 0.1f, 100.0f); setMainCamera(camera); // -------------------------------------------------------------- // Scene background // -------------------------------------------------------------- setBackgroundColor(P_COLOR_BLACK_TRANSPARENT); // -------------------------------------------------------------- // Scene objects // -------------------------------------------------------------- PMaterial *material; PAbstractGeometry *geometry; material = PNEW(PMaterial("color.pmt", resMgr)); material->parameter("color") = pColorRGBA(1.0f, 0.0f, 0.0f, 1.0f); geometry = PNEW(PGeometryPlane(resMgr)); PDrawable *plane1 = PNEW(PDrawable("plane1", this)); plane1->setMaterial(material); plane1->setGeometry(geometry); plane1->transform().setScaling(pVector3(0.2f, 0.2f, 1.0f)); plane1->transform().setTranslation(pVector3(0.0f, -0.6f, 0.0f)); material = PNEW(PMaterial("color.pmt", resMgr)); material->parameter("color") = pColorRGBA(0.0f, 1.0f, 0.0f, 1.0f); geometry = PNEW(PGeometryPlane(resMgr)); PDrawable *plane2 = PNEW(PDrawable("plane2", this)); plane2->setMaterial(material); plane2->setGeometry(geometry); plane2->transform().setScaling(pVector3(0.2f, 0.2f, 1.0f)); plane2->transform().setTranslation(pVector3(0.0f, 0.0f, 0.0f)); material = PNEW(PMaterial("color.pmt", resMgr)); material->parameter("color") = pColorRGBA(0.0f, 0.0f, 1.0f, 1.0f); geometry = PNEW(PGeometryPlane(resMgr)); PDrawable *plane3 = PNEW(PDrawable("plane3", this)); plane3->setMaterial(material); plane3->setGeometry(geometry); plane3->transform().setScaling(pVector3(0.2f, 0.2f, 1.0f)); plane3->transform().setTranslation(pVector3(0.0f, 0.6f, 0.0f)); // -------------------------------------------------------------- // Global post processing effect. // -------------------------------------------------------------- PEffectGlow *glow; glow = PNEW(PEffectGlow(this)); glow->setBlendMode(P_EFFECTBLEND_MIX); glow->setBlurIterations(1); glow->setBlurKernelSize(5); glow->setBlurDistance(2); }