[PyOpenGL-Users] Re: textures with transparency
Brought to you by:
mcfletch
From: FvW <vw...@vu...> - 2005-09-26 08:01:03
|
Hello, I found the answer. The problem was my (mis)understanding of "blending". Attached is some working code. N.B. It is not meant to be nice code. Best, Fulko from OpenGL.GL import * from OpenGL.GLUT import * from OpenGL.GLU import * from Image import * ESCAPE = '\033' window = 0 xrot = yrot = zrot = 0.0 texture = 0 def LoadTextures(): image = open("BlueFace.png") print image.format, image.size, image.mode ix = image.size[0] iy = image.size[1] image = image.tostring("raw", "RGBA", 0, -1) glPixelStorei(GL_UNPACK_ALIGNMENT,1) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glEnable(GL_BLEND) glEnable(GL_CULL_FACE) def InitGL(Width, Height): LoadTextures() glEnable(GL_TEXTURE_2D) glClearColor(0.0, 0.0, 0.0, 0.0) glClearDepth(1.0) glDepthFunc(GL_LEQUAL) glEnable(GL_DEPTH_TEST) glShadeModel(GL_SMOOTH) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0) glMatrixMode(GL_MODELVIEW) def ReSizeGLScene(Width, Height): if Height == 0: Height = 1 glViewport(0, 0, Width, Height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0) glMatrixMode(GL_MODELVIEW) def DrawGLScene(): global xrot, yrot, zrot, texture glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() glPushMatrix() glBegin(GL_QUADS) glTexCoord2f(0.0, 0.0); glVertex3f(-5.0, -5.0, -10.0) glTexCoord2f(1.0, 0.0); glVertex3f( 5.0, -5.0, -10.0) glTexCoord2f(1.0, 1.0); glVertex3f( 5.0, 5.0, -10.0) glTexCoord2f(0.0, 1.0); glVertex3f(-5.0, 5.0, -10.0) glEnd(); glPopMatrix() glPushMatrix() glTranslatef(0.0,0.0,-5.0) glRotatef(xrot,1.0,0.0,0.0) glRotatef(yrot,0.0,1.0,0.0) glRotatef(zrot,0.0,0.0,1.0) glBegin(GL_QUADS) glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 1.0) glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, 1.0) glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 1.0) glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 1.0) glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0) glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, -1.0) glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, 1.0, -1.0) glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, -1.0) glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0) glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, 1.0, 1.0) glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, 1.0, 1.0) glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, -1.0) glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, -1.0, -1.0) glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, -1.0, -1.0) glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, 1.0) glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, 1.0) glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.0) glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, -1.0) glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, 1.0, 1.0) glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, 1.0) glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0) glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, 1.0) glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, 1.0) glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0) glEnd(); glPopMatrix() xrot = xrot + 0.02 yrot = yrot + 0.02 zrot = zrot + 0.02 glutSwapBuffers() def keyPressed(*args): if args[0] == ESCAPE: glutDestroyWindow(window) sys.exit() def main(): global window glutInit(sys.argv) glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH) glutInitWindowSize(640, 480) glutInitWindowPosition(0, 0) window = glutCreateWindow("Transparency Test") glutDisplayFunc(DrawGLScene) glutIdleFunc(DrawGLScene) glutReshapeFunc(ReSizeGLScene) glutKeyboardFunc(keyPressed) InitGL(640, 480) glutMainLoop() print "Hit ESC key to quit." main() |