Needing help with integrating Bullet into renderer

SuperGastrocnemius
Posts: 5
Joined: Sat Feb 12, 2011 3:13 pm

Needing help with integrating Bullet into renderer

Post by SuperGastrocnemius »

Hi,

I'm currently working on a small game (real-time raytraced) and I would like to have Bullet Physics to drive the gameplay. I'm not a programmer (I'm able to program very simple stuff though) so I am using an open source renderer (tokaspt, a CUDA based raytracer). For the past month, I have been familiarizing myself with the Bullet Physics engine, modifying the sample code from the demos, learning the API and trying to integrate Bullet into the renderer. Sadly without much success. I hope someone on this forum can help me to achieve this goal (I'm willing to offer some compensation in return). The only thing I need is to have the "Hello World" example from the Bullet wiki page up and running in the renderer. I can add rigid bodies, constraints and physical interaction on my own. Knowledge of CUDA programming is not necessary, all the relevant code parts are C++.

Image

More info, screenshots and videos of my 'game' can be found at http://raytracey.blogspot.com and you can also download an executable at http://code.google.com/p/tokap-the-once-known-as-pong/ (a CUDA enabled card is required to run the game).



Thanks in advance.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Needing help with integrating Bullet into renderer

Post by Erwin Coumans »

It looks great, pity you don't use OpenCL.

Have you considered using SmallPtGpu2 or SmallLuxGPU?
Can you compile and link Bullet into your own project? Add the include path and a instantiate a few Bullet objects without problems?

Can you be more precise where you are getting stuck so someone can help out? Will your project be open source too?
Thanks,
Erwin
SuperGastrocnemius
Posts: 5
Joined: Sat Feb 12, 2011 3:13 pm

Re: Needing help with integrating Bullet into renderer

Post by SuperGastrocnemius »

Thanks a lot your reply Erwin!
Erwin Coumans wrote: pity you don't use OpenCL.

Have you considered using SmallPtGpu2 or SmallLuxGPU?
I have considered using SmallPTGPU and tested it extensively, but unfortunately it's much slower than tokaspt on my hardware. I'm not sure if it's due to the OpenCL driver or the fact that SmallptGPU was developed on ATI hardware, which could explain the speed difference. If I knew how to port CUDA code to OpenCL, I would do it immediately.
Erwin Coumans wrote:Can you compile and link Bullet into your own project? Add the include path and a instantiate a few Bullet objects without problems? Can you be more precise where you are getting stuck so someone can help out?
I can link Bullet in my project without problems (thanks to the very helpful wiki page) and I can compile it successfully, but I'm not sure how to use the updated transformation matrix from Bullet to update the position of objects in my game. As I've said, I'm a total newbie to programming, and I don't know how to transport variables from the main file (where the Bullet physics are running) to the file which updates the scene. I have been trying to make it work for about a month now and I don't know where to start frankly. I guess it should be really easy to solve for someone with knowledge of C++. I can post the source code here if that would offer a better insight. Any help with this would be greatly appreciated.
Erwin Coumans wrote: Will your project be open source too?
Yes, it will be completely open-source. Source code will be published on Google code, so everyone can modify it at will and hopefully someone could port it to OpenCL.
SuperGastrocnemius
Posts: 5
Joined: Sat Feb 12, 2011 3:13 pm

Re: Needing help with integrating Bullet into renderer

Post by SuperGastrocnemius »

I made some progress with the development of the game, designing the game scene. The game is now called 'Unbiased Truck Soccer': there are two 'cars' which have to bump a ball into the goal of the adversary. This is an image of the game (WIP):

Image

And a video: http://www.youtube.com/watch?v=SLq4_zOUjO4 (it renders very slowly on my underpowered GPU, but it should run fluently on recent GPUs).

If you have a CUDA enabled GPU, you can also try a little executable demo that you can download from http://code.google.com/p/tokap-the-once ... loads/list

However, I'm still stuck with the integration of Bullet into the renderer (which is an open source CUDA ray tracer). Please keep in mind that I have very little programming experience (I've recently started learning C++ though). Below is some code from the main file ("tokaspt.cc"). I tried integrating the 'Hello World' example. The engine is rendering an empty room with a ball that is falling down and I'm using the y-position of the ball in the Hello World example to update the position of the rendered ball ('ballYpos' in another file called "gl_scene.cc"), but without success. The project is linking and compiling without errors, but when I start the exe, the console first shows a list of the sphere heights (going from 50 to 0) and than starts rendering the scene with the ball remaining at sphere height 0. So the ball is falling correctly, but the physics simulation ends before the rendering process starts. How should I make the physics run simultaneously with the rendering and update the sphere position? I would greatly appreciate any help. (I have also attached the relevant source code files if someone wants to take a closer look.)

Code: Select all

