Python论坛  - 讨论区

标题:[python-chinese] Re: python-chinese Digest, Vol 30, Issue 109

2006年06月13日 星期二 16:25

V Plastic viscoplastic at gmail.com
Tue Jun 13 16:25:35 HKT 2006

Skipped content of type multipart/alternative-------------- next part --------------
if __name__ == '__build__':
	raise Exception

import string
__version__ = string.split('$Revision: 1.8 $')[1]
__date__ = string.join(string.split('$Date: 2006/06/10 04:13:56 $')[1:3], ' ')
__author__ = 'Bing Ye <viscoplastic at gmail.com>'


from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import sys
from numpy import *
from OpenGL.WGL import *	# wglUseFontBitmaps (), wglGetCurrentDC ()
import win32ui				# CreateFont (), CreateDCFromHandle ()
import Image # get PIL's functionality...

# Some api in the chain is translating the keystrokes to this octal string
# so instead of saying: ESCAPE = 27, we use the following.
ESCAPE = '\033'

# Number of the glut window.
window = 0

# Rotation angle for the triangle. 
rtri = 0.0

# Rotation angle for the quadrilateral.
rquad = 0.0

base = None
couns = 1


def BuildFont ():
	global base

	wgldc = wglGetCurrentDC ()
	hDC = win32ui.CreateDCFromHandle (wgldc)


	base = glGenLists(32+96);					# // Storage For 96 Characters, plus 32 at the start...

	# CreateFont () takes a python dictionary to specify the requested font properties. 
	font_properties = { "name" : "Courier New",
						"width" : 0 ,
						"height" : 24,
						"weight" : 1600
						}
	font = win32ui.CreateFont (font_properties)
	# // Selects The Font We Want
	oldfont = hDC.SelectObject (font)
	# // Builds 96 Characters Starting At Character 32
	wglUseFontBitmaps (wgldc, 32, 96, base+32)
	# // reset the font
	hDC.SelectObject (oldfont)
	# // Delete The Font (python will cleanup font for us...)
	return
    
def KillFont ():
	""" // Delete The Font List
	"""
	global	base
	# // Delete All 96 Characters
	glDeleteLists (base, 32+96)
	return


def glPrint (str):
	""" // Custom GL "Print" Routine
	"""
	global base
	# // If THere's No Text Do Nothing
	if (str == None):
		return
	if (str == ""):
		return
	glPushAttrib(GL_LIST_BIT);							# // Pushes The Display List Bits
	try:
		glListBase(base);								# // Sets The Base Character to 32
		glCallLists(str)									# // Draws The Display List Text
	finally:
		glPopAttrib();										# // Pops The Display List Bits
	return


# A general OpenGL initialization function.  Sets all of the initial parameters. 
def InitGL(Width, Height):				# We call this right after our OpenGL window is created.
    glClearColor(0.0, 0.0, 0.0, 0.0)	# This Will Clear The Background Color To Black
    glClearDepth(1.0)					# Enables Clearing Of The Depth Buffer
    glDepthFunc(GL_LESS)				# The Type Of Depth Test To Do
    glEnable(GL_DEPTH_TEST)				# Enables Depth Testing
    glShadeModel(GL_SMOOTH)				# Enables Smooth Color Shading
	
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()					# Reset The Projection Matrix
										# Calculate The Aspect Ratio Of The Window
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)

    glMatrixMode(GL_MODELVIEW)
    BuildFont ()

# The function called when our window is resized (which shouldn't happen if you enable fullscreen, below)
def ReSizeGLScene(Width, Height):
    if Height == 0:						# Prevent A Divide By Zero If The Window Is Too Small 
	    Height = 1

    glViewport(0, 0, Width, Height)		# Reset The Current Viewport And Perspective Transformation
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
    glMatrixMode(GL_MODELVIEW)


def Drawpolygon():
        glBegin(GL_POLYGON);
        for i in range(10):
            
            glColor3f(rand(),rand(),rand());
            glVertex3f(3*cos(4/3.0*pi)+3*cos(pi/3.0+i/9.0*pi/3.0),\
                       3*sin(4/3.0*pi)+3*sin(pi/3.0+i/9.0*pi/3.0),0);
        for i in range(9,0,-1):
            
            glColor3f(1,rand(),rand());
            glVertex3f(3*cos(2/3.0*pi)+3*cos(-pi/3.0-i/9.0*pi/3.0),\
                       3*sin(2/3.0*pi)+3*sin(-pi/3.0-i/9.0*pi/3.0),0);
        glEnd();
    

