From fc851d2106f2c5c73af9d8afbf9eb1783fd28257 Mon Sep 17 00:00:00 2001 From: thejinchao Date: Thu, 19 Jun 2025 14:19:29 +0800 Subject: [PATCH] =?UTF-8?q?SDL=E7=9A=84=E5=B1=8F=E5=B9=95=E5=9D=90?= =?UTF-8?q?=E6=A0=87=E6=98=AF=E5=B7=A6=E4=B8=8A=E8=A7=92=E4=B8=BA=E5=8E=9F?= =?UTF-8?q?=E7=82=B9=EF=BC=8C=E9=9C=80=E8=A6=81=E7=BA=A0=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/rasterization/dvr_main.cpp | 65 +++++++++++++++++---------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/test/rasterization/dvr_main.cpp b/test/rasterization/dvr_main.cpp index b4ef4c9..d42d135 100644 --- a/test/rasterization/dvr_main.cpp +++ b/test/rasterization/dvr_main.cpp @@ -52,8 +52,11 @@ TriangleVector g_allTriangles; SDL_Window* g_window = NULL; SDL_Renderer* g_renderer = NULL; -#define trans_x(x) ((float32_t)(x) * g_scale + g_lbCorner_x) -#define trans_y(y) ((float32_t)(y) * g_scale + g_lbCorner_y) +#define trans_w2s_x(x) ((float32_t)(x) * g_scale + g_lbCorner_x) +#define trans_w2s_y(y) ((float32_t)g_viewHeight - ((float32_t)(y) * g_scale + g_lbCorner_y)) + +#define trans_s2w_x(x) ((float32_t)(x - g_lbCorner_x) / g_scale) +#define trans_s2w_y(y) ((float32_t)(g_viewHeight - y - g_lbCorner_y) / g_scale) void _onFramebufferSize(int32_t w, int32_t h) { @@ -87,7 +90,7 @@ static void _drawGrid(void) SDL_SetRenderDrawColorFloat(g_renderer, lineColor[level].x, lineColor[level].y, lineColor[level].z, SDL_ALPHA_OPAQUE_FLOAT); - SDL_RenderLine(g_renderer, trans_x(0), trans_y(i), trans_x(horLength), trans_y(i)); + SDL_RenderLine(g_renderer, trans_w2s_x(0), trans_w2s_y(i), trans_w2s_x(horLength), trans_w2s_y(i)); } // Vertical lines @@ -100,7 +103,7 @@ static void _drawGrid(void) if (level > 3 - g_nDrawGridLevel) continue; SDL_SetRenderDrawColorFloat(g_renderer, lineColor[level].x, lineColor[level].y, lineColor[level].z, SDL_ALPHA_OPAQUE_FLOAT); - SDL_RenderLine(g_renderer, trans_x(j), trans_y(0), trans_x(j), trans_y(verLength)); + SDL_RenderLine(g_renderer, trans_w2s_x(j), trans_w2s_y(0), trans_w2s_x(j), trans_w2s_y(verLength)); } //cross @@ -114,13 +117,13 @@ static void _drawGrid(void) for (int j = 0; j < horLength; j++) { SDL_RenderLine(g_renderer, - trans_x(j + 0.5f - crossLength), trans_y(i + 0.5f - crossLength), - trans_x(j + 0.5f + crossLength), trans_y(i + 0.5f + crossLength) + trans_w2s_x(j + 0.5f - crossLength), trans_w2s_y(i + 0.5f - crossLength), + trans_w2s_x(j + 0.5f + crossLength), trans_w2s_y(i + 0.5f + crossLength) ); SDL_RenderLine(g_renderer, - trans_x(j + 0.5f - crossLength), trans_y(i + 0.5f + crossLength), - trans_x(j + 0.5f + crossLength), trans_y(i + 0.5f - crossLength) + trans_w2s_x(j + 0.5f - crossLength), trans_w2s_y(i + 0.5f + crossLength), + trans_w2s_x(j + 0.5f + crossLength), trans_w2s_y(i + 0.5f - crossLength) ); } } @@ -135,10 +138,10 @@ static void _drawTrianglePixel(const Triangle& triangle) for (const auto& dot : triangle.pixels) { SDL_FRect rect; - rect.x = trans_x(dot.first); - rect.y = trans_y(dot.second); + rect.x = trans_w2s_x(dot.first); + rect.y = trans_w2s_y(dot.second); rect.w = g_scale; - rect.h = g_scale; + rect.h = -g_scale; SDL_RenderFillRect(g_renderer, &rect); } @@ -149,10 +152,10 @@ void _drawTriangleBorder(const Triangle& triangle, const fVector3& color, float SDL_SetRenderDrawColorFloat(g_renderer, color[0], color[1], color[2], SDL_ALPHA_OPAQUE_FLOAT); SDL_FPoint points[4] = { - {trans_x(triangle.p0.x), trans_y(triangle.p0.y)}, - {trans_x(triangle.p1.x), trans_y(triangle.p1.y)}, - {trans_x(triangle.p2.x), trans_y(triangle.p2.y)}, - {trans_x(triangle.p0.x), trans_y(triangle.p0.y)} + {trans_w2s_x(triangle.p0.x), trans_w2s_y(triangle.p0.y)}, + {trans_w2s_x(triangle.p1.x), trans_w2s_y(triangle.p1.y)}, + {trans_w2s_x(triangle.p2.x), trans_w2s_y(triangle.p2.y)}, + {trans_w2s_x(triangle.p0.x), trans_w2s_y(triangle.p0.y)} }; SDL_RenderLines(g_renderer, points, 4); } @@ -179,12 +182,12 @@ void _drawSegmentInAABB(const fVector2& p0, const fVector2& p1, float xMin, floa float _ymax = (xMax - p0.x) * dy / dx + p0.y; if (dx > 0) { - SDL_RenderLine(g_renderer, trans_x(xMin), trans_y(_ymin), trans_x(p0.x), trans_y(p0.y) ); - SDL_RenderLine(g_renderer, trans_x(p1.x), trans_y(p1.y), trans_x(xMax), trans_y(_ymax) ); + SDL_RenderLine(g_renderer, trans_w2s_x(xMin), trans_w2s_y(_ymin), trans_w2s_x(p0.x), trans_w2s_y(p0.y) ); + SDL_RenderLine(g_renderer, trans_w2s_x(p1.x), trans_w2s_y(p1.y), trans_w2s_x(xMax), trans_w2s_y(_ymax) ); } else { - SDL_RenderLine(g_renderer, trans_x(xMin), trans_y(_ymin), trans_x(p1.x), trans_y(p1.y)); - SDL_RenderLine(g_renderer, trans_x(p0.x), trans_y(p0.y), trans_x(xMax), trans_y(_ymax)); + SDL_RenderLine(g_renderer, trans_w2s_x(xMin), trans_w2s_y(_ymin), trans_w2s_x(p1.x), trans_w2s_y(p1.y)); + SDL_RenderLine(g_renderer, trans_w2s_x(p0.x), trans_w2s_y(p0.y), trans_w2s_x(xMax), trans_w2s_y(_ymax)); } } else { @@ -192,12 +195,12 @@ void _drawSegmentInAABB(const fVector2& p0, const fVector2& p1, float xMin, floa float _xmax = (yMax - p0.y) * dx / dy + p0.x; if (dy > 0) { - SDL_RenderLine(g_renderer, trans_x(_xmin), trans_y(yMin), trans_x(p0.x), trans_y(p0.y)); - SDL_RenderLine(g_renderer, trans_x(p1.x), trans_y(p1.y), trans_x(_xmax), trans_y(yMax)); + SDL_RenderLine(g_renderer, trans_w2s_x(_xmin), trans_w2s_y(yMin), trans_w2s_x(p0.x), trans_w2s_y(p0.y)); + SDL_RenderLine(g_renderer, trans_w2s_x(p1.x), trans_w2s_y(p1.y), trans_w2s_x(_xmax), trans_w2s_y(yMax)); } else { - SDL_RenderLine(g_renderer, trans_x(_xmin), trans_y(yMin), trans_x(p1.x), trans_y(p1.y)); - SDL_RenderLine(g_renderer, trans_x(p0.x), trans_y(p0.y), trans_x(_xmax), trans_y(yMax)); + SDL_RenderLine(g_renderer, trans_w2s_x(_xmin), trans_w2s_y(yMin), trans_w2s_x(p1.x), trans_w2s_y(p1.y)); + SDL_RenderLine(g_renderer, trans_w2s_x(p0.x), trans_w2s_y(p0.y), trans_w2s_x(_xmax), trans_w2s_y(yMax)); } } } @@ -218,8 +221,8 @@ void _drawAction(void) case MT_FIRST_LINE: { SDL_RenderLine(g_renderer, - trans_x(g_currentTriangle.p0.x), trans_y(g_currentTriangle.p0.y), - trans_x(g_currentTriangle.p1.x), trans_y(g_currentTriangle.p1.y) + trans_w2s_x(g_currentTriangle.p0.x), trans_w2s_y(g_currentTriangle.p0.y), + trans_w2s_x(g_currentTriangle.p1.x), trans_w2s_y(g_currentTriangle.p1.y) ); } break; @@ -247,13 +250,13 @@ void _drawMouseCursor(void) const float32_t CursorLength = 10.f / g_scale; SDL_RenderLine(g_renderer, - trans_x(g_mousePosX - CursorLength), trans_y(g_mousePosY), - trans_x(g_mousePosX + CursorLength), trans_y(g_mousePosY) + trans_w2s_x(g_mousePosX - CursorLength), trans_w2s_y(g_mousePosY), + trans_w2s_x(g_mousePosX + CursorLength), trans_w2s_y(g_mousePosY) ); SDL_RenderLine(g_renderer, - trans_x(g_mousePosX), trans_y(g_mousePosY - CursorLength), - trans_x(g_mousePosX), trans_y(g_mousePosY + CursorLength) + trans_w2s_x(g_mousePosX), trans_w2s_y(g_mousePosY - CursorLength), + trans_w2s_x(g_mousePosX), trans_w2s_y(g_mousePosY + CursorLength) ); } } @@ -287,8 +290,8 @@ inline float _snapPosition(float pos) void _onMouseMove(float32_t x, float32_t y) { - g_mousePosX = (float)(x - g_lbCorner_x) / g_scale; - g_mousePosY = (float)(y - g_lbCorner_y) / g_scale; + g_mousePosX = trans_s2w_x(x); + g_mousePosY = trans_s2w_y(y); if (g_bSnapMode) { g_mousePosX = _snapPosition(g_mousePosX);