Skip to content

Commit

Permalink
adding ellipse shape (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
d3cod3 committed Apr 24, 2019
1 parent 5ceaeb2 commit 5ee7339
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 47 deletions.
2 changes: 1 addition & 1 deletion example-ofxMtlMapping2D/example-ofxMtlMapping2D.qbs.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.6.1, 2019-04-22T17:27:54. -->
<!-- Written by QtCreator 4.6.1, 2019-04-23T09:46:57. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
39 changes: 13 additions & 26 deletions example-ofxMtlMapping2D/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ void ofApp::draw(){
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
if(key == 's'){
void ofApp::keyPressed(ofKeyEventArgs &e){
if(e.key == 's'){
// save mapping
ofFileDialogResult saveFileResult = ofSystemSaveDialog("mapping.xml", "Save your mapping config");
if (saveFileResult.bSuccess){
_mapping->saveMappingAs(saveFileResult.getPath());
}
}else if(key == 'l'){
}else if(e.key == 'l'){
// load mapping
ofFileDialogResult openFileResult= ofSystemLoadDialog("Select a mapping file");
if (openFileResult.bSuccess){
Expand All @@ -52,41 +52,28 @@ void ofApp::keyPressed(int key){
}
}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){

}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){

void ofApp::mouseDragged(ofMouseEventArgs &e){
_mapping->mouseDragged(e);
}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){

void ofApp::mousePressed(ofMouseEventArgs &e){
_mapping->mousePressed(e);
}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
void ofApp::mouseReleased(ofMouseEventArgs &e){
_mapping->mouseReleased(e);
}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){

void ofApp::mouseScrolled(ofMouseEventArgs &e){
_mapping->mouseScrolled(e);
}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){

void ofApp::windowResized(ofResizeEventArgs &e){
_mapping->windowResized(e);
}
34 changes: 17 additions & 17 deletions example-ofxMtlMapping2D/src/ofApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@

class ofApp : public ofBaseApp{

public:
void setup();
void update();
void draw();

void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
private:
ofxMtlMapping2D* _mapping;
public:
void setup();
void update();
void draw();

void keyPressed(ofKeyEventArgs &e);

void mouseDragged(ofMouseEventArgs &e);
void mousePressed(ofMouseEventArgs &e);
void mouseReleased(ofMouseEventArgs &e);
void mouseScrolled(ofMouseEventArgs &e);

void windowResized(ofResizeEventArgs &e);

private:

ofxMtlMapping2D* _mapping;

};
13 changes: 13 additions & 0 deletions src/controls/ofxMtlMapping2DControls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ ofxMtlMapping2DControls::ofxMtlMapping2DControls(int width){

// ---- Tool box
shapeTypesAsString[MAPPING_2D_SHAPE_QUAD] = "quad";
shapeTypesAsString[MAPPING_2D_SHAPE_ELLIPSE] = "ellipse";
shapeTypesAsString[MAPPING_2D_SHAPE_GRID] = "grid";
shapeTypesAsString[MAPPING_2D_SHAPE_TRIANGLE] = "triangle";
shapeTypesAsString[MAPPING_2D_SHAPE_MASK] = "mask";
Expand All @@ -65,6 +66,7 @@ ofxMtlMapping2DControls::ofxMtlMapping2DControls(int width){
_loadMapping = false;
_editShapes = false;
_createNewQuad = false;
_createNewEllipse = false;
_createNewGrid = false;
_createNewTriangle = false;
_createNewMask = false;
Expand All @@ -79,6 +81,7 @@ ofxMtlMapping2DControls::ofxMtlMapping2DControls(int width){
// add mapping shape controls
if (ofxMtlMapping2DSettings::kIsManuallyCreatingShapeEnabled) {
_toolsCanvas->addWidgetDown(new ofxUIImageToggle(kToggleSize, kToggleSize, _createNewQuad, "GUI/quad.png", kSettingMappingCreateNewQuad));
//_toolsCanvas->addWidgetDown(new ofxUIImageToggle(kToggleSize, kToggleSize, _createNewEllipse, "GUI/quad.png", kSettingMappingCreateNewEllipse));
_toolsCanvas->addWidgetDown(new ofxUIImageToggle(kToggleSize, kToggleSize, _createNewGrid, "GUI/grid.png", kSettingMappingCreateNewGrid));
_toolsCanvas->addWidgetDown(new ofxUIImageToggle(kToggleSize, kToggleSize, _createNewTriangle, "GUI/triangle.png", kSettingMappingCreateNewTriangle));
_toolsCanvas->addWidgetDown(new ofxUIImageToggle(kToggleSize, kToggleSize, _createNewMask, "GUI/mask.png", kSettingMappingCreateNewMask));
Expand All @@ -105,6 +108,12 @@ void ofxMtlMapping2DControls::toolsUiEvent(ofxUIEventArgs &event){
_createNewQuad = true;
}
}
/*else if (name == kSettingMappingCreateNewEllipse) {
// will happen only if ofxMtlMapping2DSettings::kIsManuallyCreatingShapeEnabled is true
if(getToggleValue(name)) {
_createNewEllipse = true;
}
}*/
else if (name == kSettingMappingCreateNewGrid) {
// will happen only if ofxMtlMapping2DSettings::kIsManuallyCreatingShapeEnabled is true
if(getToggleValue(name)) {
Expand Down Expand Up @@ -167,6 +176,7 @@ void ofxMtlMapping2DControls::setUIShapeEditingState(bool isOn)

// ----
((ofxUIImageToggle *)_toolsCanvas->getWidget(kSettingMappingCreateNewQuad))->setVisible(_editShapes);
//((ofxUIImageToggle *)_toolsCanvas->getWidget(kSettingMappingCreateNewEllipse))->setVisible(_editShapes);
((ofxUIImageToggle *)_toolsCanvas->getWidget(kSettingMappingCreateNewGrid))->setVisible(_editShapes);
((ofxUIImageToggle *)_toolsCanvas->getWidget(kSettingMappingCreateNewTriangle))->setVisible(_editShapes);
((ofxUIImageToggle *)_toolsCanvas->getWidget(kSettingMappingCreateNewMask))->setVisible(_editShapes);
Expand Down Expand Up @@ -344,6 +354,9 @@ void ofxMtlMapping2DControls::resetCreateNewShape()
{
_createNewQuad = false;
((ofxUIToggle *)_toolsCanvas->getWidget(kSettingMappingCreateNewQuad))->setValue(false);

//_createNewEllipse = false;
//((ofxUIToggle *)_toolsCanvas->getWidget(kSettingMappingCreateNewEllipse))->setValue(false);

_createNewGrid = false;
((ofxUIToggle *)_toolsCanvas->getWidget(kSettingMappingCreateNewGrid))->setValue(false);
Expand Down
3 changes: 3 additions & 0 deletions src/controls/ofxMtlMapping2DControls.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define kControlsMappingShapesListPanelWidth 150

#define kSettingMappingCreateNewQuad "MAPPING:CREATE_NEW_QUAD"
#define kSettingMappingCreateNewEllipse "MAPPING:CREATE_NEW_ELLIPSE"
#define kSettingMappingCreateNewGrid "MAPPING:CREATE_NEW_GRID"
#define kSettingMappingCreateNewTriangle "MAPPING:CREATE_NEW_TRIANGLE"
#define kSettingMappingCreateNewMask "MAPPING:CREATE_NEW_MASK"
Expand All @@ -36,6 +37,7 @@ class ofxMtlMapping2DControls

const bool& editShapes() { return _editShapes; }
const bool& createNewQuad() { return _createNewQuad; }
const bool& createNewEllipse() { return _createNewEllipse; }
const bool& createNewGrid() { return _createNewGrid; }
const bool& createNewTriangle() { return _createNewTriangle; }
const bool& createNewMask() { return _createNewMask; }
Expand Down Expand Up @@ -92,6 +94,7 @@ class ofxMtlMapping2DControls

bool _editShapes;
bool _createNewQuad;
bool _createNewEllipse;
bool _createNewGrid;
bool _createNewTriangle;
bool _createNewMask;
Expand Down
19 changes: 19 additions & 0 deletions src/ofxMtlMapping2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ void ofxMtlMapping2D::update()
createQuad(ofGetWidth()/2, ofGetHeight()/2);
return;
}

if(ofxMtlMapping2DControls::mapping2DControls()->createNewEllipse()) {
ofxMtlMapping2DControls::mapping2DControls()->resetCreateNewShape();
createEllipse(ofGetWidth()/2, ofGetHeight()/2);
return;
}

if(ofxMtlMapping2DControls::mapping2DControls()->createNewGrid()) {
ofxMtlMapping2DControls::mapping2DControls()->resetCreateNewShape();
Expand Down Expand Up @@ -361,6 +367,19 @@ void ofxMtlMapping2D::createQuad(float _x, float _y)
ofxMtlMapping2DControls::mapping2DControls()->addShapeToList(ofxMtlMapping2DShape::nextShapeId, MAPPING_2D_SHAPE_QUAD);
}

//--------------------------------------------------------------
void ofxMtlMapping2D::createEllipse(float _x, float _y)
{
ofxMtlMapping2DShape::nextShapeId++;

ofxMtlMapping2DShape* newShape = new ofxMtlMapping2DEllipse();
newShape->shapeType = MAPPING_2D_SHAPE_ELLIPSE;
newShape->init(ofxMtlMapping2DShape::nextShapeId, true);
ofxMtlMapping2DShapes::pmShapes.push_front(newShape);

ofxMtlMapping2DControls::mapping2DControls()->addShapeToList(ofxMtlMapping2DShape::nextShapeId, MAPPING_2D_SHAPE_ELLIPSE);
}

//--------------------------------------------------------------
void ofxMtlMapping2D::createGrid(float _x, float _y)
{
Expand Down
2 changes: 2 additions & 0 deletions src/ofxMtlMapping2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//Mapping
#include "ofxMtlMapping2DVertex.h"
#include "ofxMtlMapping2DQuad.h"
#include "ofxMtlMapping2DEllipse.h"
#include "ofxMtlMapping2DGrid.h"
#include "ofxMtlMapping2DTriangle.h"
#include "ofxMtlMapping2DMask.h"
Expand Down Expand Up @@ -77,6 +78,7 @@ class ofxMtlMapping2D {
void render();

void createQuad(float _x, float _y);
void createEllipse(float _x, float _y);
void createGrid(float _x, float _y);
void createTriangle(float _x, float _y);
void createMask(float _x, float _y);
Expand Down
7 changes: 4 additions & 3 deletions src/ofxMtlMapping2DShapeType.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
enum ofxMtlMapping2DShapeType
{
MAPPING_2D_SHAPE_QUAD = 0,
MAPPING_2D_SHAPE_GRID = 1,
MAPPING_2D_SHAPE_TRIANGLE = 2,
MAPPING_2D_SHAPE_MASK = 3,
MAPPING_2D_SHAPE_ELLIPSE = 1,
MAPPING_2D_SHAPE_GRID = 2,
MAPPING_2D_SHAPE_TRIANGLE = 3,
MAPPING_2D_SHAPE_MASK = 4,
MAPPING_2D_SHAPE_COUNT
};
72 changes: 72 additions & 0 deletions src/shapes/ofxMtlMapping2DEllipse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "ofxMtlMapping2DEllipse.h"

//--------------------------------------------------------------
//--------------------------------------------------------------
ofxMtlMapping2DEllipse::ofxMtlMapping2DEllipse()
{
radius = 100.0f;
}

//--------------------------------------------------------------
ofxMtlMapping2DEllipse::~ofxMtlMapping2DEllipse(){

}

//--------------------------------------------------------------
void ofxMtlMapping2DEllipse::render()
{
glPushMatrix();
glMultMatrixf(inputPolygon->homoMatrix);
glBegin(GL_POLYGON);


for (int i = 0; i < inputPolygon->polyline->size(); i++) {
glTexCoord2f(inputPolygon->polyline->getVertices()[i].x, inputPolygon->polyline->getVertices()[i].y);
glVertex2f(inputPolygon->polyline->getVertices()[i].x - inputPolygon->boundingBox.x, inputPolygon->polyline->getVertices()[i].y - inputPolygon->boundingBox.y);
}


glEnd();
glPopMatrix();
}


//--------------------------------------------------------------
void ofxMtlMapping2DEllipse::createDefaultShape()
{
shapeSettings["type"] = "ellipse";

inputPolygon = new ofxMtlMapping2DInput();


int xOffset = ofGetWidth()/2.0;
int yOffset = ofGetHeight()/2.0;

for(int i=0;i<4;i++){
//Create a new vertex
ofxMtlMapping2DVertex* newVertex = new ofxMtlMapping2DVertex();
newVertex->init(xOffset + radius*cosf(i*TWO_PI/4), yOffset + radius*sinf(i*TWO_PI/4));
vertices.push_back(newVertex);

// Input
newVertex = new ofxMtlMapping2DVertex();
newVertex->init(xOffset + radius*cosf(i*TWO_PI/4), yOffset + radius*sinf(i*TWO_PI/4));
newVertex->isDefiningTectureCoord = true;
inputPolygon->vertices.push_back(newVertex);
}


// ----
inputPolygon->init(shapeId);
}

//--------------------------------------------------------------
void ofxMtlMapping2DEllipse::calcHomography()
{
updatePolyline();

inputPolygon->calcHomography(polyline->getVertices()[0].x, polyline->getVertices()[0].y,
polyline->getVertices()[1].x, polyline->getVertices()[1].y,
polyline->getVertices()[2].x, polyline->getVertices()[2].y,
polyline->getVertices()[3].x, polyline->getVertices()[3].y);
}
21 changes: 21 additions & 0 deletions src/shapes/ofxMtlMapping2DEllipse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "ofxMtlMapping2DShape.h"


//--------------------------------------------------------------
//--------------------------------------------------------------
class ofxMtlMapping2DEllipse : public ofxMtlMapping2DShape {

public:

ofxMtlMapping2DEllipse();
~ofxMtlMapping2DEllipse();

protected:
virtual void createDefaultShape();
virtual void render();
virtual void calcHomography();

float radius;
};

0 comments on commit 5ee7339

Please sign in to comment.