# The main drawing function. 
def DrawGLScene():
	global rtri, rquad, couns
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	# Clear The Screen And The Depth Buffer
	glLoadIdentity();					# Reset The View
	glTranslatef(-0,-0,-8);
	# Move Left And Into The Screen

	glRotatef(rtri,0.0,1.0,0.0);		    # Rotate The Pyramid On It's Y Axis

        # // Position The Text On The Screen
	glRasterPos2f(1,1);
 	glPrint("Drew by Bing Ye" );	# // Print GL Text To The Screen
	#glBegin(GL_TRIANGLES);					# Start Drawing The Pyramid
        glBegin(GL_LINE_LOOP);	
	#glBegin(GL_POLYGON);			# Red
	for i in range(20):
            
            glColor3f(rand(),rand(),rand());
            glVertex3f(1*cos(i/10.0*pi),1*sin(i/10.0*pi),0);

        glEnd();
        
        glBegin(GL_LINE_LOOP);
        for i in range(20):
            
            glColor3f(rand(),rand(),rand());
            glVertex3f(2*cos(i/10.0*pi),2*sin(i/10.0*pi),0);
        glEnd();

        for i in range(1,9):
            Drawpolygon()
            glRotatef(360*i/8.0,0.0,0.0,1.0);
        
        Drawpolygon()          

	rtri  = rtri + 2                  # Increase The Rotation Variable For The Triangle
	rquad = rquad - 2.15                 # Decrease The Rotation Variable For The Quad

	#  since this is double buffered, swap the buffers to display what just got drawn. 
	glutSwapBuffers()
        if couns < 10:
            width, height = 640,480
            glPixelStorei(GL_PACK_ALIGNMENT, 1)
            data = glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE)
            image = Image.fromstring( "RGB", (width, height), data )
            image = image.transpose( Image.FLIP_TOP_BOTTOM)
            image.save( 'help'+str(couns)+'.jpg' )
        couns = couns + 1


# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)  
def keyPressed(*args):
	# If escape is pressed, kill everything.
    if args[0] == ESCAPE:
	    sys.exit()

def main():
	global window
	glutInit(sys.argv)

	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
	
	# get a 640 x 480 window 
	glutInitWindowSize(640, 480)
	
	# the window starts at the upper left corner of the screen 
	glutInitWindowPosition(0, 0)

	window = glutCreateWindow("Bing's try")

	glutDisplayFunc(DrawGLScene)
	
	# Uncomment this line to get full screen.
	# glutFullScreen()

	# When we are doing nothing, redraw the scene.
	glutIdleFunc(DrawGLScene)
	
	# Register the function called when our window is resized.
	glutReshapeFunc(ReSizeGLScene)
	
	# Register the function called when the keyboard is pressed.  
	glutKeyboardFunc(keyPressed)

	# Initialize our window. 
	InitGL(640, 480)

	# Start Event Processing Engine	
	glutMainLoop()

# Print message to console, and kick off the main to get it rolling.
print "Hit ESC key to quit."
main()
    	

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年06月13日 星期二 16:58

limodou limodou at gmail.com
Tue Jun 13 16:58:41 HKT 2006

On 6/13/06, V Plastic <viscoplastic at gmail.com> wrote:
> 好像大家都是搞网络的。我不懂网络,不知道谁能不能介绍用python架设服务器的一些步骤。
>
> 我用python是因为python对其他语言程序的包容性和python的简单。个人觉得绝大部分的东西python都能做出来,而且很容易学。
>
> 我个人觉得那些讨论python的好坏的论题是不必要了,大家到这儿来是因为想学习和讨论python的一些应用,而不是天天说python好或差,难或易。
>
> 附件中是我学习python opengl做的一个练习。在python里用opengl比c里面好像简单多了。
>
>
搞网络与做web还是有区别的。其实这里做什么的都有。之所以讨论web因为这是一块很大的应用领域,也有许多可以学习的东西。

-- 
I like python!
My Blog: http://www.donews.net/limodou
My Django Site: http://www.djangocn.org
NewEdit Maillist: http://groups.google.com/group/NewEdit

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号