Re: [PyOpenGL-Users] Lighting and texturing
Brought to you by:
mcfletch
From: Richard J. <ric...@op...> - 2001-09-23 01:28:34
|
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 |