还是,番外篇链接附上。
跟新速度会稍慢一些,因为我有项目要做,请各位看官不要着急哈~
如何让画面动起来?
使用glRotatef
使模型旋转,还记得之前的茶壶么?
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
def drawFunc():
glClear(GL_COLOR_BUFFER_BIT)
glRotatef(1, 0, 1, 0)
glutWireTeapot(0.5)
glFlush()
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
glutInitWindowSize(400, 400)
glutCreateWindow("First")
glutDisplayFunc(drawFunc)
glutIdleFunc(drawFunc)
glutMainLoop()
请注意第7行
glRotatef(1, 0, 1, 0)
这里注意,一个参数是角度,剩下的是绕 x , y , z x,y,z x,y,z三轴旋转第一个参数的多少倍,例如
glRotatef(1,0,1,0)
是绕 y y y轴旋转1度glRotatef(1,1,1,1)
是绕 x , y , z x,y,z x,y,z轴各旋转1度glRoatef(2,0.5,1,0.5)
是绕 x , z x,z x,z轴旋转1度,绕 y y y轴旋转2度使用代码片使自己(摄像机,camera)旋转
在这之前,需要先知道球坐标系以及球坐标和球坐标的转换
球坐标系是使用 ( r , θ , ϕ ) (r,\theta,\phi) (r,θ,ϕ)表示一个点 p p p的坐标系
对与球坐标系中任意一点 ( r , θ , ϕ ) (r,\theta,\phi) (r,θ,ϕ),有:
{ x = r ⋅ sin θ ⋅ cos ϕ y = r ⋅ sin θ ⋅ sin ϕ z = r ⋅ cos θ \left\{ \begin{aligned} x = r \cdot \sin \theta \cdot \cos \phi \\ y = r \cdot \sin \theta \cdot \sin \phi \\ z = r \cdot \cos \theta~~~~~~~~~~ \end{aligned} \right. ⎩⎪⎨⎪⎧x=r⋅sinθ⋅cosϕy=r⋅sinθ⋅sinϕz=r⋅cosθ
所以使用如下代码
由于代码中 r r r为1,所以将其忽略
import math
theta,phi = <angle>
pos = <pos>
def target():
global theta,phi
x = math.sin(theta) * math.cos(phi)
y = math.sin(theta) * math.sin(phi)
z = math.cos(theta)
return x,y,z
def setLookAt():
global pos
tar = target()
gluLookAt(*pos,*tar,0,1,0)
glLoadIdentity()
将代码中的
替换成角度,
替换成你的位置
使用如上代码片后,调用setLookAt
函数即可。
例如:
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from PIL import Image
import math
theta,phi = 0,0
pos = (0,0,0)
def target():
global theta,phi
x = math.sin(theta) * math.cos(phi)
y = math.sin(theta) * math.sin(phi)
z = math.cos(theta)
return x,y,z
def setLookAt():
global pos
tar = target()
gluLookAt(*pos,*tar,0,1,0)
glLoadIdentity()
def drawFunc():
glClear(GL_COLOR_BUFFER_BIT)
vertex = [[[0,0,1],[1,0,1],[1,1,1],[0,1,1]],[[0,0,0],[0,1,0],[1,1,0],[1,0,0]],[[0,1,0],[0,1,1],[1,1,1],[1,1,0]],[[0,0,0],[1,0,0],[1,0,1],[0,0,1]],[[1,0,0],[1,1,0],[1,1,1],[1,0,1]],[[0,0,0],[0,0,1],[0,1,1],[0,1,0]]]
for i in range(len(vertex)):
glBindTexture(GL_TEXTURE_2D,1)
glBegin(GL_QUADS)
glTexCoord2f(0.0, 0.0)
glVertex3f(*vertex[i][0])
glTexCoord2f(1.0, 0.0)
glVertex3f(*vertex[i][1])
glTexCoord2f(1.0, 1.0)
glVertex3f(*vertex[i][2])
glTexCoord2f(0.0, 1.0)
glVertex3f(*vertex[i][3])
glEnd()
glFlush()
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
glutInitWindowSize(400, 400)
glutCreateWindow(b"OpenGL")
glClearColor(1,1,1,1)
glEnable(GL_DEPTH_TEST)
glClearDepth(1.0)
glDepthFunc(GL_LESS)
glShadeModel(GL_SMOOTH)
glEnable(GL_CULL_FACE)
glCullFace(GL_BACK)
glEnable(GL_POINT_SMOOTH)
glEnable(GL_LINE_SMOOTH)
glEnable(GL_POLYGON_SMOOTH)
glMatrixMode(GL_PROJECTION)
glHint(GL_POINT_SMOOTH_HINT,GL_NICEST)
glHint(GL_LINE_SMOOTH_HINT,GL_NICEST)
glHint(GL_POLYGON_SMOOTH_HINT,GL_FASTEST)
glMatrixMode(GL_MODELVIEW)
glEnable(GL_TEXTURE_2D)
img = Image.open('a.jpg')
width, height = img.size
img = img.tobytes('raw','RGB',0,-1)
glGenTextures(2)
glBindTexture(GL_TEXTURE_2D, 1)
glTexImage2D(GL_TEXTURE_2D, 0, 4,width,height, 0, GL_RGB,GL_UNSIGNED_BYTE,img)
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)
glutDisplayFunc(drawFunc)
glutIdleFunc(drawFunc)
glutMainLoop()
hit-road
拜拜,下课!
回到顶部