Esempio n. 1
0
void Object::Load(){

	if (!isStatic){
		btTransform trans;
		trans.setFromOpenGLMatrix(glm::value_ptr(position));
		btDefaultMotionState* motState =
			new btDefaultMotionState(trans);
		btScalar mass = 1;
		btVector3 fallInertia(0, 0, 0);
		shape->calculateLocalInertia(mass, fallInertia);
		btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, motState, shape, fallInertia);
		rigidBody = new btRigidBody(fallRigidBodyCI);
		rigidBody->setFriction(1.35f);
		if (isGhost){
			rigidBody->setCollisionFlags(rigidBody->getCollisionFlags() | rigidBody->CF_NO_CONTACT_RESPONSE);
		}
		world->addRigidBody(rigidBody);
	}
	else{
		btTransform trans;
		trans.setFromOpenGLMatrix(glm::value_ptr(position));
		btDefaultMotionState* motState =
			new btDefaultMotionState(trans);
		btRigidBody::btRigidBodyConstructionInfo
			groundRigidBodyCI(0, motState, shape, btVector3(0, 0, 0));
		rigidBody = new btRigidBody(groundRigidBodyCI);
		rigidBody->setFriction(1.35f);
		if (isGhost){
			rigidBody->setCollisionFlags(rigidBody->getCollisionFlags() | rigidBody->CF_NO_CONTACT_RESPONSE);
		}
		world->addRigidBody(rigidBody);
	}

	shader = LoadShaders(vertexName, "geometry-shader[basic].txt", fragmentName);
	cameraUniform = shader->uniform("camera");
	posNormUniform = shader->uniform("normalPos");
	texUniform = shader->uniform("tex");
	disUniform = shader->uniform("dis");
	normUniform = shader->uniform("norm");
	posUniform = shader->uniform("position");
	normal = LoadTexture(LoadBmp(normalName));
	texture = LoadTexture(LoadBmp(textureName));
	displacement = LoadTexture(LoadBmp(displacementName));

	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);

	glGenBuffers(1, &vbo);
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*vertices.size(), &vertices.front(), GL_STATIC_DRAW);

	glEnableVertexAttribArray(shader->attrib("vert_VS_in"));
	glVertexAttribPointer(shader->attrib("vert_VS_in"), 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), NULL);
	glEnableVertexAttribArray(shader->attrib("frag_VS_in"));
	glVertexAttribPointer(shader->attrib("frag_VS_in"), 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (const GLvoid*)(3 * sizeof(GLfloat)));
	glEnableVertexAttribArray(shader->attrib("normal_VS_in"));
	glVertexAttribPointer(shader->attrib("normal_VS_in"), 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (const GLvoid*)(5 * sizeof(GLfloat)));

	glGenBuffers(1, &ibo);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Index)*indices.size(), &indices.front(), GL_STATIC_DRAW);

	glBindVertexArray(0);
}
Esempio n. 2
0
int main( int argc, char* args[] )
{
//SDL
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return 1;
    }

    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return 1;
    }

    //Set the window caption
    SDL_WM_SetCaption( "Hello World", NULL );

    //Load the images
    message = load_image( "data/hello.bmp" );
    background = load_image( "data/background.bmp" );
	sphere = load_image("data/Circle.bmp");

    
//SDL

//Physics
//=> World Creation
	// Specify the dynamic AABB tree broadphase algorithm to be used to work out what objects
	// to test collision for.
	btBroadphaseInterface* broadphase = new btDbvtBroadphase();

	// The collision configuration allows you to fine tune the algorithms used
	// for the full (not broadphase) collision detection. Here be dragons!
	btDefaultCollisionConfiguration* collisionConfiguration =
		new btDefaultCollisionConfiguration();

	btCollisionDispatcher* dispatcher =
		new btCollisionDispatcher(collisionConfiguration);

	// We also need a "solver". This is what causes the objects to interact
	// properly, taking into account gravity, game logic supplied forces,
	// collisions, and hinge constraints. It does a good job as long as you
	// don't push it to extremes, and is one of the bottlenecks in any high
	// performance simulation. There are parallel versions available for some
	// threading models.
	btSequentialImpulseConstraintSolver* solver =
		new btSequentialImpulseConstraintSolver;

	// Now, we can finally instantiate the dynamics world.
	btDiscreteDynamicsWorld* dynamicsWorld =
		new btDiscreteDynamicsWorld(dispatcher, broadphase, solver,
		collisionConfiguration);

	// Set the gravity. We have chosen the Y axis to be "up".
	dynamicsWorld->setGravity(btVector3(0, -10, 0));
