コード例 #1
0
// 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();
}
コード例 #2
0
bool CApp::FrameFunc( )
{
    if( iLife <= 0 )
    {
        w_Window.setTitle( "Game Over" );
    }
    else
    {
        fSpawnTimer += 0.1f;
        if( fSpawnTimer >= 10 )
        {
            fSpawnTimer = rand( ) % 800 / 100;
            AddEnemy( 900, 100 + rand( ) % 500, 2.5f + float( ( rand( ) % 30 ) ) / 10.0f );
        }

        for( auto it : o_Object )
        {
            it->Update( );
            if( it->x < 0 )
            {
                o_Object.remove( it );
                iLife--;
                char cTmp[20];
                sprintf( cTmp, "Lifes: %d", iLife );
                w_Window.setTitle( cTmp );
                break;
            }
            if( it->x > 950 )
            {
                o_Object.remove( it );
                break;
            }

            bool bDeleted = false;
            for( auto it2 : o_Object )
            {
                if( it->sfr.intersects( it2->sfr ) && it->sfr != it2->sfr )
                {
                    o_Object.remove( it2 );
                    bDeleted = true;
                    break;
                }
            }
            if( bDeleted )
            {
                o_Object.remove( it );
                break;
            }
        }
    }

    static float fReload = 0;

    if ( sf::Keyboard::isKeyPressed( sf::Keyboard::Space ) && fReload <= 0 )
    {
        AddBullet( sf::Mouse::getPosition( ).x, sf::Mouse::getPosition( ).y );
        fReload = 1.5f;
    }

    fReload -= 0.1f;

    if( sf::Keyboard::isKeyPressed( sf::Keyboard::Escape ) )
        return false;
    return true;
}