Guest User

Untitled

a guest
May 5th, 2016
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.53 KB | None | 0 0
  1. // NMNT Project "Hashtag Space"
  2. // Based on the 3DPrimitives Openframeworks example
  3. // Edited by: Petra Kubernátová and Max Roele
  4.  
  5. #include "ofApp.h"
  6. ofImage textures[5];
  7. ofSpherePrimitive spheres[5];
  8. int songname;
  9.  
  10. //--------------------------------------------------------------
  11. void ofApp::setup(){
  12.  
  13. // Loading the background image
  14.    
  15.     bg.load("images/bg.jpg");
  16.    
  17. // Loading the mosaic pictures as textures into an array
  18.    
  19.     ofDisableArbTex();
  20.     textures[4].load("images/space.png");
  21.     textures[4].getTexture().setTextureWrap( GL_REPEAT, GL_REPEAT );
  22.     textures[3].load("images/blue.png");
  23.     textures[3].getTexture().setTextureWrap( GL_REPEAT, GL_REPEAT );
  24.     textures[2].load("images/moon.png");
  25.     textures[2].getTexture().setTextureWrap( GL_REPEAT, GL_REPEAT );
  26.     textures[1].load("images/black.png");
  27.     textures[1].getTexture().setTextureWrap( GL_REPEAT, GL_REPEAT );
  28.     textures[0].load("images/cat.png");
  29.     textures[0].getTexture().setTextureWrap( GL_REPEAT, GL_REPEAT );
  30.    
  31.    
  32. // Mouse press detection variable
  33.    
  34.     bMousePressed   = false;
  35.  
  36. // Getting the width and height variables
  37.    
  38.     float width     = ofGetWidth() * .12;
  39.     float height    = ofGetHeight() * .12;
  40.  
  41. // Setting the radiuses of the spheres
  42.    
  43.     spheres[4].setRadius( 140);
  44.     spheres[3].setRadius( 120);
  45.     spheres[2].setRadius( 80);
  46.     spheres[1].setRadius( 80);
  47.     spheres[0].setRadius( 60);
  48.    
  49. // Turning on the initial mode
  50.     mode = 1;
  51.    
  52.    
  53.  
  54. }
  55.    
  56.    
  57.  
  58.  
  59. //--------------------------------------------------------------
  60. void ofApp::update() {
  61.  
  62. // Updating the positions of the lights when the spheres are spinning
  63.    
  64.     pointLight.setPosition((ofGetWidth()*.5)+ cos(ofGetElapsedTimef()*.5)*(ofGetWidth()*.3), ofGetHeight()/2, 500);
  65.     pointLight2.setPosition((ofGetWidth()*.5)+ cos(ofGetElapsedTimef()*.15)*(ofGetWidth()*.3),
  66.                             ofGetHeight()*.5 + sin(ofGetElapsedTimef()*.7)*(ofGetHeight()), -300);
  67.    
  68.     pointLight3.setPosition(
  69.                             cos(ofGetElapsedTimef()*1.5) * ofGetWidth()*.5,
  70.                             sin(ofGetElapsedTimef()*1.5f) * ofGetWidth()*.5,
  71.                             cos(ofGetElapsedTimef()*.2) * ofGetWidth()
  72.     );
  73.    
  74.  
  75.  
  76. }
  77.  
  78. //--------------------------------------------------------------
  79. void ofApp::draw() {
  80.    
  81.     ofSetColor(255);
  82.  
  83. // Drawing the background image
  84.    
  85.     bg.draw(0,0,1280,900);
  86.  
  87.     float spinX,spinY;
  88.  
  89. // If we are in the initial mode, spin with the following speed. If the mouse is pressed, stop spinning
  90.     if(mode == 1){
  91.         spinX = 0.0f;
  92.         spinY = 0.4f;
  93.         if(bMousePressed) {
  94.             spinX = spinY = 0.0f;
  95.         }
  96.         // Loading the textures onto the spheres
  97.        
  98.         spheres[4].setMode( OF_PRIMITIVE_TRIANGLES );
  99.         triangles = spheres[4].getMesh().getUniqueFaces();
  100.         spheres[3].setMode( OF_PRIMITIVE_TRIANGLES );
  101.         triangles = spheres[3].getMesh().getUniqueFaces();
  102.         spheres[2].setMode( OF_PRIMITIVE_TRIANGLES );
  103.         triangles = spheres[2].getMesh().getUniqueFaces();
  104.         spheres[1].setMode( OF_PRIMITIVE_TRIANGLES );
  105.         triangles = spheres[1].getMesh().getUniqueFaces();
  106.         spheres[0].setMode( OF_PRIMITIVE_TRIANGLES );
  107.         triangles = spheres[0].getMesh().getUniqueFaces();
  108.     }
  109.    
  110.     ofEnableDepthTest();
  111.  
  112. // Turning on the lights
  113.    
  114.     ofEnableLighting();
  115.     pointLight.enable();
  116.     pointLight2.enable();
  117.     pointLight3.enable();
  118.    
  119. // If we are in the exploding mode, spin with the following speed. If the mouse is pressed, stop spinning
  120.     if(mode == 2){
  121.         spinX = 0.1f;
  122.         spinY = 0.2f;
  123.         if(bMousePressed) {
  124.             spinX = spinY = 0.0f;
  125.         }
  126.    
  127.     }
  128.  
  129.    
  130.  // Setting the positions of the spheres and how they rotate
  131.    
  132.     spheres[4].setPosition(ofGetWidth()*.5f, ofGetHeight()*.5, 0);
  133.     spheres[4].rotate(spinX, 0.02, 0.0, 0.0);
  134.     spheres[4].rotate(spinY, 0, 0.02, 0.0);
  135.    
  136.     spheres[3].setPosition(ofGetWidth()*.8f, ofGetHeight()*.66, 0);
  137.     spheres[3].rotate(spinX, 0.02, 0.0, 0.0);
  138.     spheres[3].rotate(spinY, 0, 0.02, 0.0);
  139.    
  140.     spheres[2].setPosition(ofGetWidth()*.7f, ofGetHeight()*.25, 0);
  141.     spheres[2].rotate(spinX, 0.02, 0.0, 0.0);
  142.     spheres[2].rotate(spinY, 0, 0.02, 0.0);
  143.    
  144.     spheres[1].setPosition(ofGetWidth()*.25f, ofGetHeight()*.4, 0);
  145.     spheres[1].rotate(spinX, 0.02, 0.0, 0.0);
  146.     spheres[1].rotate(spinY, 0, 0.02, 0.0);
  147.    
  148.     spheres[0].setPosition(ofGetWidth()*.15f, ofGetHeight()*.85, 0);
  149.     spheres[0].rotate(spinX, 0.02, 0.0, 0.0);
  150.     spheres[0].rotate(spinY, 0, 0.02, 0.0);
  151.    
  152.  
  153. // Setting texture modes and binding the individual textures on the spheres
  154.    
  155.     spheres[4].setMode( OF_PRIMITIVE_TRIANGLES );
  156.     triangles = spheres[4].getMesh().getUniqueFaces();
  157.     spheres[3].setMode( OF_PRIMITIVE_TRIANGLES );
  158.     triangles = spheres[3].getMesh().getUniqueFaces();
  159.     spheres[2].setMode( OF_PRIMITIVE_TRIANGLES );
  160.     triangles = spheres[2].getMesh().getUniqueFaces();
  161.     spheres[1].setMode( OF_PRIMITIVE_TRIANGLES );
  162.     triangles = spheres[1].getMesh().getUniqueFaces();
  163.     spheres[0].setMode( OF_PRIMITIVE_TRIANGLES );
  164.     triangles = spheres[0].getMesh().getUniqueFaces();
  165.    
  166.    
  167.     textures[0].getTexture().bind();
  168.     spheres[0].draw();
  169.     textures[1].getTexture().bind();
  170.     spheres[1].draw();
  171.     textures[2].getTexture().bind();
  172.     spheres[2].draw();
  173.     textures[3].getTexture().bind();
  174.     spheres[3].draw();
  175.     textures[4].getTexture().bind();
  176.     spheres[4].draw();
  177.    
  178. //  Exploding part, divide the texture into smaller parts and "explode" them
  179.    
  180.     if(mode == 2) {
  181.             float angle = ofGetElapsedTimef() * 3.2;
  182.             float strength = 2.5;
  183.             ofVec3f faceNormal;
  184.             for(size_t i = 0; i < triangles.size(); i++ ) {
  185.                 faceNormal = triangles[i].getFaceNormal();
  186.                 for(int j = 0; j < 3; j++ ) {
  187.                     triangles[i].setVertex( j, triangles[i].getVertex(j) + faceNormal * strength);
  188.                 }
  189.             }
  190.             spheres[4].getMesh().setFromTriangles( triangles );
  191.             spheres[3].getMesh().setFromTriangles( triangles );
  192.             spheres[2].getMesh().setFromTriangles( triangles );
  193.             spheres[1].getMesh().setFromTriangles( triangles );
  194.             spheres[0].getMesh().setFromTriangles( triangles );
  195.         }
  196.  
  197.     ofDisableLighting();
  198.  
  199. // Displaying names of the songs
  200.    
  201.     switch(songname) {
  202.         case 1:
  203.             ofFill();
  204.             ofSetColor(255,255,255);
  205.             ofDrawBitmapString("Music: David Bowie - Space Oddity (Virgin Magnetic Material remix)", 100, 100);
  206.             break;
  207.         case 2:
  208.             ofFill();
  209.             ofSetColor(255,255,255);
  210.             ofDrawBitmapString("Music: Duskus - Outer Space", 100, 100);
  211.             break;
  212.         case 3:
  213.             ofFill();
  214.             ofSetColor(255,255,255);
  215.             ofDrawBitmapString("Music: IHElectronic - Space", 100, 100);
  216.             break;
  217.         case 4:
  218.             ofFill();
  219.             ofSetColor(255,255,255);
  220.             ofDrawBitmapString("Music: Jamiroquai - Space Cowboy (Neptune Safari Remix)", 100, 100);
  221.             break;
  222.         case 5:
  223.             ofFill();
  224.             ofSetColor(255,255,255);
  225.             ofDrawBitmapString("Music: Nicolas Jaar - Space is Only Noise if You Can See", 100, 100);
  226.             break;
  227.         case 6:
  228.             ofFill();
  229.             ofSetColor(255,255,255);
  230.             ofDrawBitmapString("Music: Nigel Good - Space Plus One", 100, 100);
  231.             break;
  232.         case 7:
  233.             ofFill();
  234.             ofSetColor(255,255,255);
  235.             ofDrawBitmapString("Music: Nightcore - Blank Space (Taylor Swift cover)", 100, 100);
  236.             break;
  237.         case 8:
  238.             ofFill();
  239.             ofSetColor(255,255,255);
  240.             ofDrawBitmapString("Music: Stephen Walking - Dads in Space", 100, 100);
  241.             break;
  242.         case 9:
  243.             ofFill();
  244.             ofSetColor(255,255,255);
  245.             ofDrawBitmapString("Music: Penthouse - Thru time & space", 100, 100);
  246.             break;
  247.         case 10:
  248.             ofFill();
  249.             ofSetColor(255,255,255);
  250.             ofDrawBitmapString("Music: Space - Cactus Jack", 100, 100);
  251.             break;
  252.         case 11:
  253.             ofFill();
  254.             ofSetColor(255,255,255);
  255.             ofDrawBitmapString("Music: Feat DJ Light - Sensation", 100, 100);
  256.             break;
  257.            
  258.     }
  259.  
  260. // Writing information about the related hashtags
  261.     ofFill();
  262.     ofSetColor(255,255,255);
  263.     ofDrawBitmapString("Related hashtags: #blue #black #moon #cat", 100, 120);
  264.  
  265.    
  266.     ofDisableDepthTest();
  267.    
  268.    
  269. }
  270.  
  271. //--------------------------------------------------------------
  272. void ofApp::keyPressed(int key) {
  273.     ofEnableDepthTest();
  274.  
  275. // Detecting keypresses
  276.     switch(key) {
  277.         case OF_KEY_RIGHT:
  278.             // Switch to the exploding mode
  279.             mode = 2;
  280.             /*bSplitFaces = false;
  281.             spheres[4].setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
  282.             spheres[4].mapTexCoords(0, 0, 5, 5);
  283.            
  284.             spheres[3].setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
  285.             spheres[3].mapTexCoords(0, 0, 5, 5);
  286.            
  287.             spheres[2].setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
  288.             spheres[2].mapTexCoords(0, 0, 5, 5);
  289.            
  290.             spheres[1].setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
  291.             spheres[1].mapTexCoords(0, 0, 5, 5);
  292.            
  293.             spheres[0].setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
  294.             spheres[0].mapTexCoords(0, 0, 5, 5);*/
  295.             break;
  296.         case OF_KEY_LEFT:
  297.             // Pause the explosion
  298.             mode = 1;
  299.             break;
  300.         case OF_KEY_DOWN:
  301.             // Restart the program back to the initial mode
  302.             setup();
  303.             break;
  304.         case OF_KEY_UP:
  305.             // Randomly get a song and play it
  306.             string song = std::to_string(1 + ( std::rand() % ( 11 - 1 + 1 ) ));
  307.             songname = atoi(song.c_str());
  308.             sound.load("music/"+song+".mp3");
  309.             sound.play();
  310.             break;
  311.  
  312.     }
  313.    
  314.    
  315.    
  316.  
  317. }
  318.  
  319. //--------------------------------------------------------------
  320. void ofApp::keyReleased(int key){
  321.  
  322. }
  323.  
  324. //--------------------------------------------------------------
  325. void ofApp::mouseMoved(int x, int y ){
  326.    
  327. }
  328.  
  329. //--------------------------------------------------------------
  330. void ofApp::mouseDragged(int x, int y, int button){
  331.  
  332.  
  333. }
  334.  
  335. //--------------------------------------------------------------
  336. void ofApp::mousePressed(int x, int y, int button){
  337.     // If the mouse is pressed stop spinning
  338.     bMousePressed = true;
  339. }
  340.  
  341. //--------------------------------------------------------------
  342. void ofApp::mouseReleased(int x, int y, int button){
  343.     // If the mouse is released continue spinning
  344.     bMousePressed = false;
  345. }
  346.  
  347. //--------------------------------------------------------------
  348. void ofApp::mouseEntered(int x, int y){
  349.  
  350. }
  351.  
  352. //--------------------------------------------------------------
  353. void ofApp::mouseExited(int x, int y){
  354.  
  355. }
  356.  
  357. //--------------------------------------------------------------
  358. void ofApp::windowResized(int w, int h){
  359.  
  360. }
  361.  
  362. //--------------------------------------------------------------
  363. void ofApp::gotMessage(ofMessage msg){
  364.  
  365. }
  366.  
  367. //--------------------------------------------------------------
  368. void ofApp::dragEvent(ofDragInfo dragInfo){
  369.  
  370. }
Advertisement
Add Comment
Please, Sign In to add comment