[英]OpenGL: Generating a 2D texture from a data array to display on a quad
我編寫了一個在2D網格上執行時域有限差分計算的程序。 我正在尋找一個場分量(例如,磁場的z分量,Hz)的幅度數據,將幅度轉換為色標,然后在屏幕上顯示最終的顏色圖。 imgur鏈接中的以下圖片是通過matlab生成的,但是我想在OpenGL中完成相同類型的圖片。
我正在使用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字段的顏色數據通過以下方式存儲:
據我了解,在窗口中顯示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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.