void MovableObject::Update() { switch(this->direction) { case DOWN: if(moving) { this->position.z += this->velocity; } break; case UP: if(moving) { this->position.z -= this->velocity; } break; case RIGHT://default direction if(moving) { this->position.x += this->velocity; } break; case LEFT: if(moving) { this->position.x -= this->velocity; } break; } Matrix4Translate(position.x,position.y,position.z,&translation); this->transform = rotation * translation; }
void tBillboardBatch::UpdateWorldTransform() { tMatrix4d translation; Matrix4Translate(translation, tVector3d(m_Offset)); m_WorldTransform = translation; }
void tRenderer::RenderOverlay(const boost::shared_ptr<tOverlayRenderable>& xOverlay) { m_xRenderSystem->SetDepthTestState(false); m_xRenderSystem->SetViewMatrix(tMatrix4d::IDENTITY); // Set up world transformation for overlay tMatrix4d translation; tMatrix4d scaling; QPoint glPosition = xOverlay->ScreenPosition(); glPosition.setY((m_pRenderTarget->height() - glPosition.y()) - xOverlay->Height()); Matrix4Translate(translation, tVector3d(glPosition.x(), glPosition.y(), 0)); Matrix4Scale<double>(scaling, xOverlay->Width(), xOverlay->Height(), 1.0); m_xRenderSystem->SetWorldMatrix(translation * scaling); // Set up textures RenderSystem::ConfigureTextureStages(*xOverlay, *m_xRenderSystem); //set up blending if (xOverlay->Texture(0)->PremultipliedAlpha() == true) { m_xRenderSystem->SetSceneBlending(true, RenderSettings::eBlendFn_One, RenderSettings::eBlendFn_OneMinusSourceAlpha); } else { m_xRenderSystem->SetSceneBlending(true, RenderSettings::eBlendFn_SourceAlpha, RenderSettings::eBlendFn_OneMinusSourceAlpha); } m_xRenderSystem->Render(*xOverlay->RenderOperation()); }
bool tBillboardBatch::Commit() { QVector<tVector3f> position; QVector<tVector2f> texCoords; QList<tIndexedImage> images; tWriteLocker lock(&m_BatchLock); //Now texture coordinates only one time tho if( m_xTextureAtlas != 0 ) { QHash<QString, tRectangularTextureCoords> texInfo = m_xTextureAtlas->CoordList(); tVector3d square[] = { tVector3d(-0.5, 0.5, 1.0), tVector3d(-0.5, -0.5, 1.0), tVector3d(0.5, 0.5, 1.0), tVector3d(0.5, -0.5, 1.0) }; for(int j = 0; j < m_Billboards.count(); ++j) { tBillboardBatchItem currentItem = m_Billboards[j]; if (currentItem.xBillboard->IsHidden() == false) { boost::shared_ptr<tBillboard> xGeoBillboard = currentItem.xBillboard; tRectangularTextureCoords coords = texInfo[currentItem.key]; double lowestZ = 0; unsigned int startingIndex = position.size(); unsigned int vertexCount = 6; tVector3d forward = xGeoBillboard->Up().CrossProduct(xGeoBillboard->Right()); forward.Normalize(); tVector2f textureBottomLeft( coords.bottomLeft.x, coords.bottomLeft.y); tVector2f textureBottomRight( coords.bottomRight.x, coords.bottomRight.y); tVector2f textureTopLeft( coords.topLeft.x, coords.topLeft.y); tVector2f textureTopRight( coords.topRight.x, coords.topRight.y); SquareTriangleList( textureTopLeft, textureBottomLeft, textureTopRight, textureBottomRight, texCoords ); tMatrix4d view; Matrix4Translate( view, tVector3d(-m_Offset) ); tVector3d imageOffsetWorldRight = xGeoBillboard->ScaleFactor() * xGeoBillboard->ImagePixelOffsetX() * xGeoBillboard->Right(); tVector3d imageOffsetWorldUp = xGeoBillboard->ScaleFactor() * xGeoBillboard->ImagePixelOffsetY() * -xGeoBillboard->Up(); // Negate up because in pixels space top left is origin tVector3d imageOffsetWorldVector = imageOffsetWorldRight + imageOffsetWorldUp; tMatrix4d imageWorldOffset; Matrix4Translate( imageWorldOffset, imageOffsetWorldVector ); tMatrix4d world = xGeoBillboard->WorldTransform(); tMatrix4d mvMatrix = view * imageWorldOffset * world; tVector3d billboardTopLeft = mvMatrix * square[0]; tVector3d billboardBottomLeft = mvMatrix * square[1]; tVector3d billboardTopRight = mvMatrix * square[2]; tVector3d billboardBottomRight = mvMatrix * square[3]; tVector3f billboardTopLeftF( static_cast<float>(billboardTopLeft.x), static_cast<float>(billboardTopLeft.y), static_cast<float>(billboardTopLeft.z) ); tVector3f billboardBottomLeftF( static_cast<float>(billboardBottomLeft.x), static_cast<float>(billboardBottomLeft.y), static_cast<float>(billboardBottomLeft.z) ); tVector3f billboardTopRightF( static_cast<float>(billboardTopRight.x), static_cast<float>(billboardTopRight.y), static_cast<float>(billboardTopRight.z) ); tVector3f billboardBottomRightF( static_cast<float>(billboardBottomRight.x), static_cast<float>(billboardBottomRight.y), static_cast<float>(billboardBottomRight.z) ); SquareTriangleList( billboardTopLeftF, billboardBottomLeftF, billboardTopRightF, billboardBottomRightF, position ); lowestZ = LowestZOnSquare( billboardTopLeft, billboardBottomLeft, billboardTopRight, billboardBottomRight ); if ( xGeoBillboard->Text().isEmpty() == false ) { tRectangularTextureCoords textCoords = texInfo[ QString("%1 %2").arg(xGeoBillboard->Text()).arg(qHash(xGeoBillboard->TextStyle())) ]; textureBottomLeft = tVector2f( textCoords.bottomLeft.x, textCoords.bottomLeft.y); textureBottomRight = tVector2f( textCoords.bottomRight.x, textCoords.bottomRight.y); textureTopLeft = tVector2f( textCoords.topLeft.x, textCoords.topLeft.y); textureTopRight = tVector2f( textCoords.topRight.x, textCoords.topRight.y); SquareTriangleList( textureTopLeft, textureBottomLeft, textureTopRight, textureBottomRight, texCoords ); tVector3d textPosition = xGeoBillboard->CreateTextOffsetVertex( xGeoBillboard->TextStyle().m_align, xGeoBillboard->Up(), xGeoBillboard->Right(), xGeoBillboard->ScaleFactor(), xGeoBillboard->TextWidth(), xGeoBillboard->TextHeight() ); textPosition += (-forward * xGeoBillboard->ScaleFactor()); tMatrix4d translation; Matrix4Translate( translation, textPosition ); tMatrix4d scale; Matrix4Scale( scale, xGeoBillboard->ScaleFactor() * xGeoBillboard->TextWidth(), xGeoBillboard->ScaleFactor() * xGeoBillboard->TextHeight(), xGeoBillboard->ScaleFactor() ); tMatrix4d world = translation * xGeoBillboard->RotationMatrix() * scale; mvMatrix = view * imageWorldOffset * world; billboardTopLeft = mvMatrix * square[0]; billboardBottomLeft = mvMatrix * square[1]; billboardTopRight = mvMatrix * square[2]; billboardBottomRight = mvMatrix * square[3]; tVector3f billboardTopLeftF( static_cast<float>(billboardTopLeft.x), static_cast<float>(billboardTopLeft.y), static_cast<float>(billboardTopLeft.z) ); tVector3f billboardBottomLeftF( static_cast<float>(billboardBottomLeft.x), static_cast<float>(billboardBottomLeft.y), static_cast<float>(billboardBottomLeft.z) ); tVector3f billboardTopRightF( static_cast<float>(billboardTopRight.x), static_cast<float>(billboardTopRight.y), static_cast<float>(billboardTopRight.z) ); tVector3f billboardBottomRightF( static_cast<float>(billboardBottomRight.x), static_cast<float>(billboardBottomRight.y), static_cast<float>(billboardBottomRight.z) ); SquareTriangleList( billboardTopLeftF, billboardBottomLeftF, billboardTopRightF, billboardBottomRightF, position ); lowestZ = qMin( lowestZ, LowestZOnSquare( billboardTopLeft, billboardBottomLeft, billboardTopRight, billboardBottomRight ) ); vertexCount += 6; } // This portion of code is to keep the billboards from going into the ground. // Throughout the creation of billboards we keep track of the lowest z value // and if it is under zero (land) then you adjust it to stay above ground if ( lowestZ < 0 ) { for ( unsigned int i = 0; i < vertexCount; ++i ) { unsigned int index = startingIndex + i; position[index].z += fabs( static_cast<float>(lowestZ) ); } } } } } bool createNewVertexBuffer = false; if (static_cast<unsigned int>(position.count()) != m_NumVertices) { createNewVertexBuffer = true; } m_NumVertices = position.count(); if( position.isEmpty() == false && texCoords.isEmpty() == false && position.count() == texCoords.count()) { unsigned long positionSize = sizeof(float) * 3 * position.count(); unsigned long texCoordSize = sizeof(float) * 2 * texCoords.count(); boost::shared_ptr<tVertexDescription> xVertexDescription( new tVertexDescription() ); xVertexDescription->AddVertexBufferAttribute( tVertexDescription::eVBAT_Position, tVertexDescription::eVBADT_Float3, 0 ); xVertexDescription->AddVertexBufferAttribute( tVertexDescription::eVBAT_TextureCoord, tVertexDescription::eVBADT_Float2, positionSize ); if (createNewVertexBuffer == true) { boost::shared_ptr<tVertexBuffer> xVertexBuffer = tHardwareBufferFactory::Instance().CreateVertexBuffer( *xVertexDescription, position.count(), tHardwareBuffer::eUsage_StaticWriteOnly, false ); m_xVBAccess.reset( new tVertexBufferAccess(xVertexBuffer, xVertexDescription) ); } m_xVBAccess->Write( tVertexDescription::eVBAT_Position, position.data(), positionSize ); m_xVBAccess->Write( tVertexDescription::eVBAT_TextureCoord, texCoords.data(), texCoordSize ); m_xRenderOperation.reset( new tRenderOperation(tRenderOperation::eRenderType_TriangleList, m_xVBAccess) ); Assert(m_xRenderOperation != 0); return true; } //Assert(0) return false; }