diff --git a/src/editor.cpp b/src/editor.cpp index 9d33e24..ab6ae84 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -1,6 +1,7 @@ #include "editor.h" #include #include +#include "macros.h" namespace winged { @@ -10,13 +11,13 @@ bool hasSelection(EditorState state) { immer::set selAttachedVerts(const EditorState &state) { auto verts = state.selVerts.transient(); - for (auto e : state.selEdges) { - auto edge = e.in(state.surf), twin = edge.twin.in(state.surf); + for (let &e : state.selEdges) { + let &edge = e.in(state.surf), &twin = edge.twin.in(state.surf); verts.insert(edge.vert); verts.insert(twin.vert); } - for (auto f : state.selFaces) { - for (auto &faceEdge : FaceEdges(state.surf, f.in(state.surf))) { + for (let &f : state.selFaces) { + for (let faceEdge : FaceEdges(state.surf, f.in(state.surf))) { verts.insert(faceEdge.second.vert); } } @@ -32,14 +33,14 @@ EditorState clearSelection(EditorState state) { EditorState cleanSelection(const EditorState &state) { EditorState newState = state; - for (auto &vert : state.selVerts) + for (let &vert : state.selVerts) if (!vert.find(state.surf)) newState.selVerts = std::move(newState.selVerts).erase(vert); - for (auto &face : state.selFaces) + for (let &face : state.selFaces) if (!face.find(state.surf)) newState.selFaces = std::move(newState.selFaces).erase(face); - for (auto &e : state.selEdges) { - if (auto edge = e.find(state.surf)) { + for (let &e : state.selEdges) { + if (let edge = e.find(state.surf)) { if (!isPrimary({e, *edge})) newState.selEdges = std::move(newState.selEdges).erase(e).insert(edge->twin); } else { @@ -53,8 +54,8 @@ glm::vec3 vertsCenter(const Surface &surf, immer::set verts) { if (verts.empty()) return {}; glm::vec3 min = verts.begin()->in(surf).pos, max = min; - for (auto v : verts) { - auto pos = v.in(surf).pos; + for (let &v : verts) { + let pos = v.in(surf).pos; min = glm::min(min, pos); max = glm::max(max, pos); } diff --git a/src/file.cpp b/src/file.cpp index 6770c6e..c6c6001 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -6,6 +6,7 @@ #include #include "rendermesh.h" #include "stdutil.h" +#include "macros.h" using namespace chroma; @@ -62,13 +63,13 @@ static void write(HANDLE handle, const void *buf, DWORD size) { template static void writeSet(HANDLE handle, const immer::set &set, const std::unordered_map &map) { write(handle, tempPtr(set.size()), 4); - for (auto &v : set) + for (let &v : set) write(handle, &map.at(v), sizeof(U)); } static void writeString(HANDLE handle, const wchar_t *str) { CW2A utf8(str, CP_UTF8); - uint16_t len = (uint16_t)lstrlenA(utf8); + let len = (uint16_t)lstrlenA(utf8); write(handle, &len, 2); write(handle, utf8.m_psz, len); } @@ -93,8 +94,8 @@ void writeFile(const wchar_t *file, const EditorState &state, const ViewState &v std::vector facePaintIndices; std::vector paints; std::unordered_set usedFiles; - for (auto &face : state.surf.faces) { - if (auto paintIndex = tryGet(paintIndices, *face.second.paint)) { + for (let &face : state.surf.faces) { + if (let paintIndex = tryGet(paintIndices, *face.second.paint)) { facePaintIndices.push_back(*paintIndex); } else { paintIndices[face.second.paint] = (uint32_t)paints.size(); @@ -112,12 +113,12 @@ void writeFile(const wchar_t *file, const EditorState &state, const ViewState &v write(handle, paints.data(), (DWORD)(paints.size() * sizeof(Paint))); write(handle, facePaintIndices.data(), (DWORD)(facePaintIndices.size() * sizeof(uint32_t))); - for (auto &vert : state.surf.verts) { + for (let &vert : state.surf.verts) { write(handle, &vert.second.pos, sizeof(vert.second.pos)); vertIndices[vert.first] = (uint32_t)vertIndices.size(); } - for (auto &face : state.surf.faces) { - for (auto &edge : FaceEdges(state.surf, face.second)) { + for (let &face : state.surf.faces) { + for (let edge : FaceEdges(state.surf, face.second)) { write(handle, &vertIndices[edge.second.vert], 4); edgeIndices[edge.first] = (uint32_t)edgeIndices.size(); } @@ -130,8 +131,8 @@ void writeFile(const wchar_t *file, const EditorState &state, const ViewState &v write(handle, &state.SAVE_DATA, sizeof(EditorState) - offsetof(EditorState, SAVE_DATA)); write(handle, &view, sizeof(view)); - for (auto &id : usedFiles) { - if (auto path = tryGet(library.idPaths, id)) { + for (let &id : usedFiles) { + if (let path = tryGet(library.idPaths, id)) { wchar_t relative[MAX_PATH] = L""; if (library.rootPath.empty()) { PathRelativePathTo(relative, file, 0, path->c_str(), 0); @@ -165,14 +166,14 @@ static T readVal(HANDLE handle, DWORD size = sizeof(T)) { template static immer::set readSet(HANDLE handle, const std::vector> &vec) { immer::set set; - uint32_t size = readVal(handle); + let size = readVal(handle); for (uint32_t i = 0; i < size; i++) set = std::move(set).insert(vec[readVal(handle)].first); return set; } static void readString(HANDLE handle, wchar_t *str, size_t size) { - uint16_t len = readVal(handle); + let len = readVal(handle); std::unique_ptr buf(new char[len + 1]); read(handle, buf.get(), len); buf[len] = 0; @@ -187,15 +188,15 @@ std::tuple readFile(const wchar_t *file, throw winged_error(L"Error opening file"); if (readVal(handle) != 'WING') throw winged_error(L"Unrecognized file format"); - auto version = readVal(handle); + let version = readVal(handle); if (version != 2) throw winged_error(L"Unrecognized file version"); EditorState state; - uint32_t numPaints = readVal(handle); - uint32_t numFaces = readVal(handle); - uint32_t numVerts = readVal(handle); - uint32_t numEdges = readVal(handle); + let numPaints = readVal(handle); + let numFaces = readVal(handle); + let numVerts = readVal(handle); + let numEdges = readVal(handle); std::vector> paints; std::vector faces; std::vector verts; @@ -220,7 +221,7 @@ std::tuple readFile(const wchar_t *file, std::unordered_map, uint32_t> vertPairEdges; vertPairEdges.reserve(numEdges); for (uint32_t f = 0; f < numFaces; f++) { - size_t faceEdgeStart = edges.size(); + let faceEdgeStart = edges.size(); uint32_t v; while ((v = readVal(handle)) != (uint32_t)-1) { edge_pair edge = {genId(), {}}; @@ -239,10 +240,9 @@ std::tuple readFile(const wchar_t *file, edges.back().second.next = edges[faceEdgeStart].first; for (size_t i = faceEdgeStart; i < edges.size(); i++) { - edge_pair *edge = &edges[i]; - const edge_pair &next = (i == edges.size() - 1) ? - edges[faceEdgeStart] : edges[i + 1]; - if (auto twinI = tryGet(vertPairEdges, {edge->second.vert, next.second.vert})) { + let edge = &edges[i]; + let &next = (i == edges.size() - 1) ? edges[faceEdgeStart] : edges[i + 1]; + if (let twinI = tryGet(vertPairEdges, {edge->second.vert, next.second.vert})) { edge->second.twin = edges[*twinI].first; edges[*twinI].second.twin = edge->first; } else { @@ -250,18 +250,18 @@ std::tuple readFile(const wchar_t *file, } } } - for (auto &pair : faces) + for (let &pair : faces) state.surf.faces = std::move(state.surf.faces).insert(pair); - for (auto &pair : verts) + for (let &pair : verts) state.surf.verts = std::move(state.surf.verts).insert(pair); - for (auto &pair : edges) + for (let &pair : edges) state.surf.edges = std::move(state.surf.edges).insert(pair); state.selFaces = readSet(handle, faces); state.selVerts = readSet(handle, verts); state.selEdges = readSet(handle, edges); read(handle, &state.SAVE_DATA, sizeof(EditorState) - offsetof(EditorState, SAVE_DATA)); - ViewState view = readVal(handle); + let view = readVal(handle); Library library; library.rootPath = libraryPath; @@ -279,7 +279,7 @@ std::tuple readFile(const wchar_t *file, PathCombine(combined, folder, relative); else PathCombine(combined, libraryPath, relative); - id_t id = readVal(handle); + let id = readVal(handle); if (combined[0]) library.addFile(id, combined); } @@ -308,14 +308,14 @@ void writeObj(const wchar_t *file, const Surface &surf, const Library &library, std::unordered_map vertIndices; int v = 1; - for (auto &vert : surf.verts) { - auto pos = vert.second.pos; + for (let &vert : surf.verts) { + let pos = vert.second.pos; write(handle, buf, sprintf(buf, "v %f %f %f\n", pos.x, pos.y, pos.z)); vertIndices[vert.first] = v++; } std::unordered_map> matFaces; - for (auto &pair : surf.faces) { + for (let &pair : surf.faces) { if (pair.second.paint->material != Paint::HOLE_MATERIAL) matFaces[pair.second.paint->material].push_back(pair.second); } @@ -324,9 +324,9 @@ void writeObj(const wchar_t *file, const Surface &surf, const Library &library, std::unordered_map texCoordIndices; std::vector faceVerts; std::vector faceIndices; - for (auto &pair : matFaces) { + for (let &pair : matFaces) { std::wstring texFile; - if (auto path = tryGet(library.idPaths, pair.first)) { + if (let path = tryGet(library.idPaths, pair.first)) { texFile = PathFindFileName(path->c_str()); std::replace(texFile.begin(), texFile.end(), L' ', L'_'); } else { @@ -339,10 +339,10 @@ void writeObj(const wchar_t *file, const Surface &surf, const Library &library, matNames[matName] = pair.first; write(handle, buf, sprintf(buf, "\nusemtl %S", matName.c_str())); - for (auto &face : pair.second) { - glm::vec3 normal = faceNormal(surf, face); + for (let &face : pair.second) { + let normal = faceNormal(surf, face); int vn; - if (auto vnPtr = tryGet(normalIndices, normal)) { + if (let vnPtr = tryGet(normalIndices, normal)) { vn = *vnPtr; } else { vn = (int)normalIndices.size() + 1; @@ -352,10 +352,10 @@ void writeObj(const wchar_t *file, const Surface &surf, const Library &library, glm::mat4x2 texMat = faceTexMat(face.paint, normal); faceVerts.clear(); - for (auto &edge : FaceEdges(surf, face)) { - glm::vec2 texCoord = texMat * glm::vec4(edge.second.vert.in(surf).pos, 1); + for (let edge : FaceEdges(surf, face)) { + let texCoord = texMat * glm::vec4(edge.second.vert.in(surf).pos, 1); int vt; - if (auto vtPtr = tryGet(texCoordIndices, texCoord)) { + if (let vtPtr = tryGet(texCoordIndices, texCoord)) { vt = *vtPtr; } else { vt = (int)texCoordIndices.size() + 1; @@ -370,7 +370,7 @@ void writeObj(const wchar_t *file, const Surface &surf, const Library &library, for (size_t i = 0; i < faceIndices.size(); ) { write(handle, "\nf", 2); for (size_t j = 0; j < 3; j++, i++) { - const ObjFaceVert &ofv = faceVerts[faceIndices[i]]; + let &ofv = faceVerts[faceIndices[i]]; write(handle, buf, sprintf(buf, " %d/%d/%d", ofv.v, ofv.vt, vn)); } } @@ -391,9 +391,9 @@ void writeObj(const wchar_t *file, const Surface &surf, const Library &library, throw winged_error(L"Error saving MTL file"); char buf[256]; - for (auto &pair : matNames) { + for (let &pair : matNames) { write(handle, buf, sprintf(buf, "newmtl %S\n", pair.first.c_str())); - if (auto texPath = tryGet(library.idPaths, pair.second)) { + if (let texPath = tryGet(library.idPaths, pair.second)) { wchar_t relative[MAX_PATH] = L""; PathRelativePathTo(relative, folder, FILE_ATTRIBUTE_DIRECTORY, texPath->c_str(), 0); for (wchar_t *c = relative; *c; c++) diff --git a/src/image.cpp b/src/image.cpp index 3832a0b..252931a 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -11,6 +11,7 @@ #endif #pragma warning(disable: 4458) #include +#include "macros.h" namespace Gdi = Gdiplus; @@ -44,8 +45,8 @@ ImageData loadImage(const wchar_t *path) { if (!checkStatus(bitmap.GetLastStatus())) return image; Gdi::Rect rect(0, 0, bitmap.GetWidth(), bitmap.GetHeight()); - int stride = -rect.Width * 4; // bottom-up order - int bufSize = rect.Width * rect.Height * 4; + let stride = -rect.Width * 4; // bottom-up order + let bufSize = rect.Width * rect.Height * 4; std::unique_ptr buffer(new uint8_t[bufSize]); Gdi::BitmapData data = { (UINT)rect.Width, (UINT)rect.Height, stride, PixelFormat32bppARGB, buffer.get() + bufSize + stride, 0 }; diff --git a/src/macros.h b/src/macros.h new file mode 100644 index 0000000..b9cf755 --- /dev/null +++ b/src/macros.h @@ -0,0 +1,6 @@ +// This should only be included after all other headers! +#pragma once + +// Immutable variable declarations +// This helps me pretend C++ is a better language +#define let const auto diff --git a/src/main.cpp b/src/main.cpp index ad68916..1bd446e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ #include "resource.h" #include #include +#include "macros.h" #pragma comment(lib, "Rpcrt4.lib") #pragma comment(lib, "Opengl32.lib") @@ -63,10 +64,10 @@ static std::vector sortEdgeLoop(const Surface &surf, immer::setedge.in(newState.surf); - const HEdge &twin = edge.twin.in(newState.surf); - const HEdge &twinNext = twin.next.in(newState.surf); + let &edge = vert->edge.in(newState.surf); + let &twin = edge.twin.in(newState.surf); + let &twinNext = twin.next.in(newState.surf); if (twinNext.twin.in(newState.surf).next == vert->edge) { newState.surf = joinVerts(std::move(newState.surf), edge.prev, vert->edge); anyDeleted = true; @@ -111,11 +112,11 @@ static EditorState erase(EditorState state) { if (!anyDeleted) throw winged_error(); } else if (state.selMode == SEL_SOLIDS) { - for (auto &v : state.selVerts) + for (let &v : state.selVerts) newState.surf.verts = std::move(newState.surf.verts).erase(v); - for (auto &f : state.selFaces) + for (let &f : state.selFaces) newState.surf.faces = std::move(newState.surf.faces).erase(f); - for (auto &e : state.selEdges) + for (let &e : state.selEdges) newState.surf.edges = std::move(newState.surf.edges).erase(e) .erase(e.in(state.surf).twin); } else { @@ -136,7 +137,7 @@ INT_PTR CALLBACK matrixDlgProc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam) switch (msg) { case WM_INITDIALOG: { SetWindowLongPtr(dlg, DWLP_USER, lParam); - glm::mat3 &mat = *(glm::mat3 *)lParam; + let &mat = *(glm::mat3 *)lParam; for (int i = 0; i < 9; i++) { TCHAR buf[64]; _stprintf(buf, L"%g", mat[i % 3][i / 3]); @@ -213,20 +214,20 @@ void MainWindow::updateStatus() { SendMessage(statusWnd, SB_SETTEXT, STATUS_SELECT, (LPARAM)buf); if (g_tool == TOOL_SELECT && activeViewport->mouseMode == MOUSE_TOOL) { - auto moved = activeViewport->moved; + let moved = activeViewport->moved; _stprintf(buf, L"Move %.3g, %.3g, %.3g", VEC3_ARGS(moved)); } else if (numDrawPoints() > 0 && g_hover.type) { - glm::vec3 lastPos = g_drawVerts.empty() + let lastPos = g_drawVerts.empty() ? g_state.selVerts.begin()->in(g_state.surf).pos : g_drawVerts.back(); _stprintf(buf, L"Len: %g", glm::distance(lastPos, g_hover.point)); } else if (g_state.selEdges.size() == 1) { - HEdge edge = g_state.selEdges.begin()->in(g_state.surf); - glm::vec3 v1 = edge.vert.in(g_state.surf).pos; - glm::vec3 v2 = edge.twin.in(g_state.surf).vert.in(g_state.surf).pos; + let &edge = g_state.selEdges.begin()->in(g_state.surf); + let v1 = edge.vert.in(g_state.surf).pos; + let v2 = edge.twin.in(g_state.surf).vert.in(g_state.surf).pos; _stprintf(buf, L"Len: %g", glm::distance(v1, v2)); } else if (g_state.selVerts.size() == 1) { - glm::vec3 pos = g_state.selVerts.begin()->in(g_state.surf).pos; + let pos = g_state.selVerts.begin()->in(g_state.surf).pos; _stprintf(buf, L"Pos: %.3g, %.3g, %.3g", VEC3_ARGS(pos)); } else { buf[0] = 0; @@ -272,7 +273,7 @@ void MainWindow::updateStatus() { } SendMessage(statusWnd, SB_SETTEXT, STATUS_HELP, (LPARAM)helpText); - HMENU menu = GetMenu(wnd); + let menu = GetMenu(wnd); onInitMenu(wnd, menu); updateToolbarStates(toolbarWnd, menu); } @@ -281,7 +282,7 @@ void MainWindow::refreshAll() { g_renderMeshDirty = true; mainViewport.invalidateRenderMesh(); mainViewport.refresh(); - for (auto &viewport : extraViewports) { + for (let &viewport : extraViewports) { viewport->invalidateRenderMesh(); viewport->refresh(); } @@ -291,7 +292,7 @@ void MainWindow::refreshAllImmediate() { g_renderMeshDirty = true; mainViewport.invalidateRenderMesh(); mainViewport.refreshImmediate(); - for (auto &viewport : extraViewports) { + for (let &viewport : extraViewports) { viewport->invalidateRenderMesh(); viewport->refreshImmediate(); } @@ -310,7 +311,7 @@ void MainWindow::showError(winged_error err) { MessageBox(wnd, err.message, APP_NAME, MB_ICONERROR); } else { MessageBeep(MB_OK); - HCURSOR prevCursor = SetCursor(LoadCursor(NULL, IDC_NO)); + let prevCursor = SetCursor(LoadCursor(NULL, IDC_NO)); Sleep(300); SetCursor(prevCursor); } @@ -321,7 +322,7 @@ void MainWindow::showStdException(std::exception e) { } void MainWindow::updateHover() { - POINT pt = cursorPos(); + let pt = cursorPos(); if (hoveredViewport && WindowFromPoint(pt) == hoveredViewport->wnd) { hoveredViewport->updateHover(screenToClient(hoveredViewport->wnd, pt)); setCursorHitTest(hoveredViewport->wnd, pt); @@ -342,7 +343,7 @@ void MainWindow::setTool(Tool tool) { if (!(TOOL_FLAGS[tool] & (1 << g_state.selMode))) g_state.selMode = SEL_ELEMENTS; // TODO if ((TOOL_FLAGS[tool] & TOOLF_DRAW) && (TOOL_FLAGS[tool] & TOOLF_HOVFACE)) - if (auto face = g_hoverFace.find(g_state.surf)) + if (let face = g_hoverFace.find(g_state.surf)) g_state.workPlane = facePlane(g_state.surf, *face); updateHover(); } @@ -354,14 +355,14 @@ bool MainWindow::removeViewport(ViewportWindow *viewport) { hoveredViewport = NULL; // hack https://stackoverflow.com/a/60220391/11525734 std::unique_ptr stalePtr(viewport); - auto ret = extraViewports.erase(stalePtr); + let ret = extraViewports.erase(stalePtr); stalePtr.release(); return ret; } void MainWindow::closeExtraViewports() { activeViewport = &mainViewport; - for (auto &viewport : extraViewports) + for (let &viewport : extraViewports) viewport->destroy(); extraViewports.clear(); } @@ -387,7 +388,7 @@ void MainWindow::open(const TCHAR *path) { } bool MainWindow::saveAs() { - const TCHAR filters[] = L"WingEd File (.wing)\0*.wing\0All Files\0*.*\0\0"; + let filters = L"WingEd File (.wing)\0*.wing\0All Files\0*.*\0\0"; if (GetSaveFileName(tempPtr(makeOpenFileName(filePath, wnd, filters, L"wing")))) { writeFile(filePath, g_state, mainViewport.view, g_library); unsavedCount = 0; @@ -408,7 +409,7 @@ bool MainWindow::save() { bool MainWindow::promptSaveChanges() { if (unsavedCount) { - TCHAR *name = (filePath[0] == 0) ? L"Untitled" : PathFindFileName(filePath); + let name = (filePath[0] == 0) ? L"Untitled" : PathFindFileName(filePath); TCHAR buf[256]; _stprintf(buf, L"Save changes to %s?", name); switch (MessageBox(wnd, buf, APP_NAME, MB_YESNOCANCEL)) { @@ -445,7 +446,7 @@ BOOL MainWindow::onCreate(HWND, LPCREATESTRUCT) { {TIMG_FLIP, IDM_FLIP_NORMALS, TBSTATE_ENABLED, BTNS_BUTTON}, {TIMG_SNAP, IDM_SNAP, TBSTATE_ENABLED, BTNS_BUTTON}, }; - HBITMAP toolbarBmp = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_TOOLBAR)); + let toolbarBmp = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_TOOLBAR)); toolbarWnd = CreateToolbarEx(wnd, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | CCS_TOP | TBSTYLE_TOOLTIPS, 1, NUM_TOOLBAR_IMAGES, NULL, (UINT)(size_t)toolbarBmp, buttons, _countof(buttons), @@ -485,8 +486,8 @@ void MainWindow::onActivate(HWND, UINT state, HWND, BOOL minimized) { void MainWindow::onSize(HWND, UINT, int cx, int cy) { SendMessage(toolbarWnd, TB_AUTOSIZE, 0, 0); - int toolbarHeight = rectHeight(windowRect(toolbarWnd)); - int statusHeight = rectHeight(windowRect(statusWnd)); + let toolbarHeight = rectHeight(windowRect(toolbarWnd)); + let statusHeight = rectHeight(windowRect(statusWnd)); MoveWindow(statusWnd, 0, cy - statusHeight, cx, statusHeight, true); MoveWindow(mainViewport.wnd, 0, toolbarHeight, cx, cy - toolbarHeight - statusHeight, true); } @@ -511,7 +512,7 @@ void MainWindow::onCommand(HWND, int id, HWND ctl, UINT code) { break; case IDM_OPEN: { TCHAR newFile[MAX_PATH] = L""; - const TCHAR filters[] = L"WingEd File (.wing)\0*.wing\0\0"; + let filters = L"WingEd File (.wing)\0*.wing\0\0"; if (GetOpenFileName(tempPtr(makeOpenFileName(newFile, wnd, filters, L"wing")))) { if (promptSaveChanges()) open(newFile); @@ -530,7 +531,7 @@ void MainWindow::onCommand(HWND, int id, HWND ctl, UINT code) { lstrcpy(PathFindExtension(objFilePath), L".obj"); } TCHAR mtlFile[MAX_PATH] = L""; - const TCHAR filters[] = L"OBJ file (.obj)\0*.obj\0All Files\0*.*\0\0"; + let filters = L"OBJ file (.obj)\0*.obj\0All Files\0*.*\0\0"; auto saveFile = makeOpenFileName(objFilePath, wnd, filters, L"obj"); saveFile.lpstrFileTitle = mtlFile; saveFile.nMaxFileTitle = _countof(mtlFile); @@ -544,12 +545,12 @@ void MainWindow::onCommand(HWND, int id, HWND ctl, UINT code) { case IDM_ADD_TEXTURE: { TCHAR texFile[MAX_PATH] = L""; // https://learn.microsoft.com/en-us/windows/win32/gdiplus/-gdiplus-types-of-bitmaps-about - const TCHAR filters[] = + let filters = L"Supported Images (.png, .jpg, .jpeg, .bmp, .gif, .tif, .tiff)\0" "*.png;*.jpg;*.jpeg;*.bmp;*.gif;*.tif;*.tiff\0" "All Files\0*.*\0\0"; if (GetOpenFileName(tempPtr(makeOpenFileName(texFile, wnd, filters)))) { - std::wstring texFileStr = texFile; + let texFileStr = std::wstring(texFile); id_t texId = g_library.pathIds[texFileStr]; if (texId == id_t{}) { texId = genId(); @@ -564,7 +565,7 @@ void MainWindow::onCommand(HWND, int id, HWND ctl, UINT code) { } case IDM_RELOAD_ASSETS: mainViewport.clearTextureCache(); - for (auto &viewport : extraViewports) + for (let &viewport : extraViewports) viewport->clearTextureCache(); break; /* Tool */ @@ -614,7 +615,7 @@ void MainWindow::onCommand(HWND, int id, HWND ctl, UINT code) { #endif /* View */ case IDM_NEW_VIEWPORT: { - auto &newViewport = *extraViewports.emplace(new ViewportWindow).first; + let &newViewport = *extraViewports.emplace(new ViewportWindow).first; newViewport->view = activeViewport->view; RECT rect = defaultWindowRect(clientSize(activeViewport->wnd), false, WS_OVERLAPPEDWINDOW, WS_EX_TOOLWINDOW); @@ -657,18 +658,18 @@ void MainWindow::onCommand(HWND, int id, HWND ctl, UINT code) { EditorState newState = g_state; newState.selVerts = {}; newState.selEdges = {}; - for (auto f : g_state.selFaces) { + for (let &f : g_state.selFaces) { immer::set_transient extEdges; - for (auto e : g_state.selEdges) { - HEdge edge = e.in(newState.surf); + for (let &e : g_state.selEdges) { + let &edge = e.in(newState.surf); if (edge.face == f) extEdges.insert(e); else if (edge.twin.in(newState.surf).face == f) extEdges.insert(edge.twin); } newState.surf = extrudeFace(newState.surf, f, extEdges.persistent()); - for (auto e : extEdges) { - auto primary = primaryEdge(e.pair(newState.surf)); + for (let &e : extEdges) { + let primary = primaryEdge(e.pair(newState.surf)); newState.selEdges = std::move(newState.selEdges).insert(primary); } } @@ -677,12 +678,12 @@ void MainWindow::onCommand(HWND, int id, HWND ctl, UINT code) { break; } case IDM_SPLIT_LOOP: { - auto loop = sortEdgeLoop(g_state.surf, g_state.selEdges); + let loop = sortEdgeLoop(g_state.surf, g_state.selEdges); EditorState newState = g_state; newState.surf = splitEdgeLoop(g_state.surf, loop); newState.selVerts = {}; newState.selEdges = {}; - for (auto &e : loop) { + for (let &e : loop) { edge_id primary = primaryEdge(e.pair(newState.surf)); newState.selEdges = std::move(newState.selEdges).insert(primary); } @@ -717,8 +718,8 @@ void MainWindow::onCommand(HWND, int id, HWND ctl, UINT code) { case IDM_TRANSFORM_MATRIX: if (DialogBoxParam(GetModuleHandle(NULL), L"IDD_MATRIX", wnd, matrixDlgProc, (LPARAM)&userMatrix) == IDOK) { - auto verts = selAttachedVerts(g_state); - glm::vec3 center = vertsCenter(g_state.surf, verts); + let verts = selAttachedVerts(g_state); + let center = vertsCenter(g_state.surf, verts); EditorState newState = g_state; newState.surf = transformVertices(g_state.surf, verts, glm::translate( glm::translate(glm::mat4(1), center) * glm::mat4(userMatrix), -center)); @@ -729,14 +730,14 @@ void MainWindow::onCommand(HWND, int id, HWND ctl, UINT code) { if (DialogBoxParam(GetModuleHandle(NULL), L"IDD_MATRIX", wnd, matrixDlgProc, (LPARAM)&userPaintMatrix) == IDOK) { EditorState newState = g_state; - glm::mat3 mat = glm::inverse(userPaintMatrix); + let mat = glm::inverse(userPaintMatrix); newState.surf = transformPaint(g_state.surf, g_state.selFaces, mat); pushUndo(std::move(newState)); } break; case IDM_MARK_HOLE: EditorState newState = g_state; - Paint paint = {Paint::HOLE_MATERIAL}; + let paint = Paint{Paint::HOLE_MATERIAL}; newState.surf = assignPaint(g_state.surf, g_state.selFaces, paint); pushUndo(std::move(newState)); break; @@ -753,9 +754,9 @@ void MainWindow::onCommand(HWND, int id, HWND ctl, UINT code) { } void MainWindow::onInitMenu(HWND, HMENU menu) { - bool hasSel = hasSelection(g_state); - bool selElem = g_state.selMode == SEL_ELEMENTS; - bool selSolid = g_state.selMode == SEL_SOLIDS; + let hasSel = hasSelection(g_state); + let selElem = (g_state.selMode == SEL_ELEMENTS); + let selSolid = (g_state.selMode == SEL_SOLIDS); EnableMenuItem(menu, IDM_CLEAR_SELECT, (hasSel || numDrawPoints() > 0) ? MF_ENABLED : MF_GRAYED); EnableMenuItem(menu, IDM_UNDO, undoStack.empty() ? MF_GRAYED : MF_ENABLED); @@ -832,11 +833,11 @@ int APIENTRY _tWinMain(HINSTANCE instance, HINSTANCE, LPTSTR, int showCmd) { WNDCLASSEX mainClass = makeClass(APP_NAME, windowImplProc); mainClass.lpszMenuName = APP_NAME; RegisterClassEx(&mainClass); - HWND wnd = g_mainWindow.create(APP_NAME, defaultWindowRect({640, 480}, true)); + let wnd = g_mainWindow.create(APP_NAME, defaultWindowRect({640, 480}, true)); if (!wnd) return -1; ShowWindow(wnd, showCmd); - HACCEL mainAccel = LoadAccelerators(instance, L"Accel"); - HACCEL viewAccel = LoadAccelerators(instance, L"ViewAccel"); + let mainAccel = LoadAccelerators(instance, L"Accel"); + let viewAccel = LoadAccelerators(instance, L"ViewAccel"); MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { if (TranslateAccelerator(wnd, mainAccel, &msg) diff --git a/src/mathutil.cpp b/src/mathutil.cpp index 6b1cbe4..3f647b0 100644 --- a/src/mathutil.cpp +++ b/src/mathutil.cpp @@ -1,6 +1,7 @@ #include "mathutil.h" #include #include +#include "macros.h" namespace winged { @@ -12,8 +13,8 @@ glm::vec3 accumPolyNormal(glm::vec3 v1, glm::vec3 v2) { // Newell's method // https://web.archive.org/web/20070507025303/http://www.acm.org/tog/GraphicsGems/gemsiii/newell.c // an extension to 3D of https://stackoverflow.com/a/1165943 - glm::vec3 sum = v1 + v2; - glm::vec3 diff = v1 - v2; + let sum = v1 + v2; + let diff = v1 - v2; return glm::vec3(diff.y * sum.z, diff.z * sum.x, diff.x * sum.y); } @@ -22,7 +23,7 @@ bool intersectRayPlane(const Ray &ray, const Plane &plane, glm::vec3 *point) { if (glm::intersectRayPlane(ray.org, ray.dir, plane.org, plane.norm, t)) { *point = ray.org + t * ray.dir; // fix precision issues, make sure point lies exactly on plane - int axis = maxAxis(glm::abs(plane.norm)); + let axis = maxAxis(glm::abs(plane.norm)); (*point)[axis] = plane.org[axis] + solvePlane(*point - plane.org, plane.norm, axis); return true; } @@ -30,7 +31,7 @@ bool intersectRayPlane(const Ray &ray, const Plane &plane, glm::vec3 *point) { } float solvePlane(glm::vec3 vec, glm::vec3 norm, int axis) { - int a = (axis + 1) % 3, b = (axis + 2) % 3; + let a = (axis + 1) % 3, b = (axis + 2) % 3; return -(norm[a] * vec[a] + norm[b] * vec[b]) / norm[axis]; } diff --git a/src/ops.cpp b/src/ops.cpp index a8fc170..979bf8c 100644 --- a/src/ops.cpp +++ b/src/ops.cpp @@ -4,6 +4,7 @@ #ifdef CHROMA_DEBUG #include "winchroma.h" #endif +#include "macros.h" namespace winged { @@ -16,13 +17,13 @@ uint32_t name(id_t id) { template static void insertAll(immer::map *map, std::initializer_list> pairs) { - for (auto &p : pairs) + for (let &p : pairs) *map = std::move(*map).insert(p); } template static void eraseAll(immer::map *map, std::initializer_list keys) { - for (auto &k : keys) + for (let &k : keys) *map = std::move(*map).erase(k); } @@ -309,7 +310,7 @@ Surface splitFace(Surface surf, edge_id e1, edge_id e2, // │edge1 prev2│ // │ │ // ╰ newFace ╰ - int numPoints = (int)points.size(); + let numPoints = (int)points.size(); if (loopIndex >= 0 && loopIndex >= numPoints - 2) throw winged_error(); std::vector newEdges1 = makeEdgePairs(numPoints + 1); std::vector newEdges2 = makeEdgePairs(numPoints + 1); @@ -443,12 +444,12 @@ Surface extrudeFace(Surface surf, face_id f, const immer::set &extEdges face_pair face = f.pair(surf); std::vector topEdges, baseTwins; std::vector baseVerts; - for (auto topEdge : FaceEdges(surf, face.second)) { + for (let topEdge : FaceEdges(surf, face.second)) { topEdges.push_back(topEdge); baseTwins.push_back(topEdge.second.twin.pair(surf)); baseVerts.push_back(topEdge.second.vert.pair(surf)); } - size_t size = topEdges.size(); + let size = topEdges.size(); std::vector baseEdges = makeEdgePairs(size); std::vector topTwins = makeEdgePairs(size); std::vector joinEdges = makeEdgePairs(size); @@ -465,8 +466,8 @@ Surface extrudeFace(Surface surf, face_id f, const immer::set &extEdges // baseVert baseTwin for (size_t i = 0, j = size - 1; i < size; j = i++) { - bool extrudeEdgeI = extEdges.empty() || extEdges.count(topEdges[i].first); - bool extrudeEdgeJ = extEdges.empty() || extEdges.count(topEdges[j].first); + let extrudeEdgeI = extEdges.empty() || extEdges.count(topEdges[i].first); + let extrudeEdgeJ = extEdges.empty() || extEdges.count(topEdges[j].first); if (!extrudeEdgeI && !extrudeEdgeJ) continue; @@ -513,7 +514,7 @@ Surface extrudeFace(Surface surf, face_id f, const immer::set &extEdges for (size_t i = 0, j = size - 1; i < size; j = i++) { insertAll(&surf.edges, {topEdges[i], baseTwins[i]}); insertAll(&surf.verts, {baseVerts[i]}); - bool extrudeEdgeI = extEdges.empty() || extEdges.count(topEdges[i].first); + let extrudeEdgeI = extEdges.empty() || extEdges.count(topEdges[i].first); if (extrudeEdgeI) { insertAll(&surf.edges, {baseEdges[i], topTwins[i]}); insertAll(&surf.faces, {sideFaces[i]}); @@ -527,7 +528,7 @@ Surface extrudeFace(Surface surf, face_id f, const immer::set &extEdges } Surface splitEdgeLoop(Surface surf, const std::vector &loop) { - size_t size = loop.size(); + let size = loop.size(); std::vector newEdges1 = makeEdgePairs(size); std::vector newEdges2 = makeEdgePairs(size); std::vector newVerts = makeVertPairs(size); @@ -609,7 +610,7 @@ Surface joinEdgeLoops(Surface surf, edge_id e1, edge_id e2) { } Surface makePolygonPlane(Surface surf, const std::vector &points, face_id *newFace) { - size_t size = points.size(); + let size = points.size(); if (size < 3) throw winged_error(); std::vector edges1 = makeEdgePairs(size); @@ -648,7 +649,7 @@ Surface makePolygonPlane(Surface surf, const std::vector &points, fac } Surface transformVertices(Surface surf, const immer::set &verts, const glm::mat4 &m) { - for (auto &v : verts) { + for (let &v : verts) { vert_pair vert = v.pair(surf); vert.second.pos = m * glm::vec4(vert.second.pos, 1); insertAll(&surf.verts, {vert}); @@ -657,7 +658,7 @@ Surface transformVertices(Surface surf, const immer::set &verts, const } Surface snapVertices(Surface surf, const immer::set &verts, float grid) { - for (auto &v : verts) { + for (let &v : verts) { vert_pair vert = v.pair(surf); vert.second.pos = glm::roundEven(vert.second.pos / grid) * grid; insertAll(&surf.verts, {vert}); @@ -666,7 +667,7 @@ Surface snapVertices(Surface surf, const immer::set &verts, float grid) } Surface assignPaint(Surface surf, const immer::set &faces, immer::box paint) { - for (auto &f : faces) { + for (let &f : faces) { face_pair face = f.pair(surf); face.second.paint = paint; insertAll(&surf.faces, {face}); @@ -675,7 +676,7 @@ Surface assignPaint(Surface surf, const immer::set &faces, immer::box

