Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // NMNT Project "Hashtag Space"
- // Based on the 3DPrimitives Openframeworks example
- // Edited by: Petra Kubernátová and Max Roele
- #include "ofApp.h"
- ofImage textures[5];
- ofSpherePrimitive spheres[5];
- int songname;
- //--------------------------------------------------------------
- void ofApp::setup(){
- // Loading the background image
- bg.load("images/bg.jpg");
- // Loading the mosaic pictures as textures into an array
- ofDisableArbTex();
- textures[4].load("images/space.png");
- textures[4].getTexture().setTextureWrap( GL_REPEAT, GL_REPEAT );
- textures[3].load("images/blue.png");
- textures[3].getTexture().setTextureWrap( GL_REPEAT, GL_REPEAT );
- textures[2].load("images/moon.png");
- textures[2].getTexture().setTextureWrap( GL_REPEAT, GL_REPEAT );
- textures[1].load("images/black.png");
- textures[1].getTexture().setTextureWrap( GL_REPEAT, GL_REPEAT );
- textures[0].load("images/cat.png");
- textures[0].getTexture().setTextureWrap( GL_REPEAT, GL_REPEAT );
- // Mouse press detection variable
- bMousePressed = false;
- // Getting the width and height variables
- float width = ofGetWidth() * .12;
- float height = ofGetHeight() * .12;
- // Setting the radiuses of the spheres
- spheres[4].setRadius( 140);
- spheres[3].setRadius( 120);
- spheres[2].setRadius( 80);
- spheres[1].setRadius( 80);
- spheres[0].setRadius( 60);
- // Turning on the initial mode
- mode = 1;
- }
- //--------------------------------------------------------------
- void ofApp::update() {
- // Updating the positions of the lights when the spheres are spinning
- pointLight.setPosition((ofGetWidth()*.5)+ cos(ofGetElapsedTimef()*.5)*(ofGetWidth()*.3), ofGetHeight()/2, 500);
- pointLight2.setPosition((ofGetWidth()*.5)+ cos(ofGetElapsedTimef()*.15)*(ofGetWidth()*.3),
- ofGetHeight()*.5 + sin(ofGetElapsedTimef()*.7)*(ofGetHeight()), -300);
- pointLight3.setPosition(
- cos(ofGetElapsedTimef()*1.5) * ofGetWidth()*.5,
- sin(ofGetElapsedTimef()*1.5f) * ofGetWidth()*.5,
- cos(ofGetElapsedTimef()*.2) * ofGetWidth()
- );
- }
- //--------------------------------------------------------------
- void ofApp::draw() {
- ofSetColor(255);
- // Drawing the background image
- bg.draw(0,0,1280,900);
- float spinX,spinY;
- // If we are in the initial mode, spin with the following speed. If the mouse is pressed, stop spinning
- if(mode == 1){
- spinX = 0.0f;
- spinY = 0.4f;
- if(bMousePressed) {
- spinX = spinY = 0.0f;
- }
- // Loading the textures onto the spheres
- spheres[4].setMode( OF_PRIMITIVE_TRIANGLES );
- triangles = spheres[4].getMesh().getUniqueFaces();
- spheres[3].setMode( OF_PRIMITIVE_TRIANGLES );
- triangles = spheres[3].getMesh().getUniqueFaces();
- spheres[2].setMode( OF_PRIMITIVE_TRIANGLES );
- triangles = spheres[2].getMesh().getUniqueFaces();
- spheres[1].setMode( OF_PRIMITIVE_TRIANGLES );
- triangles = spheres[1].getMesh().getUniqueFaces();
- spheres[0].setMode( OF_PRIMITIVE_TRIANGLES );
- triangles = spheres[0].getMesh().getUniqueFaces();
- }
- ofEnableDepthTest();
- // Turning on the lights
- ofEnableLighting();
- pointLight.enable();
- pointLight2.enable();
- pointLight3.enable();
- // If we are in the exploding mode, spin with the following speed. If the mouse is pressed, stop spinning
- if(mode == 2){
- spinX = 0.1f;
- spinY = 0.2f;
- if(bMousePressed) {
- spinX = spinY = 0.0f;
- }
- }
- // Setting the positions of the spheres and how they rotate
- spheres[4].setPosition(ofGetWidth()*.5f, ofGetHeight()*.5, 0);
- spheres[4].rotate(spinX, 0.02, 0.0, 0.0);
- spheres[4].rotate(spinY, 0, 0.02, 0.0);
- spheres[3].setPosition(ofGetWidth()*.8f, ofGetHeight()*.66, 0);
- spheres[3].rotate(spinX, 0.02, 0.0, 0.0);
- spheres[3].rotate(spinY, 0, 0.02, 0.0);
- spheres[2].setPosition(ofGetWidth()*.7f, ofGetHeight()*.25, 0);
- spheres[2].rotate(spinX, 0.02, 0.0, 0.0);
- spheres[2].rotate(spinY, 0, 0.02, 0.0);
- spheres[1].setPosition(ofGetWidth()*.25f, ofGetHeight()*.4, 0);
- spheres[1].rotate(spinX, 0.02, 0.0, 0.0);
- spheres[1].rotate(spinY, 0, 0.02, 0.0);
- spheres[0].setPosition(ofGetWidth()*.15f, ofGetHeight()*.85, 0);
- spheres[0].rotate(spinX, 0.02, 0.0, 0.0);
- spheres[0].rotate(spinY, 0, 0.02, 0.0);
- // Setting texture modes and binding the individual textures on the spheres
- spheres[4].setMode( OF_PRIMITIVE_TRIANGLES );
- triangles = spheres[4].getMesh().getUniqueFaces();
- spheres[3].setMode( OF_PRIMITIVE_TRIANGLES );
- triangles = spheres[3].getMesh().getUniqueFaces();
- spheres[2].setMode( OF_PRIMITIVE_TRIANGLES );
- triangles = spheres[2].getMesh().getUniqueFaces();
- spheres[1].setMode( OF_PRIMITIVE_TRIANGLES );
- triangles = spheres[1].getMesh().getUniqueFaces();
- spheres[0].setMode( OF_PRIMITIVE_TRIANGLES );
- triangles = spheres[0].getMesh().getUniqueFaces();
- textures[0].getTexture().bind();
- spheres[0].draw();
- textures[1].getTexture().bind();
- spheres[1].draw();
- textures[2].getTexture().bind();
- spheres[2].draw();
- textures[3].getTexture().bind();
- spheres[3].draw();
- textures[4].getTexture().bind();
- spheres[4].draw();
- // Exploding part, divide the texture into smaller parts and "explode" them
- if(mode == 2) {
- float angle = ofGetElapsedTimef() * 3.2;
- float strength = 2.5;
- ofVec3f faceNormal;
- for(size_t i = 0; i < triangles.size(); i++ ) {
- faceNormal = triangles[i].getFaceNormal();
- for(int j = 0; j < 3; j++ ) {
- triangles[i].setVertex( j, triangles[i].getVertex(j) + faceNormal * strength);
- }
- }
- spheres[4].getMesh().setFromTriangles( triangles );
- spheres[3].getMesh().setFromTriangles( triangles );
- spheres[2].getMesh().setFromTriangles( triangles );
- spheres[1].getMesh().setFromTriangles( triangles );
- spheres[0].getMesh().setFromTriangles( triangles );
- }
- ofDisableLighting();
- // Displaying names of the songs
- switch(songname) {
- case 1:
- ofFill();
- ofSetColor(255,255,255);
- ofDrawBitmapString("Music: David Bowie - Space Oddity (Virgin Magnetic Material remix)", 100, 100);
- break;
- case 2:
- ofFill();
- ofSetColor(255,255,255);
- ofDrawBitmapString("Music: Duskus - Outer Space", 100, 100);
- break;
- case 3:
- ofFill();
- ofSetColor(255,255,255);
- ofDrawBitmapString("Music: IHElectronic - Space", 100, 100);
- break;
- case 4:
- ofFill();
- ofSetColor(255,255,255);
- ofDrawBitmapString("Music: Jamiroquai - Space Cowboy (Neptune Safari Remix)", 100, 100);
- break;
- case 5:
- ofFill();
- ofSetColor(255,255,255);
- ofDrawBitmapString("Music: Nicolas Jaar - Space is Only Noise if You Can See", 100, 100);
- break;
- case 6:
- ofFill();
- ofSetColor(255,255,255);
- ofDrawBitmapString("Music: Nigel Good - Space Plus One", 100, 100);
- break;
- case 7:
- ofFill();
- ofSetColor(255,255,255);
- ofDrawBitmapString("Music: Nightcore - Blank Space (Taylor Swift cover)", 100, 100);
- break;
- case 8:
- ofFill();
- ofSetColor(255,255,255);
- ofDrawBitmapString("Music: Stephen Walking - Dads in Space", 100, 100);
- break;
- case 9:
- ofFill();
- ofSetColor(255,255,255);
- ofDrawBitmapString("Music: Penthouse - Thru time & space", 100, 100);
- break;
- case 10:
- ofFill();
- ofSetColor(255,255,255);
- ofDrawBitmapString("Music: Space - Cactus Jack", 100, 100);
- break;
- case 11:
- ofFill();
- ofSetColor(255,255,255);
- ofDrawBitmapString("Music: Feat DJ Light - Sensation", 100, 100);
- break;
- }
- // Writing information about the related hashtags
- ofFill();
- ofSetColor(255,255,255);
- ofDrawBitmapString("Related hashtags: #blue #black #moon #cat", 100, 120);
- ofDisableDepthTest();
- }
- //--------------------------------------------------------------
- void ofApp::keyPressed(int key) {
- ofEnableDepthTest();
- // Detecting keypresses
- switch(key) {
- case OF_KEY_RIGHT:
- // Switch to the exploding mode
- mode = 2;
- /*bSplitFaces = false;
- spheres[4].setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
- spheres[4].mapTexCoords(0, 0, 5, 5);
- spheres[3].setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
- spheres[3].mapTexCoords(0, 0, 5, 5);
- spheres[2].setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
- spheres[2].mapTexCoords(0, 0, 5, 5);
- spheres[1].setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
- spheres[1].mapTexCoords(0, 0, 5, 5);
- spheres[0].setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
- spheres[0].mapTexCoords(0, 0, 5, 5);*/
- break;
- case OF_KEY_LEFT:
- // Pause the explosion
- mode = 1;
- break;
- case OF_KEY_DOWN:
- // Restart the program back to the initial mode
- setup();
- break;
- case OF_KEY_UP:
- // Randomly get a song and play it
- string song = std::to_string(1 + ( std::rand() % ( 11 - 1 + 1 ) ));
- songname = atoi(song.c_str());
- sound.load("music/"+song+".mp3");
- sound.play();
- break;
- }
- }
- //--------------------------------------------------------------
- void ofApp::keyReleased(int key){
- }
- //--------------------------------------------------------------
- void ofApp::mouseMoved(int x, int y ){
- }
- //--------------------------------------------------------------
- void ofApp::mouseDragged(int x, int y, int button){
- }
- //--------------------------------------------------------------
- void ofApp::mousePressed(int x, int y, int button){
- // If the mouse is pressed stop spinning
- bMousePressed = true;
- }
- //--------------------------------------------------------------
- void ofApp::mouseReleased(int x, int y, int button){
- // If the mouse is released continue spinning
- bMousePressed = false;
- }
- //--------------------------------------------------------------
- void ofApp::mouseEntered(int x, int y){
- }
- //--------------------------------------------------------------
- void ofApp::mouseExited(int x, int y){
- }
- //--------------------------------------------------------------
- void ofApp::windowResized(int w, int h){
- }
- //--------------------------------------------------------------
- void ofApp::gotMessage(ofMessage msg){
- }
- //--------------------------------------------------------------
- void ofApp::dragEvent(ofDragInfo dragInfo){
- }
Advertisement
Add Comment
Please, Sign In to add comment