Thread: [PyOpenGL-Users] Coloring Nurb Surface
Brought to you by:
mcfletch
From: Michka P. <mic...@gm...> - 2011-02-23 14:34:32
|
Hello I have managed to display a nurb surface. Now I want it to be colored in function of the height (with a color gradient). The documentation doesn't help me a lot, and the tutorials found on the internet are mostly to old or not adapted to my case. (Since most of them are written in C, not adapted to pyOpenGL or not using Nurbs) So here is my code which displays the surface (the controlpoints are random, only here to test if it works) : mat_diffuse = ( 0.7, 0.4, 0.1, 1.0 ) mat_specular = ( 1.0, 1.0, 1.0, 1.0 ) mat_shininess = 100.0 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular) glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess) glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glEnable(GL_DEPTH_TEST) glEnable(GL_AUTO_NORMAL) glEnable(GL_NORMALIZE) nurb = gluNewNurbsRenderer() gluNurbsProperty(nurb, GLU_SAMPLING_TOLERANCE, 20.0) # Space between polygons gluNurbsProperty(nurb, GLU_DISPLAY_MODE, GLU_FILL) ctrlpts = [] grid_size = 15 for u in range(grid_size): ctrlpts.append([]) for v in range(grid_size): ctrlpts[u].append([u, v, 0.0]) ctrlpts[3][3][2] = 1.0 ctrlpts[4][4][2] = 1.0 ctrlpts[3][4][2] = 1.0 ctrlpts[4][3][2] = 1.0 ctrlpts[2][3][2] = 1.0 ctrlpts[4][2][2] = 1.0 degree=3 knot_num = len(ctrlpts) + degree knots = [ float(i)/(knot_num-1) for i in range( knot_num ) ] # Render glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse) gluBeginSurface(nurb) gluNurbsSurface(nurb, knots, knots, ctrlpts, GL_MAP2_VERTEX_3) gluEndSurface(nurb) Now, I have read about GL_MAP2_COLOR_4, but I don't know how and where to use it ? Some say I should tessellate my nurb surface, use a callback function and try to change the color of the vertexes. But How ? Perhaps somebody has a tutorial or some good hints for me ? Thank you Michka Popoff |
From: Dirk R. <dir...@gm...> - 2011-02-23 15:18:01
|
Hi Michka, On 02/23/2011 08:34 AM, Michka Popoff wrote: > > Perhaps somebody has a tutorial or some good hints for me ? generally the most generic and efficient way for that is to use glTexGen to turn your height into a texture coordinate and use a 1D texture for the colors to interpolate between. This allows you to fully specify the color that are interpolated and also enables you to do non-linear mappings (e.g. have a bright band at a specific height). Give it a try! Hope it helps Dirk |
From: Ian M. <geo...@gm...> - 2011-02-23 15:19:58
|
I'd use a vertex shader. |
From: Michka P. <mic...@gm...> - 2011-02-24 08:55:19
|
Thanks for answering Dirk : generally the most generic and efficient way for that is to use glTexGen to turn your height into a texture coordinate and use a 1D texture for the colors to interpolate between. This allows you to fully specify the color that are interpolated and also enables you to do non-linear mappings (e.g. have a bright band at a specific height). Give it a try! Ian Mallett : I'd use a vertex shader. The two ideas seem okay for me, but I think I am stuck : I have no influence on the nurb displayed, because I don't know how to modify it before it is displayed. (or after). I tried a few things : 1) I used gluNurbsProperty(nurb, GLU_NURBS_MODE, GLU_NURBS_TESSELLATOR) and gluNurbsCallback(nurb,GLU_NURBS_COLOR,color). I was able to print the colors of every point in the callback function. But I can not modify them. So why does this function exist ? And as the nurb is now tesselated, how can I display it ? 2) I used gluNurbsSurface(nurb, knots, knots, ctrlpts, GL_MAP2_COLOR_4). In the callback documentation for gluNurbsCallback() I have found : This callback is effective only when the user provides a color map (*GL**_**MAP1**_**COLOR**_**4* or *GL**_**MAP2**_**COLOR**_**4*). ``color'' contains four components: R,G,B,A So where can I provide my own color map ? Documentation never says anything about this. My ideas : 1) I could subdivide my big nurb surface in a lot of little nurb patches, and put a colored texture on it. Like the typical molehill.py tutorial you can find everywhere. I think it is quite the same as Dirk's idea ? 2) I could rewrite everything with my own polygons, where I can define the colors myself. Will not be as smooth as it is now, but should work. Michka Popoff |
From: Nicolas R. <Nic...@in...> - 2011-02-24 09:42:25
|
On Feb 24, 2011, at 9:55 AM, Michka Popoff wrote: > Thanks for answering > > Dirk : > generally the most generic and efficient way for that is to use glTexGen to turn > your height into a texture coordinate and use a 1D texture for the colors to > interpolate between. This allows you to fully specify the color that are > interpolated and also enables you to do non-linear mappings (e.g. have a bright > band at a specific height). Give it a try! > > Ian Mallett : > I'd use a vertex shader. > Have a look at glumpy that implement those two ideas (1D texture + shader): http://code.google.com/p/glumpy/ Nicolas |