&faces, const glm::mat3 &m) { - for (auto &f : faces) { + for (let &f : faces) { face_pair face = f.pair(surf); Paint paint = face.second.paint; paint.texTF = glm::mat3(paint.texTF) * m; @@ -690,16 +691,16 @@ Surface duplicate(Surface surf, const immer::set &edges, std::unordered_map edgeMap; std::unordered_map vertMap; std::unordered_map faceMap; - for (auto e : edges) { + for (let &e : edges) { edgeMap[e] = genId(); edgeMap[e.in(surf).twin] = genId(); } - for (auto v : verts) + for (let &v : verts) vertMap[v] = genId(); - for (auto f : faces) + for (let &f : faces) faceMap[f] = genId(); - for (auto pair : edgeMap) { + for (let &pair : edgeMap) { edge_pair edge = {pair.second, pair.first.in(surf)}; edge.second.twin = edgeMap[edge.second.twin]; edge.second.next = edgeMap[edge.second.next]; @@ -708,12 +709,12 @@ Surface duplicate(Surface surf, const immer::set &edges, edge.second.face = faceMap[edge.second.face]; insertAll(&surf.edges, {edge}); } - for (auto pair : vertMap) { + for (let &pair : vertMap) { vert_pair vert = {pair.second, pair.first.in(surf)}; vert.second.edge = edgeMap[vert.second.edge]; insertAll(&surf.verts, {vert}); } - for (auto pair : faceMap) { + for (let &pair : faceMap) { face_pair face = {pair.second, pair.first.in(surf)}; face.second.edge = edgeMap[face.second.edge]; insertAll(&surf.faces, {face}); @@ -737,7 +738,7 @@ Surface flipAllNormals(Surface surf) { Surface flipNormals(Surface surf, const immer::set &edges, const immer::set &verts) { - for (auto e : edges) { + for (let &e : edges) { edge_pair edge = e.pair(surf); edge_pair twin = edge.second.twin.pair(surf); std::swap(edge.second.prev, edge.second.next); @@ -745,7 +746,7 @@ Surface flipNormals(Surface surf, std::swap(edge.second.vert, twin.second.vert); insertAll(&surf.edges, {edge, twin}); } - for (auto v : verts) { + for (let &v : verts) { vert_pair vert = v.pair(surf); vert.second.edge = vert.second.edge.in(surf).twin; insertAll(&surf.verts, {vert}); @@ -758,7 +759,7 @@ Surface flipNormals(Surface surf, void validateSurface(const Surface &) {} #else void validateSurface(const Surface &surf) { - auto tooMany = winged_error(L"Too many geometry errors (see log)"); + let tooMany = winged_error(L"Too many geometry errors (see log)"); #define CHECK_VALID(cond, message, ...) \ if (!(cond)) { \ LOG(message, __VA_ARGS__); \ @@ -766,15 +767,15 @@ void validateSurface(const Surface &surf) { } int invalid = 0; - for (auto &pair : surf.verts) { + for (let &pair : surf.verts) { CHECK_VALID(pair.second.edge.find(surf), "Vert %08X has invalid edge ID %08X!", name(pair), name(pair.second.edge)); } - for (auto &pair : surf.faces) { + for (let &pair : surf.faces) { CHECK_VALID(pair.second.edge.find(surf), "Face %08X has invalid edge ID %08X!", name(pair), name(pair.second.edge)); } - for (auto &pair : surf.edges) { + for (let &pair : surf.edges) { CHECK_VALID(pair.second.twin.find(surf), "Edge %08X has invalid twin ID %08X!", name(pair), name(pair.second.twin)); CHECK_VALID(pair.second.next.find(surf), "Edge %08X has invalid next ID %08X!", @@ -791,21 +792,21 @@ void validateSurface(const Surface &surf) { throw winged_error(L"Invalid element IDs (see log)"); // can't do any more checks } - for (auto &pair : surf.verts) { - for (auto &vertEdge : VertEdges(surf, pair.second)) { + for (let &pair : surf.verts) { + for (let vertEdge : VertEdges(surf, pair.second)) { CHECK_VALID(vertEdge.second.vert == pair.first, "Edge %08X attached to vert %08X references a different vert %08X!", name(vertEdge), name(pair), name(vertEdge.second.vert)); } } - for (auto &pair : surf.faces) { - for (auto &faceEdge : FaceEdges(surf, pair.second)) { + for (let &pair : surf.faces) { + for (let faceEdge : FaceEdges(surf, pair.second)) { CHECK_VALID(faceEdge.second.face == pair.first, "Edge %08X attached to face %08X references a different face %08X!", name(faceEdge), name(pair), name(faceEdge.second.face)); } } - for (auto &pair : surf.edges) { + for (let &pair : surf.edges) { CHECK_VALID(pair.second.twin != pair.first, "Edge %08X twin is itself!", name(pair)); CHECK_VALID(pair.second.next != pair.first, "Edge %08X next is itself!", name(pair)); CHECK_VALID(pair.second.prev != pair.first, "Edge %08X prev is itself!", name(pair)); @@ -822,7 +823,7 @@ void validateSurface(const Surface &surf) { CHECK_VALID(pair.second.next != pair.second.prev, "Edge %08X forms a two-sided face!", name(pair)); bool foundEdge = false; - for (auto &faceEdge : FaceEdges(surf, pair.second.face.in(surf))) { + for (let faceEdge : FaceEdges(surf, pair.second.face.in(surf))) { if (faceEdge.first == pair.first) { foundEdge = true; break; @@ -831,7 +832,7 @@ void validateSurface(const Surface &surf) { CHECK_VALID(foundEdge, "Edge %08X can't be reached from face %08X!", name(pair), name(pair.second.face)); foundEdge = false; - for (auto &vertEdge : VertEdges(surf, pair.second.vert.in(surf))) { + for (let vertEdge : VertEdges(surf, pair.second.vert.in(surf))) { if (vertEdge.first == pair.first) { foundEdge = true; break; diff --git a/src/picking.cpp b/src/picking.cpp index 05a4389..d599a9a 100644 --- a/src/picking.cpp +++ b/src/picking.cpp @@ -1,18 +1,19 @@ #include "picking.h" #include #include +#include "macros.h" namespace winged { -const float PICK_POINT_SIZE = 15; -const float PICK_EDGE_SIZE = 15; -const float EDGE_Z_OFFSET = -.001f; -const float VERT_Z_OFFSET = -.002f; +let PICK_POINT_SIZE = 15.0f; +let PICK_EDGE_SIZE = 15.0f; +let EDGE_Z_OFFSET = -.001f; +let VERT_Z_OFFSET = -.002f; // returns normalized device coords glm::vec3 projectPoint(glm::vec3 point, const glm::mat4 &project) { // https://stackoverflow.com/a/63084621 - glm::vec4 tv = project * glm::vec4(point, 1); + let tv = project * glm::vec4(point, 1); return glm::vec3(tv) / tv.w; } @@ -23,10 +24,10 @@ glm::vec2 screenPosToNDC(glm::vec2 pos, glm::vec2 windowDim) { } Ray viewPosToRay(glm::vec2 normPos, const glm::mat4 &project) { - const glm::mat4 invProj = glm::inverse(project); - glm::vec3 org = projectPoint(glm::vec3(normPos, -1), invProj); // near plane intersect - glm::vec3 farPt = projectPoint(glm::vec3(normPos, 1), invProj); - glm::vec3 dir = glm::normalize(farPt - org); + let invProj = glm::inverse(project); + let org = projectPoint(glm::vec3(normPos, -1), invProj); // near plane intersect + let farPt = projectPoint(glm::vec3(normPos, 1), invProj); + let dir = glm::normalize(farPt - org); return {org, dir}; } @@ -34,14 +35,14 @@ glm::vec3 snapPlanePoint(glm::vec3 point, const Plane &plane, float grid) { if (grid == 0) return point; point = glm::round(point / grid) * grid; - int axis = maxAxis(glm::abs(plane.norm)); + let axis = maxAxis(glm::abs(plane.norm)); point[axis] = plane.org[axis] + solvePlane(point - plane.org, plane.norm, axis); return point; } bool pickVert(glm::vec3 vertPos, glm::vec2 normCur, glm::vec2 windowDim, const glm::mat4 &project, float *depth) { - const glm::vec2 normPointDist = PICK_POINT_SIZE / windowDim; + let normPointDist = PICK_POINT_SIZE / windowDim; glm::vec3 normVert = projectPoint(vertPos, project); normVert.z += VERT_Z_OFFSET; if (glm::all(glm::lessThanEqual(glm::abs(glm::vec2(normVert) - normCur), normPointDist)) @@ -55,7 +56,7 @@ bool pickVert(glm::vec3 vertPos, glm::vec2 normCur, glm::vec2 windowDim, const g PickResult pickElement(const Surface &surf, PickType types, glm::vec2 normCur, glm::vec2 windowDim, const glm::mat4 &project, float grid, PickResult result) { if (types & PICK_VERT) { - for (auto &vert : surf.verts) { + for (let &vert : surf.verts) { float depth; if (pickVert(vert.second.pos, normCur, windowDim, project, &depth) && depth < result.depth) { @@ -66,24 +67,24 @@ PickResult pickElement(const Surface &surf, PickType types, glm::vec2 normCur, return result; // skip extra matrix calculations } - Ray ray = viewPosToRay(normCur, project); + let ray = viewPosToRay(normCur, project); if (types & PICK_EDGE) { - const glm::vec2 normEdgeDist = PICK_EDGE_SIZE / windowDim; - for (auto &edge : surf.edges) { + let normEdgeDist = PICK_EDGE_SIZE / windowDim; + for (let &edge : surf.edges) { if (!isPrimary(edge)) continue; - glm::vec3 v1 = edge.second.vert.in(surf).pos; - glm::vec3 v2 = edge.second.twin.in(surf).vert.in(surf).pos; + let v1 = edge.second.vert.in(surf).pos; + let v2 = edge.second.twin.in(surf).vert.in(surf).pos; // https://math.stackexchange.com/a/3436386 // see also: https://stackoverflow.com/q/2316490/11525734 - glm::vec3 lineDir = glm::normalize(v2 - v1); - glm::vec3 cDir = glm::normalize(glm::cross(lineDir, ray.dir)); - glm::vec3 oDiff = v1 - ray.org; - glm::vec3 projection = glm::dot(oDiff, ray.dir) * ray.dir; - glm::vec3 rejection = oDiff - projection - glm::dot(oDiff, cDir) * cDir; + let lineDir = glm::normalize(v2 - v1); + let cDir = glm::normalize(glm::cross(lineDir, ray.dir)); + let oDiff = v1 - ray.org; + let projection = glm::dot(oDiff, ray.dir) * ray.dir; + let rejection = oDiff - projection - glm::dot(oDiff, cDir) * cDir; if (glm::length2(rejection) == 0) continue; - glm::vec3 vDiff = v2 - v1; + let vDiff = v2 - v1; float t = -glm::length(rejection) / glm::dot(vDiff, glm::normalize(rejection)); t = glm::clamp(t, 0.0f, 1.0f); glm::vec3 point = v1 + t * vDiff; @@ -93,8 +94,8 @@ PickResult pickElement(const Surface &surf, PickType types, glm::vec2 normCur, && glm::abs(normPoint.x - normCur.x) < normEdgeDist.x && glm::abs(normPoint.y - normCur.y) < normEdgeDist.y) { if (grid != 0) { - int axis = maxAxis(glm::abs(vDiff)); - float rounded = glm::round(point[axis] / grid) * grid; + let axis = maxAxis(glm::abs(vDiff)); + let rounded = glm::round(point[axis] / grid) * grid; t = glm::clamp((rounded - v1[axis]) / vDiff[axis], 0.0f, 1.0f); point = v1 + t * vDiff; // DON'T update normPoint (preserve depth) } @@ -109,21 +110,21 @@ PickResult pickElement(const Surface &surf, PickType types, glm::vec2 normCur, } } if (types & PICK_FACE) { - for (auto &face : surf.faces) { - glm::vec3 normal = faceNormalNonUnit(surf, face.second); + for (let &face : surf.faces) { + let normal = faceNormalNonUnit(surf, face.second); if (glm::dot(ray.dir, normal) >= 0) continue; glm::vec3 last = face.second.edge.in(surf).prev.in(surf).vert.in(surf).pos; - Plane plane = {last, normal}; // not normalized. should be fine + let plane = Plane{last, normal}; // not normalized. should be fine glm::vec3 pt; if (!intersectRayPlane(ray, plane, &pt)) continue; - int axis = maxAxis(glm::abs(normal)); - int a = (axis + 1) % 3, b = (axis + 2) % 3; + let axis = maxAxis(glm::abs(normal)); + let a = (axis + 1) % 3, b = (axis + 2) % 3; bool inside = false; - for (auto &faceEdge : FaceEdges(surf, face.second)) { - glm::vec3 vert = faceEdge.second.vert.in(surf).pos; + for (let faceEdge : FaceEdges(surf, face.second)) { + let vert = faceEdge.second.vert.in(surf).pos; // count intersections with horizontal ray // thank you Arguru if (((vert[b] <= pt[b] && pt[b] < last[b]) @@ -133,7 +134,7 @@ PickResult pickElement(const Surface &surf, PickType types, glm::vec2 normCur, last = vert; } if (inside) { - glm::vec3 normPoint = projectPoint(pt, project); + let normPoint = projectPoint(pt, project); if (normPoint.z < result.depth) { // DON'T update normPoint (preserve depth) pt = snapPlanePoint(pt, plane, grid); diff --git a/src/rendermesh.cpp b/src/rendermesh.cpp index 5a51cde..f9eb141 100644 --- a/src/rendermesh.cpp +++ b/src/rendermesh.cpp @@ -4,6 +4,7 @@ #include #include #include "main.h" +#include "macros.h" namespace winged { @@ -17,15 +18,15 @@ struct FaceTessState { static GLUtesselator *g_tess; static void CALLBACK tessBeginCallback(GLenum mode, void *data) { - auto state = (FaceTessState *)data; + let state = (FaceTessState *)data; state->mode = mode; state->startI = state->indices->size(); } static void CALLBACK tessVertexCallback(void *vertex, void *data) { - auto state = (FaceTessState *)data; - index_t index = (index_t)(size_t)vertex; - size_t numIndices = state->indices->size(); + let state = (FaceTessState *)data; + let index = (index_t)(size_t)vertex; + let numIndices = state->indices->size(); if (state->mode == GL_TRIANGLE_STRIP && numIndices - state->startI >= 3) { if ((numIndices - state->startI) % 6 == 0) { state->indices->push_back((*state->indices)[numIndices - 3]); @@ -48,13 +49,13 @@ static void CALLBACK tessErrorCallback(GLenum error, void *data) { bool tesselateFace(std::vector &faceIsOut, const Surface &surf, const Face &face, glm::vec3 normal, index_t vertI) { // https://www.glprogramming.com/red/chapter11.html - size_t initialSize = faceIsOut.size(); + let initialSize = faceIsOut.size(); FaceTessState state; state.indices = &faceIsOut; gluTessNormal(g_tess, normal.x, normal.y, normal.z); gluTessBeginPolygon(g_tess, &state); gluTessBeginContour(g_tess); - for (auto &ep : FaceEdges(surf, face)) { + for (let ep : FaceEdges(surf, face)) { glm::dvec3 dPos = ep.second.vert.in(surf).pos; gluTessVertex(g_tess, glm::value_ptr(dPos), (void *)vertI++); } @@ -90,16 +91,16 @@ void insertFaces(RenderMesh *mesh, std::vector &errFacesOut, const std::unordered_map &edgeIDIndices, const std::unordered_map> &matFaces, const Surface &surf, RenderFaceMesh::State state) { - for (auto &pair : matFaces) { + for (let &pair : matFaces) { IndexRange range = {mesh->indices.size(), 0}; - for (auto &face : pair.second) { - glm::vec3 normal = mesh->normals[edgeIDIndices.at(face.edge)]; - index_t startI = edgeIDIndices.at(face.edge); + for (let &face : pair.second) { + let normal = mesh->normals[edgeIDIndices.at(face.edge)]; + let startI = edgeIDIndices.at(face.edge); if (!tesselateFace(mesh->indices, surf, face, normal, startI)) errFacesOut.push_back(face); } range.count = mesh->indices.size() - range.start; - RenderFaceMesh faceMesh = {pair.first, range, state}; + let faceMesh = RenderFaceMesh{pair.first, range, state}; mesh->faceMeshes.push_back(faceMesh); } } @@ -113,14 +114,14 @@ void generateRenderMesh(RenderMesh *mesh, const EditorState &state) { edgeIDIndices.reserve(state.surf.edges.size()); index_t index = 0; - for (auto &fp : state.surf.faces) { - glm::vec3 normal = faceNormal(state.surf, fp.second); + for (let &fp : state.surf.faces) { + let normal = faceNormal(state.surf, fp.second); glm::mat4x2 texMat = faceTexMat(fp.second.paint, normal); - id_t mat = fp.second.paint->material; + let mat = fp.second.paint->material; if (mat == id_t{}) texMat = glm::mat2x2(0.25f) * texMat; // apply scaling to default texture - for (auto &ep : FaceEdges(state.surf, fp.second)) { - glm::vec3 v = ep.second.vert.in(state.surf).pos; + for (let ep : FaceEdges(state.surf, fp.second)) { + let v = ep.second.vert.in(state.surf).pos; mesh->vertices.push_back(v); mesh->normals.push_back(normal); mesh->texCoords.push_back(texMat * glm::vec4(v, 1)); @@ -128,17 +129,17 @@ void generateRenderMesh(RenderMesh *mesh, const EditorState &state) { } } // no normals / texCoords! - const index_t drawVertsStartI = index; - for (auto &vec : g_drawVerts) { + let drawVertsStartI = index; + for (let &vec : g_drawVerts) { mesh->vertices.push_back(vec); index++; } - const index_t hoverI = index; + let hoverI = index; mesh->vertices.push_back(g_hover.point); if (state.selMode == SEL_ELEMENTS) { mesh->ranges[ELEM_REG_VERT].start = mesh->indices.size(); - for (auto &pair : state.surf.verts) { + for (let &pair : state.surf.verts) { if (!state.selVerts.count(pair.first)) { mesh->indices.push_back(edgeIDIndices[pair.second.edge]); mesh->ranges[ELEM_REG_VERT].count++; @@ -165,7 +166,7 @@ void generateRenderMesh(RenderMesh *mesh, const EditorState &state) { } mesh->ranges[ELEM_SEL_VERT].start = mesh->indices.size(); - for (auto &v : state.selVerts) { + for (let &v : state.selVerts) { mesh->indices.push_back(edgeIDIndices[v.in(state.surf).edge]); mesh->ranges[ELEM_SEL_VERT].count++; } @@ -192,13 +193,13 @@ void generateRenderMesh(RenderMesh *mesh, const EditorState &state) { } mesh->ranges[ELEM_SEL_EDGE].start = mesh->indices.size(); - for (auto e : state.selEdges) { + for (let &e : state.selEdges) { mesh->indices.push_back(edgeIDIndices[e]); mesh->indices.push_back(edgeIDIndices[e.in(state.surf).twin]); mesh->ranges[ELEM_SEL_EDGE].count += 2; } - if (auto hoverEdge = g_hover.edge.find(state.surf)) { + if (let hoverEdge = g_hover.edge.find(state.surf)) { mesh->ranges[ELEM_HOV_EDGE] = {mesh->indices.size(), 2}; mesh->indices.push_back(edgeIDIndices[g_hover.edge]); mesh->indices.push_back(edgeIDIndices[hoverEdge->twin]); @@ -206,7 +207,7 @@ void generateRenderMesh(RenderMesh *mesh, const EditorState &state) { } mesh->ranges[ELEM_REG_EDGE].start = mesh->indices.size(); - for (auto &pair : state.surf.edges) { + for (let &pair : state.surf.edges) { if (isPrimary(pair)) { mesh->indices.push_back(edgeIDIndices[pair.first]); mesh->indices.push_back(edgeIDIndices[pair.second.twin]); @@ -218,15 +219,15 @@ void generateRenderMesh(RenderMesh *mesh, const EditorState &state) { face_id hovFace = {}; if (g_hover.type && (g_hover.type == PICK_FACE || (TOOL_FLAGS[g_tool] & TOOLF_HOVFACE))) { - if (auto face = g_hoverFace.find(state.surf)) { + if (let face = g_hoverFace.find(state.surf)) { hovFace = g_hoverFace; if (!state.selFaces.count(hovFace)) { RenderFaceMesh faceMesh; faceMesh.material = face->paint->material; faceMesh.range.start = mesh->indices.size(); faceMesh.state = RenderFaceMesh::HOV; - glm::vec3 normal = mesh->normals[edgeIDIndices[face->edge]]; - index_t startI = edgeIDIndices[face->edge]; + let normal = mesh->normals[edgeIDIndices[face->edge]]; + let startI = edgeIDIndices[face->edge]; if (!tesselateFace(mesh->indices, state.surf, *face, normal, startI)) errFaces.push_back(*face); faceMesh.range.count = mesh->indices.size() - faceMesh.range.start; @@ -238,24 +239,24 @@ void generateRenderMesh(RenderMesh *mesh, const EditorState &state) { static std::unordered_map> matFaces; matFaces.clear(); - for (auto &pair : state.surf.faces) { + for (let &pair : state.surf.faces) { if (!state.selFaces.count(pair.first) && pair.first != hovFace) matFaces[pair.second.paint->material].push_back(pair.second); } insertFaces(mesh, errFaces, edgeIDIndices, matFaces, state.surf, RenderFaceMesh::REG); matFaces.clear(); - for (auto &f : state.selFaces) { - face_pair pair = f.pair(state.surf); - matFaces[pair.second.paint->material].push_back(pair.second); + for (let &f : state.selFaces) { + let &face = f.in(state.surf); + matFaces[face.paint->material].push_back(face); } insertFaces(mesh, errFaces, edgeIDIndices, matFaces, state.surf, RenderFaceMesh::SEL); mesh->ranges[ELEM_ERR_FACE].start = mesh->indices.size(); - for (auto &face : errFaces) { - size_t faceStart = mesh->indices.size(); - for (auto &ep : FaceEdges(state.surf, face)) { - size_t numIndices = mesh->indices.size(); + for (let &face : errFaces) { + let faceStart = mesh->indices.size(); + for (let ep : FaceEdges(state.surf, face)) { + let numIndices = mesh->indices.size(); if (numIndices - faceStart >= 3) { mesh->indices.push_back(mesh->indices[faceStart]); mesh->indices.push_back(mesh->indices[numIndices - 1]); diff --git a/src/surface.cpp b/src/surface.cpp index 448dc8f..a330407 100644 --- a/src/surface.cpp +++ b/src/surface.cpp @@ -1,6 +1,7 @@ #include "surface.h" #include #include "mathutil.h" +#include "macros.h" namespace winged { @@ -34,9 +35,9 @@ edge_id primaryEdge(const edge_pair &pair) { glm::vec3 faceNormalNonUnit(const Surface &surf, const Face &face) { glm::vec3 normal = {}; - for (auto &pair : FaceEdges(surf, face)) { - glm::vec3 v1 = pair.second.vert.in(surf).pos; - glm::vec3 v2 = pair.second.next.in(surf).vert.in(surf).pos; + for (let pair : FaceEdges(surf, face)) { + let v1 = pair.second.vert.in(surf).pos; + let v2 = pair.second.next.in(surf).vert.in(surf).pos; normal += accumPolyNormal(v1, v2); } return normal; @@ -53,7 +54,7 @@ Plane facePlane(const Surface &surf, const Face &face) { glm::mat4x2 faceTexMat(const Paint &paint, glm::vec3 normal) { glm::mat4x2 texAxes = paint.texAxes; if (texAxes == glm::mat4x2{}) { - int axis = maxAxis(glm::abs(normal)); + let axis = maxAxis(glm::abs(normal)); texAxes[(axis == 0) ? 2 : 0] = glm::vec2(((normal[axis] < 0) ^ (axis == 2)) ? 1 : -1, 0); texAxes[(axis == 1) ? 2 : 1] = glm::vec2(0, 1); } diff --git a/src/viewport.cpp b/src/viewport.cpp index a9de512..b43aff6 100644 --- a/src/viewport.cpp +++ b/src/viewport.cpp @@ -12,6 +12,7 @@ #include "glutil.h" #include "stdutil.h" #include "resource.h" +#include "macros.h" using namespace chroma; @@ -47,10 +48,10 @@ const GLfloat #define COLOR_Y_AXIS 0xff##00ff00 #define COLOR_Z_AXIS 0xff##0000ff -const float CAM_MOVE_SCALE = 600; -const float FOV = 60.0f, NEAR_CLIP = 0.5f, FAR_CLIP = 500.0f; -const float FLY_FOV = 90.0f, FLY_NEAR_CLIP = 0.2f, FLY_FAR_CLIP = 200.0f; -const int GRID_SIZE = 128; +let CAM_MOVE_SCALE = 600.0f; +let FOV = 60.0f, NEAR_CLIP = 0.5f, FAR_CLIP = 500.0f; +let FLY_FOV = 90.0f, FLY_NEAR_CLIP = 0.2f, FLY_FAR_CLIP = 200.0f; +let GRID_SIZE = 128; enum VertexAttribute { ATTR_VERTEX, ATTR_NORMAL, ATTR_COLOR, ATTR_TEXCOORD, @@ -68,8 +69,8 @@ const GLchar * const UNIFORM_NAMES[] = { "uNormalMatrix", // UNIF_NORMAL_MATRIX }; -const HCURSOR knifeCur = LoadCursor(GetModuleHandle(NULL), MAKEINTRESOURCE(IDC_KNIFE)); -const HCURSOR drawCur = LoadCursor(GetModuleHandle(NULL), MAKEINTRESOURCE(IDC_DRAW)); +let knifeCur = LoadCursor(GetModuleHandle(NULL), MAKEINTRESOURCE(IDC_KNIFE)); +let drawCur = LoadCursor(GetModuleHandle(NULL), MAKEINTRESOURCE(IDC_DRAW)); static PIXELFORMATDESCRIPTOR g_formatDesc; static HMODULE g_libGL; @@ -97,11 +98,11 @@ static void initGL() { WNDCLASSEX tempClass = SCRATCH_CLASS; tempClass.style |= CS_OWNDC; RegisterClassEx(&tempClass); - HWND tempWnd = CHECKERR(createWindow(SCRATCH_CLASS.lpszClassName)); - HDC dc = GetDC(tempWnd); - int pixelFormat = ChoosePixelFormat(dc, &g_formatDesc); + let tempWnd = CHECKERR(createWindow(SCRATCH_CLASS.lpszClassName)); + let dc = GetDC(tempWnd); + let pixelFormat = ChoosePixelFormat(dc, &g_formatDesc); SetPixelFormat(dc, pixelFormat, &g_formatDesc); - HGLRC dummyCtx = CHECKERR(wglCreateContext(dc)); + let dummyCtx = CHECKERR(wglCreateContext(dc)); if (!dummyCtx) throw winged_error(L"Couldn't create OpenGL context"); CHECKERR(wglMakeCurrent(dc, dummyCtx)); @@ -139,7 +140,7 @@ bool initViewport() { static edge_pair edgeOnHoverFace(const Surface &surf, vert_id v) { // TODO: what if there are multiple? - for (auto &edge : VertEdges(surf, v.in(surf))) { + for (let edge : VertEdges(surf, v.in(surf))) { if (edge.second.face == g_hoverFace) return edge; } @@ -150,10 +151,10 @@ static std::pair findClosestOpposingEdges( const Surface &surf, Face face1, Face face2) { float closestDist = FLT_MAX; edge_id e1 = face1.edge, e2 = face2.edge; - for (auto &f1Edge : FaceEdges(surf, face1)) { - glm::vec3 v1 = f1Edge.second.vert.in(surf).pos; - for (auto &f2Edge : FaceEdges(surf, face2)) { - float dist = glm::distance(v1, f2Edge.second.vert.in(surf).pos); + for (let f1Edge : FaceEdges(surf, face1)) { + let v1 = f1Edge.second.vert.in(surf).pos; + for (let f2Edge : FaceEdges(surf, face2)) { + let dist = glm::distance(v1, f2Edge.second.vert.in(surf).pos); if (dist < closestDist) { e1 = f1Edge.first; e2 = f2Edge.second.prev; @@ -185,7 +186,7 @@ static EditorState select(EditorState state, const PickResult pick, bool toggle) } break; case PICK_FACE: - if (auto face = pick.face.find(state.surf)) { + if (let face = pick.face.find(state.surf)) { if (toggle && state.selFaces.count(pick.face)) { state.selFaces = std::move(state.selFaces).erase(pick.face); } else { @@ -195,7 +196,7 @@ static EditorState select(EditorState state, const PickResult pick, bool toggle) } break; case PICK_EDGE: - if (auto edge = pick.edge.find(state.surf)) { + if (let edge = pick.edge.find(state.surf)) { edge_id e = primaryEdge({pick.edge, *edge}); if (toggle && state.selEdges.count(pick.edge)) state.selEdges = std::move(state.selEdges).erase(e); @@ -205,20 +206,20 @@ static EditorState select(EditorState state, const PickResult pick, bool toggle) break; } } else if (state.selMode == SEL_SOLIDS) { - if (auto face = pick.face.find(state.surf)) { - bool erase = toggle && state.selFaces.count(pick.face); + if (let face = pick.face.find(state.surf)) { + let erase = toggle && state.selFaces.count(pick.face); auto verts = state.selVerts.transient(); auto faces = state.selFaces.transient(); auto edges = state.selEdges.transient(); - auto visited = std::unordered_set(); + std::unordered_set visited; // flood-fill std::queue toSelect; toSelect.push(face->edge); while (!toSelect.empty()) { - edge_id e = toSelect.front(); + let e = toSelect.front(); toSelect.pop(); if (!visited.count(e)) { - edge_pair edge = e.pair(state.surf); + let edge = e.pair(state.surf); visited.insert(e); if (erase) { if (isPrimary(edge)) edges.erase(e); @@ -243,20 +244,20 @@ static EditorState select(EditorState state, const PickResult pick, bool toggle) static EditorState knifeToVert(EditorState state, vert_id vert) { if (state.selVerts.size() == 1 && g_hoverFace.find(state.surf)) { - edge_pair e1 = edgeOnHoverFace(state.surf, *state.selVerts.begin()); - edge_pair e2 = edgeOnHoverFace(state.surf, vert); + let e1 = edgeOnHoverFace(state.surf, *state.selVerts.begin()); + let e2 = edgeOnHoverFace(state.surf, vert); if (e1.first == e2.first) { if (g_drawVerts.empty()) return state; // clicked same vertex twice, nothing to do // loop must be clockwise in this case - glm::vec3 start = vert.in(state.surf).pos; + let start = vert.in(state.surf).pos; glm::vec3 loopNorm = accumPolyNormal(start, g_drawVerts[0]); for (size_t i = 1; i < g_drawVerts.size(); i++) loopNorm += accumPolyNormal(g_drawVerts[i - 1], g_drawVerts[i]); loopNorm += accumPolyNormal(g_drawVerts.back(), start); - glm::vec3 faceNorm = faceNormalNonUnit(state.surf, g_hoverFace.in(state.surf)); + let faceNorm = faceNormalNonUnit(state.surf, g_hoverFace.in(state.surf)); if (glm::dot(loopNorm, faceNorm) > 0) std::reverse(g_drawVerts.begin(), g_drawVerts.end()); } @@ -264,7 +265,7 @@ static EditorState knifeToVert(EditorState state, vert_id vert) { edge_id newEdge; state.surf = splitFace(std::move(state.surf), e1.first, e2.first, g_drawVerts, &newEdge); for (size_t i = 0; i < g_drawVerts.size() + 1; i++) { - edge_pair pair = newEdge.pair(state.surf); + let pair = newEdge.pair(state.surf); state.selEdges = std::move(state.selEdges).insert(primaryEdge(pair)); newEdge = pair.second.next; } @@ -283,15 +284,15 @@ static EditorState knifeToDrawVert(EditorState state, int loopI) { glm::vec3 loopNorm = {}; for (size_t i = loopI, j = g_drawVerts.size() - 1; i < g_drawVerts.size(); j = i++) loopNorm += accumPolyNormal(g_drawVerts[j], g_drawVerts[i]); - glm::vec3 faceNorm = faceNormalNonUnit(state.surf, g_hoverFace.in(state.surf)); + let faceNorm = faceNormalNonUnit(state.surf, g_hoverFace.in(state.surf)); if (glm::dot(loopNorm, faceNorm) > 0) std::reverse(g_drawVerts.begin() + loopI + 1, g_drawVerts.end()); - edge_pair e = edgeOnHoverFace(state.surf, *state.selVerts.begin()); + let e = edgeOnHoverFace(state.surf, *state.selVerts.begin()); edge_id newEdge; state.surf = splitFace(std::move(state.surf), e.first, e.first, g_drawVerts, &newEdge, loopI); for (size_t i = 0; i < g_drawVerts.size() + 1; i++) { - edge_pair pair = newEdge.pair(state.surf); + let pair = newEdge.pair(state.surf); state.selEdges = std::move(state.selEdges).insert(primaryEdge(pair)); if ((int)i == loopI + 1) state.selVerts = immer::set{}.insert(pair.second.vert); @@ -304,10 +305,10 @@ static EditorState knifeToDrawVert(EditorState state, int loopI) { static EditorState join(EditorState state) { if (g_hover.vert.find(state.surf) && state.selVerts.size() == 1) { - edge_id e1 = edgeOnHoverFace(state.surf, *state.selVerts.begin()).first; - edge_id e2 = edgeOnHoverFace(state.surf, g_hover.vert).first; + let e1 = edgeOnHoverFace(state.surf, *state.selVerts.begin()).first; + let e2 = edgeOnHoverFace(state.surf, g_hover.vert).first; state.surf = joinVerts(std::move(state.surf), e1, e2); - } else if (auto hovEdge = g_hover.edge.find(state.surf)) { + } else if (let hovEdge = g_hover.edge.find(state.surf)) { if (state.selEdges.size() != 1) throw winged_error(); edge_pair edge1 = state.selEdges.begin()->pair(state.surf); edge_pair twin1 = edge1.second.twin.pair(state.surf); @@ -322,9 +323,9 @@ static EditorState join(EditorState state) { std::swap(edge1, twin1); std::swap(edge2, twin2); } state.surf = joinEdges(state.surf, edge1.first, edge2.first); - } else if (auto face2 = g_hover.face.find(state.surf)) { + } else if (let face2 = g_hover.face.find(state.surf)) { if (state.selFaces.size() != 1) throw winged_error(); - Face face1 = state.selFaces.begin()->in(state.surf); + let &face1 = state.selFaces.begin()->in(state.surf); edge_id e1, e2; std::tie(e1, e2) = findClosestOpposingEdges(state.surf, face1, *face2); state.surf = joinEdgeLoops(std::move(state.surf), e1, e2); @@ -348,7 +349,7 @@ void ViewportWindow::refreshImmediate() { } void ViewportWindow::clearTextureCache() { - for (auto &pair : loadedTextures) + for (let &pair : loadedTextures) glDeleteTextures(1, &pair.second); loadedTextures.clear(); } @@ -370,10 +371,10 @@ void ViewportWindow::setViewMode(ViewMode mode) { } void ViewportWindow::updateProjMat() { - HDC dc = GetDC(wnd); + let dc = GetDC(wnd); CHECKERR(wglMakeCurrent(dc, context)); glViewport(0, 0, (GLsizei)viewportDim.x, (GLsizei)viewportDim.y); - float aspect = viewportDim.x / viewportDim.y; + let aspect = viewportDim.x / viewportDim.y; if (view.mode == VIEW_ORTHO) { projMat = glm::ortho(-aspect, aspect, -1.0f, 1.0f, -FAR_CLIP / 2, FAR_CLIP / 2); } else if (view.mode == VIEW_FLY) { @@ -392,17 +393,17 @@ void ViewportWindow::updateProjMat() { } glm::vec3 ViewportWindow::forwardAxis() { - glm::vec3 forward = glm::inverse(mvMat)[2]; - int axis = maxAxis(glm::abs(forward)); + let forward = glm::vec3(glm::inverse(mvMat)[2]); + let axis = maxAxis(glm::abs(forward)); glm::vec3 v = {}; v[axis] = glm::sign(forward[axis]); return v; } void ViewportWindow::updateHover(POINT pos) { - glm::vec2 normCur = screenPosToNDC({pos.x, pos.y}, viewportDim); - glm::mat4 project = projMat * mvMat; - float grid = g_state.gridOn ? g_state.gridSize : 0; + let normCur = screenPosToNDC({pos.x, pos.y}, viewportDim); + let project = projMat * mvMat; + let grid = g_state.gridOn ? g_state.gridSize : 0; PickResult result = {}; if (TOOL_FLAGS[g_tool] & TOOLF_DRAW) { @@ -419,7 +420,7 @@ void ViewportWindow::updateHover(POINT pos) { } if (g_tool == TOOL_POLY) { if (!result.type) { - Ray ray = viewPosToRay(normCur, project); + let ray = viewPosToRay(normCur, project); glm::vec3 planePoint; if (intersectRayPlane(ray, g_state.workPlane, &planePoint)) { result.point = snapPlanePoint(planePoint, g_state.workPlane, grid); @@ -468,16 +469,16 @@ void ViewportWindow::startToolAdjust(POINT pos) { } else { g_state.workPlane.norm = forwardAxis(); float closestDist = -FLT_MAX; - for (auto &vert : selAttachedVerts(g_state)) { - glm::vec3 point = vert.in(g_state.surf).pos; - float dist = glm::dot(point, g_state.workPlane.norm); + for (let &vert : selAttachedVerts(g_state)) { + let point = vert.in(g_state.surf).pos; + let dist = glm::dot(point, g_state.workPlane.norm); if (dist > closestDist) { g_state.workPlane.org = point; closestDist = dist; } } } - Ray ray = viewPosToRay(screenPosToNDC({pos.x, pos.y}, viewportDim), projMat * mvMat); + let ray = viewPosToRay(screenPosToNDC({pos.x, pos.y}, viewportDim), projMat * mvMat); startPlanePos = g_state.workPlane.org; // fallback intersectRayPlane(ray, g_state.workPlane, &startPlanePos); moved = {}; @@ -490,19 +491,19 @@ void ViewportWindow::startToolAdjust(POINT pos) { void ViewportWindow::toolAdjust(POINT pos, SIZE delta, UINT keyFlags) { switch (g_tool) { case TOOL_SELECT: { - Ray ray = viewPosToRay(screenPosToNDC({pos.x, pos.y}, viewportDim), projMat * mvMat); + let ray = viewPosToRay(screenPosToNDC({pos.x, pos.y}, viewportDim), projMat * mvMat); glm::vec3 planePos = g_state.workPlane.org; intersectRayPlane(ray, g_state.workPlane, &planePos); - glm::vec3 absNorm = glm::abs(g_state.workPlane.norm); - int normAxis = maxAxis(absNorm); - bool ortho = keyFlags & MK_CONTROL; + let absNorm = glm::abs(g_state.workPlane.norm); + let normAxis = maxAxis(absNorm); + let ortho = bool(keyFlags & MK_CONTROL); glm::vec3 amount; if (ortho) { float push = (float)delta.cy * view.zoom / CAM_MOVE_SCALE; if (g_state.gridOn) { - float snap = g_state.gridSize / absNorm[normAxis]; + let snap = g_state.gridSize / absNorm[normAxis]; snapAccum += push / snap; - int steps = (int)glm::floor(snapAccum); + let steps = (int)glm::floor(snapAccum); snapAccum -= steps; push = steps * snap; } @@ -512,7 +513,7 @@ void ViewportWindow::toolAdjust(POINT pos, SIZE delta, UINT keyFlags) { } else { glm::vec3 diff = planePos - startPlanePos; if (keyFlags & MK_SHIFT) { - int a = (normAxis + 1) % 3, b = (normAxis + 2) % 3; + let a = (normAxis + 1) % 3, b = (normAxis + 2) % 3; if (abs(diff[a]) < abs(diff[b])) diff[a] = 0; else @@ -528,7 +529,7 @@ void ViewportWindow::toolAdjust(POINT pos, SIZE delta, UINT keyFlags) { moved = diff; } if (amount != glm::vec3(0)) { - auto verts = selAttachedVerts(g_state); + let verts = selAttachedVerts(g_state); g_state.surf = transformVertices(std::move(g_state.surf), verts, glm::translate(glm::mat4(1), amount)); g_mainWindow.updateStatus(); @@ -545,9 +546,9 @@ void APIENTRY debugGLCallback(GLenum, GLenum, GLuint, GLenum, GLsizei, const GLc #endif static GLuint shaderFromResource(GLenum type, WORD id) { - GLuint shader = glCreateShader(type); + let shader = glCreateShader(type); DWORD sourceSize; - auto source = (const GLchar *)getResource(MAKEINTRESOURCE(id), RT_RCDATA, NULL, &sourceSize); + let source = (const GLchar *)getResource(MAKEINTRESOURCE(id), RT_RCDATA, NULL, &sourceSize); glShaderSource(shader, 1, &source, (GLint *)&sourceSize); glCompileShader(shader); return shader; @@ -586,8 +587,8 @@ static void writeSizedBuffer(SizedBuffer *buf, GLenum target, size_t dataSize, v } BOOL ViewportWindow::onCreate(HWND, LPCREATESTRUCT) { - HDC dc = GetDC(wnd); - int pixelFormat = ChoosePixelFormat(dc, &g_formatDesc); + let dc = GetDC(wnd); + let pixelFormat = ChoosePixelFormat(dc, &g_formatDesc); SetPixelFormat(dc, pixelFormat, &g_formatDesc); const int attribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, @@ -675,11 +676,11 @@ BOOL ViewportWindow::onCreate(HWND, LPCREATESTRUCT) { glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - GLuint vertUnlit = shaderFromResource(GL_VERTEX_SHADER, IDR_VERT_UNLIT); - GLuint vertFace = shaderFromResource(GL_VERTEX_SHADER, IDR_VERT_FACE); - GLuint fragSolid = shaderFromResource(GL_FRAGMENT_SHADER, IDR_FRAG_SOLID); - GLuint fragFace = shaderFromResource(GL_FRAGMENT_SHADER, IDR_FRAG_FACE); - GLuint fragHole = shaderFromResource(GL_FRAGMENT_SHADER, IDR_FRAG_HOLE); + let vertUnlit = shaderFromResource(GL_VERTEX_SHADER, IDR_VERT_UNLIT); + let vertFace = shaderFromResource(GL_VERTEX_SHADER, IDR_VERT_FACE); + let fragSolid = shaderFromResource(GL_FRAGMENT_SHADER, IDR_FRAG_SOLID); + let fragFace = shaderFromResource(GL_FRAGMENT_SHADER, IDR_FRAG_FACE); + let fragHole = shaderFromResource(GL_FRAGMENT_SHADER, IDR_FRAG_HOLE); programs[PROG_UNLIT] = programFromShaders(vertUnlit, fragSolid); programs[PROG_FACE] = programFromShaders(vertFace, fragFace); @@ -697,7 +698,7 @@ BOOL ViewportWindow::onCreate(HWND, LPCREATESTRUCT) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - HBITMAP defHBmp = (HBITMAP)LoadImage(GetModuleHandle(NULL), + let defHBmp = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_DEFAULT_TEXTURE), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION); BITMAP defBmp; GetObject(defHBmp, sizeof(defBmp), (void *)&defBmp); @@ -714,7 +715,7 @@ BOOL ViewportWindow::onCreate(HWND, LPCREATESTRUCT) { } void ViewportWindow::destroy() { - HDC dc = GetDC(wnd); + let dc = GetDC(wnd); CHECKERR(wglMakeCurrent(dc, context)); // doesn't work in WM_DESTROY glDeleteBuffers(1, &axisPoints); @@ -725,7 +726,7 @@ void ViewportWindow::destroy() { glDeleteBuffers(1, &indicesBuffer.id); glDeleteTextures(1, &defTexture); - for (auto &pair : loadedTextures) + for (let &pair : loadedTextures) glDeleteTextures(1, &pair.second); for (int i = 0; i < PROG_COUNT; i++) @@ -774,7 +775,7 @@ void ViewportWindow::onLButtonDown(HWND, BOOL, int x, int y, UINT keyFlags) { case PICK_EDGE: { EditorState newState = g_state; newState.surf = splitEdge(g_state.surf, g_hover.edge, g_hover.point); - vert_id newVert = g_hover.edge.in(newState.surf).next.in(newState.surf).vert; + let newVert = g_hover.edge.in(newState.surf).next.in(newState.surf).vert; newState = knifeToVert(std::move(newState), newVert); g_mainWindow.pushUndo(std::move(newState)); break; @@ -815,8 +816,8 @@ void ViewportWindow::onLButtonDown(HWND, BOOL, int x, int y, UINT keyFlags) { if (!(keyFlags & MK_SHIFT)) g_tool = TOOL_SELECT; } else { - bool toggle = keyFlags & MK_SHIFT; - bool alreadySelected = hasSelection(g_state); + let toggle = bool(keyFlags & MK_SHIFT); + let alreadySelected = hasSelection(g_state); if (!alreadySelected) { g_state = select(std::move(g_state), g_hover, toggle); g_mainWindow.refreshAllImmediate(); @@ -826,7 +827,7 @@ void ViewportWindow::onLButtonDown(HWND, BOOL, int x, int y, UINT keyFlags) { g_hover = {}; } else if (GetKeyState(VK_MENU) < 0 && !g_state.selFaces.empty() && g_hoverFace.find(g_state.surf)) { - immer::box paint = g_state.selFaces.begin()->in(g_state.surf).paint; + let paint = g_state.selFaces.begin()->in(g_state.surf).paint; EditorState newState = g_state; newState.surf = assignPaint(g_state.surf, {g_hoverFace}, paint); g_mainWindow.pushUndo(std::move(newState)); @@ -877,11 +878,11 @@ void ViewportWindow::onMouseMove(HWND, int x, int y, UINT keyFlags) { TrackMouseEvent(tempPtr(TRACKMOUSEEVENT{sizeof(TRACKMOUSEEVENT), TME_LEAVE, wnd})); trackMouse = true; } - POINT curPos = {x, y}; + let curPos = POINT{x, y}; if (mouseMode == MOUSE_NONE) { updateHover(curPos); } else if (curPos != lastCurPos) { - SIZE delta = {curPos.x - lastCurPos.x, curPos.y - lastCurPos.y}; + let delta = SIZE{curPos.x - lastCurPos.x, curPos.y - lastCurPos.y}; switch (mouseMode) { case MOUSE_TOOL: toolAdjust(curPos, delta, keyFlags); @@ -894,7 +895,7 @@ void ViewportWindow::onMouseMove(HWND, int x, int y, UINT keyFlags) { SetWindowText(wnd, APP_NAME); break; case MOUSE_CAM_PAN: { - bool shift = keyFlags & MK_SHIFT; + let shift = bool(keyFlags & MK_SHIFT); glm::vec3 deltaPos; if (view.mode == VIEW_FLY) { deltaPos = shift ? glm::vec3(-delta.cx, delta.cy, 0) @@ -902,9 +903,9 @@ void ViewportWindow::onMouseMove(HWND, int x, int y, UINT keyFlags) { } else { deltaPos = shift ? glm::vec3(0, 0, -delta.cy) : glm::vec3(delta.cx, -delta.cy, 0); } - glm::mat4 invMV = glm::inverse(mvMat); + let invMV = glm::inverse(mvMat); // there's probably a better way to do this - glm::mat3 normInvMV = { + let normInvMV = glm::mat3{ glm::normalize(invMV[0]), glm::normalize(invMV[1]), glm::normalize(invMV[2])}; view.camPivot += normInvMV * deltaPos * view.zoom / CAM_MOVE_SCALE; refresh(); @@ -912,7 +913,7 @@ void ViewportWindow::onMouseMove(HWND, int x, int y, UINT keyFlags) { } } if (mouseMode != MOUSE_TOOL || (g_tool == TOOL_SELECT && (keyFlags & MK_CONTROL))) { - POINT screenPos = clientToScreen(wnd, lastCurPos); + let screenPos = clientToScreen(wnd, lastCurPos); SetCursorPos(screenPos.x, screenPos.y); } else { lastCurPos = curPos; @@ -992,13 +993,13 @@ void ViewportWindow::onDropFiles(HWND, HDROP drop) { try { TCHAR path[MAX_PATH]; if (DragQueryFile(drop, 0, path, _countof(path))) { - TCHAR *ext = PathFindExtension(path); + let ext = PathFindExtension(path); if (ext[0] == 0) { // folder g_library.rootPath = path; } else if (lstrcmpi(ext, L".wing") == 0) { g_mainWindow.open(path); } else { // assume image - std::wstring texFileStr = path; + let texFileStr = std::wstring(path); id_t texId = g_library.pathIds[texFileStr]; if (texId == id_t{}) { texId = genId(); @@ -1076,7 +1077,7 @@ void ViewportWindow::onPaint(HWND) { mvMat = glm::rotate(mvMat, view.rotX, glm::vec3(1, 0, 0)); mvMat = glm::rotate(mvMat, view.rotY, glm::vec3(0, 1, 0)); mvMat = glm::translate(mvMat, view.camPivot); - glm::mat3 normalMat = glm::transpose(glm::inverse(mvMat)); + let normalMat = glm::mat3(glm::transpose(glm::inverse(mvMat))); for (int i = 0; i < PROG_COUNT; i++) { glUseProgram(programs[i].id); @@ -1103,17 +1104,17 @@ void ViewportWindow::onPaint(HWND) { drawMesh(g_renderMesh); // work plane grid - bool workPlaneActive = ((TOOL_FLAGS[g_tool] & TOOLF_DRAW) + let workPlaneActive = ((TOOL_FLAGS[g_tool] & TOOLF_DRAW) && ((g_state.gridOn && g_hover.type) || !(TOOL_FLAGS[g_tool] & TOOLF_HOVFACE))) || (g_tool == TOOL_SELECT && mouseMode == MOUSE_TOOL); if (workPlaneActive || (view.mode == VIEW_ORTHO && g_state.gridOn)) { - auto p = g_state.workPlane; + Plane p = g_state.workPlane; if (!workPlaneActive) { p.norm = forwardAxis(); glDisable(GL_DEPTH_TEST); } - int axis = maxAxis(glm::abs(p.norm)); - int u = (axis + 1) % 3, v = (axis + 2) % 3; + let axis = maxAxis(glm::abs(p.norm)); + let u = (axis + 1) % 3, v = (axis + 2) % 3; glm::vec3 uVec = {}, vVec = {}; uVec[u] = g_state.gridSize; vVec[v] = g_state.gridSize; uVec[axis] = solvePlane(uVec, p.norm, axis); @@ -1218,10 +1219,10 @@ void ViewportWindow::drawMesh(const RenderMesh &mesh) { glEnableVertexAttribArray(ATTR_NORMAL); glEnableVertexAttribArray(ATTR_TEXCOORD); glUseProgram(programs[PROG_FACE].id); - for (auto &faceMesh : mesh.faceMeshes) { + for (let &faceMesh : mesh.faceMeshes) { // generate color from GUID - id_t mat = faceMesh.material; - bool isHole = mat == Paint::HOLE_MATERIAL; + let mat = faceMesh.material; + let isHole = (mat == Paint::HOLE_MATERIAL); if (isHole) glUseProgram(programs[PROG_HOLE].id); else @@ -1268,8 +1269,8 @@ void ViewportWindow::bindTexture(id_t texture) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - if (auto path = tryGet(g_library.idPaths, texture)) { - ImageData image = loadImage(path->c_str()); + if (let path = tryGet(g_library.idPaths, texture)) { + let image = loadImage(path->c_str()); if (image.data) { texImageMipmaps(GL_TEXTURE_2D, GL_RGBA, image.width, image.height, GL_BGRA, GL_UNSIGNED_BYTE, image.data.get());