diff options
Diffstat (limited to 'demos/graphics_experiments')
-rw-r--r-- | demos/graphics_experiments/1.cpp | 79 | ||||
-rw-r--r-- | demos/graphics_experiments/2.cpp | 312 | ||||
-rw-r--r-- | demos/graphics_experiments/4.cpp | 161 | ||||
-rw-r--r-- | demos/graphics_experiments/CMakeLists.txt | 10 |
4 files changed, 0 insertions, 562 deletions
diff --git a/demos/graphics_experiments/1.cpp b/demos/graphics_experiments/1.cpp deleted file mode 100644 index c85387dc..00000000 --- a/demos/graphics_experiments/1.cpp +++ /dev/null @@ -1,79 +0,0 @@ -// Code referred from -// https://iq.opengenus.org/bresenham-line-drawining-algorithm/ - -#include "cru/platform/Color.h" -#include "cru/platform/bootstrap/Bootstrap.h" -#include "cru/platform/graphics/Factory.h" -#include "cru/platform/graphics/Painter.h" -#include "cru/platform/gui/UiApplication.h" -#include "cru/platform/gui/Window.h" - -#include <functional> - -void BresenhamDrawLine(int x1, int y1, int x2, int y2, - const std::function<void(int, int)>& draw_pixel) { - // calculating range for line between start and end point - int dx = x2 - x1; - int dy = y2 - y1; - - int x = x1; - int y = y1; - - // this is the case when slope(m) < 1 - if (abs(dx) > abs(dy)) { - draw_pixel(x, y); // this putpixel is for very first pixel of the line - int pk = (2 * abs(dy)) - abs(dx); - - for (int i = 0; i < abs(dx); i++) { - x = x + 1; - if (pk < 0) - pk = pk + (2 * abs(dy)); - else { - y = y + 1; - pk = pk + (2 * abs(dy)) - (2 * abs(dx)); - } - draw_pixel(x, y); - } - } else { - // this is the case when slope is greater than or equal to 1 i.e: m>=1 - draw_pixel(x, y); // this putpixel is for very first pixel of the line - int pk = (2 * abs(dx)) - abs(dy); - - for (int i = 0; i < abs(dy); i++) { - y = y + 1; - if (pk < 0) - pk = pk + (2 * abs(dx)); - else { - x = x + 1; - pk = pk + (2 * abs(dx)) - (2 * abs(dy)); - } - - draw_pixel(x, y); // display pixel at coordinate (x, y) - } - } -} - -int main() { - auto application = cru::platform::bootstrap::CreateUiApplication(); - auto window = application->CreateWindow(); - - auto brush = application->GetGraphicsFactory()->CreateSolidColorBrush( - cru::platform::colors::black); - - window->SetClientSize(cru::platform::Size(400, 200)); - - window->PaintEvent()->AddHandler([window, &brush](nullptr_t) { - auto painter = window->BeginPaint(); - auto draw_pixel = [&painter, &brush](int x, int y) { - painter->FillRectangle({static_cast<float>(x) - 0.5f, - static_cast<float>(y) - 0.5f, 1.0f, 1.0f}, - brush.get()); - }; - BresenhamDrawLine(50, 50, 100, 200, draw_pixel); - BresenhamDrawLine(50, 50, 200, 100, draw_pixel); - }); - - window->SetVisibility(cru::platform::gui::WindowVisibilityType::Show); - - return application->Run(); -} diff --git a/demos/graphics_experiments/2.cpp b/demos/graphics_experiments/2.cpp deleted file mode 100644 index 495f8246..00000000 --- a/demos/graphics_experiments/2.cpp +++ /dev/null @@ -1,312 +0,0 @@ -// Code referred from -// https://www.geeksforgeeks.org/scan-line-polygon-filling-using-opengl-c/ - -#include "cru/platform/Color.h" -#include "cru/platform/bootstrap/Bootstrap.h" -#include "cru/platform/graphics/Factory.h" -#include "cru/platform/graphics/Painter.h" -#include "cru/platform/gui/UiApplication.h" -#include "cru/platform/gui/Window.h" - -#include <cmath> -#include <cstdio> -#include <functional> - -#define maxHt 800 -#define maxWd 600 -#define maxVer 10000 - -// Start from lower left corner -typedef struct edgebucket { - int ymax; // max y-coordinate of edge - float xofymin; // x-coordinate of lowest edge point updated only in aet - float slopeinverse; -} EdgeBucket; - -typedef struct edgetabletup { - // the array will give the scanline number - // The edge table (ET) with edges entries sorted - // in increasing y and x of the lower end - - int countEdgeBucket; // no. of edgebuckets - EdgeBucket buckets[maxVer]; -} EdgeTableTuple; - -EdgeTableTuple EdgeTable[maxHt], ActiveEdgeTuple; - -// Scanline Function -void initEdgeTable() { - int i; - for (i = 0; i < maxHt; i++) { - EdgeTable[i].countEdgeBucket = 0; - } - - ActiveEdgeTuple.countEdgeBucket = 0; -} - -void printTuple(EdgeTableTuple *tup) { - int j; - - if (tup->countEdgeBucket) printf("\nCount %d-----\n", tup->countEdgeBucket); - - for (j = 0; j < tup->countEdgeBucket; j++) { - printf(" %d+%.2f+%.2f", tup->buckets[j].ymax, tup->buckets[j].xofymin, - tup->buckets[j].slopeinverse); - } -} - -void printTable() { - int i, j; - - for (i = 0; i < maxHt; i++) { - if (EdgeTable[i].countEdgeBucket) printf("\nScanline %d", i); - - printTuple(&EdgeTable[i]); - } -} - -/* Function to sort an array using insertion sort*/ -void insertionSort(EdgeTableTuple *ett) { - int i, j; - EdgeBucket temp; - - for (i = 1; i < ett->countEdgeBucket; i++) { - temp.ymax = ett->buckets[i].ymax; - temp.xofymin = ett->buckets[i].xofymin; - temp.slopeinverse = ett->buckets[i].slopeinverse; - j = i - 1; - - while ((temp.xofymin < ett->buckets[j].xofymin) && (j >= 0)) { - ett->buckets[j + 1].ymax = ett->buckets[j].ymax; - ett->buckets[j + 1].xofymin = ett->buckets[j].xofymin; - ett->buckets[j + 1].slopeinverse = ett->buckets[j].slopeinverse; - j = j - 1; - } - ett->buckets[j + 1].ymax = temp.ymax; - ett->buckets[j + 1].xofymin = temp.xofymin; - ett->buckets[j + 1].slopeinverse = temp.slopeinverse; - } -} - -void storeEdgeInTuple(EdgeTableTuple *receiver, int ym, int xm, float slopInv) { - // both used for edgetable and active edge table.. - // The edge tuple sorted in increasing ymax and x of the lower end. - (receiver->buckets[(receiver)->countEdgeBucket]).ymax = ym; - (receiver->buckets[(receiver)->countEdgeBucket]).xofymin = (float)xm; - (receiver->buckets[(receiver)->countEdgeBucket]).slopeinverse = slopInv; - - // sort the buckets - insertionSort(receiver); - - (receiver->countEdgeBucket)++; -} - -void storeEdgeInTable(int x1, int y1, int x2, int y2) { - float m, minv; - int ymaxTS, xwithyminTS, scanline; // ts stands for to store - - if (x2 == x1) { - minv = 0.000000; - } else { - m = ((float)(y2 - y1)) / ((float)(x2 - x1)); - - // horizontal lines are not stored in edge table - if (y2 == y1) return; - - minv = (float)1.0 / m; - printf("\nSlope string for %d %d & %d %d: %f", x1, y1, x2, y2, minv); - } - - if (y1 > y2) { - scanline = y2; - ymaxTS = y1; - xwithyminTS = x2; - } else { - scanline = y1; - ymaxTS = y2; - xwithyminTS = x1; - } - // the assignment part is done..now storage.. - storeEdgeInTuple(&EdgeTable[scanline], ymaxTS, xwithyminTS, minv); -} - -void removeEdgeByYmax(EdgeTableTuple *Tup, int yy) { - int i, j; - for (i = 0; i < Tup->countEdgeBucket; i++) { - if (Tup->buckets[i].ymax == yy) { - printf("\nRemoved at %d", yy); - - for (j = i; j < Tup->countEdgeBucket - 1; j++) { - Tup->buckets[j].ymax = Tup->buckets[j + 1].ymax; - Tup->buckets[j].xofymin = Tup->buckets[j + 1].xofymin; - Tup->buckets[j].slopeinverse = Tup->buckets[j + 1].slopeinverse; - } - Tup->countEdgeBucket--; - i--; - } - } -} - -void updatexbyslopeinv(EdgeTableTuple *Tup) { - int i; - - for (i = 0; i < Tup->countEdgeBucket; i++) { - (Tup->buckets[i]).xofymin = - (Tup->buckets[i]).xofymin + (Tup->buckets[i]).slopeinverse; - } -} - -void ScanlineFill(const std::function<void(float x1, float y1, float x2, - float y2)> &draw_line) { - /* Follow the following rules: - 1. Horizontal edges: Do not include in edge table - 2. Horizontal edges: Drawn either on the bottom or on the top. - 3. Vertices: If local max or min, then count twice, else count - once. - 4. Either vertices at local minima or at local maxima are drawn.*/ - - int i, j, x1, ymax1, x2, ymax2, FillFlag = 0, coordCount; - - // we will start from scanline 0; - // Repeat until last scanline: - for (i = 0; i < maxHt; i++) // 4. Increment y by 1 (next scan line) - { - // 1. Move from ET bucket y to the - // AET those edges whose ymin = y (entering edges) - for (j = 0; j < EdgeTable[i].countEdgeBucket; j++) { - storeEdgeInTuple(&ActiveEdgeTuple, EdgeTable[i].buckets[j].ymax, - EdgeTable[i].buckets[j].xofymin, - EdgeTable[i].buckets[j].slopeinverse); - } - printTuple(&ActiveEdgeTuple); - - // 2. Remove from AET those edges for - // which y=ymax (not involved in next scan line) - removeEdgeByYmax(&ActiveEdgeTuple, i); - - // sort AET (remember: ET is presorted) - insertionSort(&ActiveEdgeTuple); - - printTuple(&ActiveEdgeTuple); - - // 3. Fill lines on scan line y by using pairs of x-coords from AET - j = 0; - FillFlag = 0; - coordCount = 0; - x1 = 0; - x2 = 0; - ymax1 = 0; - ymax2 = 0; - while (j < ActiveEdgeTuple.countEdgeBucket) { - if (coordCount % 2 == 0) { - x1 = (int)(ActiveEdgeTuple.buckets[j].xofymin); - ymax1 = ActiveEdgeTuple.buckets[j].ymax; - if (x1 == x2) { - /* three cases can arrive- - 1. lines are towards top of the intersection - 2. lines are towards bottom - 3. one line is towards top and other is towards bottom - */ - if (((x1 == ymax1) && (x2 != ymax2)) || - ((x1 != ymax1) && (x2 == ymax2))) { - x2 = x1; - ymax2 = ymax1; - } - - else { - coordCount++; - } - } - - else { - coordCount++; - } - } else { - x2 = (int)ActiveEdgeTuple.buckets[j].xofymin; - ymax2 = ActiveEdgeTuple.buckets[j].ymax; - - FillFlag = 0; - - // checking for intersection... - if (x1 == x2) { - /*three cases can arrive- - 1. lines are towards top of the intersection - 2. lines are towards bottom - 3. one line is towards top and other is towards bottom - */ - if (((x1 == ymax1) && (x2 != ymax2)) || - ((x1 != ymax1) && (x2 == ymax2))) { - x1 = x2; - ymax1 = ymax2; - } else { - coordCount++; - FillFlag = 1; - } - } else { - coordCount++; - FillFlag = 1; - } - - if (FillFlag) { - // drawing actual lines... - - draw_line(x1, i, x2, i); - - // printf("\nLine drawn from %d,%d to %d,%d",x1,i,x2,i); - } - } - - j++; - } - - // 5. For each nonvertical edge remaining in AET, update x for new y - updatexbyslopeinv(&ActiveEdgeTuple); - } - - printf("\nScanline filling complete"); -} - -int edges[] = {50, 50, 100, 40, 150, 150, 100, 80, 40, 90, 50, 50}; - -void drawPolyDino(const std::function<void(float x1, float y1, float x2, - float y2)> &draw_line) { - for (int i = 0; i < sizeof(edges) / sizeof(edges[0]) - 3; i += 2) { - draw_line(edges[i], edges[i + 1], edges[i + 2], edges[i + 3]); - storeEdgeInTable(edges[i], edges[i + 1], edges[i + 2], - edges[i + 3]); // storage of edges in edge table. - } -} - -void drawDino(const std::function<void(float x1, float y1, float x2, float y2)> - &draw_line) { - initEdgeTable(); - drawPolyDino(draw_line); - printf("\nTable"); - printTable(); - - ScanlineFill(draw_line); // actual calling of scanline filling.. -} - -int main(int argc, char **argv) { - auto application = cru::platform::bootstrap::CreateUiApplication(); - auto window = application->CreateWindow(); - - auto brush = application->GetGraphicsFactory()->CreateSolidColorBrush( - cru::platform::colors::black); - - window->SetClientSize(cru::platform::Size(400, 200)); - - window->PaintEvent()->AddHandler([window, &brush](nullptr_t) { - auto painter = window->BeginPaint(); - auto draw_line = [&painter, &brush](float x1, float y1, float x2, - float y2) { - painter->DrawLine({x1, y1}, {x2, y2}, brush.get(), 1); - }; - - drawDino(draw_line); - }); - - window->SetVisibility(cru::platform::gui::WindowVisibilityType::Show); - - return application->Run(); -} diff --git a/demos/graphics_experiments/4.cpp b/demos/graphics_experiments/4.cpp deleted file mode 100644 index 60bd2db9..00000000 --- a/demos/graphics_experiments/4.cpp +++ /dev/null @@ -1,161 +0,0 @@ -#include "cru/platform/GraphicsBase.h" -#include "cru/platform/bootstrap/Bootstrap.h" -#include "cru/platform/graphics/Factory.h" -#include "cru/platform/graphics/Painter.h" -#include "cru/platform/gui/UiApplication.h" -#include "cru/platform/gui/Window.h" - -#include <dlib/matrix.h> -#include <dlib/numeric_constants.h> -#include <cmath> - -using cru::platform::Point; - -using matrix14 = dlib::matrix<float, 1, 4>; -using matrix13 = dlib::matrix<float, 1, 3>; -using matrix44 = dlib::matrix<float, 4, 4>; - -matrix44 Identity() { - matrix44 m; - m = dlib::identity_matrix<float, 4>(); - return m; -} - -matrix44 T1(float a, float b, float c) { - auto m = Identity(); - - m(0, 3) = -a; - m(1, 3) = -b; - m(2, 3) = -c; - - return m; -} - -matrix44 T2(float theta) { - auto m = Identity(); - - m(0, 0) = -std::cos(theta); - m(0, 2) = -std::sin(theta); - m(2, 0) = std::sin(theta); - m(2, 2) = -std::cos(theta); - - return m; -} - -matrix44 T3(float phi) { - auto m = Identity(); - - m(1, 1) = std::sin(phi); - m(1, 2) = -std::cos(phi); - m(2, 1) = std::cos(phi); - m(2, 2) = std::sin(phi); - - return m; -} - -matrix44 T4(float alpha) { - auto m = Identity(); - - m(0, 0) = std::cos(alpha); - m(1, 0) = std::sin(alpha); - m(0, 1) = -std::sin(alpha); - m(1, 1) = std::cos(alpha); - - return m; -} - -matrix44 T5() { - auto m = Identity(); - - m(0, 0) = -1; - - return m; -} - -struct Args { - float a; - float b; - float c; - float theta; - float phi; - float alpha; -}; - -matrix44 Tv(Args args) { - return T1(args.a, args.b, args.c) * T2(args.theta) * T3(args.phi) * - T4(args.alpha) * T5(); -} - -matrix14 Transform(matrix14 point, Args args) { return point * Tv(args); } - -matrix14 Transform(matrix13 point, Args args) { - return matrix14{point(0), point(1), point(2), 1} * Tv(args); -} - -Point TransformTo2D(matrix14 point, float d) { - return Point{point(1), point(2)}; -} - -const float length = 100; - -matrix13 points[] = { - {0, 0, 0}, - {length, 0, 0}, - {length, 0, 0}, - {length, length, 0}, - {length, length, 0}, - {0, length, 0}, - {0, length, 0}, - {0, 0, 0}, - {0, 0, 0}, - {0, 0, length}, - {length, 0, 0}, - {length, 0, length}, - {length, length, 0}, - {length, length, length}, - {0, length, 0}, - {0, length, length}, - {0, 0, length}, - {length, 0, length}, - {length, 0, length}, - {length, length, length}, - {length, length, length}, - {0, length, length}, - {0, length, length}, - {0, 0, length}, -}; - -const float pi = static_cast<float>(dlib::pi); - -Args args{30, 40, 50, pi / 3.f, pi / 4.f, pi / 5.f}; - -int main() { - std::vector<Point> points2d; - - for (auto p : points) { - auto point2d = TransformTo2D(Transform(std::move(p), args), length); - points2d.push_back(point2d); - } - - auto application = cru::platform::bootstrap::CreateUiApplication(); - auto window = application->CreateWindow(); - - auto brush = application->GetGraphicsFactory()->CreateSolidColorBrush( - cru::platform::colors::black); - - window->SetClientSize(cru::platform::Size(400, 400)); - - window->PaintEvent()->AddHandler([window, &brush, points2d](nullptr_t) { - auto painter = window->BeginPaint(); - painter->PushState(); - painter->ConcatTransform(cru::platform::Matrix::Translation(200, 200)); - for (int i = 0; i < points2d.size(); i += 2) { - painter->DrawLine(points2d[i], points2d[i + 1], brush.get(), 1.f); - } - painter->PopState(); - }); - - window->SetVisibility(cru::platform::gui::WindowVisibilityType::Show); - - return application->Run(); -} diff --git a/demos/graphics_experiments/CMakeLists.txt b/demos/graphics_experiments/CMakeLists.txt deleted file mode 100644 index 103d1cc4..00000000 --- a/demos/graphics_experiments/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -add_executable(cru_graphics_experiment_1 1.cpp) -target_link_libraries(cru_graphics_experiment_1 PRIVATE cru_demo_base) - -add_executable(cru_graphics_experiment_2 2.cpp) -target_link_libraries(cru_graphics_experiment_2 PRIVATE cru_demo_base) - -find_package(dlib CONFIG REQUIRED) - -add_executable(cru_graphics_experiment_4 4.cpp) -target_link_libraries(cru_graphics_experiment_4 PRIVATE cru_demo_base dlib::dlib) |