//=> World Creation
//=> Ground Creation

	// In this demonstration, we will place a ground plane running through
	// the origin.
	btCollisionShape* groundShape = new btStaticPlaneShape(
		btVector3(0, 1, 0), 1);

	// Instantiate the ground. Its orientation is the identity, Bullet quaternions
	// are specified in x,y,z,w form. The position is 1 metre below the ground,
	// which compensates the 1m offset we had to put into the shape itself.
	btDefaultMotionState* groundMotionState = new btDefaultMotionState(
		btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, -1, 0)));

	// The first and last parameters of the following constructor are the mass and
	// inertia of the ground. Since the ground is static, we represent this by
	// filling these values with zeros. Bullet considers passing a mass of zero
	// equivalent to making a body with infinite mass - it is immovable.
	btRigidBody::btRigidBodyConstructionInfo
		groundRigidBodyCI(0, groundMotionState, groundShape, btVector3(0, 0, 0));

	btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI);

//=> Ground Creation
//=> Sphere Creation
	// The shape that we will let fall from the sky is a sphere with a radius
	// of 1 metre. 
	btCollisionShape* fallShape = new btSphereShape(1);

	// Adding the falling sphere is very similar. We will place it 50m above the
	// ground.
	btDefaultMotionState* fallMotionState =
		new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1),
		btVector3(0, 50, 0)));

	// Since it's dynamic we will give it a mass of 1kg. I can't remember how to
	// calculate the inertia of a sphere, but that doesn't matter because Bullet
	// provides a utility function.
	btScalar mass = 1;
	btVector3 fallInertia(0, 0, 0);
	fallShape->calculateLocalInertia(mass, fallInertia);

	// Construct the rigid body just like before, and add it to the world.
	btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass,
		fallMotionState, fallShape, fallInertia);

	btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);
	
//=> Sphere Creation

	// Add the ground and sphere to the world.
	dynamicsWorld->addRigidBody(fallRigidBody);

	dynamicsWorld->addRigidBody(groundRigidBody);
//Physics
	bool go = true;
	while (go){
		SDL_Event incomingEvent;

		while (SDL_PollEvent(&incomingEvent))
		{

			switch (incomingEvent.type)
			{
			case SDL_QUIT:

				go = false;
				break;
			}
		}
		//Logic
		//Physics
		dynamicsWorld->stepSimulation(1 / 60.f, 10);

		btTransform trans;
		fallRigidBody->getMotionState()->getWorldTransform(trans);

		//std::cout << "sphere height: " << trans.getOrigin().getY() << std::endl;
		//Physics
		//SDL
			//Clear screen
			SDL_FillRect(screen, NULL, 0x000000);
			//Apply the background to the screen
			apply_surface(0, 0, background, screen);
			apply_surface(320, 0, background, screen);
			apply_surface(0, 240, background, screen);
			apply_surface(320, 240, background, screen);

			//Apply the message to the screen
			//apply_surface(180, 140, message, screen);

			apply_surface(50, -(trans.getOrigin().getY()), sphere, screen);
			//apply_surface(50, 50, sphere, screen);
			std::cout << "X: " << trans.getOrigin().getX() << " Y: " << trans.getOrigin().getY() << std::endl;
		//SDL
		//Update 
		SDL_Flip(screen);
		//Wait 2 seconds
	 SDL_Delay( 50 );
	}
    
    

    //Free the surfaces
    SDL_FreeSurface( message );
    SDL_FreeSurface( background );

    //Quit SDL
    SDL_Quit();

    return 0;
}
Esempio n. 3
0
int main(int argc, char *argv[])
{
  // Build the broadphase
  btBroadphaseInterface* broadphase = new btDbvtBroadphase();

  btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
  btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);

  // The actual physics solver
  btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;

  btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
  dynamicsWorld->setGravity(btVector3(0, -10, 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.f/60.f, 10);

    btTransform trans;
    fallRigidBody->getMotionState()->getWorldTransform(trans);

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

  dynamicsWorld->removeRigidBody(fallRigidBody);
  delete fallRigidBody->getMotionState();
  delete fallRigidBody;

  dynamicsWorld->removeRigidBody(groundRigidBody);
  delete groundRigidBody->getMotionState();
  delete groundRigidBody;

  delete fallShape;
  delete groundShape;

  // Clean up
  delete dynamicsWorld;
  delete solver;
  delete dispatcher;
  delete collisionConfiguration;
  delete broadphase;

  return 0;
}