2. PhysX Windows

Jump To:
main.cpp Source
Download

Main.CPP File:

Now that we have our workspace setup for coding with the PhysX SDK, lets get a basic program up and running that incorporates OpenGL. I am going to start off with a base OpenGL program with a display function and reshape function, and the base GLUT code in our main function. The base of this program looks simply like this:

#include <GL\glut.h>
#include <GL\gl.h>
void display(void){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    glLoadIdentity();
glutSwapBuffers();
}
void reshape(int w, int h){
    if (h == 0) h = 1;
    glViewport(0,0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, (GLfloat)w / (GLfloat)h, 0.1f, 100.0f);
    glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Physics");
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

So lets go ahead and create a function to initialise our PhysX SDK with. I am going to call this initNX. This is going to contain the code from the previous tutorial for Physics SDK:

bool initNX(void){
    gPhysicsSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION);
    return gPhysicsSDK != NULL;
}

Now that we have initialised the PhysX SDK, lets go ahead and create a cleanup function which I am going to call deinitNX:

void deinitNX(void){
    if (gPhysicsSDK != NULL) {
        gPhysicsSDK->release();
        gPhysicsSDK = NULL;
    }
}

These functions are going to be called from inside our main function:

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Physics");
initNX();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
deinitNX();
return 0;
}

In my opinion, this is looking much neater and much more like a program than the last tutorial. So next up we are going to create a virtual scene for our physics calculations to take place. Check it out here.

#include <cstdlib>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/gl.h>
#define NOMINMAX
#include <windows.h>
#include <Physics/include/NxPhysics.h>

#pragma comment(lib, "PhysXLoader.lib")
#pragma comment(lib, "NxCharacter.lib")
#pragma comment(lib, "NxCooking.lib")
#pragma comment(lib, "NxExtensions.lib")

static NxPhysicsSDK *gPhysicsSDK = NULL;
void deinitNX(void) {
    if (gPhysicsSDK != NULL) {
        gPhysicsSDK->release();
        gPhysicsSDK = NULL;
    }
}

bool initNX(void) {
    gPhysicsSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION);
    if (gPhysicsSDK == NULL) {
        return false;
    }

    return true;
}

void display(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    glLoadIdentity();
    glutSwapBuffers();
}

void reshape(int w, int h) {
    if (h == 0) h = 1;
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 0.1, 100.0);
    glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Physics");
    glEnable(GL_DEPTH_TEST);

    if (!initNX()) {
        return 1;
    }
    std::atexit(deinitNX);

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

Source & downloads

Questions or corrections? If something doesn’t work or you spot something I’ve got wrong, leave a comment below. These tutorials have been around a long time, so useful corrections are always welcome!

  • March 25, 2010