0

I have written a program which performs finite-difference time-domain calculations on a 2D grid. I am looking to take the magnitude data of one of the field components (let's say the z-component of the magnetic field, Hz), convert the magnitudes into a color scale, then display the final color map on the screen. The following picture in the imgur link was generated via matlab, but I would like to accomplish the same type of image in OpenGL.

https://i.sstatic.net/G9tXf.jpg

I am using GLFW to create the OpenGL window (800x480) with an orthographic projection using the following code:

//-->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<--//

The color data for the Hz field is stored by:

  • Creating a 1D int pointer of length 800x480*(3 colors)
  • Calculate and store the color values of the Hz field in the 1D pointer

From what I have read, the typical way to display a 2D texture in a window is to create a quad and apply the texture to the quad. I can successfully create the quad, and have successfully generated the color data (verified with matlab), but I am having trouble creating the texture to apply to the quad.

My code to generate the texture is:

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);
}

The problem which occurs is that the quad remains black.

***I know copying data back into host memory to then create a GL texture is not optimal in terms of performance, but at this point all that matters is implementing the field display on the shortest timescale possible, not achieving optimal performance (yet).

6
  • Nothing displays on the quad, it just remains black. Commented Jan 12, 2014 at 1:20
  • Well, you don't seem to be making any noob mistakes. Have you tried populating tex_Hz with random data and drawing that? Then you could at least know whether it is a data issue or an openGL issue. Commented Jan 12, 2014 at 1:26
  • Wait. If you haven't got anything to draw, I think you have your glOrtho(...) z values wrong. The last two parameters should be -1,1, not 1,-1. Commented Jan 12, 2014 at 1:28
  • Fixed it. When using the GL_INT flag in glTexImage2D(...) the color scale ranges over all values of a signed int , and I was using a color scale from 0-255 for each channel, which is essentially 0 using GL_INT. Thank you for taking the time to look the code over. I've been driving myself crazy over this. Commented Jan 12, 2014 at 2:38
  • I'm glad you got it fixed. I was actually eyeing that GL_INT flag, but I've never used it. I've always done GL_UNSIGNED_BYTE or whatever. Commented Jan 12, 2014 at 18:09

1 Answer 1

3

Fixed it. When using the GL_INT flag in glTexImage2D(...) the color scale ranges over all values of a signed int , and I was using a color scale from 0-255 for each channel, which is essentially 0 using GL_INT.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.