2017-07-12 107 views
-2

我想繪製紋理四合一,但紋理正在改變每一幀。我的紋理是128x128 rgb數組。我將每個像素的rgb值存儲在該數組中,並且我正在每幀更改該數組。我的窗口大小也是1024x1024。我想要我的像素陣列全屏,所以我正在創建一個紋理,並將此紋理添加到全尺寸的四邊形。我怎樣才能做到這一點?Opengl渲染動態紋理四合一

回答

1
GLuint texID; 

void initphase() 
{ 
    /* create texture object */ 
    glGenTextures(1, &texID) 
    /* bind texture and allocate storage */ 
    glBindTexture(GL_TEXTURE_2D, texID); 
    glTexImage2D(GL_TEXTURE_2D, 
     …, 
     NULL /* just initialize */ 
    ); 
    /* alternative: 
    * Use glTexStorage instead of glTexImage. 
    * Requires a few changed in how texture is used though */ 

    /* set parameters like filtering mode, and such */ 
    glTexParameteri(…); 
} 

void player() 
{ 
    while(playing){ 
     glClear(…); 
     glViewport(…); 

     /* draw other stuff */ 

     glBindTexture(GL_TEXTURE_2D, texID); 
     /* copy image to texture */ 
     glTexSubImage2D(GL_TEXTURE_2D, 0, …, image_data); 
     if(using_shaders){ 
      glUseProgram(…); 
      setup_modelview_and_projection_uniforms(); 
     } else { 
      glEnable(GL_TEXTURE_2D); 
      setup_modelview_and_projection_matrices(); 
     } 
     glDraw…(…); /* draw quad */ 

     /* draw other stuff */ 
     swap_buffers(); 
    } 
}