//calculator 3D pos by 2D pos long SmtGLRenderDevice::Transform2DTo3D(Vector3 &vOrg,Vector3 &vTar,const lPoint &point) { int nCursorX = point.x, nCursorY = point.y; GLfloat fWinX = 0,fWinY = 0,fWinZ = 0; GLdouble _x = 0., _y = 0., _z = 0.; GLdouble modelview[16]; GLdouble projection[16]; GLint viewport[4]; glPushMatrix(); glGetIntegerv(GL_VIEWPORT, viewport); // viewport glGetDoublev(GL_MODELVIEW_MATRIX, modelview); // view matrix glGetDoublev(GL_PROJECTION_MATRIX, projection); // projection matrix glPopMatrix(); fWinX = (double)nCursorX; fWinY = (double)viewport[3] - (double)nCursorY - 1; glReadPixels(nCursorX,(int)fWinY,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&fWinZ); if (fWinZ >= 0 && fWinZ < 1.0) { // 获取近裁剪面上的交点 gluUnProject( fWinX, fWinY,0.1, modelview, projection,viewport,&_x,&_y,&_z); vOrg.Set(_x,_y,_z); // 获取远裁剪面上的交点 gluUnProject( fWinX, fWinY,fWinZ, modelview, projection,viewport,&_x,&_y,&_z); vTar.Set(_x,_y,_z); } return SMT_ERR_NONE; }
void GLCamera::GetRayVectors(int iMouseX, int iMouseY, Vector3 & vecRay1, Vector3 & vecRay2){ Vector vec1(4), vec2(4), vecTmp; double pVal[4] = { 0.0, 0.0, 0.0, 1.0 }; vec1.Set(pVal, 4); double centered_x = iMouseX - m_Width/2; double centered_y = (m_Height - iMouseY) - m_Height/2; double unit_x = centered_x/(m_Width/2); double unit_y = centered_y/(m_Height/2); double dAspect = double( m_Width ) / double( m_Height ); double dVerticalAngle = m_ViewAngle/2; if(dAspect>1.0) dVerticalAngle /= dAspect; double full_height = tan(dVerticalAngle * PI / 180.0); double pVal2[4] = { unit_x * full_height * dAspect, unit_y * full_height, -1.0, 1.0 }; vec2.Set(pVal2, 4); // Now transform the points into world coordinates Matrix matTransform = m_ref.GetHMatrix(); vecTmp.Set(matTransform.Mult(vec1)); vecRay1.Set(vecTmp.At(0), vecTmp.At(1), vecTmp.At(2)); //std::cout << "Origin: " << vecTmp.At(0) << ", " << vecTmp.At(1) << ", " << vecTmp.At(2) << ", " << vecTmp.At(3) << std::endl; vecTmp.Set(matTransform.Mult(vec2)); vecRay2.Set(vecTmp.At(0), vecTmp.At(1), vecTmp.At(2)); //std::cout << "Vector: " << vecTmp.At(0) << ", " << vecTmp.At(1) << ", " << vecTmp.At(2) << ", " << vecTmp.At(3) << std::endl; }
void GLCamera::Move(float dx, float dy, float dz){ //if(m_hold) // m_ref = m_holdRef; Vector3 shift; Vector3 absShift; Vector3 relPos; Vector3 up(0,0,1); switch(m_Mode){ case CAMMODE_FreeMove: shift.Set(0,0,dz); shift *= 1/50.0; m_orient.Mult(shift,absShift); relPos = m_lookAtPoint - m_position; if((relPos+absShift).Dot(m_orient.GetColumn(2))<-1.0){ m_lookAtPoint += absShift; }else{ m_lookAtPoint = m_position - Vector3(relPos.Norm()*m_orient(0,2), relPos.Norm()*m_orient(1,2), relPos.Norm()*m_orient(2,2)); } shift.Set(dx,dy,0); shift *= 1/50.0; m_orient.Mult(shift,absShift); m_lookAtPoint += absShift; relPos = m_position-m_lookAtPoint; m_orient.SetColumn(relPos,2); m_orient.SetColumn(up,1); m_orient.Normalize(); break; case CAMMODE_Centered: relPos = m_position-m_lookAtPoint; float norm = relPos.Norm(); float origNorm = norm; shift.Set(dx,dy,0); shift *= norm /100.0; m_orient.Mult(shift,absShift); relPos += absShift; if(dz!=0.0){ norm += 0.05*dz/norm; norm = MAX(m_closeupDistance,fabs(norm)); relPos *= norm/origNorm; }else{ relPos *= origNorm/relPos.Norm(); } //m_position *= norm/origNorm; m_position = m_lookAtPoint + relPos; m_orient.SetColumn(relPos,2); m_orient.SetColumn(up,1); m_orient.Normalize(); break; } }
TEST(engine_test_math_vector3, Set) { float x = 10; float y = 5; float z = 9; Vector3 vector; vector.x = x; vector.y = y; vector.z = z; EXPECT_EQ(x, vector.x); EXPECT_EQ(y, vector.y); EXPECT_EQ(z, vector.z); x++; y--; z++; vector.Set(x, y, z); EXPECT_EQ(x, vector.x); EXPECT_EQ(y, vector.y); EXPECT_EQ(z, vector.z); }
Vector3 Vector3::CrossProduct(const Vector3 &vec) const { Vector3 result; result.Set(y*vec.z - z*vec.y, z*vec.x - x*vec.z, x*vec.y - y*vec.x); return result; }
void SceneManagerCMPlay::TANK_NODE(CEntity* theTank) { //**********// //Warrior // //**********// if (theTank->GetActive()) { Mesh* drawMesh = resourceManager.retrieveMesh("WARRIOR_OBJ"); drawMesh->textureID = resourceManager.retrieveTexture("WARRIOR"); if (sceneGraph->GetChildNode(theTank->GetID())->GetGameObject()->getMesh() != drawMesh) { sceneGraph->GetChildNode(theTank->GetID())->GetGameObject()->setMesh(drawMesh); } } else { Mesh* drawMesh = resourceManager.retrieveMesh("WARRIOR_OBJ"); drawMesh->textureID = resourceManager.retrieveTexture("D_WARRIOR"); if (sceneGraph->GetChildNode(theTank->GetID())->GetGameObject()->getMesh() != drawMesh) { sceneGraph->GetChildNode(theTank->GetID())->GetGameObject()->setMesh(drawMesh); } } sceneGraph->GetChildNode(theTank->GetID())->GetGameObject()->setPosition(theTank->GetPosition()); sceneGraph->GetChildNode(theTank->GetID())->GetGameObject()->setRotation(theTank->GetRotation(), 0, 1, 0); string IDPlus = theTank->GetID(); IDPlus += SWORD; IDPlus += theTank->GetID().back(); //sceneGraph->GetChildNode(IDPlus)->GetGameObject()->setPosition(Vector3(0, 0, -5)); Vector3 Temp; Temp.Set(0, theTank->GetChildTranslation(CHILD_1) + 1, -4.5); sceneGraph->GetChildNode(IDPlus)->GetGameObject()->setPosition(Temp); sceneGraph->GetChildNode(IDPlus)->GetGameObject()->setRotation(theTank->GetChildRotation(CHILD_1), 0, 0, 1); /*IDPlus = theTank->GetID(); IDPlus += SHIELD; IDPlus += theTank->GetID().back();*/ IDPlus = theTank->GetID(); IDPlus += SHIELD_PIVOT; IDPlus += theTank->GetID().back(); sceneGraph->GetChildNode(IDPlus)->GetGameObject()->setPosition(Vector3(0, 0, 0)); sceneGraph->GetChildNode(IDPlus)->GetGameObject()->setRotation(theTank->GetChildRotation(CHILD_2), 0, 1, 0); IDPlus = theTank->GetID(); IDPlus += SHIELD; IDPlus += theTank->GetID().back(); sceneGraph->GetChildNode(IDPlus)->GetGameObject()->setPosition(Vector3(0, 0, 4.5)); sceneGraph->GetChildNode(IDPlus)->GetGameObject()->setRotation(90, 0, 1, 0); }
void Camera::Move( const float x, const float y, const float z) { Vector3 translation; translation.Set(x,y,z); translation = transform.rotate.MatrixY() * translation; Translate(translation); }
AxisAngle<T> RotationMatrix3<T>::GetAxisAngle( ) const { Angle angle = ArcCos( (Trace() - 1.) / 2. ); Vector3<T> axis; if ( angle.Radians() <= 0. ) axis = Vector3<T>::UnitX; //arbitrary: there is no rotation. else if ( angle.Radians() < M_PI ) { axis.Set( m_matrix( 2, 1 ) - m_matrix( 1, 2 ), m_matrix( 0, 2 ) - m_matrix( 2, 0 ), m_matrix( 1, 0 ) - m_matrix( 0, 1 ) ); axis.Normalize( ); } else { int i0 = 0; int i1 = 1; int i2 = 2; T maxD = m_matrix( 0, 0 ); if ( m_matrix( 1, 1 ) > maxD ) { i0 = 1; i1 = 0; maxD = m_matrix( 1, 1 ); } if ( m_matrix( 2, 2 ) > maxD ) { i0 = 2; i1 = 0; i2 = 1; } T axisComps[3]; axisComps[i0] = static_cast<T>( 0.5 ) * std::sqrt( m_matrix( i0, i0 ) - m_matrix( i1, i1 ) - m_matrix( i2, i2 ) + static_cast<T>( 1. ) ); axisComps[i1] = m_matrix( i1, i0 ) / (static_cast<T>( 2. ) * axisComps[i0]); axisComps[i2] = m_matrix( i1, i0 ) / (static_cast<T>( 2. ) * axisComps[i0]); axis.Set( axisComps[0], axisComps[1], axisComps[2] ); } return AxisAngle<T>( axis, angle ); }
void Polygon3::CalculateCenterPoint(Vector3 & center) const { center.Set(0.0f, 0.0f, 0.0f); for (int p = 0; p < pointCount; ++p) { center += points[p]; } center /= (float32)pointCount; }
void TextureText::AddLetterToMesh(float x1, float y1, float x2, float y2, float u1, float v1, float u2, float v2) { int startIDX = mVertices.size(); if(mStartingGeometryIndexOfLastWord > startIDX) mStartingGeometryIndexOfLastWord = startIDX; Vector3 pos; pos.Set(x1,y1,0); mVertices.push_back(pos); pos.Set(x2,y1,0); mVertices.push_back(pos); pos.Set(x2,y2,0); mVertices.push_back(pos); pos.Set(x1,y2,0); mVertices.push_back(pos); Color c = mColor;//(1.0f,1.0f,1.0f,1.0f); mColors.push_back(c); mColors.push_back(c); mColors.push_back(c); mColors.push_back(c); Vector2 uv; uv.Set(u1,v1); mUVs.push_back(uv); uv.Set(u2,v1); mUVs.push_back(uv); uv.Set(u2,v2); mUVs.push_back(uv); uv.Set(u1,v2); mUVs.push_back(uv); mIndices.push_back(startIDX + 0); mIndices.push_back(startIDX + 2); mIndices.push_back(startIDX + 3); mIndices.push_back(startIDX + 0); mIndices.push_back(startIDX + 1); mIndices.push_back(startIDX + 2); }
Vector3 Grid::GetPosition(const Vector2& index) { Vector3 pos; pos.Set(index.x, index.y, 0); pos.x += 0.5f; pos.y += 0.5f; pos.x /= NumberOfTilesX; pos.x *= GridWidth; pos.y /= NumberOfTilesY; pos.y *= GridHeight; return pos; }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// bool SetVector3FromLua(const LuaPlus::LuaObject &dirData, Vector3 &direction) { if(!dirData.IsTable()) { return (false); } LuaPlus::LuaObject xData = dirData["x"]; LuaPlus::LuaObject yData = dirData["y"]; LuaPlus::LuaObject zData = dirData["z"]; if(!xData.IsNumber() || !yData.IsNumber() || !zData.IsNumber()) { return (false); } F32 x = static_cast<F32>(xData.GetNumber()); F32 y = static_cast<F32>(yData.GetNumber()); F32 z = static_cast<F32>(zData.GetNumber()); direction.Set(x, y, z); return (true); }
void CPathing::setWayPoints(int amount, ...) { Vector3 temp; va_list wayPoints; va_start(wayPoints, amount); for (short itr = 0; itr < amount; ++itr) { //Set Point to current Parameter; int Point = va_arg(wayPoints, int); //Loop through column for (unsigned i = MainScene::GetInstance()->ML_map.map_height - 1; i > 0; --i) { for (unsigned j = 0; j < MainScene::GetInstance()->ML_map.map_width; ++j) { //Process Tiles if (isdigit(MainScene::GetInstance()->ML_map.map_data[i][j][0])) { if (stoi(MainScene::GetInstance()->ML_map.map_data[i][j]) == Point) { //Get position of waypoint temp.Set(j * MainScene::GetInstance()->ML_map.worldSize*2.f, (MainScene::GetInstance()->ML_map.map_height - i) * MainScene::GetInstance()->ML_map.worldSize*2.f, 0); //Set Way Points WayPointList.push_back(temp); WayPointTileList.push_back(Vector3(static_cast<float>(j), static_cast<float>(i), 0.f)); } } } } } va_end(wayPoints); }
void Exporter::nodeTransform(Matrix33 &rot, Vector3 &trans, INode *node, TimeValue t, bool local) { Matrix3 tm = getNodeTransform(node, t, local); convertMatrix(rot, tm); trans.Set(tm.GetTrans().x, tm.GetTrans().y, tm.GetTrans().z); }
void MVC_Model::loadConfig() { Branch cfg = SONIO::LoadSON(m_configSONFile); const string ROOT_NAME = "ModelConfigContainer"; Vector3 camPos; Vector3 camTar; Vector3 camUp; bool camPersp = true; double camONear = 1.0f; double camOFar = 1000.0f; // Perspective-Specific float camFOV = 45; if (cfg.name == ROOT_NAME) { for (vector<Attribute>::iterator attrib = cfg.attributes.begin(); attrib != cfg.attributes.end(); ++attrib) { if (attrib->name == "BackgroundColor") { vector<float> col = StringToFloats(attrib->value); m_bgColor.Set(col[0], col[1], col[2]); } // Fog else if (attrib->name == "FogStart") { m_fog.start = stof(attrib->value); } else if (attrib->name == "FogEnd") { m_fog.end = stof(attrib->value); } else if (attrib->name == "FogStart") { m_fog.start = stof(attrib->value); } else if (attrib->name == "FogDensity") { m_fog.density = stof(attrib->value); } else if (attrib->name == "FogType") { m_fog.SetType(stoi(attrib->value)); } else if (attrib->name == "FogColor") { vector<float> col = StringToFloats(attrib->value); m_fog.col.Set(col[0], col[1], col[2]); } else if (attrib->name == "FogEnabled") { m_fog.enabled = ReadTextualBool(attrib->value); } // Camera else if (attrib->name == "CameraType") { if (attrib->value == "Camera3") { m_camera = new Camera3; } else if (attrib->value == "TerrainCamera") { m_camera = new TerrainCamera; } #ifdef _DEBUG else { std::cout << attrib->value << " is not a valid Camera Type!" << std::endl; } #endif } else if (attrib->name == "CameraMode") { if (attrib->value == "Perspective") { camPersp = true; } else if (attrib->value == "Orthographic") { camPersp = false; } #ifdef _DEBUG else { std::cout << attrib->value << " is not a valid Camera Mode!" << std::endl; } #endif } else if (attrib->name == "CameraPosition") { vector<float> pos = StringToFloats(attrib->value); camPos.Set(pos[0], pos[1], pos[2]); } else if (attrib->name == "CameraTarget") { vector<float> tar = StringToFloats(attrib->value); camTar.Set(tar[0], tar[1], tar[2]); } else if (attrib->name == "CameraUp") { vector<float> up = StringToFloats(attrib->value); camUp.Set(up[0], up[1], up[2]); } else if (attrib->name == "CameraFOV") { camFOV = stof(attrib->value); } else if (attrib->name == "CameraNear") { camONear = stof(attrib->value); } else if (attrib->name == "CameraFar") { camOFar = stof(attrib->value); } // Resource else if (attrib->name == "MeshSONFile") { m_meshSONFile = attrib->value; } else if (attrib->name == "TextureSONFile") { m_texSONFile = attrib->value; } else if (attrib->name == "MaterialSONFile") { m_materialSONFile = attrib->value; } else if (attrib->name == "ColorSONFile") { m_colorSONFile = attrib->value; } else if (attrib->name == "GameObjectSONFile") { m_goSONFile = attrib->value; } else if (attrib->name == "VisualObjectSONFile") { m_voSONFile = attrib->value; } else if (attrib->name == "LightSONFile") { m_lightSONFile = attrib->value; } else if (attrib->name == "SoundSONFile") { m_soundSONFile = attrib->value; } // Fonts else if (attrib->name == "DefaultFontMesh") { m_fontName = attrib->value; } // World else if (attrib->name == "WorldSize") { vector<float> size = StringToFloats(attrib->value); m_worldSize.Set(size[0], size[1], size[2]); } // Game Resolution else if (attrib->name == "ViewWidth") { m_viewWidth = stoi(attrib->value); } else if (attrib->name == "ViewHeight") { m_viewHeight = stoi(attrib->value); } // Other else if (attrib->name == "MouseFree") { m_bFreeMouse = ReadTextualBool(attrib->value); } } m_camera->Init(camPos, camTar, camUp, camFOV); m_camera->SetPerspective(camPersp); m_camera->SetNear(camONear); m_camera->SetFar(camOFar); } }
void Camera::Translate( const float x, const float y, const float z) { Vector3 translate; translate.Set(x, y, z); Translate(translate); }
bool SpatialPartitionManager::addNode(SceneNode* node, SpatialPartitionManager::PARTITION_TYPE type) { Vector3 nodeBox; Vector3 boxMidpoint; // only add the node if it has a body (gameObject) if (node->GetGameObject() != NULL) { switch (type) { case SpatialPartitionManager::PARTITION_2D: break; case SpatialPartitionManager::PARTITION_3D: { nodeBox.Set(node->GetGameObject()->getHitbox().getLength() / 2, node->GetGameObject()->getHitbox().getHeight() / 2, node->GetGameObject()->getHitbox().getDepth() / 2); SceneNode* tempNode = node; boxMidpoint = node->GetGameObject()->getPosition(); if (node->parentNode != NULL && node->parentNode->GetGameObject() != NULL) { Vector3 offset = node->parentNode->GetGameObject()->getPosition() + node->GetGameObject()->getPosition(); node->GetGameObject()->updateHitbox(offset); } else { node->GetGameObject()->updateHitbox(); } // calculate the index which the obj is in the world Vector3 topLeftBack, topLeftFront; Vector3 topRightBack, topRightFront; Vector3 bottomLeftBack, bottomLeftFront; Vector3 bottomRightBack, bottomRightFront; unsigned partitionIndex = 0; topLeftBack.Set((boxMidpoint.x - nodeBox.x - minWorldDimension.x) / partitionDimension.x, (boxMidpoint.y + nodeBox.y - minWorldDimension.y) / partitionDimension.y, (boxMidpoint.z - nodeBox.z - minWorldDimension.z) / partitionDimension.z); topLeftFront.Set((boxMidpoint.x - nodeBox.x - minWorldDimension.x) / partitionDimension.x, (boxMidpoint.y + nodeBox.y - minWorldDimension.y) / partitionDimension.y, (boxMidpoint.z + nodeBox.z - minWorldDimension.z) / partitionDimension.z); topRightBack.Set((boxMidpoint.x + nodeBox.x - minWorldDimension.x) / partitionDimension.x, (boxMidpoint.y + nodeBox.y - minWorldDimension.y) / partitionDimension.y, (boxMidpoint.z - nodeBox.z - minWorldDimension.z) / partitionDimension.z); topRightFront.Set((boxMidpoint.x + nodeBox.x - minWorldDimension.x) / partitionDimension.x, (boxMidpoint.y + nodeBox.y - minWorldDimension.y) / partitionDimension.y, (boxMidpoint.z + nodeBox.z - minWorldDimension.z) / partitionDimension.z); bottomLeftBack.Set((boxMidpoint.x - nodeBox.x - minWorldDimension.x) / partitionDimension.x, (boxMidpoint.y - nodeBox.y - minWorldDimension.y) / partitionDimension.y, (boxMidpoint.z - nodeBox.z - minWorldDimension.z) / partitionDimension.z); bottomLeftFront.Set((boxMidpoint.x - nodeBox.x - minWorldDimension.x) / partitionDimension.x, (boxMidpoint.y - nodeBox.y - minWorldDimension.y) / partitionDimension.y, (boxMidpoint.z + nodeBox.z - minWorldDimension.z) / partitionDimension.z); bottomRightBack.Set((boxMidpoint.x + nodeBox.x - minWorldDimension.x) / partitionDimension.x, (boxMidpoint.y - nodeBox.y - minWorldDimension.y) / partitionDimension.y, (boxMidpoint.z - nodeBox.z - minWorldDimension.z) / partitionDimension.z); bottomRightFront.Set((boxMidpoint.x + nodeBox.x - minWorldDimension.x) / partitionDimension.x, (boxMidpoint.y - nodeBox.y - minWorldDimension.y) / partitionDimension.y, (boxMidpoint.z + nodeBox.z - minWorldDimension.z) / partitionDimension.z); // check top left back partitionIndex = generatePartitionIndex(topLeftBack); if (partitionIndex > 0 && partitionIndex < partitions.size()) { partitions[partitionIndex]->addNode(node); } // check top left front partitionIndex = generatePartitionIndex(topLeftFront); if (partitionIndex > 0 && partitionIndex < partitions.size()) { partitions[partitionIndex]->addNode(node); } // check top right back partitionIndex = generatePartitionIndex(topRightBack); if (partitionIndex > 0 && partitionIndex < partitions.size()) { partitions[partitionIndex]->addNode(node); } // check top right front partitionIndex = generatePartitionIndex(topRightFront); if (partitionIndex > 0 && partitionIndex < partitions.size()) { partitions[partitionIndex]->addNode(node); } // check bottom left back partitionIndex = generatePartitionIndex(bottomLeftBack); if (partitionIndex > 0 && partitionIndex < partitions.size()) { partitions[partitionIndex]->addNode(node); } // check bottom left front partitionIndex = generatePartitionIndex(bottomLeftFront); if (partitionIndex > 0 && partitionIndex < partitions.size()) { partitions[partitionIndex]->addNode(node); } // check bottom right back partitionIndex = generatePartitionIndex(bottomRightBack); if (partitionIndex > 0 && partitionIndex < partitions.size()) { partitions[partitionIndex]->addNode(node); } // check bottom right front partitionIndex = generatePartitionIndex(bottomRightFront); if (partitionIndex > 0 && partitionIndex < partitions.size()) { partitions[partitionIndex]->addNode(node); } } break; default: break; } } // do a reursive for (unsigned i = 0; i < node->childNodes.size(); ++i) { this->addNode(node->childNodes[i], type); } return true; }
void Matrix34::Transform3x3(const Vector3 &in,Vector3 &out) const { float x=in.x*a.x + in.y*b.x + in.z*c.x; float y=in.x*a.y + in.y*b.y + in.z*c.y; float z=in.x*a.z + in.y*b.z + in.z*c.z; out.Set(x,y,z); }
// Spawn this object based on ObjectName // Returns true if successfully spawned void GameObject::LoadFromIniSection( const std::string& inObjectName, const minIni& inIni ) { ( void ) inObjectName; ( void ) inIni; //lab 3 //implement Vector3 position; Quaternion rotation; float scale; mObjectName = inObjectName; // Add mesh string defaultMeshFileName = "cube.itpmesh"; MeshComponentPtr mesh(new MeshComponent( MeshManager::Get(). GetMesh(inIni.gets(inObjectName, "Mesh", defaultMeshFileName)))); // Obtain position string positionString = inIni.gets(mObjectName, "Position", "(0,0,0)").c_str(); float positionX = 0; float positionY = 0; float positionZ = 0; sscanf(positionString.c_str(), "(%f, %f, %f)", &positionX, &positionY, &positionZ); position.Set(positionX, positionY, positionZ); // Obtain Rotation string rotationString = inIni.gets(mObjectName, "Rotation", "(0,0,0)").c_str(); float rotationX = 0; float rotationY = 0; float rotationZ = 0; sscanf(rotationString.c_str(), "(%f, %f, %f)", &rotationX, &rotationY, &rotationZ); rotation = DirectX::XMQuaternionRotationRollPitchYaw(rotationX, rotationY, rotationZ); // Obtain scale string scaleString = inIni.gets(mObjectName, "Scale", "1.0").c_str(); sscanf(scaleString.c_str(), "%f", &scale); // Obtain texture string textureFileName = inIni.gets(mObjectName, "Texture", ""); if (textureFileName != "") { wstring wTextureFileName = L"Textures\\" + wstring(textureFileName.begin(), textureFileName.end()); // convert to wstring TexturePtr texturePtr = GraphicsDriver::Get()->CreateTextureFromFile(wTextureFileName.c_str()); // pass in wchar array mesh->SetTexture(texturePtr); // set texture } // Obtain animation and add animation if available string animationString = inIni.gets(mObjectName, "Animation", ""); if (animationString.length() > 0) { animationString = "Animations\\" + inIni.gets(mObjectName, "Animation", ""); AnimComponentPtr anim(new AnimComponent(animationString.c_str())); AddAnimComponent(anim); mesh->SetAnimation(anim); } // Apply transformations and animation to mesh mesh->SetTranslation(position); mesh->SetRotation(rotation); mesh->SetScale(scale); // Add the mesh to the gameobject AddComponent(mesh); }
Vector3 AlignedBox::ClosestCorner( const Vector3& v ) const { Vector3 winner; f32 winnerDist = v.LengthSquared(); Vector3 current; f32 currentDist; current.Set(maximum.x, maximum.y, maximum.z); currentDist = (current - v).LengthSquared(); if ( currentDist < winnerDist ) { winner = current; winnerDist = currentDist; } current.Set(maximum.x, minimum.y, maximum.z); currentDist = (current - v).LengthSquared(); if ( currentDist < winnerDist ) { winner = current; winnerDist = currentDist; } current.Set(minimum.x, minimum.y, maximum.z); currentDist = (current - v).LengthSquared(); if ( currentDist < winnerDist ) { winner = current; winnerDist = currentDist; } current.Set(minimum.x, maximum.y, maximum.z); currentDist = (current - v).LengthSquared(); if ( currentDist < winnerDist ) { winner = current; winnerDist = currentDist; } current.Set(maximum.x, maximum.y, minimum.z); currentDist = (current - v).LengthSquared(); if ( currentDist < winnerDist ) { winner = current; winnerDist = currentDist; } current.Set(maximum.x, minimum.y, minimum.z); currentDist = (current - v).LengthSquared(); if ( currentDist < winnerDist ) { winner = current; winnerDist = currentDist; } current.Set(minimum.x, minimum.y, minimum.z); currentDist = (current - v).LengthSquared(); if ( currentDist < winnerDist ) { winner = current; winnerDist = currentDist; } current.Set(minimum.x, maximum.y, minimum.z); currentDist = (current - v).LengthSquared(); if ( currentDist < winnerDist ) { winner = current; winnerDist = currentDist; } return winner; }
void ObjectRenderer::AddShapeFromRay(unsigned uShapeIndex, Vector3 vecRay1, Vector3 vecRay2, double dFactor, double dSphereScale){ //std::cout << "Adding Sphere on the object's surface. Base point is " << vecRay1.x() << ", " << vecRay1.y() << ", " << vecRay1.z() << ". Vector is " << vecRay2.x() << ", " << vecRay2.y() << ", " << vecRay2.z() << ". Factor is " << dFactor << std::endl; // Compute intersection point Vector3 vecIntersection = vecRay1 + (vecRay2 - vecRay1)*dFactor; //std::cout << " Coordinates: " << vecIntersection.x() << ", " << vecIntersection.y() << ", " << vecIntersection.z() << std::endl; // Convert from absolute coordinates to coordinates respective to the object's referential Matrix matTmp = mObject->GetReferenceFrame().GetInverse().GetHMatrix(); double pIntersectionH[4] = { vecIntersection.x(), vecIntersection.y(), vecIntersection.z(), 1.0 }; Vector vecIntersectionH(pIntersectionH, 4); Vector vecTmp = matTmp.Mult(vecIntersectionH); vecIntersection.Set(vecTmp); //std::cout << "Transformed Coordinates: " << vecIntersection.x() << ", " << vecIntersection.y() << ", " << vecIntersection.z() << std::endl; // Create sphere shape pShapeStruct pNewShape = new ShapeStruct; pNewShape->strShapeName = mStrIntersectsShapeName; pNewShape->color[0] = 1.0; pNewShape->color[1] = 0.0; pNewShape->color[2] = 0.0; pNewShape->color[3] = 1.0; pNewShape->refShape.SetOrigin(vecIntersection); double dScale = 0.02; double pScaleData[3] = { dScale, dScale, dScale }; Matrix3 scale; scale.Diag(Vector3(pScaleData)); pNewShape->shape = new GL3DObject(); pNewShape->shape->GenerateSphere(16,16); pNewShape->shape->Transform(scale); pNewShape->shape->Transform(pNewShape->refShape.GetHMatrix()); mShapes.push_back(pNewShape); if(mObject){ pXmlTree conf = mObject->GetConfigTree(); pXmlTree ptList = conf->Find("PointList"); if(ptList==NULL){ ptList = new XmlTree("PointList"); conf->AddSubTree(ptList); } // Apply scale transform if any if(mShapes[uShapeIndex]->scale[0]!=1.0 || mShapes[uShapeIndex]->scale[1]!=1.0 || mShapes[uShapeIndex]->scale[2]!=1.0){ Matrix3 matScale; matScale.Diag(Vector3(mShapes[uShapeIndex]->scale[0])); matScale.RefNoCheck(0,0) = 1/mShapes[uShapeIndex]->scale[0]; matScale.RefNoCheck(1,1) = 1/mShapes[uShapeIndex]->scale[1]; matScale.RefNoCheck(2,2) = 1/mShapes[uShapeIndex]->scale[2]; Vector3 vecTmp = matScale.Mult(vecIntersection); vecIntersection = vecTmp; //std::cout << "Scaled Coordinates: " << vecIntersection.x() << ", " << vecIntersection.y() << ", " << vecIntersection.z() << std::endl; } char txt[256]; sprintf(txt,"%f %f %f",vecIntersection.x(),vecIntersection.y(),vecIntersection.z()); ptList->AddSubTree(new XmlTree("Point",txt)); //conf->Print(); } }