{"id":13732006,"url":"https://github.com/nlguillemot/arcball_camera","last_synced_at":"2025-05-08T06:31:06.336Z","repository":{"id":147131708,"uuid":"66717952","full_name":"nlguillemot/arcball_camera","owner":"nlguillemot","description":"Single-header single-function C/C++ immediate-mode camera for your graphics demos","archived":false,"fork":false,"pushed_at":"2016-08-28T16:36:55.000Z","size":33,"stargazers_count":88,"open_issues_count":0,"forks_count":49,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-08-04T02:10:42.314Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nlguillemot.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-08-27T14:17:12.000Z","updated_at":"2024-06-26T12:09:32.000Z","dependencies_parsed_at":"2024-01-06T14:50:04.095Z","dependency_job_id":null,"html_url":"https://github.com/nlguillemot/arcball_camera","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nlguillemot%2Farcball_camera","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nlguillemot%2Farcball_camera/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nlguillemot%2Farcball_camera/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nlguillemot%2Farcball_camera/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nlguillemot","download_url":"https://codeload.github.com/nlguillemot/arcball_camera/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224707586,"owners_count":17356361,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T02:01:43.571Z","updated_at":"2024-11-14T23:30:46.595Z","avatar_url":"https://github.com/nlguillemot.png","language":"C","funding_links":[],"categories":["Graphics","Geometry, Graphics Processing, and Game Development"],"sub_categories":[],"readme":"# arcball_camera\n\nSingle-header single-function C/C++ immediate-mode camera for your graphics demos\n\nJust call `arcball_camera_update` once per frame.\n\n# OpenGL Example\n\n![openglexample](http://i.imgur.com/DOX9gBX.gif)\n\nBelow is a fully-functional example program that works using OpenGL.\n\nCreate a new Visual Studio project, drop this file in it, and it should just work.\n\n```\n#define ARCBALL_CAMERA_IMPLEMENTATION\n#include \"arcball_camera.h\"\n\n#include \u003cWindows.h\u003e\n#include \u003cGL/gl.h\u003e\n#include \u003cGL/glu.h\u003e\n#include \u003cstdio.h\u003e\n\n#pragma comment(lib, \"OpenGL32.lib\")\n#pragma comment(lib, \"glu32.lib\")\n\nint g_wheel_delta;\n\nLRESULT CALLBACK MyWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)\n{\n    switch (message)\n    {\n    case WM_CLOSE:\n        ExitProcess(0);\n    case WM_MOUSEWHEEL:\n        g_wheel_delta += GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;\n        break;\n    }\n    \n    return DefWindowProc(hWnd, message, wParam, lParam);\n}\n\nint CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)\n{\n    WNDCLASSEX wc;\n    ZeroMemory(\u0026wc, sizeof(wc));\n    wc.cbSize = sizeof(wc);\n    wc.style = CS_OWNDC;\n    wc.lpfnWndProc = MyWndProc;\n    wc.hInstance = hInstance;\n    wc.hCursor = LoadCursor(NULL, IDC_ARROW);\n    wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND;\n    wc.lpszClassName = TEXT(\"WindowClass\");\n    RegisterClassEx(\u0026wc);\n\n    RECT wr = { 0, 0, 640, 480 };\n    AdjustWindowRect(\u0026wr, 0, FALSE);\n    HWND hWnd = CreateWindowEx(\n        0, TEXT(\"WindowClass\"),\n        TEXT(\"BasicGL\"),\n        WS_OVERLAPPEDWINDOW,\n        0, 0, wr.right - wr.left, wr.bottom - wr.top,\n        0, 0, hInstance, 0);\n\n    PIXELFORMATDESCRIPTOR pfd;\n    ZeroMemory(\u0026pfd, sizeof(pfd));\n    pfd.nSize = sizeof(pfd);\n    pfd.nVersion = 1;\n    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;\n    pfd.iPixelType = PFD_TYPE_RGBA;\n    pfd.cColorBits = 32;\n    pfd.cDepthBits = 24;\n    pfd.cStencilBits = 8;\n    pfd.iLayerType = PFD_MAIN_PLANE;\n\n    HDC hDC = GetDC(hWnd);\n\n    int chosenPixelFormat = ChoosePixelFormat(hDC, \u0026pfd);\n    SetPixelFormat(hDC, chosenPixelFormat, \u0026pfd);\n\n    HGLRC hGLRC = wglCreateContext(hDC);\n    wglMakeCurrent(hDC, hGLRC);\n\n    ShowWindow(hWnd, SW_SHOWNORMAL);\n\n    float pos[3] = { 3.0f, 3.0f, 3.0f };\n    float target[3] = { 0.0f, 0.0f, 0.0f };\n\n    // initialize \"up\" to be tangent to the sphere!\n    // up = cross(cross(look, world_up), look)\n    float up[3];\n    {\n        float look[3] = { target[0] - pos[0], target[1] - pos[1], target[2] - pos[2] };\n        float look_len = sqrtf(look[0] * look[0] + look[1] * look[1] + look[2] * look[2]);\n        look[0] /= look_len;\n        look[1] /= look_len;\n        look[2] /= look_len;\n\n        float world_up[3] = { 0.0f, 1.0f, 0.0f };\n\n        float across[3] = {\n            look[1] * world_up[2] - look[2] * world_up[1],\n            look[2] * world_up[0] - look[0] * world_up[2],\n            look[0] * world_up[1] - look[1] * world_up[0],\n        };\n\n        up[0] = across[1] * look[2] - across[2] * look[1];\n        up[1] = across[2] * look[0] - across[0] * look[2];\n        up[2] = across[0] * look[1] - across[1] * look[0];\n        \n        float up_len = sqrtf(up[0] * up[0] + up[1] * up[1] + up[2] * up[2]);\n        up[0] /= up_len;\n        up[1] /= up_len;\n        up[2] /= up_len;\n    }\n\n    LARGE_INTEGER then, now, freq;\n    QueryPerformanceFrequency(\u0026freq);\n    QueryPerformanceCounter(\u0026then);\n\n    POINT oldcursor;\n    GetCursorPos(\u0026oldcursor);\n\n    float oldview[16];\n\n    while (!GetAsyncKeyState(VK_ESCAPE))\n    {\n        MSG msg;\n        while (PeekMessage(\u0026msg, NULL, 0, 0, PM_REMOVE))\n        {\n            TranslateMessage(\u0026msg);\n            DispatchMessage(\u0026msg);\n        }\n\n        QueryPerformanceCounter(\u0026now);\n        float delta_time_sec = (float)(now.QuadPart - then.QuadPart) / freq.QuadPart;\n\n        POINT cursor;\n        GetCursorPos(\u0026cursor);\n        ScreenToClient(hWnd, \u0026cursor);\n\n        float view[16];\n        arcball_camera_update(\n            pos, target, up, view,\n            delta_time_sec,\n            0.1f, // zoom per tick\n            0.5f, // pan speed\n            3.0f, // rotation multiplier\n            640, 480, // screen (window) size\n            oldcursor.x, cursor.x,\n            oldcursor.y, cursor.y,\n            GetAsyncKeyState(VK_MBUTTON),\n            GetAsyncKeyState(VK_RBUTTON),\n            g_wheel_delta,\n            0);\n\n        if (memcmp(oldview, view, sizeof(float) * 16) != 0)\n        {\n            printf(\"\\n\");\n            printf(\"pos: %f, %f, %f\\n\", pos[0], pos[1], pos[2]);\n            printf(\"target: %f, %f, %f\\n\", target[0], target[1], target[2]);\n            printf(\"view: %f %f %f %f\\n\"\n                   \"      %f %f %f %f\\n\"\n                   \"      %f %f %f %f\\n\"\n                   \"      %f %f %f %f\\n\",\n                 view[0],  view[1],  view[2],  view[3],\n                 view[4],  view[5],  view[6],  view[7],\n                 view[8],  view[9], view[10], view[11],\n                view[12], view[13], view[14], view[15]);\n        }\n\n        memcpy(oldview, view, sizeof(float) * 16);\n        glClear(GL_COLOR_BUFFER_BIT);\n\n        glMatrixMode(GL_PROJECTION);\n        glLoadIdentity();\n        gluPerspective(70.0, (double)(wr.right - wr.left) / (wr.bottom - wr.top), 0.001, 100.0);\n\n        glMatrixMode(GL_MODELVIEW);\n        glLoadMatrixf(view);\n\n        glBegin(GL_TRIANGLES);\n        glColor3ub(255, 0, 0);\n        glVertex3f(-1.0f, -1.0f, 0.0f);\n        glColor3ub(0, 255, 0);\n        glVertex3f(1.0f, -1.0f, 0.0f);\n        glColor3ub(0, 0, 255);\n        glVertex3f(0.0f, 1.0f, 0.0f);\n        glEnd();\n\n        SwapBuffers(hDC);\n\n        then = now;\n        oldcursor = cursor;\n\n        g_wheel_delta = 0;\n    }\n\n    return 0;\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnlguillemot%2Farcball_camera","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnlguillemot%2Farcball_camera","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnlguillemot%2Farcball_camera/lists"}