1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| #include <stdio.h>
#include <iostream>
#include <GL/gl.h>
#include <GL/glut.h>
#include <NxPhysics.h>
using namespace std;
// Physics
static NxPhysicsSDK* gPhysicsSDK = NULL;
static NxScene* gScene = NULL;
// Rendering
static NxVec3 gEye(50.0f, 50.0f, 50.0f);
static NxVec3 gDir(-0.6f,-0.2f,-0.7f);
static NxVec3 gViewY;
static void CreateCube(const NxVec3& pos, int size=2, const NxVec3* initialVelocity=NULL) {
if(gScene == NULL) return;
// Create body
NxBodyDesc bodyDesc;
bodyDesc.angularDamping = 0.5f;
if(initialVelocity) bodyDesc.linearVelocity = *initialVelocity;
NxBoxShapeDesc boxDesc;
boxDesc.dimensions = NxVec3((float)size, (float)size, (float)size);
NxActorDesc actorDesc;
actorDesc.shapes.pushBack(&boxDesc);
actorDesc.body = &bodyDesc;
actorDesc.density = 10.0f;
actorDesc.globalPose.t = pos;
gScene->createActor(actorDesc)->userData = (void*)size_t(size);
//cout << "Total: " << gScene->getNbActors() << " actors" << endl;
}
void display(void) {
gScene->simulate(1.0f/60.0f);
// Clear buffers
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// Setup projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, (float)glutGet(GLUT_WINDOW_WIDTH)/(float)glutGet(GLUT_WINDOW_HEIGHT), 1.0f, 10000.0f);
gluLookAt(gEye.x, gEye.y, gEye.z, gEye.x + gDir.x, gEye.y + gDir.y, gEye.z + gDir.z, 0.0f, 1.0f, 0.0f);
// Setup modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Render all actors
int nbActors = gScene->getNbActors();
NxActor** actors = gScene->getActors();
while(nbActors--) {
NxActor* actor = *actors++;
if(!actor->userData) continue;
// Render actor
glPushMatrix();
float glMat[16];
actor->getGlobalPose().getColumnMajor44(glMat);
glMultMatrixf(glMat);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glutSolidCube(float(size_t(actor->userData))*2.0f);
glPopMatrix();
// Render shadow
glPushMatrix();
const static float shadowMat[]={ 1,0,0,0, 0,0,0,0, 0,0,1,0, 0,0,0,1 };
glMultMatrixf(shadowMat);
glMultMatrixf(glMat);
glDisable(GL_LIGHTING);
glColor4f(0.1f, 0.2f, 0.3f, 1.0f);
glutSolidCube(float(size_t(actor->userData))*2.0f);
glEnable(GL_LIGHTING);
glPopMatrix();
}
gScene->flushStream();
gScene->fetchResults(NX_RIGID_BODY_FINISHED, true);
glutSwapBuffers();
}
void initNX(void) {
gPhysicsSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION);
NxSceneDesc sceneDesc;
sceneDesc.gravity = NxVec3(0.0f,-9.8f,0.0f);
gScene = gPhysicsSDK->createScene(sceneDesc);
// Set default material
NxMaterial* defaultMaterial = gScene->getMaterialFromIndex(0);
defaultMaterial->setRestitution(0.0f);
defaultMaterial->setStaticFriction(0.5f);
defaultMaterial->setDynamicFriction(0.5f);
// Create ground plane
NxPlaneShapeDesc planeDesc;
NxActorDesc actorDesc;
actorDesc.shapes.pushBack(&planeDesc);
gScene->createActor(actorDesc);
}
void deinitNX(void) {
gPhysicsSDK->releaseScene(*gScene);
gScene = 0;
gPhysicsSDK->release();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_STENCIL);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Physics");
// Setup default render states
glClearColor(0.3f, 0.4f, 0.5f, 1.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
// Setup lighting
glEnable(GL_LIGHTING);
float ambientColor[] = { 0.0f, 0.1f, 0.2f, 0.0f };
float diffuseColor[] = { 1.0f, 1.0f, 1.0f, 0.0f };
float specularColor[] = { 0.0f, 0.0f, 0.0f, 0.0f };
float position[] = { 100.0f, 100.0f, 400.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientColor);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseColor);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularColor);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHT0);
initNX();
NxVec3 vit = NxVec3(0.0f,0.0f,0.0f);
CreateCube(NxVec3(0.0f, 50.0f, 0.0f),3,&vit);
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
deinitNX();
return 0;
} |
Partager