Skip to content

Instantly share code, notes, and snippets.

@admsyn
Created March 7, 2015 01:11
Show Gist options
  • Select an option

  • Save admsyn/0737cf983ad7e3bb1975 to your computer and use it in GitHub Desktop.

Select an option

Save admsyn/0737cf983ad7e3bb1975 to your computer and use it in GitHub Desktop.

Revisions

  1. admsyn created this gist Mar 7, 2015.
    188 changes: 188 additions & 0 deletions ofApp.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,188 @@
    #include "ofApp.h"

    using namespace physx;

    #define actorFlags PxActorTypeSelectionFlag::eRIGID_DYNAMIC | PxActorTypeSelectionFlag::eRIGID_STATIC

    PxDefaultAllocator defaultAllocator;
    PxDefaultErrorCallback defaultErrorCallback;
    const PxReal extent = 4;
    const PxReal halfExtent = extent * 0.5;
    const PxU32 size = 80;

    //--------------------------------------------------------------
    void ofApp::setup(){
    foundation = PxCreateFoundation(PX_PHYSICS_VERSION, defaultAllocator, defaultErrorCallback);
    physics = PxCreatePhysics(PX_PHYSICS_VERSION, *foundation, PxTolerancesScale());
    dispatcher = PxDefaultCpuDispatcherCreate(4);

    PxSceneDesc sceneDesc(physics->getTolerancesScale());
    sceneDesc.gravity = PxVec3(0, -100, 0);
    sceneDesc.cpuDispatcher = dispatcher;
    sceneDesc.filterShader = PxDefaultSimulationFilterShader;

    scene = physics->createScene(sceneDesc);
    material = physics->createMaterial(0.5, 0.4, 0.4);
    // material = physics->createMaterial(0.5, 0.4, 1.1); party mode
    boxPrimitive.setUseVbo(true);
    boxPrimitive.set(extent, extent, extent, 1, 1, 1);

    doPhysics = true;
    keyPressed(' ');

    cam.setPosition(-175, 15, 50);
    cam.lookAt(ofVec3f(0, 0, 0));
    lightNode.setParent(cam);
    lightNode.setPosition(0, 0, 0);

    shader.load("shader");
    physicsSpeed = 1;

    ofSetFrameRate(60);
    ofSetVerticalSync(true);
    ofEnableDepthTest();
    ofEnableAlphaBlending();
    }

    //--------------------------------------------------------------
    void ofApp::update(){

    float now = ofGetElapsedTimef();

    if(ofGetFrameNum() > 1) {
    scene->simulate(now - lastRender);
    scene->fetchResults(true);
    }

    lastRender = now;
    }

    //--------------------------------------------------------------
    void ofApp::draw(){

    ofBackgroundGradient(ofColor::darkGray * 0.3, ofColor::darkGray * 0.05);
    float t = ofGetElapsedTimef();

    cam.begin();

    shader.begin();

    ofFloatColor c = ofColor::aquamarine;
    shader.setUniform3f("brushColor", c.r, c.g, c.b);

    ofFloatColor a = ofColor::aqua * 0.2;
    shader.setUniform3f("ambient", a.r, a.g, a.b);

    float reflectivity = 0.9;
    float intensity = 0.9;
    ofVec3f lightVec = lightNode.getGlobalPosition();

    shader.setUniform4f("lightPosition", lightVec.x, lightVec.y, lightVec.z, 1.0);
    shader.setUniform3f("lightReflectivity", reflectivity, reflectivity, reflectivity);
    shader.setUniform3f("lightIntensity", intensity, intensity, intensity);
    shader.setUniform1f("t", t);

    PxU32 actorCount = scene->getNbActors(actorFlags);
    vector<PxRigidActor*> actors(actorCount);

    if(actorCount) {
    scene->getActors(actorFlags, (PxActor**)&actors[0], actorCount);

    for(PxRigidActor * actor : actors) {
    PxU32 numShapes = actor->getNbShapes();
    vector<PxShape*> shapes(numShapes);
    actor->getShapes(&shapes[0], numShapes);

    for(PxShape * shape : shapes) {
    const PxMat44 shapePose(PxShapeExt::getGlobalPose(*shape, *actor));
    ofMatrix4x4 modelMat((float *)&shapePose);
    ofMatrix3x3 normalMat(modelMat._mat[0].x,modelMat._mat[0].y,modelMat._mat[0].z,
    modelMat._mat[1].x,modelMat._mat[1].y,modelMat._mat[1].z,
    modelMat._mat[2].x,modelMat._mat[2].y,modelMat._mat[2].z);
    normalMat.invert();
    normalMat.transpose();

    shader.setUniformMatrix3f("normalMatrix", normalMat);
    shader.setUniformMatrix4f("modelMatrix", modelMat);
    boxPrimitive.getMesh().draw();
    }
    }
    }

    shader.end();
    cam.end();

    ofDrawBitmapStringHighlight(ofToString((int)ofGetFrameRate()), ofVec2f(20, 20));
    }

    //--------------------------------------------------------------
    void ofApp::keyPressed(int key){

    if(key == ' ') {
    PxU32 actorCount = scene->getNbActors(actorFlags);
    vector<PxRigidActor*> actors(actorCount);
    scene->getActors(actorFlags, (PxActor**)&actors[0], actorCount);
    scene->removeActors((PxActor **)&actors[0], actorCount);

    PxRigidStatic * groundPlane = PxCreatePlane(*physics, PxPlane(0, 1, 0, 0), *material);
    scene->addActor(*groundPlane);

    PxTransform t(PxVec3(0,100,0));
    PxShape* shape = physics->createShape(PxBoxGeometry(halfExtent, halfExtent, halfExtent), *material);
    int count = 0;
    for(PxU32 i=0; i<size;i++) {
    for(PxU32 j=0;j<size-i;j++) {
    PxTransform localTm(PxVec3(PxReal(j*2) - PxReal(size-i), PxReal(i*2+1), 0) * halfExtent);
    PxRigidDynamic* body = physics->createRigidDynamic(t.transform(localTm));
    body->attachShape(*shape);
    PxRigidBodyExt::updateMassAndInertia(*body, 1.0f);
    scene->addActor(*body);
    count++;
    }
    }
    shape->release();
    cout << count << endl;
    } else if(key == 'v') {
    doPhysics = !doPhysics;
    }
    }

    //--------------------------------------------------------------
    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){

    }

    //--------------------------------------------------------------
    void ofApp::mouseReleased(int x, int y, int button){

    }

    //--------------------------------------------------------------
    void ofApp::windowResized(int w, int h){

    }

    //--------------------------------------------------------------
    void ofApp::gotMessage(ofMessage msg){

    }

    //--------------------------------------------------------------
    void ofApp::dragEvent(ofDragInfo dragInfo){

    }
    42 changes: 42 additions & 0 deletions ofApp.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    #pragma once

    #include "ofMain.h"
    #include "ofxGrabCam.h"

    #define _DEBUG
    #include "PxPhysicsAPI.h"
    #undef _DEBUG

    class ofApp : public ofBaseApp{

    public:
    void setup();
    void update();
    void draw();

    void keyPressed(int key);
    void keyReleased(int key);
    void mouseMoved(int x, int y );
    void mouseDragged(int x, int y, int button);
    void mousePressed(int x, int y, int button);
    void mouseReleased(int x, int y, int button);
    void windowResized(int w, int h);
    void dragEvent(ofDragInfo dragInfo);
    void gotMessage(ofMessage msg);

    physx::PxFoundation * foundation;
    physx::PxPhysics * physics;
    physx::PxDefaultCpuDispatcher * dispatcher;
    physx::PxScene * scene;
    physx::PxMaterial * material;

    float lastRender;

    ofxGrabCam cam;
    ofShader shader;
    ofNode lightNode;
    ofBoxPrimitive boxPrimitive;

    bool doPhysics;
    float physicsSpeed;
    };