int main(int argc, char *argv[]) {

const char title[] = "the once known as pong with Bullet!";
	//FIXME: both CUDA and GLUT want to parse that command line, find a way to gracefuly integrate it all.
	int w = window_width, h = window_height;
	const char *filename = "default.scene";
	for (int i=1; i<argc; ++i)
		if (argv[i][0] == '-')
			switch(argv[i][1]) {
				case 'w': if (++i < argc) w = std::atoi(argv[i]); break;
				case 'h': if (++i < argc) h = std::atoi(argv[i]); break;
				default: // assume it's some argument for cuda/glut, skip
					++i;
			}
		else
			filename = argv[i];

	{	// CUDA setup.
		CUT_DEVICE_INIT(argc, argv);
		int dev;
		cudaGetDevice(&dev);
		cudaDeviceProp prop;
		misc::wipe(prop);
		cudaGetDeviceProperties(&prop, dev);
		if (prop.major == 9999 && prop.minor == 9999)
			fatal("no CUDA support?!");

		// HACK: Useful place to play with some render params
		misc::wipe(params);
		params.framebuffer = 0;
		params.gamma = 2.2;
		params.is_progressive = false;
		params.max_paths = 3;
		params.num_spheres = 0;
		params.pass = 0;
		params.regs_per_block = prop.regsPerBlock;
		params.spp = SS*SS;
		params.verbose = true;
	}
	{	// OpenGL / GLUT setup
		glutInit(&argc, argv);
		glutInitDisplayString(glut_init_display_string);
		glutInitWindowSize(w, h);
		glutCreateWindow(title);
		{
			int doublebuffer = glutGet(GLUT_WINDOW_DOUBLEBUFFER);
			int depth = glutGet(GLUT_WINDOW_DEPTH_SIZE);
			int multi = glutGet(GLUT_WINDOW_NUM_SAMPLES);
			printf("glut: doublebuffer %s depth bits %d multisamples %d\n", doublebuffer ? "yes" : "no", depth, multi);

			int ms_buf, ms;
			glGetIntegerv (GL_SAMPLE_BUFFERS_ARB, &ms_buf);
			glGetIntegerv (GL_SAMPLES_ARB, &ms);
			printf("OpenGl: %d sample buffers, %d samples.\n", ms_buf, ms);
		}

		// initialize necessary OpenGL extensions
		glewInit();
		if (!glewIsSupported("GL_VERSION_2_0 GL_ARB_pixel_buffer_object GL_EXT_framebuffer_object"))
		fatal("missing OpenGL 2.0+PBO+FBO");

		ui::init();
		gl::init(point_t(w, h), filename);

		// register callbacks
		glutDisplayFunc(display);
		glutKeyboardFunc(keyboard_regular<1>);
		glutKeyboardUpFunc(keyboard_regular<0>);
		glutSpecialFunc(keyboard_special<1>);
		glutSpecialUpFunc(keyboard_special<0>);
		glutReshapeFunc(reshape);
		glutIdleFunc(idle);

		glutMouseFunc(mouse);
		glutMotionFunc(motion);

		glutIgnoreKeyRepeat(true);
	}
	{

	// Initialize bullet, this is the 'Hello World' example from Wiki

		btBroadphaseInterface* broadphase = new btDbvtBroadphase();
        btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
        btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
        btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
        btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfiguration);
        dynamicsWorld->setGravity(btVector3(0,-2,0));
 
        btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),1);

		btCollisionShape* fallShape = new btSphereShape(1);
 
        btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,-1,0)));
        btRigidBody::btRigidBodyConstructionInfo
                groundRigidBodyCI(0,groundMotionState,groundShape,btVector3(0,0,0));
        btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI);
        dynamicsWorld->addRigidBody(groundRigidBody);
 
        btDefaultMotionState* fallMotionState =
                new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,50,0)));
        btScalar mass = 1;
        btVector3 fallInertia(0,0,0);
        fallShape->calculateLocalInertia(mass,fallInertia);
        btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass,fallMotionState,fallShape,fallInertia);
        btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);
        dynamicsWorld->addRigidBody(fallRigidBody);
 
        for (int i=0 ; i<300 ; i++) 
		{
                dynamicsWorld->stepSimulation(1/60.f,10);
 
                btTransform trans;
                fallRigidBody->getMotionState()->getWorldTransform(trans);

				ballYpos = trans.getOrigin().getY();
				
 
               std::cout << "sphere height: " << trans.getOrigin().getY() << std::endl;
		
		}

			
		glutMainLoop();

        dynamicsWorld->removeRigidBody(fallRigidBody);
        delete fallRigidBody->getMotionState();
        delete fallRigidBody;
 
        dynamicsWorld->removeRigidBody(groundRigidBody);
        delete groundRigidBody->getMotionState();
        delete groundRigidBody;
        delete fallShape;
        delete groundShape;
        delete dynamicsWorld;
        delete solver;
        delete collisionConfiguration;
        delete dispatcher;
        delete broadphase;

	}

	//glutMainLoop();

	return -1;
}
You do not have the required permissions to view the files attached to this post.