From b1796d191188b9ff1d64ebf7718a8f4bf56db9c3 Mon Sep 17 00:00:00 2001 From: dipsywong98 Date: Thu, 28 Feb 2019 22:56:55 +0800 Subject: [PATCH] polygon brush and its scattered version --- ImpBrush.h | 2 + ImpressionistDoc.cpp | 5 ++ ImpressionistUI.cpp | 2 + PolygonBrush.cpp | 88 +++++++++++++++++++++++++++++++++++ PolygonBrush.h | 18 +++++++ impressionist.vcxproj | 2 + impressionist.vcxproj.filters | 6 +++ readme.md | 3 +- 8 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 PolygonBrush.cpp create mode 100644 PolygonBrush.h diff --git a/ImpBrush.h b/ImpBrush.h index d0a3cec..8df5907 100644 --- a/ImpBrush.h +++ b/ImpBrush.h @@ -25,6 +25,8 @@ enum BLUR_BRUSH, SHARPEN_BRUSH, PIXELIZE_BRUSH, + POLYGON_BRUSH, + SCATTERED_POLYGON_BRUSH, NUM_BRUSH_TYPE // Make sure this stays at the end! }; diff --git a/ImpressionistDoc.cpp b/ImpressionistDoc.cpp index 7b746b3..a3696af 100644 --- a/ImpressionistDoc.cpp +++ b/ImpressionistDoc.cpp @@ -26,6 +26,7 @@ #include "Bayesian.h" #include "KernelBrush.h" #include "PixelizeBrush.h" +#include "PolygonBrush.h" #define DESTROY(p) { if ((p)!=NULL) {delete [] p; p=NULL; } } @@ -75,6 +76,10 @@ ImpressionistDoc::ImpressionistDoc() = new KernelBrush( this, "Sharpen Brush" ); ImpBrush::c_pBrushes[PIXELIZE_BRUSH] = new PixelizeBrush( this, "Pixelize Brush" ); + ImpBrush::c_pBrushes[POLYGON_BRUSH] + = new PolygonBrush( this, "Polygon Brush" ); + ImpBrush::c_pBrushes[SCATTERED_POLYGON_BRUSH] + = new PolygonBrush( this, "Polygon Brush", true ); std::vector> blurKernel = { {1,2,1}, diff --git a/ImpressionistUI.cpp b/ImpressionistUI.cpp index 36542b1..f180172 100644 --- a/ImpressionistUI.cpp +++ b/ImpressionistUI.cpp @@ -824,6 +824,8 @@ Fl_Menu_Item ImpressionistUI::brushTypeMenu[NUM_BRUSH_TYPE + 1] = { { "Blur Brush", FL_ALT + 'a', (Fl_Callback*)ImpressionistUI::cb_brushChoice, (void*)BLUR_BRUSH }, { "Sharpen Brush", FL_ALT + 'a', (Fl_Callback*)ImpressionistUI::cb_brushChoice, (void*)SHARPEN_BRUSH }, { "Pixelize Brush", FL_ALT + 'a', (Fl_Callback*)ImpressionistUI::cb_brushChoice, (void*)PIXELIZE_BRUSH }, + { "Polygon Brush", FL_ALT + 'a', (Fl_Callback*)ImpressionistUI::cb_brushChoice, (void*)POLYGON_BRUSH }, + { "Scattered Polygon Brush", FL_ALT + 'a', (Fl_Callback*)ImpressionistUI::cb_brushChoice, (void*)SCATTERED_POLYGON_BRUSH }, { 0 } }; diff --git a/PolygonBrush.cpp b/PolygonBrush.cpp new file mode 100644 index 0000000..39b55f4 --- /dev/null +++ b/PolygonBrush.cpp @@ -0,0 +1,88 @@ +#include "PolygonBrush.h" +// +// PolygonBrush.cpp +// +// The implementation of Line Brush. It is a kind of ImpBrush. All your brush implementations +// will look like the file with the different GL primitive calls. +// + +#include "impressionistDoc.h" +#include "impressionistUI.h" +#include +#include + +extern float frand(); +extern int irand(int max); +extern double degToRad(double); + +PolygonBrush::PolygonBrush(ImpressionistDoc* pDoc, char* name,bool scattered) : + ImpBrush(pDoc, name),scattered(scattered) +{ +} +void PolygonBrush::BrushBegin(const Point source, const Point target) +{ + ImpressionistDoc* pDoc = GetDocument(); + ImpressionistUI* dlg = pDoc->m_pUI; + + BrushMove(source, target); +} + +void PolygonBrush::BrushMove(const Point source, const Point target) +{ + ImpressionistDoc* pDoc = GetDocument(); + ImpressionistUI* dlg = pDoc->m_pUI; + + if (pDoc == NULL) { + printf("PolygonBrush::BrushMove document is NULL\n"); + return; + } + + if (source.x > pDoc->m_nPaintWidth || source.y < 0) + { + return; + } + + int size = pDoc->getSize(); + if(!scattered) + { + SetColor(source); + DrawPolygon(target, size,nSides?nSides: irand(10)); + }else + { + for (int i = 0; i < 0.3*irand(size); i++) + { + const double dx = -size / 2.f + irand(size); + const double dy = -size / 2.f + irand(size); + const Point sourcePt(source.x + dx, source.y + dy); + const Point targetPt(target.x + dx, target.y + dy); + if (sourcePt.x > pDoc->m_nPaintWidth || sourcePt.y < 0) + { + continue; + } + SetColor(sourcePt); + DrawPolygon(targetPt, size, nSides ? nSides : irand(10)); + } + } +} + +void PolygonBrush::BrushEnd(const Point source, const Point target) +{ +} + +void PolygonBrush::DrawPolygon(const Point target, int size, int nSides) +{ + glBegin(GL_POLYGON); + if (nSides < 3)nSides = 3; + double incre = 360.f / nSides; + int offset = irand(360); + for (double i = 0; i < 360; i+=incre) + { + double rad = degToRad(i + offset); + glVertex2f( + static_cast(target.x) + cos(rad)*size / 2.f, + static_cast(target.y) + sin(rad)*size / 2.f + ); + } + + glEnd(); +} \ No newline at end of file diff --git a/PolygonBrush.h b/PolygonBrush.h new file mode 100644 index 0000000..ee440b8 --- /dev/null +++ b/PolygonBrush.h @@ -0,0 +1,18 @@ +#pragma once +#include "ImpBrush.h" +class PolygonBrush : + public ImpBrush +{ +public: + PolygonBrush(ImpressionistDoc* pDoc = NULL, char* name = NULL, bool scattered = false); + + void BrushBegin(const Point source, const Point target); + void BrushMove(const Point source, const Point target); + void BrushEnd(const Point source, const Point target); + void DrawPolygon(Point target, int size, int nSides); + char* BrushName(void); + PolygonBrush(); + ~PolygonBrush() = default; + bool scattered; + int nSides = 0; +}; diff --git a/impressionist.vcxproj b/impressionist.vcxproj index b55b47c..4ab8da6 100644 --- a/impressionist.vcxproj +++ b/impressionist.vcxproj @@ -148,6 +148,7 @@ + @@ -178,6 +179,7 @@ + diff --git a/impressionist.vcxproj.filters b/impressionist.vcxproj.filters index d9859a2..6542b23 100644 --- a/impressionist.vcxproj.filters +++ b/impressionist.vcxproj.filters @@ -99,6 +99,9 @@ Source Files + + Source Files + @@ -188,5 +191,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/readme.md b/readme.md index 1081532..23c7265 100644 --- a/readme.md +++ b/readme.md @@ -15,7 +15,8 @@ Programming project 1 of HKUST Computer Graphics course COMP4411 - [x] (1W each) more brushes - [ ] pattern brush - [x] pixelize brush - - [ ] random polygon + - [X] random polygon + - [X] random scattered polygon - [ ] clone brush - [x] **(1W)** clipped bushes - [x] **(1W)** cursor on original image