X

NV5 Geospatial Blog

Each month, NV5 Geospatial posts new blog content across a variety of categories. Browse our latest posts below to learn about important geospatial information or use the search bar to find a specific topic or author. Stay informed of the latest blog posts, events, and technologies by joining our email list!



NV5 at ESA’s Living Planet Symposium 2025

NV5 at ESA’s Living Planet Symposium 2025

9/16/2025

We recently presented three cutting-edge research posters at the ESA Living Planet Symposium 2025 in Vienna, showcasing how NV5 technology and the ENVI® Ecosystem support innovation across ocean monitoring, mineral exploration, and disaster management. Explore each topic below and access the full posters to learn... Read More >

Monitor, Measure & Mitigate: Integrated Solutions for Geohazard Risk

Monitor, Measure & Mitigate: Integrated Solutions for Geohazard Risk

9/8/2025

Geohazards such as slope instability, erosion, settlement, or seepage pose ongoing risks to critical infrastructure. Roads, railways, pipelines, and utility corridors are especially vulnerable to these natural and human-influenced processes, which can evolve silently until sudden failure occurs. Traditional ground surveys provide only periodic... Read More >

Geo Sessions 2025: Geospatial Vision Beyond the Map

Geo Sessions 2025: Geospatial Vision Beyond the Map

8/5/2025

Lidar, SAR, and Spectral: Geospatial Innovation on the Horizon Last year, Geo Sessions brought together over 5,300 registrants from 159 countries, with attendees representing education, government agencies, consulting, and top geospatial companies like Esri, NOAA, Airbus, Planet, and USGS. At this year's Geo Sessions, NV5 is... Read More >

Not All Supernovae Are Created Equal: Rethinking the Universe’s Measuring Tools

Not All Supernovae Are Created Equal: Rethinking the Universe’s Measuring Tools

6/3/2025

Rethinking the Reliability of Type 1a Supernovae   How do astronomers measure the universe? It all starts with distance. From gauging the size of a galaxy to calculating how fast the universe is expanding, measuring cosmic distances is essential to understanding everything in the sky. For nearby stars, astronomers use... Read More >

Using LLMs To Research Remote Sensing Software: Helpful, but Incomplete

Using LLMs To Research Remote Sensing Software: Helpful, but Incomplete

5/26/2025

Whether you’re new to remote sensing or a seasoned expert, there is no doubt that large language models (LLMs) like OpenAI’s ChatGPT or Google’s Gemini can be incredibly useful in many aspects of research. From exploring the electromagnetic spectrum to creating object detection models using the latest deep learning... Read More >

1345678910Last
«September 2025»
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
15498 Rate this article:
1.5

OpenGL shader example

Anonym

OpenGL shader programming has been supported in IDL since version 6.4. The IDLgrShader functionality can be used to program a wide range of display manipulation functions that run on the graphics processor instead of the CPU, and frees up the CPU to work on other things. A great upside is that the graphics processor will be able to run very many threads in parallel which is great for dealing with image data where pixels can be treated independently. I will show an example here of how to perform a calculation where every output pixel depends on a neighborhood of 5x5 pixels in the input image as opposed to the simpler examples where each pixel is manipulated without influence from neighboring pixels. This makes use of a so called "sampler", which is a built in type in the shader language and allows for fast interpolation (nearest neighbor, linear) in an image.

There are some considerations not included in this example. The first is if the image is larger than the maximum texture dimensions (which can be queried), then the image is tiled internally. Using the TILE_BORDER_SIZE keyword is then necessary in order to make an example like this one work properly. The other consideration for large images is that code needs to be written to handle resolution levels and tile requests.

Note that this example requires a physical graphics card in order to run the shader code, virtual environments typically will not have access to a graphics card. In the IDL code you can query what version of the shader language (GLSL) is available at runtime and write the code to be prepared for different possibilities.

