Esempio n. 1
0
void kzsGLUtil::drawAxes()
{
	glPushMatrix();
	glColor3f( 0, 0, 1 );
	glPushMatrix();
	glTranslatef( 0, 0, 0.8f );
	glutSolidCone( 0.0325, 0.2, 4, 1 );
	// Draw label Z
	glTranslatef(0,0.0625,0.225f);
	RenderSpacedBitmapString(0,0,0,GLUT_BITMAP_HELVETICA_10, "Z");
	glPopMatrix();
	glutSolidCone(0.0225,1, 4,1);


	glColor3f( 1, 0, 0 );
	glRotatef( 90, 0, 1, 0 );
	glPushMatrix();
	glTranslatef( 0, 0, 0.8f );
	glutSolidCone( 0.0325, 0.2, 4, 1 );
	// Draw label X
	glTranslatef( 0, 0.0625, 0.225f );
	RenderSpacedBitmapString( 0, 0, 0, GLUT_BITMAP_HELVETICA_10, "X" );
	glPopMatrix();
	glutSolidCone( 0.0225, 1, 4, 1 );

	glColor3f( 0, 1, 0 );
	glRotatef( 90, -1, 0, 0 );
	glPushMatrix();
	glTranslatef( 0, 0, 0.8f );
	glutSolidCone( 0.0325, 0.2, 4, 1 );
	// Draw label Y
	glTranslatef( 0, 0.0625, 0.225f );
	RenderSpacedBitmapString( 0, 0, 0, GLUT_BITMAP_HELVETICA_10, "Y" );
	glPopMatrix();
	glutSolidCone( 0.0225, 1, 4, 1 );
	glPopMatrix();
}
// 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();
}
// 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();
}