void CDrawView::OnDraw(CDC* pDC) { CDrawDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); CDC dc; CDC* pDrawDC = pDC; CBitmap bitmap; CBitmap* pOldBitmap; // only paint the rect that needs repainting CRect client; pDC->GetClipBox(client); //检取当前裁剪边界的最近限定矩形的大小 CRect rect = client; DocToClient(rect); rect.NormalizeRect(); TRACE("CDrawView::OnDraw\n"); if (!pDC->IsPrinting())//不加会闪屏 { // draw to offscreen bitmap for fast looking repaints if (dc.CreateCompatibleDC(pDC)) //创建一个与指定设备兼容的内存设备上下文环境 { if (bitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height()))//创建与指定的设备环境相关的设备兼容的位图 { OnPrepareDC(&dc, NULL); pDrawDC = &dc; // offset origin more because bitmap is just piece of the whole drawing dc.OffsetViewportOrg(-rect.left, -rect.top);//相对于当前视区起点坐标修正视区起点坐标 pOldBitmap = dc.SelectObject(&bitmap); //选择一对象到指定的设备上下文环境中 dc.SetBrushOrg(rect.left % 8, rect.top % 8);//指定GDI将分配给下画笔应用程序选择到设备上下文的原点 // might as well clip to the same rectangle dc.IntersectClipRect(client); //创建了一个新的剪切区域,该区域是当前剪切区域和一个特定矩形的交集 } } } // paint background CBrush brush; if (!brush.CreateSolidBrush(pDoc->GetPaperColor())) return; brush.UnrealizeObject(); //重置一个逻辑调色板 pDrawDC->FillRect(client, &brush); //用指定的画刷填充矩形 Painter::GetInstancePtr()->SetCDC(pDrawDC); Painter::GetInstancePtr()->SetCWnd(this); if (!pDC->IsPrinting() && m_bGrid) DrawGrid(pDrawDC); g_pGuiManager->Draw(); if (pDrawDC != pDC) { pDC->SetViewportOrg(0, 0); pDC->SetWindowOrg(0,0); pDC->SetMapMode(MM_TEXT); dc.SetViewportOrg(0, 0); dc.SetWindowOrg(0,0); dc.SetMapMode(MM_TEXT); pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), &dc, 0, 0, SRCCOPY); dc.SelectObject(pOldBitmap); } }
//--------------------------------------------------------------------------- void __fastcall TMapDialog::UpdateMap(const double *sol, const double *ref, const double *vel, const int *stat, int psol, int psols, int psole, int nsol, AnsiString *solstr, int currentstat) { TColor color[]={clWhite,clGreen,CLORANGE,clFuchsia,clBlue,clRed,clTeal}; TCanvas *c=Plot->Canvas; AnsiString s; TPoint p0; double pos[3],posr[3],rr[3],enu[3],enuv[3],dr[3],dir,*pntpos; int gint[]={20,20,16,20,20,16,20,20,16,20,20,16,20,20,16,20,20,16,20}; int ng[]={10,5,5,10,5,5,10,5,5,10,5,5,10,5,5,10,5,5,10}; TColor colsol=color[stat[psol]]; TRect r(0,0,Plot->Width,Plot->Height); c->Brush->Style=bsSolid; c->Brush->Color=clWhite; c->FillRect(r); for (int i=0;i<3;i++) { CurrentPos[i]=sol[i+psol*3]; } if (norm(RefPos,3)<=0.0) { if (ref&&norm(ref+psol,3)>0.0) { RefName="Base Station"; for (int i=0;i<3;i++) RefPos[i]=ref[i+psol*3]; UpdatePntList(); } else { for (int i=0;i<3;i++) RefPos[i]=CurrentPos[i]; } } ecef2pos(RefPos,posr); ecef2enu(posr,vel+psol*3,enuv); DrawGrid(PosToPoint(RefPos),gint[Scale],ng[Scale],CLLGRAY,clSilver); if (BtnPnt->Down) { for (int i=0;i<MainForm->NMapPnt+1;i++) { DrawPoint(i==0?RefPos:MainForm->PntPos[i-1],PntList->Items->Strings[i], i==PntIndex?clGray:clSilver); } } if (BtnTrack->Down) { TPoint *p =new TPoint[nsol]; TColor *col=new TColor[nsol]; int n=0; for (int i=psols;i!=psole;) { p[n]=PosToPoint(sol+i*3); col[n++]=color[stat[i]]; if (++i>=nsol) i=0; } c->Pen->Color=CLLGRAY; c->Pen->Style=psSolid; c->Polyline(p,n-1); for (int i=0;i<n;i++) { DrawCircle(p[i],HISSIZE,col[i],col[i]); } delete [] p; delete [] col; } p0=PosToPoint(CurrentPos); c->Pen->Color=clBlack; c->MoveTo(p0.x-SOLSIZE/2-4,p0.y); c->LineTo(p0.x+SOLSIZE/2+4,p0.y); c->MoveTo(p0.x,p0.y-SOLSIZE/2-4); c->LineTo(p0.x,p0.y+SOLSIZE/2+4); DrawCircle(p0,SOLSIZE+4,clBlack,clWhite); DrawCircle(p0,SOLSIZE,clBlack,color[currentstat]); DrawVel(enuv); DrawScale(); c->Brush->Style=bsSolid; c->Brush->Color=clWhite; DrawText(6,1,solstr[0],currentstat?colsol:clGray,0); TSize off=c->TextExtent(solstr[2]); DrawText(Plot->Width-off.cx-6,1,solstr[2],clGray,0); DrawText(50,1,solstr[1],clBlack,0); if (BtnPnt->Down) { pntpos=PntIndex>0?MainForm->PntPos[PntIndex-1]:RefPos; for (int i=0;i<3;i++) { rr[i]=pntpos[i]-CurrentPos[i]; } ecef2pos(pntpos,pos); ecef2enu(posr,rr,enu); dir=norm(enu,2)<1E-9?0.0:atan2(enu[0],enu[1])*R2D; if (dir<0.0) dir+=360.0; s.sprintf("To %s: Distance %.3fm Direction %.1f%s", PntList->Items->Strings[PntIndex].c_str(),norm(enu,2),dir,CHARDEG); int y=Plot->Height-off.cy/2-2; if (BtnGraph->Down) y-=GRPHEIGHT+2; DrawText(Plot->Width/2,y,s,clGray,1); } if (BtnGraph->Down) { DrawVertGraph(sol,stat,psol,psols,psole, nsol,currentstat); } Disp->Invalidate(); }
int main() { // Initialization //-------------------------------------------------------------------------------------- int screenWidth = 800; int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera free"); // Define the camera to look into our 3d world Camera camera = {{ 0.0, 10.0, 10.0 }, { 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }}; Vector3 cubePosition = { 0.0, 0.0, 0.0 }; SetCameraMode(CAMERA_FREE); // Set a free camera mode SetCameraPosition(camera.position); // Set internal camera position to match our camera position SetCameraTarget(camera.target); // Set internal camera target to match our camera target SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- UpdateCamera(&camera); // Update internal camera and our camera //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(WHITE); Begin3dMode(camera); DrawCube(cubePosition, 2, 2, 2, RED); DrawCubeWires(cubePosition, 2, 2, 2, MAROON); DrawGrid(10.0, 1.0); End3dMode(); DrawText("Free camera default controls:", 20, 20, 10, GRAY); DrawText("- Mouse Wheel to Zoom in-out", 40, 50, 10, DARKGRAY); DrawText("- Mouse Wheel Pressed to Pan", 40, 70, 10, DARKGRAY); DrawText("- Alt + Mouse Wheel Pressed to Rotate", 40, 90, 10, DARKGRAY); DrawText("- Alt + Ctrl + Mouse Wheel Pressed for Smooth Zoom", 40, 110, 10, DARKGRAY); DrawText("- Z to zoom to (0, 0, 0)", 40, 130, 10, DARKGRAY); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
bool FieldView::on_expose_event(GdkEventExpose* event){ DrawGrid(); DrawAllDots(); DrawAllPaths(); return true; }
int main() { // Initialization //-------------------------------------------------------------------------------------- int screenWidth = 800; int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [models] example - box collisions"); // Define the camera to look into our 3d world Camera camera = {{ 0.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 }; Vector3 playerPosition = { 0.0f, 1.0f, 2.0f }; Vector3 playerSize = { 1.0f, 2.0f, 1.0f }; Color playerColor = GREEN; Vector3 enemyBoxPos = { -4.0f, 1.0f, 0.0f }; Vector3 enemyBoxSize = { 2.0f, 2.0f, 2.0f }; Vector3 enemySpherePos = { 4.0f, 0.0f, 0.0f }; float enemySphereSize = 1.5f; bool collision = false; SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- // Move player if (IsKeyDown(KEY_RIGHT)) playerPosition.x += 0.2f; else if (IsKeyDown(KEY_LEFT)) playerPosition.x -= 0.2f; else if (IsKeyDown(KEY_DOWN)) playerPosition.z += 0.2f; else if (IsKeyDown(KEY_UP)) playerPosition.z -= 0.2f; collision = false; // Check collisions player vs enemy-box if (CheckCollisionBoxes( (BoundingBox){(Vector3){ playerPosition.x - playerSize.x/2, playerPosition.y - playerSize.y/2, playerPosition.z - playerSize.z/2 }, (Vector3){ playerPosition.x + playerSize.x/2, playerPosition.y + playerSize.y/2, playerPosition.z + playerSize.z/2 }}, (BoundingBox){(Vector3){ enemyBoxPos.x - enemyBoxSize.x/2, enemyBoxPos.y - enemyBoxSize.y/2, enemyBoxPos.z - enemyBoxSize.z/2 }, (Vector3){ enemyBoxPos.x + enemyBoxSize.x/2, enemyBoxPos.y + enemyBoxSize.y/2, enemyBoxPos.z + enemyBoxSize.z/2 }})) collision = true; // Check collisions player vs enemy-sphere if (CheckCollisionBoxSphere( (BoundingBox){(Vector3){ playerPosition.x - playerSize.x/2, playerPosition.y - playerSize.y/2, playerPosition.z - playerSize.z/2 }, (Vector3){ playerPosition.x + playerSize.x/2, playerPosition.y + playerSize.y/2, playerPosition.z + playerSize.z/2 }}, enemySpherePos, enemySphereSize)) collision = true; if (collision) playerColor = RED; else playerColor = GREEN; //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); // Draw enemy-box DrawCube(enemyBoxPos, enemyBoxSize.x, enemyBoxSize.y, enemyBoxSize.z, GRAY); DrawCubeWires(enemyBoxPos, enemyBoxSize.x, enemyBoxSize.y, enemyBoxSize.z, DARKGRAY); // Draw enemy-sphere DrawSphere(enemySpherePos, enemySphereSize, GRAY); DrawSphereWires(enemySpherePos, enemySphereSize, 16, 16, DARKGRAY); // Draw player DrawCubeV(playerPosition, playerSize, playerColor); DrawGrid(10, 1.0f); // Draw a grid EndMode3D(); DrawText("Move player with cursors to collide", 220, 40, 20, GRAY); DrawFPS(10, 10); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
// Main window program int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow) { HWND hwnd = NULL; MSG msg = {0}; WNDCLASSEX wndclassex = {0}; // Init fields we care about wndclassex.cbSize = sizeof(WNDCLASSEX); // Always set to size of WNDCLASSEX wndclassex.style = CS_HREDRAW | CS_VREDRAW; wndclassex.lpfnWndProc = WinProc; wndclassex.hInstance = hinstance; wndclassex.lpszClassName = kClassName; wndclassex.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_SHARED); RegisterClassEx(&wndclassex); // Register the WNDCLASSEX RECT rect = { 0, 0, kWinWid, kWinHgt }; // Desired window client rect DWORD winStyleEx = WS_EX_CLIENTEDGE; DWORD winStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_CLIPCHILDREN | WS_CLIPSIBLINGS; // Adjust window rect so it gives us our desired client rect when we // create the window AdjustWindowRectEx(&rect, winStyle, false, winStyleEx); // Create the window hwnd = CreateWindowEx(winStyleEx, // Window extended style kClassName, "www.GameTutorials.com -- D3D Fog", winStyle, // Window style CW_USEDEFAULT, CW_USEDEFAULT, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, hinstance, NULL); // Init our global 3D object if(g3D->init(hwnd) == false) return EXIT_FAILURE; // There's been an error, lets get out of this joint // Get the client rect and make sure our client is the size we want GetClientRect(hwnd, &rect); assert(rect.right == kWinWid && rect.bottom == kWinHgt); // We set up our projection matrix once because it will never change g3D->setProjMatrix(DEG2RAD(60), (float)rect.right / (float)rect.bottom, 1.0f, 8192.0f); // If we can initialize the fog, exit the application if(!InitFog()) return EXIT_FAILURE; ShowCursor(FALSE); // Hide cursor ShowWindow(hwnd, ishow); UpdateWindow(hwnd); // Slam the cursor to the middle of the screen SetCursorPos(GetSystemMetrics(SM_CXSCREEN) >> 1, GetSystemMetrics(SM_CYSCREEN) >> 1); // While the app is running... while(1) { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Handle messages from the OS { if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else if(LockFrameRate()) // Otherwise if it's time to draw { static float angle = 0; // Angle of rotation D3DXMATRIX wMat; // World matrix CameraMouseInput(); // Move camera via the mouse // Set the world view to the current camera view g3D->setViewMatrix(gCamera); g3D->begin(); // Begin drawing g3D->clear(kFogColor); // Clear the screen to the fog color // Rotate around the Y-axis g3D->setWorldMatrix(D3DXMatrixRotationY(&wMat, DEG2RAD(++angle))); // Draw 4 cubes DrawCube(CPos(2,0,0), 0.5f, D3DCOLOR_ARGB(255, 255, 0, 0)); DrawCube(CPos(-2,0,0), 0.5f, D3DCOLOR_ARGB(255, 255, 0, 0)); DrawCube(CPos(0,0,2), 0.5f, D3DCOLOR_ARGB(255, 255, 0, 0)); DrawCube(CPos(0,0,-2), 0.5f, D3DCOLOR_ARGB(255, 255, 0, 0)); // Draw the grid g3D->setWorldMatrix(D3DXMatrixIdentity(&wMat)); DrawGrid(CPos(0,-2,0), 32, D3DCOLOR_ARGB(255, 0, 200, 0)); g3D->end(); // Finish drawing } else Sleep(1); // Give the OS a tiny bit of time to process other things } // end of while(1) ShowCursor(TRUE); // Reshow cursor g3D->deinit(); // Free up CD3DObj UnregisterClass(kClassName,hinstance); // Free up WNDCLASSEX return EXIT_SUCCESS; // Application was a success }
int _tmain(int argc, _TCHAR* argv[]) { /* Начальная инициализация параметров. этот код требуется запустить один раз. TVAInitParams params; memcpy(¶ms.Camera, &g_camera, sizeof(TVACamera)); params.NumZones = 0; params.EventSens = 0.5; params.EventTimeSens = 1000; SaveInitParams("params.xml", ¶ms); */ // инициализация зон наблюдения. for (int i = 0; i < C_MAX_OBJECTS; i++) { g_contours[i].IsRect = false; g_contours[i].NumPoints = C_MAX_POINTS; g_contours[i].Points = (TVAPoint*)malloc(C_MAX_POINTS*sizeof(TVAPoint)); } cvInitFont(&g_font, CV_FONT_HERSHEY_PLAIN,1, 1); CvCapture* capture = NULL; if (argc < 2) capture = cvCaptureFromCAM(0); else capture = cvCaptureFromFile(argv[1]); if (capture == NULL) { printf("%s\n", "Cannot open camera."); return -1; } double fps = cvGetCaptureProperty ( // Получаем частоту кадров capture, CV_CAP_PROP_FPS ); CvSize size = cvSize( // Получаем размер (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT) ); g_mask = cvCreateImage(size, IPL_DEPTH_8U, 1); CvVideoWriter* writer = NULL; cvNamedWindow(_MODULE_); cvSetMouseCallback(_MODULE_, on_mouse); /* Цикл получения и обработки изображения. */ for (;;) { IplImage* frame = NULL; frame = cvQueryFrame(capture); if (!frame) break; /* отрисовка прямоугольников */ for (int i = 0; i < g_rects_count; i++) { CvPoint p1 = cvPoint(g_rects[i].x, g_rects[i].y); CvPoint p2 = cvPoint(p1.x + g_rects[i].width, p1.y + g_rects[i].height); cvRectangle(frame, p1, p2, CV_RGB(255,0,0)); } /* Отрисовка зон наблюдения. */ for (int i = 0; i < g_contours_count; i++) { if (g_contours[i].NumPoints > 0) { for (int j = 1; j < g_contours[i].NumPoints; j++) { CvPoint p1 = cvPoint((int)g_contours[i].Points[j-1].X, (int)g_contours[i].Points[j-1].Y); CvPoint p2 = cvPoint((int)g_contours[i].Points[j].X, (int)g_contours[i].Points[j].Y); cvLine(frame, p1,p2, CV_RGB(255,0,0)); } CvPoint p1 = cvPoint((int)g_contours[i].Points[g_contours[i].NumPoints-1].X, (int)g_contours[i].Points[g_contours[i].NumPoints-1].Y); CvPoint p2 = cvPoint((int)g_contours[i].Points[0].X, (int)g_contours[i].Points[0].Y); cvLine(frame, p1,p2, CV_RGB(255,0,0)); } } /* Отображение полученного изображения в окне. */ ProcessFrame(frame); if (g_grid_visible) DrawGrid(frame); DrawStatus(frame); cvShowImage(_MODULE_, frame); /* Запись фрейма */ if (g_record_video) cvWriteFrame( writer, frame ); /* Анализ клавиатуры */ bool state = g_set_rects || g_set_contours || g_set_zones; int c; c = cvWaitKey(10); if ((char)c == 27) break; if ((char)c == 's') { cvSaveImage("out.png", frame); } else if ((char)c == 'l') { if (!state) LoadRects(size.width, size.height); } else if ((char)c == 'g') { if (!state) LoadContours(size.width, size.height); } else if ((char)c == 'k') { if (!state) LoadZones(size.width, size.height); } else if ((char)c == 'r') { if (g_record_video) { // завершаем запись на диск cvReleaseVideoWriter( &writer ); writer = NULL; g_record_video = false; printf("Stop recording.\n"); } else { // открываем файл для записи и связываем с ним // переменную writer writer = cvCreateVideoWriter("out.avi",CV_FOURCC('D','I','V','X'), fps, size ); if (writer == NULL) { printf("%s\n", "Cannot create writer."); } else { g_record_video = true; printf("Start recording.\n"); } } } else if ((char)c == 't') { if (g_set_rects) { SaveRects(size.width, size.height); if (!g_set_zones) { g_rects_count = 0; ClearMask(g_mask); } g_set_rects = false; } else if (!g_set_contours) { g_set_rects = true; } } else if ((char)c == 'c') { if (g_set_contours) { SaveContours(size.width, size.height); if (!g_set_zones) { g_contours_count = 0; ClearMask(g_mask); } g_set_contours = false; g_open_contour = false; } else if (!g_set_rects) { g_set_contours = true; } } else if ((char)c == 'z') { if (g_set_zones) { SaveZones(size.width, size.height); g_set_zones = false; g_contours_count = 0; g_rects_count = 0; ClearMask(g_mask); } else if (!g_set_rects && !g_set_contours) { g_set_zones = true; } } else if ((char)c == 'w') { g_grid_visible = !g_grid_visible; } } cvReleaseVideoWriter( &writer ); cvDestroyWindow(_MODULE_); cvReleaseCapture(&capture); cvReleaseImage(&g_mask); // освобождение памяти for (int i = 0; i < C_MAX_OBJECTS; i++) { free(g_contours[i].Points); } return 0; }
void MorphanView::OnDraw(wxDC& dc) { wxGraphicsContext* gc = wxGraphicsContext::Create(panel); if (!gc) return; wxGCDC gcdc(gc); wxPoint origin = dc.GetDeviceOrigin(); gcdc.SetUserScale(zoom, zoom); gcdc.SetDeviceOrigin(origin.x, origin.y); if (show_grid) DrawGrid(gcdc); Morphan* morphan = GetDocument(); if (!morphan) return; const MorphanKeyFrame& keyFrame = morphan->Get(current_frame); wxPen cpbox(*wxBLACK, 2); wxBrush cpfill(*wxWHITE); MorphanDrawContext context(gcdc, keyFrame.GetOpacity()); for (const Primitive* p : keyFrame.GetPrimitives()) { p->Draw(context); if (show_points) { gcdc.SetPen(cpbox); gcdc.SetBrush(cpfill); const std::vector<wxRealPoint> points = p->GetControlPoints(); for (const auto& point : points) gcdc.DrawRectangle(point.x - 3, point.y - 3, 6, 6); } } if (in_window && (tool && tool->CanPreview())) { gcdc.SetPen(wxPen(wxColour(255, 0, 0, 255), outlineWidth)); gcdc.SetBrush(*wxTRANSPARENT_BRUSH); tool->Preview(gcdc, mouse, wxGetKeyState(WXK_SHIFT)); } if (in_window && (modifyTool && modifyTool->HasSelection())) { std::set<PrimitiveSelection> selection = modifyTool->PreviewModify(mouse); cpbox.SetColour(*wxRED); MorphanDrawContext context(gcdc, 1.0f); for (PrimitiveSelection ps : selection) { ps.primitive->SetOutline(*wxRED); ps.primitive->Draw(context); if (show_points) { gcdc.SetPen(cpbox); gcdc.SetBrush(cpfill); const std::vector<wxRealPoint> points = ps.primitive->GetControlPoints(); for (const auto& point : points) gcdc.DrawRectangle(point.x - 3, point.y - 3, 6, 6); } delete ps.primitive; } } }