Python 3学习笔记(15):PyOpenGL之绘制螺杆

本文介绍了一个使用OpenGL实现三维螺杆绘制的方法。通过矩形条近似圆柱侧面,并利用法线向量确保阴影平滑过渡。同时展示了如何通过顶点指定法线来规范曲面效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 目的

绘制螺帽,可用箭头键切换视角。 

  • 内容  

螺杆只是一个挨着螺帽的圆柱。(立着的圆柱)可以通过在一个圆上标记xz的值分解圆柱,然后取得在这两个点上的y值,得到近似圆柱侧面的平面多边形。然而这次,完全由矩形条组成这个圆柱侧面,因为每个相连的矩形可以共享一条法线产生光滑的阴影。

就像前面创建螺帽时做的一样,这次我们也用同样的方法创建螺杆的底部。

为每个顶点指定法线是为了要规范当前曲面(法线和光照效果)。

  • 代码  

#coding:utf-8

import sys
from math import pi as PI
from math import sin, cos
from math import sqrt

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

#将指定为三个坐标组的法线向量换算。
def ReduceToUnit(vector): 
	# Calculate the length of the vector		
	length = sqrt((vector[0]*vector[0]) + (vector[1]*vector[1]) +(vector[2]*vector[2]));
	#Keep the program from blowing up by providing an exceptable
	#value for vectors that may calculated too close to zero.
	if (length == 0.0):
		length = 1.0 
	#Dividing each element by the length will result in a	unit normal vector.
	vector[0] /= length 
	vector[1] /= length 
	vector[2] /= length 
 
# Points p1, p2, & p3 specified in counter clock-wise order
# float v[3][3], float out[3]
def calcNormal(v,out): 
	#float v1[3],v2[3];
	v1=[0.0 for i in range(3)]
	v2=[0.0 for i in range(3)]
	out=[0.0 for i in range(3)]	
	xx = 0 
	yy = 1 
	zz = 2 

	# Calculate two vectors from the three points
	v1[xx] = v[0][xx] - v[1][xx] 
	v1[yy] = v[0][yy] - v[1][yy] 
	v1[zz] = v[0][zz] - v[1][zz] 

	v2[xx] = v[1][xx] - v[2][xx] 
	v2[yy] = v[1][yy] - v[2][yy] 
	v2[zz] = v[1][zz] - v[2][zz] 

	# Take the cross product of the two vectors to get
	# the normal vector which will be stored in out
	out[xx] = v1[yy]*v2[zz] - v1[zz]*v2[yy] 
	out[yy] = v1[zz]*v2[xx] - v1[xx]*v2[zz] 
	out[zz] = v1[xx]*v2[yy] - v1[yy]*v2[xx] 

	# Normalize the vector (shorten length to one)
	ReduceToUnit(out) 

def RenderShaft():
	global x,z,angle	# Calculated positions
	height = 75.0		# Height of the cylinder
	diameter = 20.0		# Diameter of the cylinder
	#float normal[3],corners[2][3];    // Storeage of vertices and normals
	corners = [[0.0 for i in range(3)] for i in range(2)]
	normal=[0.0 for i in range(3)]
	step = (PI/50.0)     # Approximate the cylinder wall with 100 flat segments
    
	# Set material color for head of bolt
	glColor3f(0.0, 0.0, 0.7) 

    # ----------开始一个绘制螺杆------
	glBegin(GL_QUAD_STRIP)

	angle=(2.0*PI)
	while angle>0 :
		#Calculate x and y position of the next vertex
		x = diameter* sin(angle)
		z = diameter* cos(angle) 
		      
		# Get the coordinate for this point and extrude the length of the cylinder.
		corners[0][0] = x
		corners[0][1] = -height/2.0
		corners[0][2] = z
		corners[1][0] = x
		corners[1][1] = height/2.0
		corners[1][2] = z
		# Instead of using real normal to actual flat section
		# Use what the normal would be if the surface was really
		# curved. Since the cylinder goes up the Y axis, the normal 
		# points from the Y axis out directly through each vertex. 
		# Therefore we can use the vertex as the normal, as long as
		# we reduce it to unit length first and assume the y component to be zero
		normal[0] = corners[1][0]
		normal[1] = 0.0
		normal[2] = corners[1][2]
		# Reduce to length of one and specify for this point
		ReduceToUnit(normal)
		glNormal3fv(normal)
		glVertex3fv(corners[0])
		glVertex3fv(corners[1])
		angle-=step

	#Make sure there are no gaps by extending last quad to the original location
	glVertex3f(diameter*sin(2.0*PI),-height/2.0,diameter*cos(2.0*PI))
	glVertex3f(diameter*sin(2.0*PI),height/2.0,diameter*cos(2.0*PI))
	glEnd()

	#--------Begin a new triangle fan to cover the bottom-----
	glBegin(GL_TRIANGLE_FAN)
	#Normal points down the Y axis
	glNormal3f(0.0, -1.0, 0.0)
		
	#Center of fan is at the origin
	glVertex3f(0.0, -height/2.0, 0.0)
	
	angle=(2.0*PI)
	while angle>0 :
		x = diameter* sin(angle)
		z = diameter* cos(angle) 
		glVertex3f(x, -height/2.0, z)
		angle-=step
	# Be sure loop is closed by specifiying initial vertexon arc as the last too
	glVertex3f(diameter*sin(2.0*PI),-height/2.0,diameter*cos(2.0*PI))
	glEnd()		
		

