void Camera::Pan(float x, float y, float dx, float dy) { Vector3f origin; Vector3f unit; GetRay(Vector2i(static_cast<int>(x), static_cast<int>(y)), origin, unit); Vector3f tap_intersection = origin - unit * (origin.y / unit.y); GetRay(Vector2i(static_cast<int>(x - dx), static_cast<int>(y - dy)), origin, unit); Vector3f drag_intersection = origin - unit * (origin.y / unit.y); Vector3f delta = drag_intersection - tap_intersection; mCameraOriginEnd += delta; mCameraOriginStart += delta; mCameraOriginStart.x = std::max(mCameraOriginStart.x, 0.0f); mCameraOriginStart.x = std::min(mCameraOriginStart.x, (float)WORLD_WIDTH); mCameraOriginStart.z = std::max(mCameraOriginStart.z, 0.0f); mCameraOriginStart.z = std::min(mCameraOriginStart.z , (float)WORLD_BREADTH); mCameraOriginEnd.x = std::max(mCameraOriginEnd.x, 0.0f); mCameraOriginEnd.x = std::min(mCameraOriginEnd.x, (float)WORLD_WIDTH); mCameraOriginEnd.z = std::max(mCameraOriginEnd.z, 0.0f); mCameraOriginEnd.z = std::min(mCameraOriginEnd.z , (float)WORLD_BREADTH); }
Vector3D PPC::GetPoint(Vector3D projP){ // projP[0] is "u" // projP[1] is "v" // projP[2] is "1/w" return C + GetRay(projP[0], projP[1])/projP[2]; }
noVec3& App::Pick() { D3DXVECTOR3 orig, dir; GetRay(dir, orig); float DistToSurface = 0.f; g_TerrainDX11Render.RayCast(orig, dir, &DistToSurface); /*float height = DistToSurface; pickPos_.x = orig.x; pickPos_.y = height; pickPos_.z = orig.z;*/ { D3DXVECTOR3 temp = orig + dir * DistToSurface; pickPos_ = noVec3(temp.x, temp.y, temp.z); orig = temp; orig.y += 500.f; dir = D3DXVECTOR3(0.0f, -1.0f, 0.f); g_TerrainDX11Render.RayCast(orig, dir, &DistToSurface); temp = orig + dir * DistToSurface; pickPos_ = noVec3(temp.x, temp.y, temp.z); } //if (g_TerrainDX11Render.GetCurrPatch()) // pickPos_.y = g_TerrainDX11Render.GetCurrPatch()->GetHeight(noVec2(pickPos_.x, pickPos_.z)); return pickPos_; }
void CEasyGenView::OnRButtonDown(UINT nFlags, CPoint point) { CEasyGenDoc *pDoc = GetDocument(); CNmVec3 ray; CNmVec3 p; int tab; BOOL kCTRL = nFlags & MK_CONTROL; BOOL kSHIFT = nFlags & MK_SHIFT; BOOL GotPoint = GetXYZ(point, p); BOOL GotRay = GetRay(point, ray); m_RDP = point; SetCapture(); switch (tab = pDoc->GetActiveTab()) { case (TAB_GRID): break; case (TAB_MODIFIER): break; case (TAB_ALPHAMAP): break; case (TAB_MODELS): // MD3 - Move an Md3Highlighted model if (!kCTRL && kSHIFT && GotRay && m_Md3Highlight) { CNmVec3 pos; float dist; if (g_Grid.GetDistanceTrisRay(m_Eye, ray, pos, dist)) { m_Md3Highlight->m_egvPos = pos; InvalidateRect(NULL, FALSE); } } break; } CView::OnRButtonDown(nFlags, point); }
void MOUSE::CalculateMappos(TERRAIN &terrain) { //Get Mouse Ray D3DXMATRIX world; D3DXMatrixIdentity(&world); m_pDevice->SetTransform(D3DTS_WORLD, &world); RAY mRay = GetRay(); float minDistance = 10000.0f; for(int i=0;i<(int)terrain.m_patches.size();i++) { if(mRay.Intersect(terrain.m_patches[i]->m_BBox) > 0.0f) { // Collect only the closest intersection BOOL hit; DWORD dwFace; float hitU, hitV, dist; D3DXIntersect(terrain.m_patches[i]->m_pMesh, &mRay.org, &mRay.dir, &hit, &dwFace, &hitU, &hitV, &dist, NULL, NULL); if(hit && dist < minDistance) { minDistance = dist; int tiles = dwFace / 2; //Two faces to each map tile int tilesPerRow = terrain.m_patches[i]->m_mapRect.right - terrain.m_patches[i]->m_mapRect.left; int y = tiles / tilesPerRow, x = tiles - y * tilesPerRow; if(dwFace % 2 == 0) //Hit upper left face { if(hitU > 0.5f)x++; else if(hitV > 0.5f)y++; } else //Hit lower right face { if(hitU + hitV < 0.5f)y++; else if(hitU > 0.5f)x++; else {x++;y++;} } m_mappos.Set(terrain.m_patches[i]->m_mapRect.left + x, terrain.m_patches[i]->m_mapRect.top + y); m_ballPos = terrain.GetWorldPos(m_mappos); } } } }
int RayScene::RayTrace(const int& width,const int& height,const int& rLimit,const double& cLimit,Image32& img){ int i,j; Ray3D ray; Point3D c; Pixel32 p; int rayCount=0; if(!img.setSize(width,height)){return 0;} ray.position=camera->position; for(i=0;i<width;i++){ printf(" \r"); printf("%3.1f\r",(float)i/width*100); for(j=0;j<height;j++){ ray=GetRay(camera,i,height-j-1,width,height); c=GetColor(ray,rLimit,Point3D(cLimit,cLimit,cLimit)); p.r=(int)(c[0]*255); p.g=(int)(c[1]*255); p.b=(int)(c[2]*255); img(i,j)=p; } } return 1; }
void CEasyGenView::OnLButtonDblClk(UINT nFlags, CPoint point) { CEasyGenDoc *pDoc = GetDocument(); CNmVec3 ray; CNmVec3 p; BOOL kCTRL = nFlags & MK_CONTROL; BOOL kSHIFT = nFlags & MK_SHIFT; BOOL GotPoint = GetXYZ(point, p); BOOL GotRay = GetRay(point, ray); switch (pDoc->GetActiveTab()) { case (TAB_GRID): // player place if (!kCTRL && kSHIFT && GotPoint && g_Grid.PtInGrid(p.x, p.y)) { pDoc->m_vPlayer.x = p.x; pDoc->m_vPlayer.y = p.y; S_POSINFO inf; if (g_Grid.GetInfo(pDoc->m_vPlayer.x, pDoc->m_vPlayer.y, &inf)) { pDoc->m_vPlayer.z = inf.z; } m_vGenericPos.Set(p.x, p.y, pDoc->m_vPlayer.z); UpdateStatusBar(); InvalidateRect(NULL, FALSE); } break; case (TAB_MODIFIER): // modifier place if (kCTRL && !kSHIFT && GotPoint && g_Grid.PtInGrid(p.x, p.y)) { pDoc->m_modX = p.x; pDoc->m_modY = p.y; g_Mod.PositionSet(p.x, p.y); UpdateStatusBar(); InvalidateRect(NULL, FALSE); pDoc->m_bInvalidateGridPreview = TRUE; } break; case (TAB_ALPHAMAP): break; case (TAB_MODELS): // MD3 - Insert model if (kCTRL && !kSHIFT && GotRay) { CNmVec3 pos; float dist; S_MD3PTR *md3ptr; if (g_Grid.GetDistanceTrisRay(m_Eye, ray, pos, dist)) { md3ptr = g_Md3Man.m_Placed.Append(); // g_Md3Man.m_Placed.Append( pDoc->GetMd3SelectedTabModels() ); if (md3ptr) { md3ptr->pMd3 = pDoc->GetMd3SelectedTabModels(); md3ptr->m_egvPos = pos; InvalidateRect(NULL, FALSE); } } } break; } // CAMERA - camera place [EVERY TAB] if (!kSHIFT && !kCTRL && GotPoint && g_Grid.PtInGrid(p.x, p.y)) { m_Eye.x = p.x; m_Eye.y = p.y; InvalidateRect(NULL, FALSE); } CView::OnLButtonDblClk(nFlags, point); }
void CEasyGenView::OnLButtonDown(UINT nFlags, CPoint point) { CEasyGenDoc *pDoc = GetDocument(); S_POSINFO inf; CTris *t; CNmVec3 p; int tab; m_LDP = point; BOOL kCTRL = nFlags & MK_CONTROL; BOOL kSHIFT = nFlags & MK_SHIFT; BOOL Refresh = FALSE; int mod = pDoc->GetModifier(); switch (tab = pDoc->GetActiveTab()) { case (TAB_GRID): break; case (TAB_MODIFIER): // Get a vertex to drag(CTRL+SHIFT+LeftBut) if (kCTRL && kSHIFT) { if (GetXYZ(point, p)) { if (g_Grid.GetInfo(p.x, p.y, &inf)) { m_VertexGot = g_Grid.GetK(inf.i, inf.j); } } } // Exclude triangle from export | to prevent erroneus exclusion if (!kCTRL && kSHIFT && g_Grid.OptionExcludedTrisGet()) { if (GetXYZ(point, p)) { t = g_Grid.GetTris(g_Grid.GetTris(p.x, p.y)); if (t) { t->SetExcluded(!t->GetExcluded()); pDoc->m_bInvalidateGridColor = TRUE; Refresh = TRUE; } } } break; case (TAB_ALPHAMAP): int curindex; curindex = g_AlphamapMan.GetCurSel(); // paint the alphamap (CTRL+SHIFT+LeftBut) if (kCTRL && kSHIFT && curindex != -1) { if (GetXYZ(point, p)) { if (g_Grid.GetInfo(p.x, p.y, &inf)) { m_VertexGot = g_Grid.GetK(inf.i, inf.j); // g_Grid.SetAlphaIndex(m_VertexGot, curindex ); g_Grid.PaintAlphaIndex(inf.i, inf.j, pDoc->m_dwPaintRadius, curindex); Refresh = TRUE; } } } int indexgot; // copy alphamap index (SHIFT+LeftBut) if (!kCTRL && kSHIFT) { if (GetXYZ(point, p)) { if (g_Grid.GetInfo(p.x, p.y, &inf)) { m_VertexGot = g_Grid.GetK(inf.i, inf.j); indexgot = g_Grid.GetAlphaIndex(m_VertexGot); if (indexgot != g_AlphamapMan.GetCurSel()) { g_AlphamapMan.SetCurSel(indexgot); pDoc->UpdateFormView(TAB_ALPHAMAP); } } } } break; case (TAB_MODELS): // MD3 - Selecting if (!kCTRL && kSHIFT) { CNmVec3 ray, dummy; float md3_dist, hitterra_dist; bool hitterra; S_MD3PTR *oldh = m_Md3Highlight; S_MD3PTR *hit_md3; if (GetRay(point, ray)) { if (g_Md3Man.GetByRay(m_Eye, ray, hit_md3, md3_dist)) { // check if is covered by terrain!!! hitterra = g_Grid.GetDistanceTrisRay(m_Eye, ray, dummy, hitterra_dist); if (!hitterra) { m_Md3Highlight = hit_md3; } else if (hitterra_dist > md3_dist) { m_Md3Highlight = hit_md3; } } } if (m_Md3Highlight != oldh) { Refresh = TRUE; } } break; } // rotate tris (CTRL+LeftBut) if ((tab == TAB_GRID || tab == TAB_MODIFIER || tab == TAB_ALPHAMAP) && mod == MOD_NONE && kCTRL && !kSHIFT) { if (GetXYZ(point, p)) { if (g_Grid.GetInfo(p.x, p.y, &inf)) { int i, j, k; int mt1; S_FACEDATAEX *f; mt1 = g_Grid.GetTris(p.x, p.y); mt1 &= ~1; k = mt1 / 2; i = k % g_Grid.GetCellsX(); j = k / g_Grid.GetCellsX(); f = g_Grid.GetFace(k); if ((f->flags & FACE_FL_TRISINV)) { f->flags &= ~FACE_FL_TRISINV; } else { f->flags |= FACE_FL_TRISINV; } g_Grid.FaceOrientateTris(i, j, f->flags & FACE_FL_TRISINV); Refresh = TRUE; } } } if (Refresh) { InvalidateRect(NULL, FALSE); } // SetCapture(); CView::OnLButtonDown(nFlags, point); }
int Alpha(TPoint *ray){ /* 0. Subinitialization - clear auxiliary variables and empty and nonsignificant input axes */ properties.resize(d); for (unsigned int i = 0; i < d; i++){properties[i] = i;} // initialize properties: all available features.clear(); outMatrix(x); /* 1. Null-cycle */ if (numStartFeatures == 2){ // start with two features? Feature optFeatureX; Feature optFeatureY; for (unsigned int i = 0; i < properties.size() - 1; i++){ for (unsigned int j = i + 1; j < properties.size(); j++){ /* Calculating minimal error on the plane of the i-th and the j-th properties */ Feature tmpFeature; curFeature = x[properties[i]]; unsigned int error = DGetMinError(properties[j], &tmpFeature); #ifdef DEF_OUT_ALPHA if (OUT_ALPHA){ Rcout << properties[i] << ", " << properties[j] << ", " << tmpFeature.angle << ", " << error << ", " << endl; } #endif if (error < optFeatureY.error){optFeatureX.number = properties[i]; optFeatureY = tmpFeature;} } } features.push_back(optFeatureX); features.push_back(optFeatureY); for (unsigned int i = 0; i < properties.size(); i++){ // delete recently found X and Y properties if (properties[i] == optFeatureX.number){properties.erase(properties.begin() + i);} if (properties[i] == optFeatureY.number){properties.erase(properties.begin() + i);} } curFeature = x[features[0].number]; UpdateCurFeature(); outString("Feature 1:"); outVector(curFeature); } /* 2. Main cycle */ /* Searching an optimal feature space while empirical error rate decreases */ while(features[features.size() - 1].error > 0 && properties.size() > 0){ Feature optFeature; for (unsigned int i = 0; i < properties.size(); i++){ /* Calculating minimal error on the plane of the curFeature and the j-th properties */ Feature tmpFeature; unsigned int error = DGetMinError(properties[i], &tmpFeature); #ifdef DEF_OUT_ALPHA if (OUT_ALPHA){ Rcout << properties[i] << ", " << tmpFeature.angle << ", " << error << ", " << endl; } #endif if (error < optFeature.error){optFeature = tmpFeature;} } if (optFeature.error < features[features.size() - 1].error){ features.push_back(optFeature); for (unsigned int i = 0; i < properties.size(); i++){ // delete recently found property if (properties[i] == optFeature.number){properties.erase(properties.begin() + i);} } UpdateCurFeature(); outString("Feature :"); outVector(curFeature); }else{break;} } outString("Features:"); outFeatures(features); /* Restoring the projection vector */ GetRay(ray); return features[features.size() - 1].error; }