Thread: [PyOpenGL-Users] Lighting and texturing
Brought to you by:
mcfletch
|
From: Richard J. <ric...@op...> - 2001-09-23 00:11:02
|
I can't seem to figure how to have a surface lit and textured. The demo that
comes with pyopengl (NeHe/lesson18.py) doesn't work, and my code looks very
similar to it...
Ideas?
Richard
|
|
From: Richard J. <ric...@op...> - 2001-09-23 01:28:34
Attachments:
point_64.png
|
On Sat, 22 Sep 2001 20:15, Richard Jones wrote:
> I can't seem to figure how to have a surface lit and textured. The demo
> that comes with pyopengl (NeHe/lesson18.py) doesn't work, and my code looks
> very similar to it...
>
> Ideas?
I'm also having trouble getting things to be transparent, and I get the funny
feeling they're related problems...
OK, this time I'll actually send some code :)
First up, the code that initialises things:
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
glutInitWindowSize(self.width, self.height)
glutCreateWindow("GLE demo")
# display all the time
glutDisplayFunc(self.on_display)
glutIdleFunc(self.on_display)
# go with both normal and passive (mouse button down and not)
glutPassiveMotionFunc(self.on_motion)
glutMotionFunc(self.on_motion)
# and detect mouse clicks
glutMouseFunc(self.on_mouse)
# and key clicks
glutKeyboardFunc(self.on_key)
glutReshapeFunc(self.on_reshape)
if '-fs' in args:
glutFullScreen()
# set up the display specifics
glClearDepth(1.0)
glEnable(GL_DEPTH_TEST)
glClearColor(0.0, 0.0, 0.0, 0.0)
glShadeModel(GL_FLAT)
glMatrixMode(GL_MODELVIEW)
# initialize lighting
glLightfv(GL_LIGHT0, GL_POSITION, (-40.0, 40, -100.0, 0.0))
glLightfv(GL_LIGHT0, GL_DIFFUSE, (0.99, 0.99, 0.99, 1.0))
glEnable(GL_LIGHT0)
glLightfv(GL_LIGHT1, GL_POSITION, (-100.0, 40, -40.0, 0.0))
glLightfv(GL_LIGHT1, GL_DIFFUSE, (0.99, 0.99, 0.99, 1.0))
glEnable(GL_LIGHT1)
glEnable(GL_LIGHTING)
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE)
glEnable(GL_COLOR_MATERIAL)
Now, the code that is supposed to light and texture map surfaces:
self.texture = Image.open('texmap_256.png')
ix, iy = self.texture.size
image = self.texture.tostring("raw", "RGB", 0, -1)
self.maptex = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, self.maptex)
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGB,
GL_UNSIGNED_BYTE, image)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
print "... done"
... later ...
glPushMatrix()
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, self.maptex)
glEnable(GL_LIGHTING)
glShadeModel(GL_FLAT)
[ "i" loop ]
[ "j" loop ]
glBegin(GL_TRIANGLES)
norm1, norm2, p1, p2, p3, p4 = z_row[j]
glNormal(*norm1)
glTexCoord2f(i*4./256., j*4./256.)
glVertex(*p1)
glTexCoord2f(i*4./256., (j*4.+4)/256.)
glVertex(*p2)
glTexCoord2f((i*4.+4)/256., (j*4.+4)/256.)
glVertex(*p3)
glNormal(*norm2)
glTexCoord2f(i*4./256., j*4./256.)
glVertex(*p1)
glTexCoord2f((i*4.+4)/256., (j*4.+4)/256.)
glVertex(*p3)
glTexCoord2f((i*4.+4)/256., j*4./256.)
glVertex(*p4)
glEnd()
glDisable(GL_TEXTURE_2D)
Next up, the code that is supposed to use a texture with an alpha channel:
self.texture = Image.open('point_64.png')
ix, iy = self.texture.size
image = self.texture.tostring("raw", "RGBA", 0, -1)
self.pointtex = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, self.pointtex)
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA,
GL_UNSIGNED_BYTE, image)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
... later ...
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, self.pointtex)
glDisable(GL_LIGHTING)
glBegin(GL_QUADS)
[ loop ]
glTexCoord2f(0, 0)
glVertex3f(x, y, z)
glTexCoord2f(1, 0)
glVertex3f(x+2, y, z)
glTexCoord2f(1, 1)
glVertex3f(x+2, y+2, z)
glTexCoord2f(0, 1)
glVertex3f(x, y+2, z)
glEnd()
glEnable(GL_LIGHTING)
glDisable(GL_TEXTURE_2D)
I've also attached the point_64.png image, generated in the GIMP. I'm
_pretty_ sure it's transparent - unlike Photoshop, the GIMP doesn't show me
the alpha channel explicitly (unless I'm doing something wrong, which is
possible :)
I can post the complete code, but it's supposed to be a surprise :)
Richard
|
|
From: Mike C. F. <mcf...@ho...> - 2001-09-23 09:36:47
|
21.030 Why doesn't lighting work when I turn on texture mapping?
There are many well-meaning texture map demos available on the Web that set
the texture environment to GL_DECAL or GL_REPLACE. These environment modes
effectively replace the primitive color with the texture color. Because
lighting values are calculated before texture mapping (lighting is a per
vertex operation, while texture mapping is a per fragment operation), the
texture color replaces the colors calculated by lighting. The result is that
lighting appears to stop working when texture mapping is enabled.
The default texture environment is GL_MODULATE, which multiplies the texture
color by the primitive (or lighting) color. Most applications that use both
OpenGL lighting and texture mapping use the GL_MODULATE texture environment.
Look for the following line in your code:
glTexEnv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); /* or GL_REPLACE
*/
You should change GL_DECAL to GL_MODULATE, or simply delete the line
entirely (since GL_MODULATE is the default).
Might that be the problem in your code? I'll take a look at your code
tomorrow if I can get some time and see if I can see anything. Also need to
make sure none of my code is well meaning :o) , pretty sure I have a few of
those hanging around...
Mike
-----Original Message-----
From: pyo...@li...
[mailto:pyo...@li...]On Behalf Of Richard
Jones
Sent: September 22, 2001 07:33
To: pyo...@li...
Subject: Re: [PyOpenGL-Users] Lighting and texturing
On Sat, 22 Sep 2001 20:15, Richard Jones wrote:
> I can't seem to figure how to have a surface lit and textured. The demo
> that comes with pyopengl (NeHe/lesson18.py) doesn't work, and my code
looks
> very similar to it...
>
> Ideas?
...
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
...
|
|
From: Richard J. <ric...@op...> - 2001-09-23 12:54:11
|
Thanks Mike, the problem was in fact a GL_DECAL use. The lesson18 source in
the NeHe demo directory needs to be fixed to remove it. Works a treat then.
The other problem I had - the transparency not working - is still a problem.
For that code, on Pete's suggestion, I've used GL_BLEND, but the resultant
image turns blue! I suspect my image's alpha channel is not up to scratch.
This is the image I attached to my last message. Any suggestions for paint
packages that produce reliable alpha channels? The GIMP seems to (in editing)
but I just loaded my PNG into another package and that said the image is 24
bit. That tells me that either the GIMP is writing 6 bits per channel (which
as far as I can tell, it's not) or it's not writing the alpha channel. And
yet when I load the image back into the GIMP, it's got transparency...
Mike - that info you posted looked like an FAQ entry - if it is, what's the
URL please?
Richard
|
|
From: Richard J. <ric...@op...> - 2001-09-23 13:17:03
|
On Sun, 23 Sep 2001 08:58, Richard Jones wrote:
> The other problem I had - the transparency not working - is still a
> problem. For that code, on Pete's suggestion, I've used GL_BLEND, but the
> resultant image turns blue! I suspect my image's alpha channel is not up to
> scratch. This is the image I attached to my last message. Any suggestions
> for paint packages that produce reliable alpha channels? The GIMP seems to
> (in editing) but I just loaded my PNG into another package and that said
> the image is 24 bit. That tells me that either the GIMP is writing 6 bits
> per channel (which as far as I can tell, it's not) or it's not writing the
> alpha channel. And yet when I load the image back into the GIMP, it's got
> transparency...
Actually, the image is fine... I just got the alpha stuff going - the
GL_BLEND isn't the TexEnv, it's just something that needs glEnable'ing. Fixed!
Thanks for the help guys! :)
Richard
|
|
From: Mike C. F. <mcf...@ho...> - 2001-09-23 16:08:22
|
Just a note, beyond just enabling blending, you normally want to sort the triangles from farthest to nearest for rendering. The OpenGLContext/scenegraph/arraygeometry classes (and some supporting modules) do that if you need sample code. It's not fancy (no resolution of equi-distant interfering geometry, for instance, no tessellation where triangles intersect for another), but it can avoid having some of the artefacts. Enjoy, Mike -----Original Message----- From: pyo...@li... [mailto:pyo...@li...]On Behalf Of Richard Jones Sent: September 22, 2001 19:21 To: pyo...@li... Subject: Re: [PyOpenGL-Users] Lighting and texturing ... Actually, the image is fine... I just got the alpha stuff going - the GL_BLEND isn't the TexEnv, it's just something that needs glEnable'ing. Fixed! ... |
|
From: Richard J. <ric...@op...> - 2001-09-24 10:23:01
|
On Mon, 24 Sep 2001 02:10, Mike C. Fletcher wrote:
> Just a note, beyond just enabling blending, you normally want to sort the
> triangles from farthest to nearest for rendering.
Yep, noticed that pretty quickly. Fortunately, there's some characteristics
of the stuff I'm drawing that I can exploit so that I can draw in the right
order.
Richard
|
|
From: Mike C. F. <mcf...@ho...> - 2001-09-23 15:26:05
|
http://www.frii.com/~martz/oglfaq/ I found it by doing a search for OpenGL lighting texturing on Google. HTH, Mike -----Original Message----- From: pyo...@li... [mailto:pyo...@li...]On Behalf Of Richard Jones Sent: September 22, 2001 18:59 To: pyo...@li... Subject: Re: [PyOpenGL-Users] Lighting and texturing ... Mike - that info you posted looked like an FAQ entry - if it is, what's the URL please? ... |