Re: [PyOpenGL-Users] Texture colour wrapping
Brought to you by:
mcfletch
|
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( )
|