This example uses a convolution kernel in a way that allows filling in missing data (set to zero) very effectively. The hole filling is done on the fly at the time of display, so the pixel data still reflect the zero's where data is missing. There are ways to read back out the outputs from the shader code, but that is not included in this example.

Image on the left is the original input pixels with 0's for the missing data. The image on the right is the display of the same image data run though the 5x5 filter.

The following lists the complete source code that produced the images.

pro shader_tricks

  compile_opt idl2, logical_predicate
 
  im = read_image(filepath('elev_t.jpg', subdir=['examples','data']), /order)
  im = total(float(im),1)
  if min(im) eq 0 then im++
 
  help, im
 
  ; simulate random missing data, half of the pixels are missing
  im[floor(randomu(1, n_elements(im)/2)*n_elements(im))] = 0
 
  dim = size(im, /dimension)
 
  w = where(im ne 0, n)
  ord = sort(im[w])
  p0 = im[w[ord[n/50]]]
  p1 = im[w[ord[-n/50]]]
  print, p0, p1
 
  tlb = widget_base(title='Shader example', /row)
  w1 = widget_draw(tlb, xsize=dim[0], ysize=dim[1], retain=2)
  w2 = widget_draw(tlb, xsize=dim[0], ysize=dim[1], $
    graphics_level=2, retain=0, renderer=0)
 
  widget_control, tlb, /realize
  widget_control, w1, get_value=winid
  wset, winid
  tv, bytscl(im, min=p0, max=p1)
  widget_control, w2, get_value=win
 
  vertexProgram = $
    [ 'void main (void) {', $
    '  gl_TexCoord[0] = gl_MultiTexCoord0;', $
    '  gl_Position = ftransform();', $
    '  gl_ClipVertex = gl_ModelViewMatrix * gl_Vertex;', $
    '}' ]
  fragProgram = $
    ['uniform sampler2D _IDL_ImageTexture;' $
    ,'uniform float min;' $
    ,'uniform float max;' $
    ,'uniform float kernel[25];' $
    ,'uniform vec2 _IDL_ImageStep;' $
    ,'void main(void)' $
    ,'{' $
    ,'vec2 adj = vec2(2*_IDL_ImageStep.x,2*_IDL_ImageStep.y);' $
    ,'vec2 tc = gl_TexCoord[0].st - adj;' $
    ,'float sum = 0.0;' $
    ,'float nsum = 0.0;' $
    ,'for (int i=0; i<4; i++) {' $
    , 'adj.y = i * _IDL_ImageStep.y;' $
    ,' for (int j=0; j<4; j++) {' $
    ,'   adj.x = j * _IDL_ImageStep.x;' $
    ,'   float p = texture2D(_IDL_ImageTexture, tc + adj).r;' $
    ,'   float k = kernel[j+i*5];' $
    ,'   sum += p*k;' $
    ,'   nsum += (p != 0)*k;' $
    ,'  }' $
    ,'}' $
    ,'nsum += (nsum == 0);' $
    ,'sum = (sum/nsum - min) / (max - min);' $
    ,'gl_FragColor = vec4(sum,sum,sum,1.0);' $
    ,'}' $
    ]
 
 
  g = gaussian_function([0.5,0.5],width=5,maximum=1)
  shader = IDLgrShader()
  shader->SetProperty, $
    fragment_program_string=strjoin(fragProgram, string(10b)), $
    vertex_program_string=strjoin(vertexProgram, string(10b))
  shader->SetUniformVariable, 'min', float(p0)
  shader->SetUniformVariable, 'max', float(p1)
  shader->SetUniformVariable, 'kernel', float(g)
  view = IDLgrView(viewplane_rect=[0,0,dim])
  model = IDLgrModel()
  img = IDLgrImage(float(im), internal_data_type=3, shader=shader)
 
  view->Add, model
  model->Add, img
  win->Draw, view
  xmanager, 'shader_tricks', tlb, /no_block
 
end

Please login or register to post comments.