2012-02-14 8 views
1

私の迷路タイプのゲームをより速くするために、私はテクスチャの中にボールを置くことにしました。なぜなら、すべての部屋でそれを一度描画しなければならず、ステンシルバッファを使用すると、テクスチャを使用するよりも時間がかかります。問題は、ゲームの開始から3番目のフレームをレンダリングしているときにバックバッファーから正しくテクスチャの中に入れていることです。私の質問はなぜそうですか?バックバッファの内容をOpenGLのテクスチャに戻す

私は喉の詰まったフレームからテクスチャを使用しているとき、私は固体の白色のテクスチャを持っているので、内部に何もありません。 2番目のフレームからテクスチャを使用しているときは、目的のテクスチャの黒い背景のみがあり、3番目のフレームからテクスチャを取得すると、テクスチャが必要になります。フレーム数については、 "drawTexture"関数の内部で静的変数 "done"を使用します。最初のフレームから

コピー:

enter image description here

第3フレームからコピー(望ましい結果):

enter image description here第二フレームから

enter image description here

コピー

void DrawBall::drawTexture(float imageD) { 
    static int done = 0; 

    if (done < 3) { 
     drawToTexture(imageD); 
     done++; 
    } 

    glEnable(GL_TEXTURE_2D); 
    glBindTexture (GL_TEXTURE_2D, texture); 

    glColor3f(1, 1, 1); 
    glBegin (GL_QUADS); 
     glTexCoord2f (0.0, 0.0); glVertex3f (0.0, 0.0, -imageD);  
     glTexCoord2f (1.0, 0.0); glVertex3f (5.0, 0.0, -imageD); 
     glTexCoord2f (1.0, 1.0); glVertex3f (5.0, 5.0, -imageD);  
     glTexCoord2f (0.0, 1.0); glVertex3f (0.0, 5.0, -imageD); 
    glEnd(); 

    glDisable(GL_TEXTURE_2D); 
} 

void DrawBall::drawToTexture(float imageD) { 
    int viewport[4]; 
    glGetIntegerv(GL_VIEWPORT, (int*) viewport); 

    int textureWidth = 64; 
    int textureHeight = 64; 

    texture = genEmptyTexture(textureWidth, textureHeight); 

    glViewport(0, 0, textureWidth, textureHeight); 
    glMatrixMode(GL_PROJECTION);        
    glLoadIdentity();          
    gluPerspective(45.0f, 1, 1, 100); 
    glMatrixMode(GL_MODELVIEW);       
    glLoadIdentity();          

    /* 
     This function calculates the vertexes for the ball 
     inside a vector<vector<float>> variable "test" 
    */ 
    _calculateCircleVertexes(0.0f, 0.0f, -2.0f, 0.249f, &test, 20); 

    _displayBall(&test, 0.0f, 0.0f, 0.5f, -2.0f, &*smallBallColor); 

    glBindTexture(GL_TEXTURE_2D, texture); 
    glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, textureWidth, textureHeight, 0); 

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 

    glViewport(viewport[0], viewport[1], viewport[2], viewport[3]); 
    glMatrixMode(GL_PROJECTION);  
    glLoadIdentity();    
    gluPerspective(45.0f, (GLfloat)viewport[2]/(GLfloat)viewport[3], 1.0f, imageD + 10.0f); 
    glMatrixMode(GL_MODELVIEW);  
    glLoadIdentity(); 
} 

GLuint DrawBall::genEmptyTexture(unsigned int width, unsigned int height) { 
    GLuint txtIndex; 

    glGenTextures(1, &txtIndex);          
    glBindTexture(GL_TEXTURE_2D, txtIndex);       

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, 
    GL_RGB, GL_UNSIGNED_BYTE, NULL);        

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 

    return txtIndex; 
} 

void DrawBall::_displayBall(vector<vector<GLfloat>> *vertexes, GLfloat x, GLfloat y 
         , GLfloat imageW, GLfloat imageD, color *color) { 
    glTranslatef(x, y, imageD); 

    glClearStencil(0); 
    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 
    glEnable(GL_STENCIL_TEST); 
    glStencilFunc(GL_NEVER, 0, 1); 
    glStencilOp(GL_INVERT, GL_INVERT, GL_INVERT); 

    glBegin(GL_POLYGON); 
     vector<vector<GLfloat>>::iterator it = vertexes->begin(); 
     for (; it != vertexes->end(); it++) { 
      glVertex3f((*it)[0], (*it)[1], 0.0f); 
     } 
    glEnd(); 

    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 
    glStencilFunc(GL_EQUAL, 1, 1); 
    glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO); 

    glColor3f(color->r, color->g, color->b); 
    glBegin(GL_QUADS); 
     glVertex3f(-(imageW/2.0f), -(imageW/2.0f), 0.0f); 
     glVertex3f((imageW/2.0f), -(imageW/2.0f), 0.0f); 
     glVertex3f((imageW/2.0f), (imageW/2.0f), 0.0f); 
     glVertex3f(-(imageW/2.0f), (imageW/2.0f), 0.0f); 
    glEnd(); 

    glDisable(GL_STENCIL_TEST); 

    glTranslatef(x, y, -imageD); 
} 

答えて

0

まあ、Datenwolf、あなたの提案をありがとう、あなたはおそらく正しいですが、私はちょうど先進的なものをできるだけ少なくしたいと思って、私は間違いを見つけました。ステンシルテストをまだ有効にしていなかったので、2番目のフレームの前に希望の結果が得られませんでした。最初のフレームの前には、ウィンドウの作成でWindowsがWM_SIZEメッセージを送信し、その中に描画メッセージがありましたが、その時点でOpenGLが正しく設定されていないため、目的の結果が得られませんでした。

2

レンダリングからテクスチャ操作にウィンドウフレームバッファ(バックバッファとフロントバッファの両方を含む)を使用しないでください。それは簡単に(あなたはそれを経験した)に壊れます。フレームバッファオブジェクトと呼ばれる、いわゆるをレンダリング対象として使用してください。

関連する問題