view UI code/3DboxGL.h @ 52:89944ab3e129 tip

fix oF linker errors ios8
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Tue, 03 Feb 2015 13:18:23 +0000
parents 3af380769779
children
line wrap: on
line source
//
//  3DboxGL.h
//  tweakathlon
//
//  Created by Robert Tubb on 17/04/2014.
//
// same as 3d biox but with better 3d visualisation

#ifndef __tweakathlon___DboxGL__
#define __tweakathlon___DboxGL__

#include <iostream>
#include "3Dbox.h"
#include "UIElement.h"
#include "algorithms.h"

//Universal function which sets normals for the triangle meshvoid
void setNormals( ofMesh &mesh );

//float euclideanDistance(vector<float> v1, vector<float> v2);

class Leap3DBoxGL : public Leap3DBox {
public:
    Leap3DBoxGL(float ax,
              float ay,
              float awidth,
              float aheight,
              float azx,
              float azy,
              const UIProps& props);
    
    void draw(){
        if(hidden)return;
        if(on){
            ofSetColor(foregroundHi);
        }else{
            ofSetColor(foregroundLo);
            
        }
        if(inactive){
            ofSetColor(fgInactive);
        }

        
        
        ofPushMatrix();
        ofDisableAlphaBlending();
        ofEnableDepthTest();
        // move to correct pos
        
        ofTranslate( x+width/2, y+height/2, camTrans);
        ofRotate( angleX, 1, 0, 0 );
        ofRotate( angleY, 0, 1, 0 );

        
        ofSetColor(foregroundHi);
        boxMesh.draw();
        
                 // draw indicators
        drawIndicator();
        ofPopMatrix();
        
        drawLabels();
    };
    
    void drawIndicator(){

        if (hintShowing && (hintZ > zVal)){
            hintColor = calculateHintColor();
            draw3DCrossHairs(hintX, hintY, hintZ,hintColor, true);
            
        }
        
        // draw indicator
        draw3DCrossHairs(xVal,yVal,zVal, indicatorColor);
        // put light in indicateor
        //
        
        
        if (hintShowing && hintZ <= zVal){
            hintColor = calculateHintColor();
            draw3DCrossHairs(hintX, hintY, hintZ,hintColor, true);
            
        }
    }
    void draw3DCrossHairs(float x , float y, float z, ofColor c, bool isHint = false){
        
        float ix = x*width - width/2;
        float iy = (1-y)*width - width/2;
        float iz = (1-z)*width - width/2;
        
        float left = - width/2;
        float bot = width/2;
        float front = width/2;
        //
        ofSetColor(c);
        ofSetLineWidth(2.);
        // line to bottom
        ofLine(ix,iy,iz,ix,bot,iz);
        // line to left
        ofLine(ix,iy,iz,left,iy,iz);

        //blob
        drawIndicatorBlob(ix,iy,iz,c, isHint);
        // line to front (a bit wierde?)
        ofLine(ix,iy,iz,ix,iy,front);
        

        
    }
    virtual void drawIndicatorBlob( float x, float y, float z, ofColor c, bool isHint = false){
        ofSetColor(c);
        ofDrawIcoSphere(x,y,z,12);
    }
    ofColor calculateHintColor(){

        
        // this is all duplicate code and may change so a bit bad >:(
        float dist = sqrt( 127.*127.*((xVal - hintX)*(xVal - hintX) + (yVal - hintY)*(yVal - hintY) + (zVal - hintZ)*(zVal - hintZ)) );
        
        auto dimComp = sqrt(3.0);
        int band = -1;
        if (dist < TARGET_SCORE_CC_BAND*dimComp){

            band = 1;

        }else if (dist < TARGET_SCORE_CC_BAND*2*dimComp){

            band = 2;

            
        }else if (dist < TARGET_SCORE_CC_BAND*3*dimComp){

            band = 3;

            
        }else if (dist < TARGET_SCORE_CC_BAND*4*dimComp){

            band = 4;

            
        }else if (dist < TARGET_SCORE_CC_BAND*6*dimComp){ // 30

            band = 5;
            
        }else if (dist < TARGET_SCORE_CC_BAND*9*dimComp){ // 45
            band = 6;
            
        }else{
            band = 7;
 
        }

        
        ofColor c;
        if(band == 1){
            // yellow red blue
            c = ofColor(255,255,0,255);
        }else if(band == 2){
            c = ofColor(255,0,0,255);
        }else if(band == 3){
            c = ofColor(45,45,255,255);
        }else if(band == 4){
            c = ofColor(0,255,0,255);
        }else{
            c = ofColor(150,235,200,255);
        }
        return c;
        
    }
    void drawCylinders(){
        ofPushMatrix();
        ofTranslate( x, y, camTrans);
        // vertical
        ofSetColor(0,255,0);
        ofDrawCylinder(0, 0, 32, 500*yVal);
        
        // into screen
        ofSetColor(255,0,0);
        ofRotate( 90, 1, 0, 0 );
        ofDrawCylinder(70, 0, 32, 500*zVal);
        ofRotate( -90, 1, 0, 0 );
        // l/r horizona
        ofSetColor(0,0,255);
        ofRotate( 90, 0, 0, 1 );
        ofDrawCylinder(70, 300, 32, 500*xVal);
        
        
        ofPopMatrix();
    }
    float valToScreen(float val){
        float sc;
        return sc;
    };
    
    bool handleMyTouch(int x, int y, touchType ttype, int touchID){
        static float lastTX = 0.,lastTY = 0.;


        if (ttype == TOUCH_MOVED && touchID == 0){
            float dx = x -lastTX;
            float dy = y - lastTY;
            
            angleY += dx/3;
            angleX += dy/3;

        }
        
        if (ttype == TOUCH_MOVED && touchID == 1){
            float dx = x -lastTX;
            float dy = y - lastTY;
            
            // adjust light angle?
            angleY += dx/3;
            angleX += dy/3;
            
        }
        
        //cout << angleX << " : " << angleY << endl;
        
        lastTX = x;
        lastTY = y;
        return true;

    };
    
    
    void makeTexFace(ofMesh * theMesh, ofPoint LT, ofPoint RT, ofPoint  RB, ofPoint LB){
        
        ofPoint texCoordLT = ofPoint(0, 1.);
        ofPoint texCoordLB = ofPoint(0, 0);
        ofPoint texCoordRT = ofPoint(1., 1.);
        ofPoint texCoordRB = ofPoint(1., 0.);
        
        theMesh->addVertex(LT);
        theMesh->addTexCoord(texCoordLT);
        theMesh->addVertex(LB);
        theMesh->addTexCoord(texCoordLB);
        theMesh->addVertex(RB);
        theMesh->addTexCoord(texCoordRB);
        
        
        
        theMesh->addVertex(LT);
        theMesh->addTexCoord(texCoordLT);
        
        theMesh->addVertex(RB);
        theMesh->addTexCoord(texCoordRB);
        theMesh->addVertex(RT);
        theMesh->addTexCoord(texCoordRT);
    }
    
    float angleX;
    float angleY;
    float depth;
    ofMesh boxMesh;
    float camTrans;
    ofColor indicatorColor;
    
};

#endif /* defined(__tweakathlon___DboxGL__) */