Skip to content

Instantly share code, notes, and snippets.

@unohee
Created May 2, 2016 08:04
Show Gist options
  • Select an option

  • Save unohee/a21d5b224a354d22f5ac9499369aba87 to your computer and use it in GitHub Desktop.

Select an option

Save unohee/a21d5b224a354d22f5ac9499369aba87 to your computer and use it in GitHub Desktop.
//
// simpleSphere.cpp
// Processing-like 3D primitive-based particle class.
// Created by UNO H
//
//
#include "simpleSphere.h"
simpleSphere::simpleSphere(){
//inits
sphereDetail(5); //resolution of 3D Object
isMoving(true); //boolean for controlling object's effect.
setMovement(TAN); // type of effect
sphere.setScale(.5f); //scale.
}
//INITS------------------------------------------
void simpleSphere::initLocation(float x, float y, float z){
sphere.setPosition(x,y,z);
}
void simpleSphere::setSize(int radius){
sphere.setRadius(radius);
// sphere.setMode(OF_PRIMITIVE_TRIANGLES); //deprecated
}
//-----------------------------------------------
//Effect state-----------------------------------
//put this on oF'update, or setup.
bool simpleSphere::isMoving(bool state){
moveNormal = state;
}
void simpleSphere::setMovement(Vertices_Movement mode){
movement = mode;
}
//-----------------------------------------------
/*
//Color Change-----------------------------------
ofColor simpleSphere::sphereColor(float hue, float saturation, float brightness){
colorHSB.getHsb(hue, saturation, brightness);
}
//-----------------------------------------------
*/
//Updating position------------------------------
//goes update.
void simpleSphere::moveTo(float _x, float _y, float _z){
x += (_x - x) * 0.1;
y += (_y - y) * 0.1;
z += (_z - z) * 0.1;
setPosition(x,y,z);
}
void simpleSphere::moveTo(float _x, float _y){
x += (_x - x) * 0.1;
y += (_y - y) * 0.1;
setPosition(x,y,z);
}
void simpleSphere::moveTo(ofVec3f location){
//add function like above.
location3D.x += (location.x - location3D.x) * 0.5;
location3D.y += (location.y - location3D.y) * 0.5;
location3D.z += (location.z - location3D.z) * 0.5;
setPosition(location3D.x, location3D.y, location3D.z);
}
void simpleSphere::moveto(ofVec2f location){
//add function like above.
location2D.x += (location.x - location2D.x) * 0.5;
location2D.y += (location.y - location2D.y) * 0.5;
setPosition(location2D.x, location2D.y);
}
//-----------------------------------------------
//Check Edge-------------------------------------
void simpleSphere::checkEdges(){
//when it reaches one edge, set location to the other.
int width = ofGetWindowWidth();
int height = ofGetWindowHeight();
//and it has to check all vector and variable in moveTo method above.
if(location2D.x > width || x > width || location3D.x > width ){
location2D.x = 0;
location3D.x = 0;
x = 0;
} else if(location2D.x < 0 || x < 0 || location3D.x < 0){
location2D.x = width;
location3D.x = width;
x = width;
}
if(location2D.y > height || y > height || location3D.y > height){
location2D.y = 0;
location3D.y = 0;
y = 0;
} else if(location2D.y < 0 || y < 0 || location3D.y < 0){
location2D.y = height;
location3D.y = height;
y = height;
}
}
//-----------------------------------------------
void simpleSphere::update(){
angle = ofGetElapsedTimef() * -0.5;
if(moveNormal){
if(movement == SIN){
//SINE
strength = (sin( angle+.25 )) * .5f * 5.f;
}else if(movement == COS){
//COSINE
strength = (cos( angle+.25 )) * .5f * 5.f;
}else if(movement == TAN){
//TANGENT - My favorite.
strength = (tan( angle+.25 )) * .5f * 5.f;
}else{
//throws an error.
}//ENDIF
absPower = fabs(strength);
if (absPower > 25){
strength = 0;
}
}
}
//set Position of Sphere-------------------------
void simpleSphere::setPosition(float xPos, float yPos, float zPos){
//init the position- put in ofApp::setup().
sphere.setPosition(xPos,yPos,zPos);
}
void simpleSphere::setPosition(float xPos, float yPos){
sphere.setPosition(xPos,yPos,0);
}
//-----------------------------------------------
void simpleSphere::sphereDetail(int detail){
sphere.setResolution(detail);
}
void simpleSphere::draw(){
ofRotate(ofGetElapsedTimef()/ PI*0.5); //rotate object
if(moveNormal){ //Effect!
triangles = sphere.getMesh().getUniqueFaces();
material.begin();
ofVec3f faceNormal;
for(size_t i = 0; i < triangles.size(); i++){
// store the face normal here.
// we change the vertices, which makes the face normal change
// every time that we call getFaceNormal //
faceNormal = triangles[i].getFaceNormal();
for(int j = 0; j < 3; j++ ) {
triangles[i].setVertex( j, triangles[i].getVertex(j) + faceNormal * strength);
}
}//END OF ITERATION
//sphere mesh is gonna be a triangle.
sphere.getMesh().setFromTriangles( triangles );
sphere.draw();//draw sphere.
material.end();
}
else{//without effect.
// ofSetColor(colorHSB); //deprecated.
sphere.draw();
}//ENDIF
}
//
// simpleSphere.hpp
// FFT_Motion
// Processing-like 3D primitive-based particle class.
// Created by UNO H
//
#ifndef simpleSphere_h
#define simpleSphere_h
#include "ofMain.h"
enum Vertices_Movement {SIN, COS, TAN};
class simpleSphere{
public:
//setup-------------------------------
simpleSphere();
void initLocation(float x, float y, float z);
void setSize(int radius);
void setMovement(Vertices_Movement mode);
bool isMoving(bool state);
//update------------------------------
//movements
void moveTo(float x, float y, float z);
void moveTo(float x, float y);
void moveTo(ofVec3f location);
void moveto(ofVec2f location);
//update parameters in each frame.
void update(); //goes ofApp's update()
void checkEdges();
ofVec3f getPosition; //returns object's coordinate.
//velocity
//draw--------------------------------
void draw();
//color-------------------------------
// ofColor sphereColor(float hue, float saturation, float brightness); //deprecated
private:
void setPosition(float x, float y, float z); //3D
void setPosition(float x, float y); //2D
void sphereDetail(int detail);
bool moveNormal = false;
//color variables.
float hue, saturation, brightness;
//MOVEMENT BEHAVIOR
Vertices_Movement movement;
//vector location, velocity
ofVec3f location3D, velocity3D;
ofVec2f location2D, velocity2D;
float x,y,z;
float angle, strength, absPower;
protected:
ofSpherePrimitive sphere;
ofColor color; //deprecated
vector<ofMeshFace> triangles;
ofMaterial material;
};
#endif /* simpleSphere_h */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment