2012-02-14 7 views
1

미로 타입의 게임을 더 빠르게하려면 모든 드로잉을 한 방에 한 번만해야하고 오목한 폴리곤처럼 그려야하기 때문에 드로잉 된 공을 텍스처 안에 넣기로했습니다. 스텐실 버퍼를 사용하면 텍스처를 사용하는 것보다 더 많은 시간이 걸립니다. 문제는 게임의 시작 이후 세 번째 프레임을 렌더링 할 때 백 버퍼의 텍스처 내부에 제대로 들어갈 수 있다는 것이고 내 질문은 왜 그렇습니까?버퍼 내용을 OpenGL의 텍스처로 백

갈증 프레임에서 텍스처를 사용할 때, 나는 단색의 흰색 텍스처가있어서 내부가 없습니다. 두 번째 프레임의 텍스처를 사용할 때 원하는 텍스처의 검정색 배경 만 있고 세 번째 프레임의 텍스처를 가져올 때 원하는 텍스처가 있습니다. 프레임 수의 경우 "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, 당신의 제안에 감사드립니다, 당신은 아마 맞을지 모르지만 나는 가능한 한 적은 고급 재료를 사용하고 싶고 실수를 찾았습니다. 스텐실 테스트를 아직 사용하지 않았기 때문에 두 번째 프레임 전에 원하는 결과를 얻지 못했습니다. 첫 번째 프레임 이전에는 윈도우 생성 창에서 WM_SIZE 메시지를 보내고 그 안에 메시지를 그려 넣었으므로 그 결과를 얻지 못했지만 OpenGL은 아직 제대로 설정되지 않았기 때문에 원하는 결과를 얻지 못했습니다.

2

텍스처와 렌더링 작업에 window framebuffer (backbuffer와 frontbuffer를 모두 포함)를 사용하지 마십시오. 그냥 쉽게 깨져 버렸습니다 (당신은 그것을 경험했습니다). 대신 텍스처를 렌더링 대상으로 사용하여 프레임 버퍼 객체을 사용하십시오.

관련 문제