OpenGL:从数据阵列生成2D纹理以显示在四边形上



我编写了一个程序,在2D网格上执行时域有限差分计算。我想获取其中一个场分量的幅度数据(比如磁场的z分量,Hz),将幅度转换为色标,然后在屏幕上显示最终的彩色图。imgur链接中的下图是通过matlab生成的,但我想在OpenGL中完成相同类型的图像。

https://i.stack.imgur.com/G9tXf.jpg

我正在使用GLFW创建具有正交投影的OpenGL窗口(800x480),使用以下代码:

//-->Construct GLFW Window<--//
int WINDOW_WIDTH = 800*(xIndex/4000);
int WINDOW_HEIGHT = 480*(yIndex/2400);
GLFWwindow* window;
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Hz Component Magnitude", NULL, NULL);
//GLFW terminate and error function setup not shown
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
glewInit();
glfwGetFramebufferSize(window, &WINDOW_WIDTH, &WINDOW_HEIGHT);
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Create orthographic view
glMatrixMode(GL_PROJECTION);
glOrtho(0, 0, -1.f, 1.f, 1.f, -1.f);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glLoadIdentity();
glfwSwapBuffers(window);
glfwPollEvents();
//-->END Construct GLFW Window<--//

Hz字段的颜色数据通过以下方式存储:

  • 创建长度为800x480*(3种颜色)的1D int指针
  • 计算并存储1D指针中Hz字段的颜色值

根据我所读到的内容,在窗口中显示2D纹理的典型方法是创建一个四边形并将纹理应用于四边形。我可以成功地创建四边形,并成功地生成了颜色数据(用matlab验证),但我在创建应用于四边形的纹理时遇到了问题。

我生成纹理的代码是:

int num_colors = 3;    //Number of colors per pixel
int tex_size = num_colors*WINDOW_WIDTH*WINDOW_HEIGHT;    //Number of entries in tex_Hz
int size = tex_size*sizeof(int);
//Create pointer for storing color map
int* tex_Hz = (int*)malloc(size);
for(int i = 0; i < tex_size; i++)    //Initialize colormap to 0
tex_Hz[i] = 0;
//OpenCL buffer for the color map
cl::Buffer texture_mem(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, size, tex_Hz, &error);
//Generate a texture ID
GLuint tex_id;
glGenTextures(1, &tex_id);
glBindTexture(GL_TEXTURE_2D, tex_id);
//*A bunch of irrelevant code*//
//-->Start of time loop<--//
//*A bunch of irrelevant code*//
//-->Update the Hz field<--//
time_loop_queue.enqueueNDRangeKernel(timeLoopKernels[1], 0, global_size, local_size);
time_loop_queue.finish();
//-->Generate 2D image every 100 time steps<--//
if(step%100 == 0)
{
//Read colormap values from OpenCL buffer
time_loop_queue.enqueueReadBuffer(texture_mem, NULL, 0, size, tex_Hz, NULL, NULL);
time_loop_queue.finish();
//Used only as a debug runtime check to make sure data copied back
cout << tex_Hz[3*800*240 + 3*350] << "n";
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, WINDOW_WIDTH, WINDOW_HEIGHT, 0, GL_RGB, GL_INT, tex_Hz);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, tex_id);
//Draw quad and set texture coords
glBegin(GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
glTexCoord2d(1.0,0.0); glVertex2d(1.0,-1.0);
glTexCoord2d(1.0,1.0); glVertex2d(1.0,1.0);
glTexCoord2d(0.0,1.0); glVertex2d(-1.0,1.0);
glEnd();
glfwSwapBuffers(window);
}

出现的问题是四边形保持黑色。

***我知道将数据复制回主机内存以创建GL纹理在性能方面不是最佳的,但在这一点上,重要的是在尽可能短的时间内实现现场显示,而不是实现最佳性能。

修复了这个问题。当在glTexImage2D(…)中使用GL_INT标志时,色阶的范围在有符号INT的所有值上,我对每个通道使用0-255的色阶,使用GL_INT基本上是0。

最新更新