void FCE_AxisAlignedBoundingBox::AddPoints( UINT count, const D3DXVECTOR3* pPoint ) { if ( count == 0) { return; } if( isNull ){ isNull = false; minPos = maxPos = pPoint[0]; } for( UINT iPt = 1; iPt < count; ++iPt ){ D3DXVec3Minimize( &minPos, &(pPoint[iPt]), &minPos ); D3DXVec3Maximize( &maxPos, &(pPoint[iPt]), &maxPos ); } }
void Cone3D::buildTexCoords(IDirect3DDevice9* gd3dDevice) { D3DVERTEXELEMENT9 elements[64]; UINT numElements = 0; VertexPos::Decl->GetDeclaration(elements, &numElements); ID3DXMesh* temp = 0; HR(mpMesh->CloneMesh(D3DXMESH_SYSTEMMEM, elements, gd3dDevice, &temp)); ReleaseCOM(mpMesh); // Now generate texture coordinates for each vertex. VertexPos* vertices = 0; HR(temp->LockVertexBuffer(0, (void**)&vertices)); D3DXVECTOR3 maxPoint(-FLT_MAX, -FLT_MAX, -FLT_MAX); D3DXVECTOR3 minPoint(FLT_MAX, FLT_MAX, FLT_MAX); for (UINT i = 0; i < temp->GetNumVertices(); ++i) { D3DXVec3Maximize(&maxPoint, &maxPoint, &vertices[i].mPos); D3DXVec3Minimize(&minPoint, &minPoint, &vertices[i].mPos); } float a = minPoint.z; float b = maxPoint.z; float h = b - a; for (int i = 0; i < (int)temp->GetNumVertices(); i++) { float x = vertices[i].mPos.x; float y = vertices[i].mPos.z; float z = vertices[i].mPos.y; float theta = atan2f(z, x); float y2 = y - b; float u = theta / (2.0f * D3DX_PI); float v = y2 / -h; vertices[i].mTexCoord.x = u; vertices[i].mTexCoord.y = v; } HR(temp->UnlockVertexBuffer()); HR(temp->CloneMesh(D3DXMESH_MANAGED | D3DXMESH_WRITEONLY, elements, gd3dDevice, &mpMesh)); ReleaseCOM(temp); }
void Collider_Sphere::MinMaxVertices(std::vector<D3DXVECTOR3> & vertexList, const D3DXMATRIXA16* matRot /*= nullptr*/) { D3DXVECTOR3 vecComp; for (auto iter = vertexList.begin( ); iter != vertexList.end( ); ++iter) { if (matRot != nullptr) { D3DXVec3TransformNormal(&vecComp, &*iter, &*matRot); } else { vecComp = (*iter); } D3DXVec3Maximize(&vecMax, &vecComp, &vecMax); D3DXVec3Minimize(&vecMin, &vecComp, &vecMin); } }
BoundingBox* ActionBox::getBoundingBox() { static BoundingBox *retVal = NULL; Vec3 bbmin = Vec3(9999999999, 99999999999, 99999999999); Vec3 bbmax = Vec3(-9999999999, -9999999999, -9999999999); Vec3 min1 = m_min; Vec3 max1 = m_max; Vec3 min2 = m_min; Vec3 max2 = m_max; min2.z = max1.z; min2.y = max1.y; max2.z = min1.z; max2.y = min1.y; Mat tempMat = m_world; /*tempMat._41 = 0; tempMat._42 = 0; tempMat._43 = 0;*/ D3DXVec3TransformCoord(&min1, &min1, &tempMat); D3DXVec3TransformCoord(&min2, &min2, &tempMat); D3DXVec3TransformCoord(&max1, &max1, &tempMat); D3DXVec3TransformCoord(&max2, &max2, &tempMat); /*min1 += m_position; min2 += m_position; max1 += m_position; max2 += m_position;*/ D3DXVec3Minimize(&bbmin, &bbmin, &min1); D3DXVec3Minimize(&bbmin, &bbmin, &min2); D3DXVec3Minimize(&bbmin, &bbmin, &max1); D3DXVec3Minimize(&bbmin, &bbmin, &max2); D3DXVec3Maximize(&bbmax, &bbmax, &min1); D3DXVec3Maximize(&bbmax, &bbmax, &min2); D3DXVec3Maximize(&bbmax, &bbmax, &max1); D3DXVec3Maximize(&bbmax, &bbmax, &max2); /*if(bbmin.x > min1.x) bbmin.x = min1.x; if(bbmin.x > min2.x) bbmin.x = min2.x; if(bbmin.y > min1.y) bbmin.y = min1.y; if(bbmin.y > min2.y) bbmin.y = min2.y; if(bbmin.z > min1.z) bbmin.z = min1.z; if(bbmin.z > min2.z) bbmin.z = min2.z; if(bbmax.x < max1.x) bbmax.x = max1.x; if(bbmax.x < max2.x) bbmax.x = max2.x; if(bbmax.y < max1.y) bbmax.y = max1.y; if(bbmax.y < max2.y) bbmax.y = max2.y; if(bbmax.z < max1.z) bbmax.z = max1.z; if(bbmax.z < max2.z) bbmax.z = max2.z;*/ /* Vec3 p0 = m_min; Vec3 p1 = m_max; Vec3 p2 = Vec3(m_max.x, 0, m_min.z); Vec3 p2_2 = Vec3(m_min.x, 0, m_max.z); Vec3 min1, max1; min1 = m_min; max1 = m_max; min1.y = m_max.y; D3DXVec3TransformCoord(&p0, &p0, &m_world); D3DXVec3TransformCoord(&p1, &p1, &m_world); D3DXVec3TransformCoord(&p2, &p2, &m_world); D3DXVec3TransformCoord(&p2_2, &p2_2, &m_world); Vec3 tempMin = p0; Vec3 tempMax = p0; if(tempMin.x > p1.x) tempMin.x = p1.x; if(tempMin.x > p2.x) tempMin.x = p2.x; if(tempMin.x > p2_2.x) tempMin.x = p2_2.x; if(tempMin.z > p1.z) tempMin.z = p1.z; if(tempMin.z > p2.z) tempMin.z = p2.z; if(tempMin.z > p2_2.z) tempMin.z = p2_2.z; if(tempMax.x < p1.x) tempMax.x = p1.x; if(tempMax.x < p2.x) tempMax.x = p2.x; if(tempMax.x < p2_2.x) tempMax.x = p2_2.x; if(tempMax.z < p1.z) tempMax.z = p1.z; if(tempMax.z < p2.z) tempMax.z = p2.z; if(tempMax.z < p2_2.z) tempMax.z = p2_2.z; */ m_boundingBox->reset(bbmin, bbmax); return m_boundingBox; }
void CascadedShadowMap::CalcShadowMapMatrices(D3DXVECTOR3 sunLight, Camera* cam, int i) { //calculate points (in world space) for the frustum slice D3DXVECTOR4 camPos = D3DXVECTOR4(cam->GetPositionD3DX(), 1.0f); D3DXVECTOR4 camForward = D3DXVECTOR4(cam->GetForwardD3DX(), 1.0f); D3DXVECTOR4 camRight = D3DXVECTOR4(cam->GetRightVectorD3DX(), 1.0f); D3DXVECTOR4 camUp = D3DXVECTOR4(cam->GetUpVectorD3DX(), 1.0f); float ww = (float)this->params.windowWidth; float hh = (float)this->params.windowHeight; float aspectRatio = ww / hh; float tmp = tan(this->params.FOV) * 2; //Near plane float halfNearHeight = tmp * this->shadowMappingSplitDepths[i]; float halfNearWidth = halfNearHeight * aspectRatio; //Near points D3DXVECTOR4 nearCenter = camPos + camForward * this->shadowMappingSplitDepths[i]; //near plane of slice D3DXVECTOR4 nearTopLeft = nearCenter + (camUp * halfNearHeight) - (camRight * halfNearWidth); D3DXVECTOR4 nearTopRight = nearCenter + (camUp * halfNearHeight) + (camRight * halfNearWidth); D3DXVECTOR4 nearBottomLeft = nearCenter - (camUp * halfNearHeight) - (camRight * halfNearWidth); D3DXVECTOR4 nearBottomRight = nearCenter - (camUp * halfNearHeight) + (camRight * halfNearWidth); //Far plane float halfFarHeight = tmp * this->shadowMappingSplitDepths[i + 1]; float halfFarWidth = halfFarHeight * aspectRatio; //Far points D3DXVECTOR4 farCenter = camPos + camForward * this->shadowMappingSplitDepths[i + 1]; //far plane of slice D3DXVECTOR4 farTopLeft = farCenter + (camUp * halfFarHeight) - (camRight * halfFarWidth); D3DXVECTOR4 farTopRight = farCenter + (camUp * halfFarHeight) + (camRight * halfFarWidth); D3DXVECTOR4 farBottomLeft = farCenter - (camUp * halfFarHeight) - (camRight * halfFarWidth); D3DXVECTOR4 farBottomRight = farCenter - (camUp * halfFarHeight) + (camRight * halfFarWidth); //Put them in an array D3DXVECTOR4 frustumPoints[8] = {nearTopLeft, nearTopRight, nearBottomLeft, nearBottomRight, farTopLeft, farTopRight, farBottomLeft, farBottomRight }; //**tillman, code above should work (liknande kod är testad & fungerar) //Calculate the light's view matrix. D3DXVECTOR3 lightPos = D3DXVECTOR3(-1000, 1000, -1000); //**tillman** D3DXVECTOR3 lightLookAt = lightPos + sunLight; //sunlight = the direction the sun is "looking at". D3DXVECTOR3 lightUp = D3DXVECTOR3(0.0f, 1.0f, 0.0f); D3DXMATRIX lightViewMatrix; D3DXMatrixIdentity(&lightViewMatrix); D3DXMatrixLookAtLH(&lightViewMatrix, &lightPos, &lightLookAt, &lightUp); //Transform the points from world space to light’s homogeneous space. for(int index = 0; index < 8; index++) { //reset W frustumPoints[index].w = 1.0f; D3DXVec4Transform(&frustumPoints[index], &frustumPoints[index], &lightViewMatrix); } //Calculate the min and max values for the orthographic projection.*A* //**tillman, find the near and far plane *A* float infinity = std::numeric_limits<float>::infinity(); D3DXVECTOR3 minValue = D3DXVECTOR3(infinity, infinity, infinity); D3DXVECTOR3 maxValue = D3DXVECTOR3(-infinity, -infinity, -infinity); D3DXVECTOR3 vLightCameraOrthographicMin = D3DXVECTOR3(infinity, infinity, infinity); //**tillman pot opt, samma som minValue D3DXVECTOR3 vLightCameraOrthographicMax = D3DXVECTOR3(-infinity, -infinity, -infinity);//**tillman pot opt, samma som maxValue for(int index = 0; index < 8; index++) { D3DXVECTOR3 vec3 = D3DXVECTOR3(frustumPoints[index].x, frustumPoints[index].y, frustumPoints[index].z); D3DXVec3Minimize(&minValue, &minValue, &vec3); D3DXVec3Maximize(&maxValue, &maxValue, &vec3); // Find the closest point. D3DXVec3Minimize (&vLightCameraOrthographicMin, &vec3, &vLightCameraOrthographicMin ); D3DXVec3Maximize (&vLightCameraOrthographicMax, &vec3, &vLightCameraOrthographicMax ); } D3DXVECTOR3 tmpNearPlanePoint = minValue; tmpNearPlanePoint += sunLight * 100.0f; //set the near plane to be closer to the light to include more potential occluders.//**tillman opt || variablifiera (((i + 1) )) float nearPlane = tmpNearPlanePoint.z; float farPlane = maxValue.z; // Create the orthographic projection for this cascade/frustum slice. D3DXMATRIX lightProjMatrix; D3DXMatrixIdentity(&lightProjMatrix); D3DXMatrixOrthoOffCenterLH( &lightProjMatrix, vLightCameraOrthographicMin.x, vLightCameraOrthographicMax.x, vLightCameraOrthographicMin.y, vLightCameraOrthographicMax.y, nearPlane, farPlane); this->viewProj[i] = lightViewMatrix * lightProjMatrix; /* //**Tillman: old code** //Transform points into light’s homogeneous space. D3DXMATRIX lightViewMatrix; //M D3DXMatrixLookAtLH(&lightViewMatrix, &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &sunLight, &D3DXVECTOR3(0, 1, 0)); //** D3DXMATRIX lightProjMatrix; //P D3DXMatrixIdentity(&lightProjMatrix); //Far D3DXVECTOR4 farTopLeftH = D3DXVECTOR4(farTopLeft.x, farTopLeft.y, farTopLeft.z, 1.0f); D3DXVECTOR4 farTopRightH = D3DXVECTOR4(farTopRight.x, farTopRight.y, farTopRight.z, 1.0f); D3DXVECTOR4 farBottomLeftH = D3DXVECTOR4(farBottomLeft.x, farBottomLeft.y, farBottomLeft.z, 1.0f); D3DXVECTOR4 farBottomRightH = D3DXVECTOR4(farBottomRight.x, farBottomRight.y, farBottomRight.z, 1.0f); //Near D3DXVECTOR4 nearTopLeftH = D3DXVECTOR4(nearTopLeft.x, nearTopLeft.y, nearTopLeft.z, 1.0f); D3DXVECTOR4 nearTopRightH = D3DXVECTOR4(nearTopRight.x, nearTopRight.y, nearTopRight.z, 1.0f); D3DXVECTOR4 nearBottomLeftH = D3DXVECTOR4(nearBottomLeft.x, nearBottomLeft.y, nearBottomLeft.z, 1.0f); D3DXVECTOR4 nearBottomRightH = D3DXVECTOR4(nearBottomRight.x, nearBottomRight.y, nearBottomRight.z, 1.0f); D3DXMATRIX lightViewProjMatrix = lightProjMatrix * lightViewMatrix; //Far D3DXVec4Transform(&farTopLeftH, &farTopLeftH, &lightViewProjMatrix); D3DXVec4Transform(&farTopRightH, &farTopRightH, &lightViewProjMatrix); D3DXVec4Transform(&farBottomLeftH, &farBottomLeftH, &lightViewProjMatrix); D3DXVec4Transform(&farBottomRightH, &farBottomRightH, &lightViewProjMatrix); //Near D3DXVec4Transform(&nearTopLeftH, &nearTopLeftH, &lightViewProjMatrix); D3DXVec4Transform(&nearTopRightH, &nearTopRightH, &lightViewProjMatrix); D3DXVec4Transform(&nearBottomLeftH, &nearBottomLeftH, &lightViewProjMatrix); D3DXVec4Transform(&nearBottomRightH, &nearBottomRightH, &lightViewProjMatrix); //Calculate crop matrix //Find min & max values in each dimension float minX = min(farTopLeftH.x, min(farTopRightH.x, min(farBottomLeftH.x, min(farBottomRightH.x, min(nearTopLeftH.x, min(nearTopRightH.x, min(nearBottomLeftH.x, nearBottomRightH.x))))))); float maxX = max(farTopLeftH.x, max(farTopRightH.x, max(farBottomLeftH.x, max(farBottomRightH.x, max(nearTopLeftH.x, max(nearTopRightH.x, max(nearBottomLeftH.x, nearBottomRightH.x))))))); float minY = min(farTopLeftH.y, min(farTopRightH.y, min(farBottomLeftH.y, min(farBottomRightH.y, min(nearTopLeftH.y, min(nearTopRightH.y, min(nearBottomLeftH.y, nearBottomRightH.y))))))); float maxY = max(farTopLeftH.y, max(farTopRightH.y, max(farBottomLeftH.y, max(farBottomRightH.y, max(nearTopLeftH.y, max(nearTopRightH.y, max(nearBottomLeftH.y, nearBottomRightH.y))))))); float minZ = min(farTopLeftH.z, min(farTopRightH.z, min(farBottomLeftH.z, min(farBottomRightH.z, min(nearTopLeftH.z, min(nearTopRightH.z, min(nearBottomLeftH.z, nearBottomRightH.z))))))); float maxZ = max(farTopLeftH.z, max(farTopRightH.z, max(farBottomLeftH.z, max(farBottomRightH.z, max(nearTopLeftH.z, max(nearTopRightH.z, max(nearBottomLeftH.z, nearBottomRightH.z))))))); //Determine scale float scaleX = 2 / maxX - minX; float scaleY = 2 / maxY - minY; //Determine offset float offsetX = -0.5f * (maxX + minX) * scaleX; float offsetY = -0.5f * (maxY + minY) * scaleY; //Create cropmatrix and projection matrix D3DXMATRIX cropMatrix, lightProjMatrix2; //lightProjMatrix2 (Pz) D3DXMatrixIdentity(&cropMatrix); cropMatrix._11 = scaleX; cropMatrix._22 = scaleY; cropMatrix._41 = offsetX; cropMatrix._42 = offsetY; //D3DXMatrixOrthoLH(&lightProjMatrix2, width, height, minZ, maxZ); //**width? height? //Lastly, modify the projection matrix (P=CPz) lightProjMatrix = cropMatrix * lightProjMatrix2; D3DXMATRIX View; D3DXMatrixLookAtLH(&View, &(cam->GetPositionD3DX() - sunLight), &sunLight, &D3DXVECTOR3(0, 1, 0)); float fNearPlane = this->shadowMappingSplitDepths[i]; float fFarPlane = this->shadowMappingSplitDepths[i + 1]; D3DXMATRIX Proj; float size = pow(5.0f, (i + 1.0f)); D3DXMatrixOrthoLH(&Proj, size, size, fNearPlane, fFarPlane); //D3DXMatrixOrthoOffCenterLH(&Proj, -size, size, -size, size, 0.001f, fFarPlane); this->viewProj[i] = View * Proj; */ /* D3DXMATRIX viewportToTex = D3DXMATRIX( 0.5f, 0.f, 0.f, 0.f, 0.f, -0.5f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.5f, 0.5f, SHADOW_MAP_DEPTH_BIAS, 0.f); outShadowMapTexXform = outViewProj * viewportToTex; */ }
void GraphicsEngineImp::DebugDummyFunction(Vector3* arr) //**tillman** { //Transform the points from world space to light’s homogeneous space. //View matrix D3DXVECTOR3 lookAt = D3DXVECTOR3(0, 0, 0); D3DXVECTOR3 pos = D3DXVECTOR3(-50, 50, -50); D3DXVECTOR3 posToLookAt = lookAt - pos; //Pos --> lookAt D3DXVECTOR3 dir; D3DXVECTOR3 up = D3DXVECTOR3(0, 1, 0); D3DXVec3Normalize(&dir, &posToLookAt); lookAt = pos + dir; D3DXMATRIX lightViewMatrix; D3DXMatrixLookAtLH(&lightViewMatrix, &pos, &lookAt, &up); //for(int i = 0; i < 30; i++) for(int i = 0; i < 24; i++) { D3DXVECTOR4 vertex = D3DXVECTOR4(arr[i].x, arr[i].y, arr[i].z, 1.0f); // Transform the point from world space to Light Camera Space. D3DXVec4Transform(&vertex, &vertex, &lightViewMatrix); arr[i].x = vertex.x; arr[i].y = vertex.y; arr[i].z = vertex.z; } // This next section of code calculates the min and max values for the orthographic projection. D3DXVECTOR3 vLightCameraOrthographicMin = D3DXVECTOR3(9999, 9999, 9999); D3DXVECTOR3 vLightCameraOrthographicMax = D3DXVECTOR3(-9999, -9999, -9999); D3DXVECTOR3 element = D3DXVECTOR3(0, 0, 0); //for(int i = 1; i < 10; i++) for(int i = 0; i < 8; i++) { //if(i != 5) //{ element = D3DXVECTOR3(arr[i].x, arr[i].y, arr[i].z); // Find the closest point. D3DXVec3Minimize (&vLightCameraOrthographicMin, &element, &vLightCameraOrthographicMin ); D3DXVec3Maximize (&vLightCameraOrthographicMax, &element, &vLightCameraOrthographicMax ); //} } D3DXVECTOR3 minValue = D3DXVECTOR3(9999, 9999, 9999); D3DXVECTOR3 maxValue = D3DXVECTOR3(-9999, -9999, -9999); element = maxValue; //for(int index = 1; index < 10; index++) for(int i = 0; i < 8; i++) { //if(index != 5) //{ element = D3DXVECTOR3(arr[i].x, arr[i].y, arr[i].z); D3DXVec3Minimize(&minValue, &minValue, &element); D3DXVec3Maximize(&maxValue, &maxValue, &element); //} } float nearPlane = minValue.z; float farPlane = maxValue.z; //Create orthographics projection. // Craete the orthographic projection for this cascade. D3DXMATRIX lightProjMatrix; D3DXMatrixOrthoOffCenterLH( &lightProjMatrix, vLightCameraOrthographicMin.x, vLightCameraOrthographicMax.x, vLightCameraOrthographicMin.y, vLightCameraOrthographicMax.y, nearPlane, farPlane); //test D3DXMATRIX inverseLightViewMatrix; D3DXMatrixIdentity(&inverseLightViewMatrix); D3DXMatrixInverse(&inverseLightViewMatrix, NULL, &lightViewMatrix); D3DXVECTOR4 minV = D3DXVECTOR4(minValue, 1.0f); D3DXVECTOR4 maxV = D3DXVECTOR4(maxValue, 1.0f); //for(int i = 2; i < 30; i++) for(int i = 0; i < 24; i++) { D3DXVECTOR4 vertex = D3DXVECTOR4(arr[i].x, arr[i].y, arr[i].z, 1.0f); // Transform the point from world space to Light Camera Space. D3DXVec4Transform(&vertex, &vertex, &inverseLightViewMatrix); arr[i].x = vertex.x; arr[i].y = vertex.y; arr[i].z = vertex.z; } D3DXVec4Transform(&minV, &minV, &inverseLightViewMatrix); D3DXVec4Transform(&maxV, &maxV, &inverseLightViewMatrix); ofstream out("Media/CSMDebug.obj"); stringstream buffer; buffer << "# This file uses centimeters as units for non-parametric coordinates. \n" << "mtllib CSMDebug.mtl \n" << "g default \n"; //for(int i = 1; i < 30; i++) for(int i = 0; i < 24; i++) { //if(i % 5 != 0) { buffer << "v " << arr[i].x << " " << arr[i].y << " " << arr[i].z << "\n"; } } //for(int i = 1; i < 30; i++) for(int i = 0; i < 24; i++) { //if(i % 5 != 0) { buffer << "vt 0 0\n"; } } //for(int i = 1; i < 30; i++) for(int i = 0; i < 24; i++) { //if(i % 5 != 0) { buffer << "vn 1 0 0\n"; } } for(int i = 0; i < 3; i++) { buffer << "usemtl initialShadingGroup" + MaloW::convertNrToString(i + 1) + "\n"; string nr1 = MaloW::convertNrToString(i * 8 + 1); string nr2 = MaloW::convertNrToString(i * 8 + 2); string nr3 = MaloW::convertNrToString(i * 8 + 3); buffer << "f " << nr1 << "/" << nr1 << "/" << nr1 << " " << nr2 << "/" << nr2 << "/" << nr2 << " " << nr3 << "/" << nr3 << "/" << nr3 << "\n"; string nr4 = MaloW::convertNrToString(i * 8 + 2); string nr5 = MaloW::convertNrToString(i * 8 + 3); string nr6 = MaloW::convertNrToString(i * 8 + 4); buffer << "f " << nr4 << "/" << nr4 << "/" << nr4 << " " << nr5 << "/" << nr5 << "/" << nr5 << " " << nr6 << "/" << nr6 << "/" << nr6 << "\n"; //TOP nr4 = MaloW::convertNrToString(i * 8 + 1); nr5 = MaloW::convertNrToString(i * 8 + 5); nr6 = MaloW::convertNrToString(i * 8 + 6); buffer << "f " << nr4 << "/" << nr4 << "/" << nr4 << " " << nr5 << "/" << nr5 << "/" << nr5 << " " << nr6 << "/" << nr6 << "/" << nr6 << "\n"; nr4 = MaloW::convertNrToString(i * 8 + 1); nr5 = MaloW::convertNrToString(i * 8 + 6); nr6 = MaloW::convertNrToString(i * 8 + 2); buffer << "f " << nr4 << "/" << nr4 << "/" << nr4 << " " << nr5 << "/" << nr5 << "/" << nr5 << " " << nr6 << "/" << nr6 << "/" << nr6 << "\n"; //BOTTOM nr4 = MaloW::convertNrToString(i * 8 + 3); nr5 = MaloW::convertNrToString(i * 8 + 7); nr6 = MaloW::convertNrToString(i * 8 + 8); buffer << "f " << nr4 << "/" << nr4 << "/" << nr4 << " " << nr5 << "/" << nr5 << "/" << nr5 << " " << nr6 << "/" << nr6 << "/" << nr6 << "\n"; nr4 = MaloW::convertNrToString(i * 8 + 3); nr5 = MaloW::convertNrToString(i * 8 + 8); nr6 = MaloW::convertNrToString(i * 8 + 4); buffer << "f " << nr4 << "/" << nr4 << "/" << nr4 << " " << nr5 << "/" << nr5 << "/" << nr5 << " " << nr6 << "/" << nr6 << "/" << nr6 << "\n"; //LEFT SIDE nr4 = MaloW::convertNrToString(i * 8 + 3); nr5 = MaloW::convertNrToString(i * 8 + 7); nr6 = MaloW::convertNrToString(i * 8 + 5); buffer << "f " << nr4 << "/" << nr4 << "/" << nr4 << " " << nr5 << "/" << nr5 << "/" << nr5 << " " << nr6 << "/" << nr6 << "/" << nr6 << "\n"; nr4 = MaloW::convertNrToString(i * 8 + 3); nr5 = MaloW::convertNrToString(i * 8 + 5); nr6 = MaloW::convertNrToString(i * 8 + 1); buffer << "f " << nr4 << "/" << nr4 << "/" << nr4 << " " << nr5 << "/" << nr5 << "/" << nr5 << " " << nr6 << "/" << nr6 << "/" << nr6 << "\n"; //RIGHT SIDE nr4 = MaloW::convertNrToString(i * 8 + 2); nr5 = MaloW::convertNrToString(i * 8 + 6); nr6 = MaloW::convertNrToString(i * 8 + 8); buffer << "f " << nr4 << "/" << nr4 << "/" << nr4 << " " << nr5 << "/" << nr5 << "/" << nr5 << " " << nr6 << "/" << nr6 << "/" << nr6 << "\n"; nr4 = MaloW::convertNrToString(i * 8 + 2); nr5 = MaloW::convertNrToString(i * 8 + 8); nr6 = MaloW::convertNrToString(i * 8 + 4); buffer << "f " << nr4 << "/" << nr4 << "/" << nr4 << " " << nr5 << "/" << nr5 << "/" << nr5 << " " << nr6 << "/" << nr6 << "/" << nr6 << "\n"; } out << buffer.str().c_str() << endl; out.close(); //arr[0] = Vector3(minV.x, minV.y, minV.z); //arr[1] = Vector3(maxV.x, maxV.y, maxV.z); }
void SphereCylDemo::genCylTexCoords(AXIS axis) { // D3DXCreate* functions generate vertices with position // and normal data. But for texturing, we also need // tex-coords. So clone the mesh to change the vertex // format to a format with tex-coords. D3DVERTEXELEMENT9 elements[64]; UINT numElements = 0; VertexPNT::Decl->GetDeclaration(elements, &numElements); ID3DXMesh* temp = 0; HR(mCylinder->CloneMesh(D3DXMESH_SYSTEMMEM, elements, gd3dDevice, &temp)); ReleaseCOM(mCylinder); // Now generate texture coordinates for each vertex. VertexPNT* vertices = 0; HR(temp->LockVertexBuffer(0, (void**)&vertices)); // We need to get the height of the cylinder we are projecting the // vertices onto. That height depends on which axis the client has // specified that the cylinder lies on. The height is determined by // finding the height of the bounding cylinder on the specified axis. D3DXVECTOR3 maxPoint(-FLT_MAX, -FLT_MAX, -FLT_MAX); D3DXVECTOR3 minPoint(FLT_MAX, FLT_MAX, FLT_MAX); for(UINT i = 0; i < temp->GetNumVertices(); ++i) { D3DXVec3Maximize(&maxPoint, &maxPoint, &vertices[i].pos); D3DXVec3Minimize(&minPoint, &minPoint, &vertices[i].pos); } float a = 0.0f; float b = 0.0f; float h = 0.0f; switch( axis ) { case X_AXIS: a = minPoint.x; b = maxPoint.x; h = b-a; break; case Y_AXIS: a = minPoint.y; b = maxPoint.y; h = b-a; break; case Z_AXIS: a = minPoint.z; b = maxPoint.z; h = b-a; break; } // Iterate over each vertex and compute its texture coordinate. for(UINT i = 0; i < temp->GetNumVertices(); ++i) { // Get the coordinates along the axes orthogonal to the // axis the cylinder is aligned with. float x = 0.0f; float y = 0.0f; float z = 0.0f; switch( axis ) { case X_AXIS: x = vertices[i].pos.y; z = vertices[i].pos.z; y = vertices[i].pos.x; break; case Y_AXIS: x = vertices[i].pos.x; z = vertices[i].pos.z; y = vertices[i].pos.y; break; case Z_AXIS: x = vertices[i].pos.x; z = vertices[i].pos.y; y = vertices[i].pos.z; break; } // Convert to cylindrical coordinates. float theta = atan2f(z, x); float y2 = y - b; // Transform [a, b]-->[-h, 0] // Transform theta from [0, 2*pi] to [0, 1] range and // transform y2 from [-h, 0] to [0, 1]. float u = theta / (2.0f*D3DX_PI); float v = y2 / -h; // Save texture coordinates. vertices[i].tex0.x = u; vertices[i].tex0.y = v; } HR(temp->UnlockVertexBuffer()); // Clone back to a hardware mesh. HR(temp->CloneMesh(D3DXMESH_MANAGED | D3DXMESH_WRITEONLY, elements, gd3dDevice, &mCylinder)); ReleaseCOM(temp); }