pyopengl 重力小球

该博客介绍了如何利用PyOpenGL在Python中创建一个重力小球的模拟程序。程序需要两个特定的纹理文件,并且用户可以通过点击窗口来重新生成小球的位置。
摘要由CSDN通过智能技术生成
#! /usr/bin/env python
# -*- coding: utf8 -*-
"""Port of NeHe Lesson 26 by Ivan Izuver <izuver@users.sourceforge.net>"""
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
# from Image import *
from PIL.Image import *
import sys,gc
import random


ESCAPE = '\033'

# Number of the glut window.
window = 0

LightAmb=(0.7,0.7,0.7)  #Окружающий свет
LightDif=(1.0,1.0,0.0)  #Рассеянный свет
LightPos=(4.0,4.0,6.0,1.0) #Позиция источника освещения
#q=GLUquadricObj()
xrot=yrot=0.0 #Вращение по Х Y

xrotspeed=yrotspeed=0.0 #Скорость вращения по X Y
zoom=-3.0 #Глубина сцены в экране
height=0.5 #Высота мяча над полом
textures = {}

goblelength=2
ballr=0.05
x=0
y=0
z=0

vx=0.01
vy=0.01
vz=0.01

ax=0
ay=0
ag=-0.0098#重力加速度
az=0
time=0

def LoadTextures(fname):
	if textures.get( fname ) is not None:
		return textures.get( fname )
	texture = textures[fname] = glGenTextures(1)
	image = open(fname)
	
	ix = image.size[0]
	iy = image.size[1]
	image = image.tobytes("raw", "RGBX", 0, -1)
	
	# Create Texture    
	glBindTexture(GL_TEXTURE_2D, texture)   # 2d texture (x and y size)
	
	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_NEAREST)
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
	return texture

# 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.2, 0.5, 1.0, 1.0)    # This Will Clear The Background Color To Black
	glClearDepth(1.0)                    # Enables Clearing Of The Depth Buffer
	glClearStencil(0)
	glDepthFunc(GL_LEQUAL)                # The Type Of Depth Test To Do
	glEnable(GL_DEPTH_TEST)           
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值