rt300@0: // rt300@0: // 3DboxGL.h rt300@0: // tweakathlon rt300@0: // rt300@0: // Created by Robert Tubb on 17/04/2014. rt300@0: // rt300@0: // same as 3d biox but with better 3d visualisation rt300@0: rt300@0: #ifndef __tweakathlon___DboxGL__ rt300@0: #define __tweakathlon___DboxGL__ rt300@0: rt300@0: #include rt300@0: #include "3Dbox.h" rt300@0: #include "UIElement.h" rt300@34: #include "algorithms.h" rt300@0: rt300@0: //Universal function which sets normals for the triangle meshvoid rt300@0: void setNormals( ofMesh &mesh ); rt300@0: rt300@34: //float euclideanDistance(vector v1, vector v2); rt300@0: rt300@0: class Leap3DBoxGL : public Leap3DBox { rt300@0: public: rt300@0: Leap3DBoxGL(float ax, rt300@0: float ay, rt300@0: float awidth, rt300@0: float aheight, rt300@0: float azx, rt300@0: float azy, rt300@0: const UIProps& props); rt300@0: rt300@0: void draw(){ rt300@0: if(hidden)return; rt300@0: if(on){ rt300@0: ofSetColor(foregroundHi); rt300@0: }else{ rt300@0: ofSetColor(foregroundLo); rt300@0: rt300@0: } rt300@0: if(inactive){ rt300@0: ofSetColor(fgInactive); rt300@0: } rt300@0: rt300@0: rt300@0: rt300@0: ofPushMatrix(); rt300@0: ofDisableAlphaBlending(); rt300@0: ofEnableDepthTest(); rt300@0: // move to correct pos rt300@0: rt300@0: ofTranslate( x+width/2, y+height/2, camTrans); rt300@0: ofRotate( angleX, 1, 0, 0 ); rt300@0: ofRotate( angleY, 0, 1, 0 ); rt300@0: rt300@25: rt300@0: ofSetColor(foregroundHi); rt300@0: boxMesh.draw(); rt300@0: rt300@0: // draw indicators rt300@0: drawIndicator(); rt300@0: ofPopMatrix(); rt300@0: rt300@0: drawLabels(); rt300@0: }; rt300@0: rt300@0: void drawIndicator(){ rt300@0: rt300@0: if (hintShowing && (hintZ > zVal)){ rt300@0: hintColor = calculateHintColor(); rt300@26: draw3DCrossHairs(hintX, hintY, hintZ,hintColor, true); rt300@0: rt300@0: } rt300@0: rt300@0: // draw indicator rt300@0: draw3DCrossHairs(xVal,yVal,zVal, indicatorColor); rt300@0: // put light in indicateor rt300@0: // rt300@0: rt300@0: rt300@0: if (hintShowing && hintZ <= zVal){ rt300@0: hintColor = calculateHintColor(); rt300@26: draw3DCrossHairs(hintX, hintY, hintZ,hintColor, true); rt300@0: rt300@0: } rt300@0: } rt300@26: void draw3DCrossHairs(float x , float y, float z, ofColor c, bool isHint = false){ rt300@0: rt300@0: float ix = x*width - width/2; rt300@0: float iy = (1-y)*width - width/2; rt300@0: float iz = (1-z)*width - width/2; rt300@0: rt300@0: float left = - width/2; rt300@0: float bot = width/2; rt300@0: float front = width/2; rt300@0: // rt300@0: ofSetColor(c); rt300@0: ofSetLineWidth(2.); rt300@0: // line to bottom rt300@0: ofLine(ix,iy,iz,ix,bot,iz); rt300@0: // line to left rt300@0: ofLine(ix,iy,iz,left,iy,iz); rt300@0: rt300@0: //blob rt300@26: drawIndicatorBlob(ix,iy,iz,c, isHint); rt300@23: // line to front (a bit wierde?) rt300@0: ofLine(ix,iy,iz,ix,iy,front); rt300@0: rt300@0: rt300@0: rt300@0: } rt300@26: virtual void drawIndicatorBlob( float x, float y, float z, ofColor c, bool isHint = false){ rt300@23: ofSetColor(c); rt300@23: ofDrawIcoSphere(x,y,z,12); rt300@23: } rt300@0: ofColor calculateHintColor(){ rt300@0: rt300@0: rt300@0: // this is all duplicate code and may change so a bit bad >:( rt300@0: float dist = sqrt( 127.*127.*((xVal - hintX)*(xVal - hintX) + (yVal - hintY)*(yVal - hintY) + (zVal - hintZ)*(zVal - hintZ)) ); rt300@0: rt300@0: auto dimComp = sqrt(3.0); rt300@0: int band = -1; rt300@0: if (dist < TARGET_SCORE_CC_BAND*dimComp){ rt300@0: rt300@0: band = 1; rt300@0: rt300@0: }else if (dist < TARGET_SCORE_CC_BAND*2*dimComp){ rt300@0: rt300@0: band = 2; rt300@0: rt300@0: rt300@0: }else if (dist < TARGET_SCORE_CC_BAND*3*dimComp){ rt300@0: rt300@0: band = 3; rt300@0: rt300@0: rt300@0: }else if (dist < TARGET_SCORE_CC_BAND*4*dimComp){ rt300@0: rt300@0: band = 4; rt300@0: rt300@0: rt300@0: }else if (dist < TARGET_SCORE_CC_BAND*6*dimComp){ // 30 rt300@0: rt300@0: band = 5; rt300@0: rt300@0: }else if (dist < TARGET_SCORE_CC_BAND*9*dimComp){ // 45 rt300@0: band = 6; rt300@0: rt300@0: }else{ rt300@0: band = 7; rt300@0: rt300@0: } rt300@0: rt300@0: rt300@0: ofColor c; rt300@0: if(band == 1){ rt300@0: // yellow red blue rt300@0: c = ofColor(255,255,0,255); rt300@0: }else if(band == 2){ rt300@0: c = ofColor(255,0,0,255); rt300@0: }else if(band == 3){ rt300@0: c = ofColor(45,45,255,255); rt300@0: }else if(band == 4){ rt300@0: c = ofColor(0,255,0,255); rt300@0: }else{ rt300@0: c = ofColor(150,235,200,255); rt300@0: } rt300@0: return c; rt300@0: rt300@0: } rt300@0: void drawCylinders(){ rt300@0: ofPushMatrix(); rt300@0: ofTranslate( x, y, camTrans); rt300@0: // vertical rt300@0: ofSetColor(0,255,0); rt300@0: ofDrawCylinder(0, 0, 32, 500*yVal); rt300@0: rt300@0: // into screen rt300@0: ofSetColor(255,0,0); rt300@0: ofRotate( 90, 1, 0, 0 ); rt300@0: ofDrawCylinder(70, 0, 32, 500*zVal); rt300@0: ofRotate( -90, 1, 0, 0 ); rt300@0: // l/r horizona rt300@0: ofSetColor(0,0,255); rt300@0: ofRotate( 90, 0, 0, 1 ); rt300@0: ofDrawCylinder(70, 300, 32, 500*xVal); rt300@0: rt300@0: rt300@0: ofPopMatrix(); rt300@0: } rt300@0: float valToScreen(float val){ rt300@0: float sc; rt300@0: return sc; rt300@0: }; rt300@0: rt300@0: bool handleMyTouch(int x, int y, touchType ttype, int touchID){ rt300@0: static float lastTX = 0.,lastTY = 0.; rt300@0: rt300@0: rt300@0: if (ttype == TOUCH_MOVED && touchID == 0){ rt300@0: float dx = x -lastTX; rt300@0: float dy = y - lastTY; rt300@0: rt300@0: angleY += dx/3; rt300@0: angleX += dy/3; rt300@0: rt300@0: } rt300@0: rt300@0: if (ttype == TOUCH_MOVED && touchID == 1){ rt300@0: float dx = x -lastTX; rt300@0: float dy = y - lastTY; rt300@0: rt300@0: // adjust light angle? rt300@0: angleY += dx/3; rt300@0: angleX += dy/3; rt300@0: rt300@0: } rt300@0: rt300@0: //cout << angleX << " : " << angleY << endl; rt300@0: rt300@0: lastTX = x; rt300@0: lastTY = y; rt300@0: return true; rt300@0: rt300@0: }; rt300@0: rt300@32: rt300@32: void makeTexFace(ofMesh * theMesh, ofPoint LT, ofPoint RT, ofPoint RB, ofPoint LB){ rt300@32: rt300@32: ofPoint texCoordLT = ofPoint(0, 1.); rt300@32: ofPoint texCoordLB = ofPoint(0, 0); rt300@32: ofPoint texCoordRT = ofPoint(1., 1.); rt300@32: ofPoint texCoordRB = ofPoint(1., 0.); rt300@32: rt300@32: theMesh->addVertex(LT); rt300@32: theMesh->addTexCoord(texCoordLT); rt300@32: theMesh->addVertex(LB); rt300@32: theMesh->addTexCoord(texCoordLB); rt300@32: theMesh->addVertex(RB); rt300@32: theMesh->addTexCoord(texCoordRB); rt300@32: rt300@32: rt300@32: rt300@32: theMesh->addVertex(LT); rt300@32: theMesh->addTexCoord(texCoordLT); rt300@32: rt300@32: theMesh->addVertex(RB); rt300@32: theMesh->addTexCoord(texCoordRB); rt300@32: theMesh->addVertex(RT); rt300@32: theMesh->addTexCoord(texCoordRT); rt300@32: } rt300@32: rt300@0: float angleX; rt300@0: float angleY; rt300@0: float depth; rt300@0: ofMesh boxMesh; rt300@0: float camTrans; rt300@0: ofColor indicatorColor; rt300@0: rt300@0: }; rt300@0: rt300@24: #endif /* defined(__tweakathlon___DboxGL__) */