Thread: [PyOpenGL-Users] Texture colour wrapping
Brought to you by:
mcfletch
From: Brice T. <bps...@gm...> - 2011-11-04 14:24:57
|
Good Afternoon, I am new to OpenGL and I was wondering if somebody could tell me if what I am intending to do is feasible with PyOpenGL. Here is my problem, I have data contained in a 2D numpy array of type float. The values in the array are phase data in polar coordinates. From the array, I am creating a texture which I mapped using quad strips in order to get the polar to cartesian conversion. Once this mapping is done I would like to "wrap" the phase/colour value. For example, I could have phase value ranging from 0.0 to 6.2 and I want to ignore the integer par of the value so value(1.3) = value(2.3) =value(3.3) etc... I can not do the "colour wrapping" before the mapping (I've tried). The results is not right an limited by the resolution of the texture. I was wondering if it would be possible to do such a wrapping, I have been thinking maybe a lookup table or a shader could be used... This is where I would appreciate getting some advices. Thanks in advance. Regards Brice |
From: Ian M. <geo...@gm...> - 2011-11-04 16:51:47
|
What do you mean by wrapping? Is this like wrapping geometry? Or implicitly wrapping in texture space? Or doing filtering across texel boundaries? Can you describe basically what you're trying to do? Ian |
From: Brice T. <bps...@gm...> - 2011-11-05 12:14:32
|
Good evening, Thank you for your reply.I do not mean wrapping geometry. Basically I have some phase data expressed in radians, which could be anything between -15ð and +15ð. I want to display this phase wrapped with min and max value between 2ð. I am getting the phase map as 2D numpy array in polar coordinates. And i need to display this phase map not as a square but as a disk, which i can do with texture mapping. The phase map has type float and i want the grayscale to be ranging between 0 and 2ð and not between -15ð and 15ð. I have tried to apply the grayscale "wrapping" before the mapping but it does not lead to satisfactory result, I believe applying it after the polar to cartesian is better. It is how it is currently done with the CPU but I am interested to implement it on GPU. I hope it is a bit clearer. Please do not hesitate to ask me to clarify things. Best On 4 November 2011 16:51, Ian Mallett <geo...@gm...> wrote: > What do you mean by wrapping? Is this like wrapping geometry? Or > implicitly wrapping in texture space? Or doing filtering across texel > boundaries? > > Can you describe basically what you're trying to do? > > Ian > |
From: Henry G. <he...@ca...> - 2011-11-05 18:23:41
|
On Sat, 2011-11-05 at 12:14 +0000, Brice Thurin wrote: > I am getting the phase map as 2D numpy array in polar coordinates. And > i need to display this phase map not as a square but as a disk, which > i can do with texture mapping. The phase map has type float and i want > the grayscale to be ranging between 0 and 2ð and not between -15ð and > 15ð. I have tried to apply the grayscale "wrapping" before the mapping > but it does not lead to satisfactory result, I believe applying it > after the polar to cartesian is better. It is how it is currently done > with the CPU but I am interested to implement it on GPU. It should be fairly trivial to write a shader to do this, either by computing it directly or using an indirect texture lookup, since your phases are well defined to be in the range -15pi to 15pi (that symbol was meant to be pi right?). Someone more competent that I am could surely tell you which would be the better option to use in this case (and I'd be interested to know the answer!). Cheers, Henry |
From: Ian M. <geo...@gm...> - 2011-11-05 18:28:31
|
So, the range of the input data is -15pi to 15pi, and you want to map it onto a disk, such that the input data maps to the 0-2pi (i.e., phase data for 2pi and 4pi both map to the same physical location?) Ian |
From: Brice T. <bps...@gm...> - 2011-11-05 18:46:56
|
Thanks for both your reply. The range of the input data vary but I could fix it if it makes things easier. Note I do not want 2pi and 4pi to both map to the same physical location, I want 2pi and 4pi to be displayed with the same gray level or colour. Brice On 5 November 2011 18:28, Ian Mallett <geo...@gm...> wrote: > So, the range of the input data is -15pi to 15pi, and you want to map it > onto a disk, such that the input data maps to the 0-2pi (i.e., phase data > for 2pi and 4pi both map to the same physical location?) > > Ian > > > ------------------------------------------------------------------------------ > RSA(R) Conference 2012 > Save $700 by Nov 18 > Register now > http://p.sf.net/sfu/rsa-sfdev2dev1 > _______________________________________________ > PyOpenGL Homepage > http://pyopengl.sourceforge.net > _______________________________________________ > PyOpenGL-Users mailing list > PyO...@li... > https://lists.sourceforge.net/lists/listinfo/pyopengl-users > > |
From: Dirk R. <dir...@gm...> - 2011-11-05 19:02:22
|
Hi Brice, On 11/05/2011 01:46 PM, Brice Thurin wrote: > Thanks for both your reply. The range of the input data vary but I could fix it > if it makes things easier. > > Note I do not want 2pi and 4pi to both map to the same physical location, I want > 2pi and 4pi to be displayed with the same gray level or colour. Ah, ok, I was starting to get confused. You really do want the basic input value -> color mapping. That's pretty easy. Define the color map that you want to use as a texture (1D is fine, 2D works too). Set the texture wrapping mode to REPEAT (glTexParameter(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT)). This makes OpenGL accept any texture coordinate value and just use the fractional part to index into the texture. Use your input values (your -15pi to 15pi) as the texture coordinate for each vertex. The texture coordinates will always be between 0 and 1, so if you want the texture to repeat after 2pi you will either need to divide them by 2pi before giving them to OpenGL, or use a texture matrix to divide. Hope it helps Dirk |
From: Brice T. <bps...@gm...> - 2011-11-05 20:37:53
|
Hi Dirk, Thanks for your help. So if I understand properly, I first do the geometrical mapping to do the polar to cartesian transformation. Then I do the color mapping? That's sound pretty neat to me. Thanks Brice On 5 November 2011 19:02, Dirk Reiners <dir...@gm...> wrote: > > Hi Brice, > > On 11/05/2011 01:46 PM, Brice Thurin wrote: > > Thanks for both your reply. The range of the input data vary but I could > fix it > > if it makes things easier. > > > > Note I do not want 2pi and 4pi to both map to the same physical > location, I want > > 2pi and 4pi to be displayed with the same gray level or colour. > > Ah, ok, I was starting to get confused. You really do want the basic input > value > -> color mapping. > > That's pretty easy. Define the color map that you want to use as a texture > (1D > is fine, 2D works too). Set the texture wrapping mode to REPEAT > (glTexParameter(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT)). This makes > OpenGL > accept any texture coordinate value and just use the fractional part to > index > into the texture. Use your input values (your -15pi to 15pi) as the > texture > coordinate for each vertex. The texture coordinates will always be between > 0 and > 1, so if you want the texture to repeat after 2pi you will either need to > divide > them by 2pi before giving them to OpenGL, or use a texture matrix to > divide. > > Hope it helps > > Dirk > > > > ------------------------------------------------------------------------------ > RSA(R) Conference 2012 > Save $700 by Nov 18 > Register now > http://p.sf.net/sfu/rsa-sfdev2dev1 > _______________________________________________ > PyOpenGL Homepage > http://pyopengl.sourceforge.net > _______________________________________________ > PyOpenGL-Users mailing list > PyO...@li... > https://lists.sourceforge.net/lists/listinfo/pyopengl-users > |
From: Dirk R. <dir...@gm...> - 2011-11-06 05:23:22
|
Hi Brice, On 11/05/2011 03:37 PM, Brice Thurin wrote: > Hi Dirk, > > Thanks for your help. So if I understand properly, I first do the geometrical > mapping to do the polar to cartesian transformation. Then I do the color mapping? Yes and no. The actual mapping to color is done by the texture, you just need to feed your data into it in a way that puts the wraparound point at 0 and 1. I appended a simple example. > That's sound pretty neat to me. It is! :) Its really cool, as you can mess with texture to show specific important values or ranges, make it smooth or stepped (like in my example) and many other cool tricks. Hope it helps Dirk #!/usr/bin/python2.4 # # Use texture for visualizating a data value. # # Based on texturedQuad.py by "Peter Roesch" <Pet...@fh...> # # This code is licensed under the PyOpenGL License. # Details are given in the file license.txt included in this distribution. import sys import array import math try: from OpenGL.GLUT import * from OpenGL.GL import * from OpenGL.GLU import * except: print ''' Error PyOpenGL not installed properly !!''' sys.exit( ) def display( ): """Glut display function.""" glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) glColor3f( 1, 1, 1 ) # Draw a regular grid gridres=40 for y in [y * 1. / gridres - 0.5 for y in range(0, gridres)]: glBegin(GL_QUAD_STRIP) for x in [x * 1. / gridres - 0.5 for x in range(0, gridres + 1)]: # This is where the data value goes... glTexCoord1f( math.sin(x*x + y*y) * 5 ) glVertex3f( x, y, 0 ) y1 = y + 1. / gridres glTexCoord1f( math.sin(x*x + y1*y1) * 5 ) glVertex3f( x, y1, 0 ) glEnd( ) glutSwapBuffers ( ) def init( ): """Glut init function.""" glClearColor ( 0, 0, 0, 0 ) glShadeModel( GL_SMOOTH ) # Build texture. Just a trivial grey step one for now # For a real app you would create a nice lookup table nsteps = 8 ns = 256/nsteps tmpList = [ int(i/ns)*ns for i in range(0, 256) ] texdata = array.array( 'B', tmpList ).tostring( ) texwidth = 256 glTexParameterf( GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT ) glTexParameterf( GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ) glTexParameterf( GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ) glTexImage1D( GL_TEXTURE_1D, 0, 3, texwidth, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, texdata) glEnable( GL_TEXTURE_1D ) glutInit( sys.argv ) glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ) glutInitWindowSize( 250, 250 ) glutInitWindowPosition( 100, 100 ) glutCreateWindow( sys.argv[0] ) init( ) glutDisplayFunc( display ) glutMainLoop( ) |