Tuesday, April 26, 2011

PhysX Basics Tutorial: Using GLUT with PhysX



This tutorial is on how to setup freeglut with NVIDIA PhysX sdk.


#include <iostream>
#include <GL/freeglut.h>
#include <NxPhysics.h>

using namespace std;

#pragma comment(lib, "PhysXLoader.lib")

const int WINDOW_WIDTH=1024,
WINDOW_HEIGHT=768;

static NxPhysicsSDK* gPhysicsSDK = NULL;

In the above lines, we first include the required headers. Then the requried linker library is linked programmatically. Next we setup the variables for the screen size and a global pointer to the PhysX sdk. This is done so that PhysX is conveniently accessible throughout the code.


void InitializePhysX() {
gPhysicsSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION);
if(gPhysicsSDK == NULL) {
cerr<<"Error creating PhysX device."<<endl;
cerr<<"Exiting..."<<endl;
exit(1);
}
}
void ShutdownPhysX() {
NxReleasePhysicsSDK(gPhysicsSDK);
}

These two functions were discussed in the previous tutorial so nothing new here. For the NXCreatePhysicsSDK function, the other parameters are ignored.


void InitGL() {
}

void OnReshape(int nw, int nh) {
glViewport(0,0,nw, nh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)nw / (GLfloat)nh, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}

void OnRender() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glutSwapBuffers();
}

void OnShutdown() {
ShutdownPhysX();
}
void main(int argc, char** argv) {
atexit(OnShutdown);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("GLUT PhysX Demo");

glutDisplayFunc(OnRender);
glutReshapeFunc(OnReshape);

InitGL();
InitializePhysX();

glutMainLoop();
}

These lines setup a glut window with double buffer support. It also hooks up two functions for resize handling and display. Thats it. The glut window is now setup with PhysX. The next tutorial will discuss how to add a basic cube that falls due to gravity into a glut window. Source codes are here

0 comments:

Popular Posts

Copyright (C) 2011 - Movania Muhammad Mobeen. Awesome Inc. theme. Powered by Blogger.