def RenderScene():
	global xRot,yRot
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)					

	# Save the matrix state
	glMatrixMode(GL_MODELVIEW) 
	glPushMatrix() 
    # 绕X轴和Y轴旋转(角度,x,y,z)
	glRotatef(xRot, 1.0, 0.0, 0.0)
	glRotatef(yRot, 0.0, 0.0, 1.0)	
	# Render just the hexagonial head of the nut
	RenderShaft()
	glPopMatrix()
    #双缓冲的刷新模式; Swap buffers
	glutSwapBuffers()					

#设置渲染状态
def SetupRC():	
	# Light values and coordinates光照 值与坐标;环境光,漫射光,镜面光,光的坐标,
	ambientLight = [0.4, 0.4, 0.4, 1.0 ]
	diffuseLight = [0.7, 0.7, 0.7, 1.0 ]
	specular = [ 0.9, 0.9, 0.9, 1.0]
	lightPos = [ -50.0, 200.0, 200.0, 1.0]
	specref =  [ 0.6, 0.6, 0.6, 1.0]


	glEnable(GL_DEPTH_TEST)	# Hidden surface removal
	glEnable(GL_CULL_FACE)	# Do not calculate inside of solid object
	glFrontFace(GL_CCW) 
	
	glEnable(GL_LIGHTING) 

	# Setup light 0
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambientLight) 
	glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight) 
	glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight) 
	glLightfv(GL_LIGHT0,GL_SPECULAR,specular) 

	# Position and turn on the light
	glLightfv(GL_LIGHT0,GL_POSITION,lightPos) 
	glEnable(GL_LIGHT0) 

	# Enable color tracking
	glEnable(GL_COLOR_MATERIAL) 
	
	# Set Material properties to follow glColor values
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) 

	# All materials hereafter have full specular reflectivity with a moderate shine
	glMaterialfv(GL_FRONT, GL_SPECULAR,specref) 
	glMateriali(GL_FRONT,GL_SHININESS,64) 
	
	glClearColor(0.0, 0.0, 0.0, 1.0)  #背景黑色
	

#改变窗口大小时调用
def ChangeSize(w,h):
	nRange = 100.0
	if(h == 0):     #防止除数为0
		h = 1
	glViewport(0, 0, w, h)						#设置视区大小
	glMatrixMode(GL_PROJECTION)					#投影矩阵模式
	glLoadIdentity()							#矩阵堆栈清空
	#设置裁剪窗口大小
	if (w <= h): 
		glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange*2.0, nRange*2.0)	
	else: 
		glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange*2.0, nRange*2.0)	
						
	glMatrixMode(GL_MODELVIEW)					#模型矩阵模式
	glLoadIdentity()

def SpecialKeys(key,x,y):
	global xRot,yRot
	if(key == GLUT_KEY_UP):
		xRot-= 5.0
	if(key == GLUT_KEY_DOWN):
		xRot += 5.0
	if(key == GLUT_KEY_LEFT):
		yRot -= 5.0
	if(key == GLUT_KEY_RIGHT):
		yRot += 5.0
	if(key > 356.0):
		xRot = 0.0
	if(key < -1.0):
		xRot = 355.0
	if(key > 356.0):
		yRot = 0.0
	if(key < -1.0):
		yRot = 355.0
	glutPostRedisplay()

xRot=0.0
yRot=0.0
print("三维螺杆,按箭头键改变视角!")
#使用glut初始化OpenGL
glutInit()
glutInitWindowSize(700,700)
#设置显示模式;(注意双缓冲)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB| GLUT_DEPTH)
glutCreateWindow("Bolt Head")

glutReshapeFunc(ChangeSize)
glutSpecialFunc(SpecialKeys)     #注册键盘回调函数
#调用函数绘制图像
glutDisplayFunc(RenderScene)
SetupRC()
#主循环
glutMainLoop()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值