// main render function called by glut
void OnRender() {
	//Calculate fps
	totalFrames++;
	int current = glutGet(GLUT_ELAPSED_TIME);
	float elapsedTime = float(current - startTime);
	static float lastfpsTime = 0.0f;
	if ((current - lastfpsTime) > 1000.0f)
	{
		fps = ((totalFrames * 1000.0f) / ((current - lastfpsTime)));
		totalFrames = 0;
		lastfpsTime = float(current);
	}
	startTime = current;

	sprintf_s(buffer, "FPS: %3.2f", fps);

	//Update PhysX	
	if (gScene)
	{
		stepPhysics(elapsedTime / 1000.0f);
	}

	// start render
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glLoadIdentity();
	glTranslatef(0, 0, dist);
	glRotatef(rX, 1, 0, 0);
	glRotatef(rY, 0, 1, 0);

	//Draw the grid and axes
	DrawAxes();
	DrawGrid(100);
	// draw physics objects
	glEnable(GL_LIGHTING);
	RenderPhysXScene();
	glDisable(GL_LIGHTING);

	//Show the fps
	SetOrthoForFont(WINDOW_WIDTH, WINDOW_HEIGHT);
	glColor3f(1, 1, 1);
	RenderSpacedBitmapString(20, 20, 0, GLUT_BITMAP_HELVETICA_12, buffer);

	ResetPerspectiveProjection();
	// finish render
	glutSwapBuffers();
}
示例#2
0
void Universe::update(const double timeStep) {
	//calculate the number of substeps the simulator needs to take
	int subSteps =
			ceil(
					pow(2,
							PhysicsConfiguration::SIMULATION_SPEEDS[mSimulationSpeed])
							* timeStep
							/ PhysicsConfiguration::SIMULATOR_PHYSICS_FIXED_STEP_SIZE_SEC);

	for (int i = 0; i < subSteps; i++) {
		stepPhysics(
				pow(2,
						PhysicsConfiguration::SIMULATION_SPEEDS[mSimulationSpeed])
						* timeStep / ((float) subSteps));
		mEvaluationController.update(
				pow(2,
						PhysicsConfiguration::SIMULATION_SPEEDS[mSimulationSpeed])
						* timeStep / ((float) subSteps));
	}
}
示例#3
0
文件: test.cpp 项目: janoldsen/Onager
void Test::run()
{
	
	bool running = true;
	int lastTime = SDL_GetTicks();

	m_physicsTimer = 0.0f;
	float fps = 0.0f;
	int fpsCount = 1;
	while (running)
	{
		int thisTime = SDL_GetTicks();
		float dt = (thisTime - lastTime) / 1000.0f;
		lastTime = thisTime;

		if (dt >= 1.0f)
			continue;


		fps += dt;
		fpsCount++;
		if (fps >= 1.0f)
		{
			fps -= 1.0f;
			printf("FPS: %d\n", fpsCount);
			fpsCount = 0;
		}
		

		bool step = false;

		SDL_Event event;
		while (SDL_PollEvent(&event))
		{
			if (procEvent(event))
				continue;


			switch (event.type)
			{
			case SDL_QUIT:
				running = false;
				break;
			case SDL_KEYDOWN:
				if (event.key.keysym.scancode == SDL_SCANCODE_K)
					step = true;
				else if ((event.key.keysym.scancode == SDL_SCANCODE_SPACE))
					m_stepping = !m_stepping;
				else if ((event.key.keysym.scancode == SDL_SCANCODE_R))
				{
					m_entities.clear();
					delete m_world;
					gBox = ShapePtr();
					gCapsule = ShapePtr();
					gFloor = ShapePtr();
					gSlope = ShapePtr();
					gSphere = ShapePtr();
					init();
				}
				else if (event.key.keysym.scancode == SDL_SCANCODE_V)
				{
					m_playerSpec = !m_playerSpec;
				}
				break;
			case SDL_MOUSEWHEEL:
				m_eye.p += rotate(vec3(0, 0, event.wheel.y), m_eye.q);
				break;
			case SDL_MOUSEMOTION:


				if (SDL_GetMouseState(0, 0) & SDL_BUTTON(SDL_BUTTON_LEFT))
				{
					m_eye.q = QuatFromAxisAngle(vec3(0, 1, 0), event.motion.xrel / 4.0f * 3.14 / 180.0f) * m_eye.q;
					m_eye.q = QuatFromAxisAngle(rotate(vec3(1, 0, 0), m_eye.q), event.motion.yrel / 4.0f * 3.14 / 180.0f) * m_eye.q;
				}

				if (SDL_GetMouseState(0, 0) & SDL_BUTTON(SDL_BUTTON_MIDDLE))
				{
					m_eye.p += rotate(vec3(-event.motion.xrel / 80.0f, event.motion.yrel / 80.0f, 0.0f), m_eye.q);
				}
			}

		}




		if (m_stepping)
		{
			if (step)
			{
				int numSteps = 1;
				for (int i = 0; i < numSteps; ++i)
				{
					printf("%d\n", m_numSteps);
					m_world->step(1.0f / 60.0f);
					++m_numSteps;
				}
			}
		}
		else
		{
			stepPhysics(dt);
		}
		
		std::vector<Entity*> deadEntities;

		for (int i = 0; i < m_entities.size(); ++i)
		{
			m_entities[i]->update(dt);
			if (m_entities[i]->isDead())
			{
				if (m_entities[i] == m_player)
					m_player = 0;

				//m_entities[i]->destroy(m_toAdd);

				//delete m_entities[i];
				m_entities[i] = m_entities.back();
				m_entities.pop_back();
				--i;
			}
		}

		for (Entity* entity : m_toAdd)
		{
			m_entities.push_back(entity);
		}
		m_toAdd.clear();


		update(dt);
		// render


		//
		glClear(GL_COLOR_BUFFER_BIT);

		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(60.0f, 800.0f / 600.0f, 0.1, 1000.0f);

		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();


		//gluLookAt(eye.x, eye.y, eye.z,
		//	0.0f, 0.0f, 0.0f,
		//	0.0f, 1.0f, 0.0f);
		float view[16];
		if (m_playerSpec && m_player)
			getViewMat(m_player->getView(), view);
		else
			getViewMat(m_eye, view);
		glMultMatrixf(view);



		for (Entity* e : m_entities)
		{
			
			e->render(m_colorLocation);
		}

		render();

		SDL_GL_SwapWindow(m_window);


	}
}
// main render function called by glut
void OnRender() {
	//Calculate fps
	totalFrames++;
	int current = glutGet(GLUT_ELAPSED_TIME);
	float elapsedTime = float(current - startTime);
	static float lastfpsTime = 0.0f;
	if ((current - lastfpsTime) > 1000.0f)
	{
		fps = ((totalFrames * 1000.0f) / ((current - lastfpsTime)));
		totalFrames = 0;
		lastfpsTime = float(current);
	}
	startTime = current;

	sprintf_s(buffer, "FPS: %3.2f", fps);

	//Update PhysX	
	if (gScene)
	{
		stepPhysics(elapsedTime / 1000.0f);
	}

	// start render
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	glTranslatef(0, 0, dist);
	glRotatef(rX, 1, 0, 0);
	glRotatef(rY, 0, 1, 0);
	// shoot
	if (shoot)
	{
		GLdouble matModelView[16], matProjection[16];
		GLdouble camera_pos[3];
		int viewport[4];
		// get matrixs and viewport:
		glGetDoublev(GL_MODELVIEW_MATRIX, matModelView);
		glGetDoublev(GL_PROJECTION_MATRIX, matProjection);
		glGetIntegerv(GL_VIEWPORT, viewport);
		gluUnProject((viewport[2] - viewport[0]) / 2, (viewport[3] - viewport[1]) / 2,
			0.0, matModelView, matProjection, viewport,
			&camera_pos[0], &camera_pos[1], &camera_pos[2]);

		//PxVec3 p(camera_pos[0], camera_pos[1], camera_pos[2]);
		//AddBullet(p, -p.getNormalized() * 10.0f);
		shoot = false;

		//
		//GLdouble mpos[3];
		//gluUnProject(oldX, oldY, 0,
		//	matModelView,
		//	matProjection, viewport, &mpos[0], &mpos[1], &mpos[2]);
		GLfloat winX, winY, winZ; //variables to hold screen x,y,z coordinates
		GLdouble worldX, worldY, worldZ; //variables to hold world x,y,z coordinates

		winX = (float)(viewport[2] - viewport[0]) / 2;
		winY = (float)(viewport[3] - viewport[1]) / 2;
		glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);


		//get the world coordinates from the screen coordinates
		gluUnProject(winX, winY, 0.0, matModelView, matProjection
			, viewport, &worldX, &worldY, &worldZ);
		GLdouble fx, fy, fz;
		gluUnProject(winX, winY, 1.0, matModelView, matProjection
			, viewport, &fx, &fy, &fz);

		PxVec3 pp(worldX, worldY, worldZ);
		PxVec3 vv(fx - worldX, fy - worldY, fz - worldZ);
		AddBullet(pp, vv.getNormalized() * 10.0f);
	}
	//Draw the grid and axes
	DrawAxes();
	DrawGrid(100);
	// draw physics objects
	glEnable(GL_LIGHTING);
	RenderPhysXScene();
	glDisable(GL_LIGHTING);

	//Show the fps
	SetOrthoForFont(WINDOW_WIDTH, WINDOW_HEIGHT);
	glColor3f(1, 1, 1);
	RenderSpacedBitmapString(20, 20, 0, GLUT_BITMAP_HELVETICA_12, buffer);

	ResetPerspectiveProjection();
	// finish render
	glutSwapBuffers();
}