OpenGL (Python) - applying an STMap (UVmap) to an image? -


i have following code loads , displays (using pyqt) image on screen:

gvshader = """               attribute vec4 position;               attribute vec2 texture_coordinates;               varying vec2 v_texture_coordinates;               void main() {                   v_texture_coordinates = texture_coordinates;                   v_texture_coordinates.y = 1.0 - v_texture_coordinates.y;                   gl_position = position;               }"""  gfshader = """               uniform sampler2d texture1;               uniform sampler2d texture2;               varying vec2 v_texture_coordinates;                void main() {                   gl_fragcolor = texture2d(texture1, texture2.rg);               }"""   class projectiveglviewer(qtopengl.qglwidget):      def __init__(self, parent=none):         super(projectiveglviewer, self).__init__(parent)      def initializegl(self):          vshader = qtopengl.qglshader(qtopengl.qglshader.vertex, self)         if not vshader.compilesourcecode(gvshader):             print vshader.log()          fshader = qtopengl.qglshader(qtopengl.qglshader.fragment, self)         if not fshader.compilesourcecode(gfshader):             print fshader.log()          self._program = qtopengl.qglshaderprogram()         self._program.addshader(vshader)         self._program.addshader(fshader)         self._program.link()         self._program.bind()          # data array (2 [position], 4 [color])          data = np.array([-1.0, -1.0, 0.0, 0.0, 1.0, -1.0, 1.0, 0.0, -1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=np.float32)          self.bufferid = glgenbuffers(1)         glbindbuffer(gl_array_buffer, self.bufferid)         glbufferdata(gl_array_buffer, data.nbytes, data, gl_dynamic_draw)          loc = self._program.attributelocation("position")         glenablevertexattribarray(loc)         glvertexattribpointer(loc, 2, gl_float, false, 16, ctypes.c_void_p(0))          loc = self._program.attributelocation("texture_coordinates")         glenablevertexattribarray(loc)         glvertexattribpointer(loc, 2, gl_float, false, 16, ctypes.c_void_p(8))          image = qtgui.qimage("image.jpg")         ptr = image.bits()         ptr.setsize(image.bytecount())         image_data = np.asarray(ptr).reshape(image.width(), image.height(), 4)          self._imagetextureid = glgentextures(1)          glbindtexture(gl_texture_2d,  self._imagetextureid)         glteximage2d(gl_texture_2d, 0, gl_rgba, image.width(), image.height(), 0, gl_bgra, gl_unsigned_byte, image_data)          gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_nearest)         gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_nearest)         gltexparameteri(gl_texture_2d, gl_texture_wrap_s, gl_repeat)         gltexparameteri(gl_texture_2d, gl_texture_wrap_t, gl_repeat)          glactivetexture(gl_texture0)         self._program.setuniformvalue('texture1', 0)       image2 = qtgui.qimage("st_map.tif")     ptr2 = image2.bits()     ptr2.setsize(image2.bytecount())     image_data2 = np.asarray(ptr).reshape(image2.width(), image2.height(), 4)      self._sttextureid = glgentextures(1)      glbindtexture(gl_texture_2d,  self._sttextureid)     glteximage2d(gl_texture_2d, 0, gl_rgba, image2.width(), image2.height(), 0, gl_bgra, gl_unsigned_byte, image_data2)      gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_nearest)     gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_nearest)     gltexparameteri(gl_texture_2d, gl_texture_wrap_s, gl_repeat)     gltexparameteri(gl_texture_2d, gl_texture_wrap_t, gl_repeat)      self._program.setuniformvalue('texture2', 1)      def paintgl(self):         glbindbuffer(gl_array_buffer, self.bufferid)         glclearcolor(0, 0.2, 0.3, 1.0)         glcleardepth(1.0)         glclear(gl_color_buffer_bit|gl_depth_buffer_bit)         gldrawarrays(gl_triangle_strip, 0, 4)      def resizegl(self, w, h):         glviewport(0, 0, w, h) 

now, want use stmap (uvmap) remap pixels of image. of aren't familiar it, stmap looks this: link have many shapes. it's same size original image, , contains red , green channels (blue empty). red represents x axis , green represents y. example, new coordinates of pixel that's @ position [100,100] in original image, go same position in stmap , pixel's red channel value (that new x value) , green value (new y value).

so how can that? i'm not sure.. tried adding stmap texture wouldn't let me use it's red , green channel values (see fragment shader).thanks!

edit:

i've edited code tried , didn't work..

since not have resources cannot run code here.

from shader point of view, can modify texture1 coordinates using texture2 coordinates. there many ways that, example translate them adding uvmap values it. here, texture2 contains uvmap data:

 uniform sampler2d texture1;  uniform sampler2d texture2;  varying vec2 v_texture_coordinates;   vec2 my_new_coords;  vec4 uvmap;  void main() {      // uv map data @ current position      uvmap = texture2d(texture2, v_texture_coordinates);       // map current position using uv map data      my_new_coords.x = v_texture_coordinates.x + uvmap.r;      my_new_coords.y = v_texture_coordinates.y + uvmap.g;       // sample texture 2 new mapped coordinates      gl_fragcolor = texture2d(texture1, my_new_coords);  } 

or if want use coordinates uvmap can do

 gl_fragcolor = texture2d(texture1, uvmap); 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -