Example #1
0
	def __init__(self):
		# Create two lists: one with vertex positions, and normals, and the 
		# other one with the indices that indicate which vertices should be 
		# used for each triangle (two per face of the cube)
		pos = []
		for i in xrange(8):
			pos.append([
						1.0 if i & 1 else -1.0,
						1.0 if i & 2 else -1.0,
						1.0 if i & 4 else -1.0,
					])
		
		faces = [[0,1,3,2],[1,5,7,3],[2,3,7,6],[0,4,5,1],[0,2,6,4],[4,6,7,5]]

		vertices = []
		indices = []
		i = 0
		for f in faces:
			p_arr = map(lambda i:floatArray(pos[i]), f)

			n = list(planeNormal(p_arr[0], p_arr[1], p_arr[2]))

			indices.extend([i,i+1,i+3,i+1,i+2,i+3])
			i +=4

			for j,v in enumerate(f):
				# notice vertices is a list of lists. Each row holds the data of a single vertex
				# and each column is a float
				vertices.append(list(p_arr[j]) + n)

		# convert the list of lists into a Numpy array of floats
		vertices = floatArray(vertices)

		# create two VBO, one for the data of each vertex (a GL_ARRAY_BUFFER), 
		# and one for the indices into that array (a GL_ELEMENT_ARRAY_BUFFER)
		self._data_vbo = DataVbo(vertices)
		self._indices_vbo = IndexVbo(indices)

		# we specify what is stored in each record (a rown in vertices) in the data_vbo mean
		# we do this so sPyGlass can automatically bind the attributes in the shaders. That means
		# the names we specify here, must match both in name and dimension with the attributes
		# of the shaders that make use of this vbo

		self._data_vbo.defineFields(("position",3),("normal",3))

		# now that we have the VBOs, we call the superclass constructor
		super(Cube,self).__init__(self._data_vbo, self._indices_vbo)

		# we're going to specify a "Batch". a "Batch" is a combination of VBOs, indices range, 
		# a shader, and a material

		# load the shader program (vertex and fragment shaders)
		shader = R.loadShaderProgram("phong")

		# load the material red_plastic from the file default.mat
		mat = R.loadMaterial("default:red_plastic")

		# 0 - start this batch at index 0
		# 12 - use enough indices for 12 primitives (GL_TRIANGLES in this case)
		self.addBatch(shader, mat, 0, 12)
Example #2
0
	def __init__(self):
		super(MainScene,self).__init__()

		# use font "test-font" (.zip)
		self._fnt = R.loadFont("test-font")

		self._cube = Cube()

		cam = PerspectiveCamera()

		self.setCamera(cam)

		cam.setPosition(floatArray((0,0,5)),floatArray((0,0,0)))

		self._text = Text()
		self._text.setFont(self._fnt)
	
		self._text.setText("This is\na test")