0

FPS の計算には、Web で見つけたコードを使用していますが、うまく機能しています。しかし、私はそれを本当に理解していません。私が使用する関数は次のとおりです。

void computeFPS()
{
  numberOfFramesSinceLastComputation++;
  currentTime = glutGet(GLUT_ELAPSED_TIME);

  if(currentTime - timeSinceLastFPSComputation > 1000)
  {
    char fps[256];
    sprintf(fps, "FPS: %.2f", numberOfFramesSinceLastFPSComputation * 1000.0 / (currentTime . timeSinceLastFPSComputation));
    glutSetWindowTitle(fps);
    timeSinceLastFPSComputation = currentTime;
    numberOfFramesSinceLastComputation = 0;
  }
 }

私の質問は、実際には割り当てていないため、スプリント呼び出しで計算された値が fps 配列にどのように格納されるかです。

4

2 に答える 2

3

これは OpenGL に関する質問ではなく、C 標準ライブラリに関する質問です。s(n)printf のリファレンス ドキュメントを読むと、次のことが役立ちます。

男 s(n)printf: http://linux.die.net/man/3/sprintf

簡単に言うと、snprintf は、ユーザーが指定したバッファーとフォーマット文字列へのポインターを取り、フォーマット文字列と追加パラメーターで指定された値に従ってバッファーを埋めます。


これが私の提案です。そのようなことについて尋ねなければならない場合は、まだ OpenGL に取り組まないでください。バッファー オブジェクト データとシェーダー ソースの提供に関しては、ポインターとバッファーの使用に精通している必要があります。これに C を使用する予定がある場合は、C に関する本を入手して、最初に C を徹底的に学習してください。また、C++ とは異なり、C は実際には数か月かけてかなりの程度まで学習できます。

于 2013-01-30T21:00:48.320 に答える
1

この関数は、メインループが再描画されるたびに(フレームごとに)呼び出されると思われます。つまり、フレームのカウンターを増やし、このフレームが表示されている現在の時刻を取得することです。そして、1秒に1回(1000ms)、そのカウンターをチェックして0にリセットします。したがって、1秒ごとにカウンター値を取得すると、その値が取得され、ウィンドウのタイトルとして表示されます。

/**
 * This function has to be called at every frame redraw.
 * It will update the window title once per second (or more) with the fps value.
 */
void computeFPS()
{
  //increase the number of frames
  numberOfFramesSinceLastComputation++;

  //get the current time in order to check if it has been one second
  currentTime = glutGet(GLUT_ELAPSED_TIME);

  //the code in this if will be executed just once per second (1000ms)
  if(currentTime - timeSinceLastFPSComputation > 1000)
  {
    //create a char string with the integer value of numberOfFramesSinceLastComputation and assign it to fps
    char fps[256];
    sprintf(fps, "FPS: %.2f", numberOfFramesSinceLastFPSComputation * 1000.0 / (currentTime . timeSinceLastFPSComputation));

    //use fps to set the window title
    glutSetWindowTitle(fps);

    //saves the current time in order to know when the next second will occur
    timeSinceLastFPSComputation = currentTime;

    //resets the number of frames per second.
    numberOfFramesSinceLastComputation = 0;
  }
 }
于 2013-01-30T21:08:43.507 に答える