def draw_face(self, f, f_color): #add normal format = GeomVertexFormat.getV3n3cp() vdata = GeomVertexData('vert', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') color = GeomVertexWriter(vdata, 'color') normal = GeomVertexWriter(vdata, 'normal') vertex.addData3f(f.v1.pos) normal.addData3f(f.v1.norm.x, f.v1.norm.y, f.v1.norm.z) color.addData4f(f_color) vertex.addData3f(f.v2.pos) normal.addData3f(f.v2.norm.x, f.v2.norm.y, f.v2.norm.z) color.addData4f(f_color) vertex.addData3f(f.v3.pos) normal.addData3f(f.v3.norm.x, f.v3.norm.y, f.v3.norm.z) color.addData4f(f_color) mesh = Geom(vdata) tri = GeomTriangles(Geom.UHDynamic) tri.addVertex(0) tri.addVertex(1) tri.addVertex(2) tri.closePrimitive() mesh.addPrimitive(tri) face_node = GeomNode(self.mesh.name + '_face_' + str(f.ID)) face_node.addGeom(mesh) face_node.setTag('ID', str(f.ID)) rendered_face = self.render_root.attachNewNode(face_node) rendered_face.setTwoSided(True) self.render_nodes['face_' + str(f.ID)] = rendered_face
def draw_face(self,f,f_color): #add normal format = GeomVertexFormat.getV3n3cp() vdata=GeomVertexData('vert', format, Geom.UHDynamic) vertex=GeomVertexWriter(vdata, 'vertex') color=GeomVertexWriter(vdata, 'color') normal=GeomVertexWriter(vdata, 'normal') vertex.addData3f(f.v1.pos) normal.addData3f(f.v1.norm.x, f.v1.norm.y, f.v1.norm.z) color.addData4f(f_color) vertex.addData3f(f.v2.pos) normal.addData3f(f.v2.norm.x, f.v2.norm.y, f.v2.norm.z) color.addData4f(f_color) vertex.addData3f(f.v3.pos) normal.addData3f(f.v3.norm.x, f.v3.norm.y, f.v3.norm.z) color.addData4f(f_color) mesh = Geom(vdata) tri = GeomTriangles(Geom.UHDynamic) tri.addVertex(0) tri.addVertex(1) tri.addVertex(2) tri.closePrimitive() mesh.addPrimitive(tri) face_node = GeomNode(self.mesh.name+'_face_'+str(f.ID)) face_node.addGeom(mesh) face_node.setTag('ID',str(f.ID)) rendered_face = self.render_root.attachNewNode(face_node) rendered_face.setTwoSided(True) self.render_nodes['face_'+str(f.ID)] = rendered_face
def create_mesh(parentnp, debug=False, invert=False): '''This creates a simple 17x17 grid mesh for the sides of our cube. The ultimate goal is to use a LOD system, probably based on quadtrees. If debug is true then we get a color gradiant on our vertexes.''' x = -1.0 y = -1.0 vertex_count = 0 u = 0.0 v = 0.0 WIDTH_STEP = 2 / 16.0 while y <= 1.0: while x <= 1.0: vertex.addData3f(x, y, 0) if invert: normal.addData3f( myNormalize((Vec3(2 * x + 1, 2 * y + 1, 2 * 0 - 1)))) else: normal.addData3f( myNormalize((Vec3(2 * x - 1, 2 * y - 1, 2 * 0 - 1)))) if debug: color.addData4f(1.0, u, v, 1.0) texcoord.addData2f(u, v) vertex_count += 1 x += WIDTH_STEP u += WIDTH_STEP / 2.0 x = -1.0 u = 0 y += WIDTH_STEP v += WIDTH_STEP / 2.0 print vertex_count triangles = [] for y in range(0, 16): for x in range(0, 16): v = 17 * y + x tri = GeomTriangles(Geom.UHDynamic) tri.addVertex(v) tri.addVertex(v + 1) tri.addVertex(v + 17) tri.closePrimitive() triangles.append(tri) tri = GeomTriangles(Geom.UHDynamic) tri.addVertex(v + 1) tri.addVertex(v + 18) tri.addVertex(v + 17) tri.closePrimitive() triangles.append(tri) mesh = Geom(vdata) for t in triangles: mesh.addPrimitive(t) mnode = GeomNode('quadface') mnode.addGeom(mesh) nodePath = parentnp.attachNewNode(mnode) return nodePath
def draw(self): if self.rendered_mesh != None: self.reset_draw() format = GeomVertexFormat.getV3n3cp() vdata = GeomVertexData('tri', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') v_mapping = {} i = 0 for v in self.verts.values(): vertex.addData3f(v.pos.x, v.pos.y, v.pos.z) normal.addData3f(v.norm.x, v.norm.y, v.norm.z) color.addData4f(v.color[0], v.color[1], v.color[2], v.color[3]) v_mapping[v.ID] = i i += 1 mesh = Geom(vdata) for f in self.faces.values(): tri = GeomTriangles(Geom.UHDynamic) tri.addVertex(v_mapping[f.v1.ID]) tri.addVertex(v_mapping[f.v2.ID]) tri.addVertex(v_mapping[f.v3.ID]) tri.closePrimitive() mesh.addPrimitive(tri) snode = GeomNode(self.name) snode.addGeom(mesh) self.rendered_mesh = render.attachNewNode(snode) self.rendered_mesh.setTwoSided(True)
def draw(self): if self.rendered_mesh != None: self.reset_draw() format=GeomVertexFormat.getV3n3cp() vdata=GeomVertexData('tri', format, Geom.UHDynamic) vertex=GeomVertexWriter(vdata, 'vertex') normal=GeomVertexWriter(vdata, 'normal') color=GeomVertexWriter(vdata, 'color') v_mapping = {} i=0 for v in self.verts.values(): vertex.addData3f(v.pos.x,v.pos.y,v.pos.z) normal.addData3f(v.norm.x, v.norm.y, v.norm.z) color.addData4f(v.color[0],v.color[1],v.color[2],v.color[3]) v_mapping[v.ID] = i i += 1 mesh = Geom(vdata) for f in self.faces.values(): tri = GeomTriangles(Geom.UHDynamic) tri.addVertex(v_mapping[f.v1.ID]) tri.addVertex(v_mapping[f.v2.ID]) tri.addVertex(v_mapping[f.v3.ID]) tri.closePrimitive() mesh.addPrimitive(tri) snode = GeomNode(self.name) snode.addGeom(mesh) self.rendered_mesh = render.attachNewNode(snode) self.rendered_mesh.setTwoSided(True)
def create_mesh(parentnp, debug=False, invert=False): """This creates a simple 17x17 grid mesh for the sides of our cube. The ultimate goal is to use a LOD system, probably based on quadtrees. If debug is true then we get a color gradiant on our vertexes.""" x = -1.0 y = -1.0 vertex_count = 0 u = 0.0 v = 0.0 WIDTH_STEP = 2 / 16.0 while y <= 1.0: while x <= 1.0: vertex.addData3f(x, y, 0) if invert: normal.addData3f(myNormalize((Vec3(2 * x + 1, 2 * y + 1, 2 * 0 - 1)))) else: normal.addData3f(myNormalize((Vec3(2 * x - 1, 2 * y - 1, 2 * 0 - 1)))) if debug: color.addData4f(1.0, u, v, 1.0) texcoord.addData2f(u, v) vertex_count += 1 x += WIDTH_STEP u += WIDTH_STEP / 2.0 x = -1.0 u = 0 y += WIDTH_STEP v += WIDTH_STEP / 2.0 print vertex_count triangles = [] for y in range(0, 16): for x in range(0, 16): v = 17 * y + x tri = GeomTriangles(Geom.UHDynamic) tri.addVertex(v) tri.addVertex(v + 1) tri.addVertex(v + 17) tri.closePrimitive() triangles.append(tri) tri = GeomTriangles(Geom.UHDynamic) tri.addVertex(v + 1) tri.addVertex(v + 18) tri.addVertex(v + 17) tri.closePrimitive() triangles.append(tri) mesh = Geom(vdata) for t in triangles: mesh.addPrimitive(t) mnode = GeomNode("quadface") mnode.addGeom(mesh) nodePath = parentnp.attachNewNode(mnode) return nodePath
def make_square(x1, y1, z1, x2, y2, z2, tex_coord): format = GeomVertexFormat.getV3n3t2() vdata = GeomVertexData('square', format, Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') texcoord = GeomVertexWriter(vdata, 'texcoord') #make sure we draw the sqaure in the right plane if x1 != x2: vertex.addData3f(x1, y1, z1) vertex.addData3f(x2, y1, z1) vertex.addData3f(x2, y2, z2) vertex.addData3f(x1, y2, z2) else: vertex.addData3f(x1, y1, z1) vertex.addData3f(x2, y2, z1) vertex.addData3f(x2, y2, z2) vertex.addData3f(x1, y1, z2) normal.addData3f(my_normalize(Vec3(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1))) normal.addData3f(my_normalize(Vec3(2 * x2 - 1, 2 * y2 - 1, 2 * z1 - 1))) normal.addData3f(my_normalize(Vec3(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1))) normal.addData3f(my_normalize(Vec3(2 * x1 - 1, 2 * y1 - 1, 2 * z2 - 1))) #adding different colors to the vertex for visibility u1, v1, u2, v2 = tex_coord texcoord.addData2f(u1, v2) texcoord.addData2f(u1, v1) texcoord.addData2f(u2, v1) texcoord.addData2f(u2, v2) #quads arent directly supported by the Geom interface #you might be interested in the CardMaker class if you are #interested in rectangle though tri1 = GeomTriangles(Geom.UHStatic) tri2 = GeomTriangles(Geom.UHStatic) tri1.addVertex(0) tri1.addVertex(1) tri1.addVertex(3) tri2.addConsecutiveVertices(1, 3) tri1.closePrimitive() tri2.closePrimitive() square = Geom(vdata) square.addPrimitive(tri1) square.addPrimitive(tri2) return square
def make_square(x1, y1, z1, x2, y2, z2, tex_coord): format = GeomVertexFormat.getV3n3t2() vdata = GeomVertexData("square", format, Geom.UHStatic) vertex = GeomVertexWriter(vdata, "vertex") normal = GeomVertexWriter(vdata, "normal") texcoord = GeomVertexWriter(vdata, "texcoord") # make sure we draw the sqaure in the right plane if x1 != x2: vertex.addData3f(x1, y1, z1) vertex.addData3f(x2, y1, z1) vertex.addData3f(x2, y2, z2) vertex.addData3f(x1, y2, z2) else: vertex.addData3f(x1, y1, z1) vertex.addData3f(x2, y2, z1) vertex.addData3f(x2, y2, z2) vertex.addData3f(x1, y1, z2) normal.addData3f(my_normalize(Vec3(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1))) normal.addData3f(my_normalize(Vec3(2 * x2 - 1, 2 * y2 - 1, 2 * z1 - 1))) normal.addData3f(my_normalize(Vec3(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1))) normal.addData3f(my_normalize(Vec3(2 * x1 - 1, 2 * y1 - 1, 2 * z2 - 1))) # adding different colors to the vertex for visibility u1, v1, u2, v2 = tex_coord texcoord.addData2f(u1, v2) texcoord.addData2f(u1, v1) texcoord.addData2f(u2, v1) texcoord.addData2f(u2, v2) # quads arent directly supported by the Geom interface # you might be interested in the CardMaker class if you are # interested in rectangle though tri1 = GeomTriangles(Geom.UHStatic) tri2 = GeomTriangles(Geom.UHStatic) tri1.addVertex(0) tri1.addVertex(1) tri1.addVertex(3) tri2.addConsecutiveVertices(1, 3) tri1.closePrimitive() tri2.closePrimitive() square = Geom(vdata) square.addPrimitive(tri1) square.addPrimitive(tri2) return square
def createTriangle(v1, v2, v3, is_flat=False): x1 = v1.x y1 = v1.y z1 = v1.z x2 = v2.x y2 = v2.y z2 = v2.z x3 = v3.x y3 = v3.y z3 = v3.z format=GeomVertexFormat.getV3n3cp() vdata=GeomVertexData('tri', format, Geom.UHDynamic) vertex=GeomVertexWriter(vdata, 'vertex') normal=GeomVertexWriter(vdata, 'normal') color=GeomVertexWriter(vdata, 'color') vertex.addData3f(x1, y1, z1) vertex.addData3f(x2, y2, z2) vertex.addData3f(x3, y3, z3) if is_flat: normVector = norm(Vec3( (x1 + x2 + x3)/3.0, (y1 + y2 + y3)/3.0, (z1+ z2+ z3)/3.0)) normal.addData3f(normVector) normal.addData3f(normVector) normal.addData3f(normVector) else: normal.addData3f(norm(Vec3(x1,y1,z1))) normal.addData3f(norm(Vec3(x2,y2,z2))) normal.addData3f(norm(Vec3(x3,y3,z3))) #adding different colors to the vertex for visibility color.addData4f(0.5,0.5,0.5,1.0) color.addData4f(0.5,0.5,0.5,1.0) color.addData4f(0.5,0.5,0.5,1.0) tri = GeomTriangles(Geom.UHDynamic) tri.addVertex(0) tri.addVertex(1) tri.addVertex(2) tri.closePrimitive() output_tri = Geom(vdata) output_tri.addPrimitive(tri) return output_tri
def load(self): if panda3d is None: raise ImportError("Cannot locate Panda3D") node = GeomNode(self.name) for n in range(len(self.faces)): facevertices = self.faces[n] vertices = [self.vertices[v] for v in facevertices] normals = None if self.normals is not None: normals = (self.normals[n],) * len(facevertices) colors = None if self.facecolors is not None: colors = (self.facecolors[n],) * len(facevertices) elif self.fvcolors is not None: colors = self.fvcolors[n] texcoords = None if self.texcoords is not None: texcoords = self.texcoords[n] #KLUDGE if self.material is not None and colors is None: col = self.material.getAmbient() col = (col[0] / col[3], col[1] / col[3], col[2] / col[3], 1.0) colors = (col,) * len(vertices) #/KLUDGE geom = make_geom(vertices, normals, colors, texcoords) prim = GeomTriangles(Geom.UHStatic) #TODO: proper tesselation! the current one only works for convex faces curr = 1 for v in range(2, len(facevertices)): prim.addVertex(curr) prim.addVertex(v) prim.addVertex(0) curr = v prim.closePrimitive() geom.addPrimitive(prim) node.addGeom(geom) self.node = NodePath(node) self.node.setTwoSided(True) if self.material is not None: self.node.setMaterial(self.material, priority=1) self.loaded = True
def load(self): if panda3d is None: raise ImportError("Cannot locate Panda3D") #KLUDGE if self.material is not None and self.colors is None: col = self.material.getAmbient() col = (col[0] / col[3], col[1] / col[3], col[2] / col[3], 1.0) self.colors = (col,) * len(self.vertices) #/KLUDGE geom = make_geom(self.vertices, self.normals, self.colors, self.texcoords) prim = GeomTriangles(Geom.UHStatic) for face in self.faces: #TODO: proper tesselation! the current one only works for convex faces first = face[0] curr = face[1] for v in face[2:]: prim.addVertex(curr) prim.addVertex(v) prim.addVertex(first) curr = v prim.closePrimitive() geom.addPrimitive(prim) node = GeomNode(self.name) node.addGeom(geom) self.node = NodePath(node) self.node.setTwoSided(True) if self.material is not None: self.node.setMaterial(self.material, priority=1) self.loaded = True
def createPlane(width,height): format=GeomVertexFormat.getV3() vdata=GeomVertexData("vertices", format, Geom.UHStatic) vertexWriter=GeomVertexWriter(vdata, "vertex") vertexWriter.addData3f(0,0,0) vertexWriter.addData3f(width,0,0) vertexWriter.addData3f(width,height,0) vertexWriter.addData3f(0,height,0) #step 2) make primitives and assign vertices to them tris=GeomTriangles(Geom.UHStatic) #have to add vertices one by one since they are not in order tris.addVertex(0) tris.addVertex(1) tris.addVertex(3) #indicates that we have finished adding vertices for the first triangle. tris.closePrimitive() #since the coordinates are in order we can use this convenience function. tris.addConsecutiveVertices(1,3) #add vertex 1, 2 and 3 tris.closePrimitive() #step 3) make a Geom object to hold the primitives squareGeom=Geom(vdata) squareGeom.addPrimitive(tris) #now put squareGeom in a GeomNode. You can now position your geometry in the scene graph. squareGN=GeomNode("square") squareGN.addGeom(squareGeom) terrainNode = NodePath("terrNode") terrainNode.reparentTo(render) terrainNode.attachNewNode(squareGN) terrainNode.setX(-width/2) texGrass = loader.loadTexture("textures/envir-ground.jpg") terrainNode.setTexture(texGrass)
def make_cube_geom(self, pos=(0, 0, 0), geom_color=(1, 1, 1, 0.5), faces_no_draw=(0, 0, 0, 0, 0, 0)): format = GeomVertexFormat.getV3n3cpt2() shift_x, shift_y, shift_z = pos cube_vertex_list = deepcopy(cube_data["vertexPositions"]) for vert in cube_vertex_list: vert[0] += shift_x vert[1] += shift_y vert[2] += shift_z vdata = GeomVertexData('square', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') test_textcoords = [] # define available vertexes here [vertex.addData3(*v) for v in cube_vertex_list] [normal.addData3(*n) for n in cube_data["vertexNormals"]] [color.addData4f(*geom_color) for _ in cube_vertex_list] [texcoord.addData2f(*t) for t in texture_mapping] """ CREATE A NEW PRIMITIVE """ prim = GeomTriangles(Geom.UHStatic) # convert from numpy arr mapping to normal faces mapping... excluded_normals = [ normal_face_mapping[self.dict_mapping[i]] for i in range(len(faces_no_draw)) if faces_no_draw[i] == 1 ] # only use acceptable indices indices = [ ind for ind in cube_data['indices'] if cube_data['vertexNormals'][ind] not in excluded_normals ] [prim.addVertex(v) for v in indices] # N.B: this must correspond to a vertex defined in vdata geom = Geom(vdata) # create a new geometry geom.addPrimitive(prim) # add the primitive to the geometry return geom
def make_cube_geom(pos=(0, 0, 0), geom_color=(1, 1, 1, 1)): format = GeomVertexFormat.getV3n3cpt2() shift_x, shift_y, shift_z = pos cube_vertex_list = deepcopy(cube_data["vertexPositions"]) for vert in cube_vertex_list: # vert[0] *= scale # vert[1] *= scale # vert[2] *= scale vert[0] += shift_x vert[1] += shift_y vert[2] += shift_z vdata = GeomVertexData('square', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') # define available vertexes here [normal.addData3(*n) for n in cube_data["vertexNormals"]] [vertex.addData3(*v) for v in cube_vertex_list] [color.addData4f(*geom_color) for _ in cube_vertex_list] [texcoord.addData2f(1, 1) for _ in cube_vertex_list] """ CREATE A NEW PRIMITIVE """ prim = GeomTriangles(Geom.UHStatic) # [prim.addVertex(v) for v in vertexes] indices = [ind for ind in cube_data['indices']] [prim.addVertex(v) for v in indices] # N.B: this must correspond to a vertex defined in vdata geom = Geom(vdata) # create a new geometry geom.addPrimitive(prim) # add the primitive to the geometry return geom
def createModelClone(self, modelFilename): newVisualCar = loader.loadModel(modelFilename) geomNodeCollection = newVisualCar.findAllMatches('**/+GeomNode') simpleTris = [] self.unmodifiedVertexData = [] for nodePath in geomNodeCollection: geomNode = nodePath.node() for i in range(geomNode.getNumGeoms()): geom = geomNode.getGeom(i) vdata = geom.getVertexData() vertex = GeomVertexReader(vdata, 'vertex') while not vertex.isAtEnd(): v = vertex.getData3f() vertexModelX, vertexModelY, vertexModelZ = v self.unmodifiedVertexData.append( [vertexModelX, vertexModelY, vertexModelZ]) for primitiveIndex in range(geom.getNumPrimitives()): prim = geom.getPrimitive(primitiveIndex) prim = prim.decompose() for p in range(prim.getNumPrimitives()): s = prim.getPrimitiveStart(p) e = prim.getPrimitiveEnd(p) singleTriData = [] for i in range(s, e): vertex.setRow(prim.getVertex(s)) vi = prim.getVertex(i) singleTriData.append(vi) simpleTris.append(singleTriData) simpleVertices = self.unmodifiedVertexData format = GeomVertexFormat.getV3() vdata = GeomVertexData('shadow', format, Geom.UHDynamic) self.pandaVertexData = vdata vertex = GeomVertexWriter(vdata, 'vertex') for vertexIndex in range(len(simpleVertices)): simpleVertex = simpleVertices[vertexIndex] vertex.addData3f(0, 0, 0) tris = GeomTriangles(Geom.UHStatic) for index in range(len(simpleTris)): simpleTri = simpleTris[index] tris.addVertex(simpleTri[0]) tris.addVertex(simpleTri[1]) tris.addVertex(simpleTri[2]) tris.closePrimitive() shadow = Geom(vdata) shadow.addPrimitive(tris) snode = GeomNode('shadow') snode.addGeom(shadow) self.snode = snode
def createModelClone(self,modelFilename): newVisualCar = loader.loadModel(modelFilename) geomNodeCollection = newVisualCar.findAllMatches('**/+GeomNode') simpleTris=[] self.unmodifiedVertexData=[] for nodePath in geomNodeCollection: geomNode = nodePath.node() for i in range(geomNode.getNumGeoms()): geom = geomNode.getGeom(i) vdata = geom.getVertexData() vertex = GeomVertexReader(vdata, 'vertex') while not vertex.isAtEnd(): v=vertex.getData3f() vertexModelX,vertexModelY,vertexModelZ=v self.unmodifiedVertexData.append([vertexModelX,vertexModelY,vertexModelZ]) for primitiveIndex in range(geom.getNumPrimitives()): prim=geom.getPrimitive(primitiveIndex) prim=prim.decompose() for p in range(prim.getNumPrimitives()): s = prim.getPrimitiveStart(p) e = prim.getPrimitiveEnd(p) singleTriData=[] for i in range(s, e): vertex.setRow(prim.getVertex(s)) vi = prim.getVertex(i) singleTriData.append(vi) simpleTris.append(singleTriData) simpleVertices=self.unmodifiedVertexData format=GeomVertexFormat.getV3() vdata=GeomVertexData('shadow', format, Geom.UHDynamic) self.pandaVertexData=vdata vertex=GeomVertexWriter(vdata, 'vertex') for vertexIndex in range(len(simpleVertices)): simpleVertex=simpleVertices[vertexIndex] vertex.addData3f(0,0,0) tris=GeomTriangles(Geom.UHStatic) for index in range(len(simpleTris)): simpleTri=simpleTris[index] tris.addVertex(simpleTri[0]) tris.addVertex(simpleTri[1]) tris.addVertex(simpleTri[2]) tris.closePrimitive() shadow=Geom(vdata) shadow.addPrimitive(tris) snode=GeomNode('shadow') snode.addGeom(shadow) self.snode=snode
class Chunk2GeomDecoder: """ Chunk decoder that decodes chunks to Panda3D Geoms/Nodes """ def __init__(self): self.textures={} for k in texMap: v=texMap[k] tex=loader.loadTexture("gfx/%s"%v) tex.setWrapU(Texture.WM_clamp) tex.setWrapV(Texture.WM_clamp) self.textures[k]=tex self.viewPoint=(0,0,0) self.cubeVtxSrc=[ # 14 vertices per cube mapped # so that UV coords utilize # the typical cube unwrap: # # #### <-- square texture # ##U# # BLFR # ##D# #=unused # # This form uses 14 vertices per cube since # the extra (worldspace overlapped) vertices # map different U,V coordinates at the same # worldspace coordinates, depending on face # # x y z u v (1.00,1.00,1.00,0.00,0.50), # A (1.00,1.00,0.00,0.00,0.25), # B (0.00,1.00,1.00,0.25,0.50), # C (0.00,1.00,0.00,0.25,0.25), # D (0.00,0.00,1.00,0.50,0.50), # E (0.00,0.00,0.00,0.50,0.25), # F (1.00,0.00,1.00,0.75,0.50), # G (1.00,0.00,0.00,0.75,0.25), # H (1.00,1.00,1.00,1.00,0.50), # I (1.00,1.00,0.00,1.00,0.25), # J (0.00,1.00,1.00,0.50,0.75), # K (1.00,1.00,1.00,0.75,0.75), # L (0.00,1.00,0.00,0.50,0.00), # M (1.00,1.00,0.00,0.75,0.00) # N ] self.triSrc=[ # # Faces (QUAD/TRIPAIR) # using RHR vertex ordering for # correct face surface normals # # front: EFHG/EFH+HGE # rear: CABD/CAB+BDC # left: CDFE/CDF+FEC # right: GHJI/GHJ+JIG # upper: KEGL/KEG+GLK # lower: FMNH/FMN+NHF # "EFH","HGE", "CAB","BDC", "CDF","FEC", "GHJ","JIG", "KEG","GLK", "FMN","NHF" ] # # setup cube # # one cube node will be generated per non-air block # since different block id's potentially refer to # different textures and thus must be separate nodes # of the scene graph. # # 1. vertices self.cubeVtx=GeomVertexData('blockCube',GeomVertexFormat.getV3t2(),Geom.UHStatic) self.cubeVtx.setNumRows(len(self.cubeVtxSrc)) vtxW=GeomVertexWriter(self.cubeVtx,'vertex') txcW=GeomVertexWriter(self.cubeVtx,'texcoord') for vertex in self.cubeVtxSrc: vtxW.addData3f(*vertex[0:3]) txcW.addData2f(*vertex[3:5]) # 2. mesh self.cubeMesh=GeomTriangles(Geom.UHStatic) for tri in self.triSrc: for triV in tri: triVtxId=ord(triV)-65 # changea 'A'-'N' to 0-13 self.cubeMesh.addVertex(triVtxId) self.cubeMesh.close_primitive() # 3. geometry (primitive+vertex pair) self.cubeGeom=Geom(self.cubeVtx) self.cubeGeom.addPrimitive(self.cubeMesh) def setViewpoint(x,y,z): self.viewPoint=(x,y,z) @decompress def Chunk2Geom(self,sChunk,origin): """ Decodes chunk into Panda3D geometry format @param sChunk: encoded chunk origin: chunk location in world as 3-tuple @return A panda3D Node object translated appropriately to place the decoded chunk correctly within the world. """ # determine where chunk should be placed in world space orgX=origin[0] orgY=origin[1] orgZ=origin[2] # determine chunk's coordinate chunkX=orgX/16 chunkY=orgY/16 # generate name tags for the various parts chunkId="%s_%s" % (chunkX,chunkY) vtxName="vtx_%s" % chunkId nodeName="chunk_%s" % chunkId # create empty node for entire chunk chunkNode=render.attachNewNode(nodeName) # convert string chunk to numeric chunk=[ord(c) for c in sChunk] # TODO: read chunk data and generate cube nodes flags=chunk[0]+(chunk[1]<<8)+(chunk[2]<<16)+(chunk[3]<<24) chunk=chunk[4:] # remove biome/flagbits for cY in range(16): for cX in range(16): for cZ in range(256): cell=cZ+(cX<<8)+(cY<<12) # lookup neighbours me=chunk[cell] if me>0: n_up=chunk[cell-1] if cZ>0 else 0 n_dn=chunk[cell+1] if cZ<255 else 0 n_lt=chunk[cell-256] if cX>0 else 0 n_rt=chunk[cell+256] if cX<15 else 0 n_bk=chunk[cell-4096] if cY>0 else 0 n_fd=chunk[cell+4096] if cY<15 else 0 if n_up==0 or n_dn==0 or \ n_lt==0 or n_rt==0 or \ n_bk==0 or n_fd==0: # for any non-obscured block # generate a cube block=GeomNode("%s_block_%s_%s_%s"%(nodeName,cX,cY,cZ)) block.addGeom(self.cubeGeom) blockNode=chunkNode.attachNewNode(block) blockNode.setTexture(self.textures[me]) blockNode.setPos(cX,cY,cZ) chunkNode.setPos(chunkX*16,chunkY*16,-64) return chunkNode
class Track3d(object): ''' Generate the 3d Mesh out of the StreetData and the 2dTrack ''' def __init__(self, res, x, y, z=200, player_count=1, street_data=""): ''' ''' self._notify = DirectNotify().newCategory("TrackGen3D") self._notify.info("New Track3D-Object created: %s" % (self)) # street_data = (Vec2(4.0,4.0), Vec2(10.0,10.0), Vec2(10.0,0.0), Vec2(4.0,0.0), Vec2(0.0,-1.0)) # street_data = StreetData(Vec2(15.0,1.0), Vec2(15.0,-5.0), Vec2(0.0,-5.0), mirrored=True) #, Vec2(15.0,0.0) self.street_data = StreetData() # self.street_data.readFile("data/road/road01.xml") # self.street_data.readFile("data/road/tube.xml") if street_data == "": datas = ["road01", "tube"] street_data = datas[random.randint(0, len(datas) - 1)] self.street_data.readFile("data/road/" + street_data + ".xml") self.streetTextrange = 0.0 self.track = Track(x, y, z) self.track.generateTestTrack(player_count) # self.track.generateTrack(player_count) self.track_points = self.track.getInterpolatedPoints(res) self.varthickness = [] # Generate the Vector for thickness of the road self.generateNormals() # for i in range(len(self.track_points)-1): # if i == 0: # self.varthickness.append(self.calcTheVector(self.track_points[i],self.track_points[i],self.track_points[i+1])) #First # continue # self.varthickness.append(self.calcTheVector(self.track_points[i-1],self.track_points[i],self.track_points[i+1])) # self.varthickness.append(self.calcTheVector(self.track_points[len(self.track_points)-2],self.track_points[len(self.track_points)-1],self.track_points[len(self.track_points)-1])) #Last ## # Normalizing the Vector # for i in self.varthickness: # i.normalize() ## # print self.varthickness[-1] # print self.varthickness[0] # print self.varthickness[1] # print self.varthickness[2] # Spin the last 100 Points a litte bit to Vec3(-1,0,0) for i in range(-100, 1): # print self.varthickness[i] * (-i / 100), self.varthickness[i] , ((i* -1) / 100.0), i # print ((i* -1) / 100.0), self.varthickness[i], self.varthickness[i] * ((i* -1) / 100.0) self.varthickness[i] = self.varthickness[i] * (((i + 1) * -1) / 100.0) + Vec3(-1, 0, 0) self.normals[i] = self.normals[i] * (((i + 1) * -1) / 100.0) + Vec3(0, 0, 1) self.varthickness[i].normalize() self.normals[i].normalize() # print self.varthickness[i] # print self.varthickness[-1] # print self.varthickness[0] # print self.varthickness[1] # print self.varthickness[2] # print self.varthickness # for i in range(len(self.varthickness)): # if self.varthickness[i-1].almostEqual(self.varthickness[i], 0.3): # pass # else: # print "varthickness", self.varthickness[i-1], self.varthickness[i] # ------------------------------------------------------------------------------------- def resetWriters(self): ''' ''' self.vdata = GeomVertexData('street', GeomVertexFormat.getV3n3c4t2(), Geom.UHStatic) self.vertex = GeomVertexWriter(self.vdata, 'vertex') self.normal = GeomVertexWriter(self.vdata, 'normal') self.color = GeomVertexWriter(self.vdata, 'color') self.texcoord = GeomVertexWriter(self.vdata, 'texcoord') self.prim = GeomTriangles(Geom.UHStatic) # ------------------------------------------------------------------------------------- def calcTheVector(self, pre, now, past): vector1 = (pre[0] - now[0], pre[1] - now[1]) vector2 = (now[0] - past[0], now[1] - past[1]) high = pre[2] - past[2] return Vec3(((vector1[1] + vector2[1]) / 2.0), ((vector1[0] + vector2[0]) / 2.0), high) # ------------------------------------------------------------------------------------- def getVarthickness(self): return self.varthickness # ------------------------------------------------------------------------------------- def setTrackPoints(self, track_points): ''' ''' self.track_points = track_points def getTrackPoints(self): ''' ''' return self.track_points trackpoints = property(fget=getTrackPoints, fset=setTrackPoints) # ------------------------------------------------------------------------------------- def generateNormals(self): ''' ''' self.varthickness = [] self.normals = [] last_normal = Vec3(0, 0, 1) # last_vec = Vec3(0, 1, 0) for i in range(len(self.track_points)): if i == 0: vec = self.track_points[0] - self.track_points[1] elif i + 1 == len(self.track_points): vec = self.track_points[i - 1] - self.track_points[0] else: vec = self.track_points[i - 1] - self.track_points[i + 1] # calculate here the direction out of the street vector and the last normal last_normal.normalize() vec.normalize() mat = Mat3() mat.setRotateMat(-90, last_normal) # turn the direction around the last_normal turned_vec = mat.xform(vec) turned_vec.normalize() last_normal = turned_vec.cross(vec) # calculate the new normal turned_vec.normalize() self.varthickness.append(turned_vec) self.normals.append(last_normal) # ------------------------------------------------------------------------------------- def createVertices(self, track_points=None, street_data=None): ''' ''' if track_points is None: track_points = self.track_points if street_data is None: street_data = self.street_data self.resetWriters() texcoordinates = [] # street_data_length = len(street_data) texcoordinates = street_data.getTexCoordinates() tracklength = self.track.getLength() tex_step = 0.006 * (0.0002 * tracklength) for i in range(len(track_points)): turned_vec = self.varthickness[i] last_normal = self.normals[i] j = 0 for shapedot in street_data: # this is like a layer in 3d [Ebenengleichung] # vec = vec + vec*scalar + vec*scalar # this is used to transform the 2d-Streetshape to 3d point = track_points[i] + (turned_vec * shapedot[0]) + (last_normal * shapedot[1]) self.vertex.addData3f(point[0], point[1], point[2]) self.normal.addData3f(0, 0, 1) # KA how to calc self.streetTextrange += tex_step self.texcoord.addData2f(texcoordinates[j], self.streetTextrange) j += 1 # ------------------------------------------------------------------------------------- def connectVertices(self, street_data): # param j = len(street_Data) j = len(street_data) for i in range(self.vdata.getNumRows() - (j)): # -j?????? oder +-1 if (i + 1) % j != 0: self.prim.addVertex(i) self.prim.addVertex(i + 1) self.prim.addVertex(i + j + 1) self.prim.closePrimitive() self.prim.addVertex(i) self.prim.addVertex(i + j + 1) self.prim.addVertex(i + j) self.prim.closePrimitive() else: # close mesh's bottom side self.prim.addVertex(i + 1 - j) self.prim.addVertex(i + 1) self.prim.addVertex(i) self.prim.closePrimitive() self.prim.addVertex(i) self.prim.addVertex(i + 1) self.prim.addVertex(i + j) self.prim.closePrimitive() # close start and end k = self.vdata.getNumRows() - j for i in range(j): if (i + 1) % j != 0: self.prim.addVertex(i) self.prim.addVertex(i + k + 1) self.prim.addVertex(i + 1) self.prim.closePrimitive() self.prim.addVertex(i) self.prim.addVertex(i + k) self.prim.addVertex(i + k + 1) self.prim.closePrimitive() else: # close mesh's bottom side self.prim.addVertex(i) self.prim.addVertex(i + k - j + 1) self.prim.addVertex(i - j + 1) self.prim.closePrimitive() self.prim.addVertex(i) self.prim.addVertex(i + k) self.prim.addVertex(i + k - j + 1) self.prim.closePrimitive() # ------------------------------------------------------------------------------------- def createRoadMesh(self): ''' ''' # Creating the Vertex self.createVertices() # Connect the Vertex self.connectVertices(self.street_data) geom = Geom(self.vdata) geom.addPrimitive(self.prim) node = GeomNode('street') node.addGeom(geom) # nodePath = self.render.attachNewNode(node) return node # ------------------------------------------------------------------------------------- def createUninterpolatedRoadMesh(self): ''' ''' # Creating the Vertex self.createVertices(self.track.getPoints(), self.street_data) # Connect the Vertex self.connectVertices(self.street_data) geom = Geom(self.vdata) geom.addPrimitive(self.prim) node = GeomNode('street') node.addGeom(geom) # nodePath = self.render.attachNewNode(node) return node # ------------------------------------------------------------------------------------- def createBorderLeftMesh(self): ''' ''' # Creating the Vertex self.createVertices(self.track_points, self.street_data.border_l) # Connect the Vertex self.connectVertices(self.street_data.border_l) geom = Geom(self.vdata) geom.addPrimitive(self.prim) node = GeomNode('border_l') node.addGeom(geom) # nodePath = self.render.attachNewNode(node) return node # ------------------------------------------------------------------------------------- def createBorderRightMesh(self): ''' ''' # Creating the Vertex self.createVertices(self.track_points, self.street_data.border_r) # Connect the Vertex self.connectVertices(self.street_data.border_r) geom = Geom(self.vdata) geom.addPrimitive(self.prim) node = GeomNode('border_r') node.addGeom(geom) # nodePath = self.render.attachNewNode(node) return node # ------------------------------------------------------------------------------------- def createBorderLeftCollisionMesh(self): ''' ''' # Creating the Vertex self.createVertices(self.track_points, self.street_data.border_l_coll) # Connect the Vertex self.connectVertices(self.street_data.border_l_coll) geom = Geom(self.vdata) geom.addPrimitive(self.prim) node = GeomNode('border_l_coll') node.addGeom(geom) # nodePath = self.render.attachNewNode(node) return node # ------------------------------------------------------------------------------------- def createBorderRightCollisionMesh(self): ''' ''' # Creating the Vertex self.createVertices(self.track_points, self.street_data.border_r_coll) # Connect the Vertex self.connectVertices(self.street_data.border_r_coll) geom = Geom(self.vdata) geom.addPrimitive(self.prim) node = GeomNode('border_r_coll') node.addGeom(geom) # nodePath = self.render.attachNewNode(node) return node
def makeTrack(segments): format = GeomVertexFormat.getV3t2() vdata = GeomVertexData('track', format, Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') texcoord = GeomVertexWriter(vdata, 'texcoord') vdata2 = GeomVertexData('startline', format, Geom.UHStatic) vertex2 = GeomVertexWriter(vdata2, 'vertex') texcoord2 = GeomVertexWriter(vdata2, 'texcoord') numVertices = 0 twat = 0 for segmentIndex in range(len(segments)): currentSegment = segments[segmentIndex] if segmentIndex == 0: nextSegment = segments[segmentIndex + 1] currentStartLineLeftPoint = Geometry.Point.CreateByInterpolation( currentSegment.getMidPoint(), currentSegment.getLeftPoint(), 100, 77.5) currentStartLineRightPoint = Geometry.Point.CreateByInterpolation( currentSegment.getMidPoint(), currentSegment.getRightPoint(), 100, 77.5) nextStartLineLeftPoint = Geometry.Point.CreateByInterpolation( nextSegment.getMidPoint(), nextSegment.getLeftPoint(), 100, 77.5) nextStartLineRightPoint = Geometry.Point.CreateByInterpolation( nextSegment.getMidPoint(), nextSegment.getRightPoint(), 100, 77.5) vertex2.addData3f(nextStartLineLeftPoint.getX(), nextStartLineLeftPoint.getY(), 0) vertex2.addData3f(nextStartLineRightPoint.getX(), nextStartLineRightPoint.getY(), 0) vertex2.addData3f(currentStartLineLeftPoint.getX(), currentStartLineLeftPoint.getY(), 0) vertex2.addData3f(currentStartLineRightPoint.getX(), currentStartLineRightPoint.getY(), 0) texcoord2.addData2f(0.0, 0.0) texcoord2.addData2f(12.0, 0.0) texcoord2.addData2f(0.0, 2.0) texcoord2.addData2f(12.0, 2.0) vertex.addData3f(currentSegment.leftPoint.getX(), currentSegment.leftPoint.getY(), 0) vertex.addData3f(currentSegment.rightPoint.getX(), currentSegment.rightPoint.getY(), 0) texcoord.addData2f(0.0, 1.0 / 8 * float(twat)) texcoord.addData2f(1.0, 1.0 / 8 * float(twat)) #print('vertex at '+str(segmentIndex)+' with v='+str(1.0/8*float(twat))) numVertices += 2 twat = twat + 1 if twat == 9: #print('surplus at '+str(segmentIndex)+' with v=0.0') vertex.addData3f(currentSegment.leftPoint.getX(), currentSegment.leftPoint.getY(), 0) vertex.addData3f(currentSegment.rightPoint.getX(), currentSegment.rightPoint.getY(), 0) texcoord.addData2f(0.0, 0.0) texcoord.addData2f(1.0, 0.0) numVertices += 2 twat = 1 #print('vertices created: '+str(numVertices)) tris = GeomTriangles(Geom.UHDynamic) bob = 0 vertexOffset = 0 for index in range(len(segments)): vertexOffsets = [] for innerIndex in range(4): offset = vertexOffset + innerIndex if offset > numVertices - 1: offset = offset - numVertices vertexOffsets.append(offset) tris.addVertex(vertexOffsets[0]) tris.addVertex(vertexOffsets[1]) tris.addVertex(vertexOffsets[3]) tris.closePrimitive() tris.addVertex(vertexOffsets[0]) tris.addVertex(vertexOffsets[3]) tris.addVertex(vertexOffsets[2]) tris.closePrimitive() vertexOffset += 2 bob += 1 if bob == 8: vertexOffset += 2 bob = 0 tris2 = GeomTriangles(Geom.UHDynamic) tris2.addVertex(0) tris2.addVertex(3) tris2.addVertex(1) tris2.closePrimitive() tris2.addVertex(0) tris2.addVertex(2) tris2.addVertex(3) tris2.closePrimitive() track = Geom(vdata) track.addPrimitive(tris) startLine = Geom(vdata2) startLine.addPrimitive(tris2) snode = GeomNode('tarmac') snode.addGeom(track) snode2 = GeomNode('startline') snode2.addGeom(startLine) returnValues = {"tarmac": snode, "startline": snode2} return returnValues return snode
def makeMountains(): format = GeomVertexFormat.getV3t2() vdata = GeomVertexData('mountains', format, Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') texcoord = GeomVertexWriter(vdata, 'texcoord') format2 = GeomVertexFormat.getV3c4() vdata2 = GeomVertexData('sky', format2, Geom.UHStatic) vertex2 = GeomVertexWriter(vdata2, 'vertex') color2 = GeomVertexWriter(vdata2, 'color') numQuads = 32 angle = 0 textureX = 0 angleAdd = math.pi * 2 / numQuads textureXAdd = 1.0 / numQuads currentQuad = 0 numVertices = 0 while currentQuad < numQuads: if currentQuad == 0: vertexX = math.sin(angle) * 3000 vertexY = math.cos(angle) * 3000 vertex.addData3f(vertexX, vertexY, 0.0) vertex.addData3f(vertexX, vertexY, 360.0) texcoord.addData2f(1.0, 0.0) texcoord.addData2f(1.0, 1.0) numVertices = numVertices + 2 vertexX = math.sin(angle) * 3000 vertexY = math.cos(angle) * 3000 vertex.addData3f(vertexX, vertexY, 0.0) vertex.addData3f(vertexX, vertexY, 360.0) vertex2.addData3f(vertexX, vertexY, 360.0) color2.addData4f(45.0 / 255.0, 112.0 / 255.0, 255.0 / 255.0, 1.0) #color2.addData4f(1.0,1.0,1.0,1.0) #print('created vertex at '+str(vertexX)+','+str(vertexY)+',2') #print('created vertex at '+str(vertexX)+','+str(vertexY)+',0') texcoord.addData2f(textureX, 0.0) texcoord.addData2f(textureX, 1.0) #print('texturex is '+str(textureX)) #print('creating vertices v'+str(numVertices)+' and v'+str(numVertices+1)) numVertices = numVertices + 2 currentQuad = currentQuad + 1 textureX = textureX + textureXAdd angle = angle + angleAdd vertex2.addData3f(0.0, 0.0, 360.0) #color2.addData4f(1.0,1.0,1.0,1.0) color2.addData4f(45.0 / 255.0, 112.0 / 255.0, 255.0 / 255.0, 1.0) currentQuad = 0 currentOffset = 2 tris = GeomTriangles(Geom.UHDynamic) #print('creating tris - numVertices is '+str(numVertices)) while currentQuad < numQuads: vertexOffsets = [] for innerIndex in range(4): offset = currentOffset + innerIndex #print('comparing '+str(offset)+' with '+str(numVertices-1)) if offset > numVertices - 1: offset = offset - numVertices vertexOffsets.append(offset) #print('adding tri connecting v'+str(vertexOffsets[0])+', v'+str(vertexOffsets[1])+', v'+str(vertexOffsets[3])) #print('adding tri connecting v'+str(vertexOffsets[1])+', v'+str(vertexOffsets[3])+', v'+str(vertexOffsets[2])) tris.addVertex(vertexOffsets[0]) tris.addVertex(vertexOffsets[2]) tris.addVertex(vertexOffsets[1]) tris.closePrimitive() tris.addVertex(vertexOffsets[1]) tris.addVertex(vertexOffsets[2]) tris.addVertex(vertexOffsets[3]) tris.closePrimitive() currentOffset = currentOffset + 2 currentQuad = currentQuad + 1 tris2 = GeomTriangles(Geom.UHDynamic) currentOffset = 1 numTris = numQuads currentTri = 0 while currentTri < numTris: tris2.addVertex(numTris) tris2.addVertex(currentTri) if currentTri == numTris - 1: tris2.addVertex(0) else: tris2.addVertex(currentTri + 1) tris2.closePrimitive() currentTri = currentTri + 1 mountains = Geom(vdata) mountains.addPrimitive(tris) sky = Geom(vdata2) sky.addPrimitive(tris2) snode = GeomNode('mountains') snode.addGeom(mountains) snode2 = GeomNode('sky') snode2.addGeom(sky) returnValues = {"mountains": snode, "sky": snode2} return returnValues
class Chunk2GeomDecoder: """ Chunk decoder that decodes chunks to Panda3D Geoms/Nodes """ def __init__(self): self.textures = {} for k in texMap: v = texMap[k] tex = loader.loadTexture("gfx/%s" % v) tex.setWrapU(Texture.WM_clamp) tex.setWrapV(Texture.WM_clamp) self.textures[k] = tex self.viewPoint = (0, 0, 0) self.cubeVtxSrc = [ # 14 vertices per cube mapped # so that UV coords utilize # the typical cube unwrap: # # #### <-- square texture # ##U# # BLFR # ##D# #=unused # # This form uses 14 vertices per cube since # the extra (worldspace overlapped) vertices # map different U,V coordinates at the same # worldspace coordinates, depending on face # # x y z u v (1.00, 1.00, 1.00, 0.00, 0.50), # A (1.00, 1.00, 0.00, 0.00, 0.25), # B (0.00, 1.00, 1.00, 0.25, 0.50), # C (0.00, 1.00, 0.00, 0.25, 0.25), # D (0.00, 0.00, 1.00, 0.50, 0.50), # E (0.00, 0.00, 0.00, 0.50, 0.25), # F (1.00, 0.00, 1.00, 0.75, 0.50), # G (1.00, 0.00, 0.00, 0.75, 0.25), # H (1.00, 1.00, 1.00, 1.00, 0.50), # I (1.00, 1.00, 0.00, 1.00, 0.25), # J (0.00, 1.00, 1.00, 0.50, 0.75), # K (1.00, 1.00, 1.00, 0.75, 0.75), # L (0.00, 1.00, 0.00, 0.50, 0.00), # M (1.00, 1.00, 0.00, 0.75, 0.00) # N ] self.triSrc = [ # # Faces (QUAD/TRIPAIR) # using RHR vertex ordering for # correct face surface normals # # front: EFHG/EFH+HGE # rear: CABD/CAB+BDC # left: CDFE/CDF+FEC # right: GHJI/GHJ+JIG # upper: KEGL/KEG+GLK # lower: FMNH/FMN+NHF # "EFH", "HGE", "CAB", "BDC", "CDF", "FEC", "GHJ", "JIG", "KEG", "GLK", "FMN", "NHF" ] # # setup cube # # one cube node will be generated per non-air block # since different block id's potentially refer to # different textures and thus must be separate nodes # of the scene graph. # # 1. vertices self.cubeVtx = GeomVertexData('blockCube', GeomVertexFormat.getV3t2(), Geom.UHStatic) self.cubeVtx.setNumRows(len(self.cubeVtxSrc)) vtxW = GeomVertexWriter(self.cubeVtx, 'vertex') txcW = GeomVertexWriter(self.cubeVtx, 'texcoord') for vertex in self.cubeVtxSrc: vtxW.addData3f(*vertex[0:3]) txcW.addData2f(*vertex[3:5]) # 2. mesh self.cubeMesh = GeomTriangles(Geom.UHStatic) for tri in self.triSrc: for triV in tri: triVtxId = ord(triV) - 65 # changea 'A'-'N' to 0-13 self.cubeMesh.addVertex(triVtxId) self.cubeMesh.close_primitive() # 3. geometry (primitive+vertex pair) self.cubeGeom = Geom(self.cubeVtx) self.cubeGeom.addPrimitive(self.cubeMesh) def setViewpoint(x, y, z): self.viewPoint = (x, y, z) @decompress def Chunk2Geom(self, sChunk, origin): """ Decodes chunk into Panda3D geometry format @param sChunk: encoded chunk origin: chunk location in world as 3-tuple @return A panda3D Node object translated appropriately to place the decoded chunk correctly within the world. """ # determine where chunk should be placed in world space orgX = origin[0] orgY = origin[1] orgZ = origin[2] # determine chunk's coordinate chunkX = orgX / 16 chunkY = orgY / 16 # generate name tags for the various parts chunkId = "%s_%s" % (chunkX, chunkY) vtxName = "vtx_%s" % chunkId nodeName = "chunk_%s" % chunkId # create empty node for entire chunk chunkNode = render.attachNewNode(nodeName) # convert string chunk to numeric chunk = [ord(c) for c in sChunk] # TODO: read chunk data and generate cube nodes flags = chunk[0] + (chunk[1] << 8) + (chunk[2] << 16) + ( chunk[3] << 24) chunk = chunk[4:] # remove biome/flagbits for cY in range(16): for cX in range(16): for cZ in range(256): cell = cZ + (cX << 8) + (cY << 12) # lookup neighbours me = chunk[cell] if me > 0: n_up = chunk[cell - 1] if cZ > 0 else 0 n_dn = chunk[cell + 1] if cZ < 255 else 0 n_lt = chunk[cell - 256] if cX > 0 else 0 n_rt = chunk[cell + 256] if cX < 15 else 0 n_bk = chunk[cell - 4096] if cY > 0 else 0 n_fd = chunk[cell + 4096] if cY < 15 else 0 if n_up==0 or n_dn==0 or \ n_lt==0 or n_rt==0 or \ n_bk==0 or n_fd==0: # for any non-obscured block # generate a cube block = GeomNode("%s_block_%s_%s_%s" % (nodeName, cX, cY, cZ)) block.addGeom(self.cubeGeom) blockNode = chunkNode.attachNewNode(block) blockNode.setTexture(self.textures[me]) blockNode.setPos(cX, cY, cZ) chunkNode.setPos(chunkX * 16, chunkY * 16, -64) return chunkNode
def __init__(self, center: Vec3, width, height, x_grid, y_grid, refinement_level=1, is_hill=True, map_style="pointy"): Hexagon.counter += 1 self.vx_center: Vec3 = center self.refinement_level = refinement_level self.width = width self.height = height self.x_grid = x_grid self.y_grid = y_grid self.adjacent_hexagons = [] if map_style == "pointy": self.adjacent_hexagons = [ Orientation.North, Orientation.NorthEast, Orientation.SouthEast, Orientation.South, Orientation.SouthWest, Orientation.NorthWest ] # self.master_point_indices: List[int] = [] # self.ref_points_indices: List[List[int]] = [] self.triangulation: List[GeomTriangles] = [] self.ref_points_indices: List[List[int]] = [] self.connector_points_indices: List[int] = [] from src.world.hexmap import MapPointMngr # print(f"ref level ({self.x_grid}|{self.y_grid}): {refinement_level}") # build up the vertices for i in range(refinement_level + 1): z_elevation = self.get_elevation(i) if i == 0: # center point uv_map = self.get_uv_mapping(z_elevation, self.vx_center, None, i) idx = MapPointMngr.instance().add_critical_point( (self.vx_center.x, self.vx_center.y, z_elevation), self.x_grid, self.y_grid, uv_map) self.ref_points_indices.append([idx]) continue self.ref_points_indices.append([]) fac = self.get_offset_factor(i) for o in self.adjacent_hexagons: p = self.get_vector(o, size=fac) + self.vx_center.xy n = self.get_normal(o, z_elevation) uv_map = self.get_uv_mapping(z_elevation, p, o, i) idx = MapPointMngr.instance().add_critical_point( (p.x, p.y, z_elevation), self.x_grid, self.y_grid, uv_map, n=n) self.ref_points_indices[i].append(idx) # construct geometric triangulation # print(self.ref_points_indices) for i in range(refinement_level): if i == 0: for k in range(1, 7, 1): t = GeomTriangles(Geom.UHStatic) t.addVertex(self.ref_points_indices[i][0]) t.addVertex(self.ref_points_indices[i + 1][k % 6]) t.addVertex(self.ref_points_indices[i + 1][(k + 1) % 6]) t.closePrimitive() self.triangulation.append(t) else: t = GeomTristrips(Geom.UHStatic) for k in range(7): t.addVertex(self.ref_points_indices[i][k % 6]) t.addVertex(self.ref_points_indices[i + 1][k % 6]) t.closePrimitive() # aat.decompose() self.triangulation.append(t)
def makeTrack(segments): format=GeomVertexFormat.getV3t2() vdata=GeomVertexData('track', format, Geom.UHStatic) vertex=GeomVertexWriter(vdata, 'vertex') texcoord=GeomVertexWriter(vdata, 'texcoord') vdata2=GeomVertexData('startline', format, Geom.UHStatic) vertex2=GeomVertexWriter(vdata2, 'vertex') texcoord2=GeomVertexWriter(vdata2, 'texcoord') numVertices=0 twat=0 for segmentIndex in range(len(segments)): currentSegment=segments[segmentIndex] if segmentIndex==0: nextSegment=segments[segmentIndex+1] currentStartLineLeftPoint=Geometry.Point.CreateByInterpolation(currentSegment.getMidPoint(),currentSegment.getLeftPoint(),100,77.5); currentStartLineRightPoint=Geometry.Point.CreateByInterpolation(currentSegment.getMidPoint(),currentSegment.getRightPoint(),100,77.5); nextStartLineLeftPoint=Geometry.Point.CreateByInterpolation(nextSegment.getMidPoint(),nextSegment.getLeftPoint(),100,77.5); nextStartLineRightPoint=Geometry.Point.CreateByInterpolation(nextSegment.getMidPoint(),nextSegment.getRightPoint(),100,77.5); vertex2.addData3f(nextStartLineLeftPoint.getX(),nextStartLineLeftPoint.getY(),0) vertex2.addData3f(nextStartLineRightPoint.getX(),nextStartLineRightPoint.getY(),0) vertex2.addData3f(currentStartLineLeftPoint.getX(),currentStartLineLeftPoint.getY(),0) vertex2.addData3f(currentStartLineRightPoint.getX(),currentStartLineRightPoint.getY(),0) texcoord2.addData2f(0.0, 0.0) texcoord2.addData2f(12.0, 0.0) texcoord2.addData2f(0.0, 2.0) texcoord2.addData2f(12.0, 2.0) vertex.addData3f(currentSegment.leftPoint.getX(),currentSegment.leftPoint.getY(),0) vertex.addData3f(currentSegment.rightPoint.getX(),currentSegment.rightPoint.getY(),0) texcoord.addData2f(0.0, 1.0/8*float(twat)) texcoord.addData2f(1.0, 1.0/8*float(twat)) #print('vertex at '+str(segmentIndex)+' with v='+str(1.0/8*float(twat))) numVertices+=2 twat=twat+1 if twat==9: #print('surplus at '+str(segmentIndex)+' with v=0.0') vertex.addData3f(currentSegment.leftPoint.getX(),currentSegment.leftPoint.getY(),0) vertex.addData3f(currentSegment.rightPoint.getX(),currentSegment.rightPoint.getY(),0) texcoord.addData2f(0.0, 0.0) texcoord.addData2f(1.0, 0.0) numVertices+=2 twat=1 #print('vertices created: '+str(numVertices)) tris=GeomTriangles(Geom.UHDynamic) bob=0 vertexOffset=0 for index in range(len(segments)): vertexOffsets=[] for innerIndex in range(4): offset=vertexOffset+innerIndex if offset>numVertices-1: offset=offset-numVertices vertexOffsets.append(offset) tris.addVertex(vertexOffsets[0]) tris.addVertex(vertexOffsets[1]) tris.addVertex(vertexOffsets[3]) tris.closePrimitive() tris.addVertex(vertexOffsets[0]) tris.addVertex(vertexOffsets[3]) tris.addVertex(vertexOffsets[2]) tris.closePrimitive() vertexOffset+=2 bob+=1 if bob==8: vertexOffset+=2 bob=0 tris2=GeomTriangles(Geom.UHDynamic) tris2.addVertex(0) tris2.addVertex(3) tris2.addVertex(1) tris2.closePrimitive() tris2.addVertex(0) tris2.addVertex(2) tris2.addVertex(3) tris2.closePrimitive() track=Geom(vdata) track.addPrimitive(tris) startLine=Geom(vdata2) startLine.addPrimitive(tris2) snode=GeomNode('tarmac') snode.addGeom(track) snode2=GeomNode('startline') snode2.addGeom(startLine) returnValues={"tarmac":snode,"startline":snode2} return returnValues return snode
def makeSquare(x1,y1,z1, x2,y2,z2): format=GeomVertexFormat.getV3n3cpt2() vdata=GeomVertexData('square', format, Geom.UHDynamic) vertex=GeomVertexWriter(vdata, 'vertex') normal=GeomVertexWriter(vdata, 'normal') color=GeomVertexWriter(vdata, 'color') texcoord=GeomVertexWriter(vdata, 'texcoord') #make sure we draw the sqaure in the right plane if x1!=x2: vertex.addData3f(x1, y1, z1) vertex.addData3f(x2, y1, z1) vertex.addData3f(x2, y2, z2) vertex.addData3f(x1, y2, z2) normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y1-1, 2*z1-1))) normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y1-1, 2*z1-1))) normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y2-1, 2*z2-1))) normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y2-1, 2*z2-1))) else: vertex.addData3f(x1, y1, z1) vertex.addData3f(x2, y2, z1) vertex.addData3f(x2, y2, z2) vertex.addData3f(x1, y1, z2) normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y1-1, 2*z1-1))) normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y2-1, 2*z1-1))) normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y2-1, 2*z2-1))) normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y1-1, 2*z2-1))) #adding different colors to the vertex for visibility color.addData4f(1.0,0.0,0.0,1.0) color.addData4f(0.0,1.0,0.0,1.0) color.addData4f(0.0,0.0,1.0,1.0) color.addData4f(1.0,0.0,1.0,1.0) texcoord.addData2f(0.0, 1.0) texcoord.addData2f(0.0, 0.0) texcoord.addData2f(1.0, 0.0) texcoord.addData2f(1.0, 1.0) #quads arent directly supported by the Geom interface #you might be interested in the CardMaker class if you are #interested in rectangle though tri1=GeomTriangles(Geom.UHDynamic) tri2=GeomTriangles(Geom.UHDynamic) tri1.addVertex(0) tri1.addVertex(1) tri1.addVertex(3) tri2.addConsecutiveVertices(1,3) tri1.closePrimitive() tri2.closePrimitive() square=Geom(vdata) square.addPrimitive(tri1) square.addPrimitive(tri2) return square
def __init__(self, position, gridshape): self.geomnode = GeomNode("rhomdo") super().__init__(self.geomnode) z, y, x = position depth, height, width = gridshape self.color = (x / width), (y / height), (z / depth), 1 format = GeomVertexFormat.getV3c4() vdata = GeomVertexData("vertices", format, Geom.UHStatic) vdata.setNumRows(14) vertexWriter = GeomVertexWriter(vdata, "vertex") colorWriter = GeomVertexWriter(vdata, "color") for i in range(14): colorWriter.addData4f(*self.color) self.color = max(self.color[0] - .03, 0), max(self.color[1] - .03, 0), max(self.color[2] - .03, 0), 1 realX = x * 2 - width realY = y * 2 - height realZ = z * SR2 - SR2 * .5 * depth odd = z % 2 == 1 if odd: realX += 1 realY += 1 vertexWriter.addData3f(realX, realY, realZ + SR2) vertexWriter.addData3f(realX - 1, realY, realZ + (SR2 / 2)) vertexWriter.addData3f(realX, realY - 1, realZ + (SR2 / 2)) vertexWriter.addData3f(realX + 1, realY, realZ + (SR2 / 2)) vertexWriter.addData3f(realX, realY + 1, realZ + (SR2 / 2)) vertexWriter.addData3f(realX - 1, realY - 1, realZ) vertexWriter.addData3f(realX + 1, realY - 1, realZ) vertexWriter.addData3f(realX + 1, realY + 1, realZ) vertexWriter.addData3f(realX - 1, realY + 1, realZ) vertexWriter.addData3f(realX - 1, realY, realZ - (SR2 / 2)) vertexWriter.addData3f(realX, realY - 1, realZ - (SR2 / 2)) vertexWriter.addData3f(realX + 1, realY, realZ - (SR2 / 2)) vertexWriter.addData3f(realX, realY + 1, realZ - (SR2 / 2)) vertexWriter.addData3f(realX, realY, realZ - SR2) # step 2) make primitives and assign vertices to them tris = GeomTriangles(Geom.UHStatic) # top tris.addVertex(0) tris.addVertex(1) tris.addVertex(2) tris.closePrimitive() tris.addVertex(0) tris.addVertex(2) tris.addVertex(3) tris.closePrimitive() tris.addVertex(0) tris.addVertex(3) tris.addVertex(4) tris.closePrimitive() tris.addVertex(0) tris.addVertex(4) tris.addVertex(1) tris.closePrimitive() tris.addVertex(1) tris.addVertex(5) tris.addVertex(2) tris.closePrimitive() tris.addVertex(2) tris.addVertex(6) tris.addVertex(3) tris.closePrimitive() tris.addVertex(3) tris.addVertex(7) tris.addVertex(4) tris.closePrimitive() tris.addVertex(4) tris.addVertex(8) tris.addVertex(1) tris.closePrimitive() # middle tris.addVertex(1) tris.addVertex(8) tris.addVertex(9) tris.closePrimitive() tris.addVertex(1) tris.addVertex(9) tris.addVertex(5) tris.closePrimitive() tris.addVertex(2) tris.addVertex(5) tris.addVertex(10) tris.closePrimitive() tris.addVertex(2) tris.addVertex(10) tris.addVertex(6) tris.closePrimitive() tris.addVertex(3) tris.addVertex(6) tris.addVertex(11) tris.closePrimitive() tris.addVertex(3) tris.addVertex(11) tris.addVertex(7) tris.closePrimitive() tris.addVertex(4) tris.addVertex(7) tris.addVertex(12) tris.closePrimitive() tris.addVertex(4) tris.addVertex(12) tris.addVertex(8) tris.closePrimitive() # bottom tris.addVertex(5) tris.addVertex(9) tris.addVertex(10) tris.closePrimitive() tris.addVertex(6) tris.addVertex(10) tris.addVertex(11) tris.closePrimitive() tris.addVertex(7) tris.addVertex(11) tris.addVertex(12) tris.closePrimitive() tris.addVertex(8) tris.addVertex(12) tris.addVertex(9) tris.closePrimitive() tris.addVertex(9) tris.addVertex(13) tris.addVertex(10) tris.closePrimitive() tris.addVertex(10) tris.addVertex(13) tris.addVertex(11) tris.closePrimitive() tris.addVertex(11) tris.addVertex(13) tris.addVertex(12) tris.closePrimitive() tris.addVertex(12) tris.addVertex(13) tris.addVertex(9) tris.closePrimitive() rhomGeom = Geom(vdata) rhomGeom.addPrimitive(tris) self.geomnode.addGeom(rhomGeom)
def __heightmap_primitive(self): n1 = self.__n + 1 n12 = n1 * n1 prim = GeomTriangles(Geom.UHStatic) for x in range(3): for s in range(2): for i in range(self.__n): for j in range(self.__n): if (s == 0): prim.addVertex(x * 2 * n12 + s * n12 + (j + 0) * n1 + i + 0) prim.addVertex(x * 2 * n12 + s * n12 + (j + 0) * n1 + i + 1) prim.addVertex(x * 2 * n12 + s * n12 + (j + 1) * n1 + i + 1) prim.addVertex(x * 2 * n12 + s * n12 + (j + 0) * n1 + i + 0) prim.addVertex(x * 2 * n12 + s * n12 + (j + 1) * n1 + i + 1) prim.addVertex(x * 2 * n12 + s * n12 + (j + 1) * n1 + i + 0) else: prim.addVertex(x * 2 * n12 + s * n12 + (j + 0) * n1 + i + 0) prim.addVertex(x * 2 * n12 + s * n12 + (j + 1) * n1 + i + 1) prim.addVertex(x * 2 * n12 + s * n12 + (j + 0) * n1 + i + 1) prim.addVertex(x * 2 * n12 + s * n12 + (j + 0) * n1 + i + 0) prim.addVertex(x * 2 * n12 + s * n12 + (j + 1) * n1 + i + 0) prim.addVertex(x * 2 * n12 + s * n12 + (j + 1) * n1 + i + 1) return prim
def connect(self, other: Hexagon): from src.world.hexmap import MapPointMngr direction = self.vx_center - other.vx_center direction.normalize() print(f"Performing connection algorithm : dir {direction}") print(Orientation.get_orient_from_normal_vec2(direction)) facing: Orientation = Orientation.get_orient_from_normal_vec2( direction) v1_orient = None v2_orient = None rotation = None a = 0 if facing is Orientation.West: v1_orient = Orientation.NorthEast v2_orient = Orientation.SouthEast rotation = 0 a = 0 elif facing is Orientation.East: v1_orient = Orientation.NorthWest v2_orient = Orientation.SouthWest rotation = 3 a = 1 # elif facing is Orientation.NorthEast: # v1_orient = Orientation.SouthWest # v2_orient = Orientation.South # rotation = 3 # a = 0 # elif facing is Orientation.SouthWest: # v1_orient = Orientation.North # v2_orient = Orientation.NorthEast # rotation = 3 # a = 1 current_lvl = 3 v1 = self.get_vector(v1_orient, size=self.get_offset_factor(current_lvl)) v2 = self.get_vector(v2_orient, size=self.get_offset_factor(current_lvl)) v1 = v1 + self.vx_center.xy v2 = v2 + self.vx_center.xy z_elevation = self.get_elevation(0) # v1 uv_map = self.get_uv_mapping(z_elevation, v1, v1_orient, current_lvl) idx = MapPointMngr.instance().add_critical_point( (v1.x, v1.y, z_elevation), self.x_grid, self.y_grid, uv_map) self.connector_points_indices.append(idx) # v2 uv_map = self.get_uv_mapping(z_elevation, v2, v2_orient, current_lvl) idx = MapPointMngr.instance().add_critical_point( (v2.x, v2.y, z_elevation), self.x_grid, self.y_grid, uv_map) self.connector_points_indices.append(idx) t = GeomTriangles(Geom.UHStatic) t.addVertex(self.connector_points_indices[a % 2]) t.addVertex(self.ref_points_indices[1][1 + rotation]) t.addVertex(self.ref_points_indices[2][1 + rotation]) t.closePrimitive() self.triangulation.append(t) t = GeomTriangles(Geom.UHStatic) t.addVertex(self.connector_points_indices[a % 2]) t.addVertex(self.ref_points_indices[1][2 + rotation]) t.addVertex(self.ref_points_indices[1][1 + rotation]) t.closePrimitive() self.triangulation.append(t) t = GeomTriangles(Geom.UHStatic) t.addVertex(self.connector_points_indices[(a + 1) % 2]) t.addVertex(self.ref_points_indices[2][2 + rotation]) t.addVertex(self.ref_points_indices[1][2 + rotation]) t.closePrimitive() self.triangulation.append(t) t = GeomTriangles(Geom.UHStatic) t.addVertex(self.connector_points_indices[a % 2]) t.addVertex(self.connector_points_indices[(a + 1) % 2]) t.addVertex(self.ref_points_indices[1][2 + rotation]) t.closePrimitive() self.triangulation.append(t) t = GeomTriangles(Geom.UHStatic) t.addVertex(self.connector_points_indices[a % 2]) t.addVertex(self.ref_points_indices[2][1 + rotation]) t.addVertex(self.ref_points_indices[3][1 + rotation]) t.closePrimitive() self.triangulation.append(t) t = GeomTriangles(Geom.UHStatic) t.addVertex(self.connector_points_indices[(a + 1) % 2]) t.addVertex(self.ref_points_indices[3][2 + rotation]) t.addVertex(self.ref_points_indices[2][2 + rotation]) t.closePrimitive() self.triangulation.append(t)
def RingFaceGeometry(up, inner_radius, outer_radius, nbOfPoints): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('ring', format, Geom.UHStatic) vdata.unclean_set_num_rows(nbOfPoints) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') for i in range(nbOfPoints): angle = 2 * pi / nbOfPoints * i x = cos(angle) y = sin(angle) vertex.addData3f(outer_radius * x, outer_radius * y, 0) normal.addData3f(0, 0, up) color.addData4f(1, 1, 1, 1) texcoord.addData2f(1, 0) vertex.addData3f(inner_radius * x, inner_radius * y, 0) normal.addData3f(0, 0, up) color.addData4f(1, 1, 1, 1) texcoord.addData2f(0, 0) triangles = GeomTriangles(Geom.UHStatic) triangles.reserve_num_vertices(nbOfPoints - 1) for i in range(nbOfPoints - 1): if up < 0: triangles.addVertex(i * 2 + 0) triangles.addVertex(i * 2 + 1) triangles.addVertex(i * 2 + 2) triangles.closePrimitive() triangles.addVertex(i * 2 + 2) triangles.addVertex(i * 2 + 1) triangles.addVertex(i * 2 + 3) triangles.closePrimitive() else: triangles.addVertex(i * 2 + 2) triangles.addVertex(i * 2 + 1) triangles.addVertex(i * 2 + 0) triangles.closePrimitive() triangles.addVertex(i * 2 + 3) triangles.addVertex(i * 2 + 1) triangles.addVertex(i * 2 + 2) triangles.closePrimitive() if up < 0: triangles.addVertex((nbOfPoints - 1) * 2 + 0) triangles.addVertex((nbOfPoints - 1) * 2 + 1) triangles.addVertex(0) triangles.closePrimitive() triangles.addVertex(0) triangles.addVertex((nbOfPoints - 1) * 2 + 1) triangles.addVertex(1) triangles.closePrimitive() else: triangles.addVertex(0) triangles.addVertex((nbOfPoints - 1) * 2 + 1) triangles.addVertex((nbOfPoints - 1) * 2 + 0) triangles.closePrimitive() triangles.addVertex(1) triangles.addVertex((nbOfPoints - 1) * 2 + 1) triangles.addVertex(0) triangles.closePrimitive() geom = Geom(vdata) geom.addPrimitive(triangles) return geom
def makeMountains(): format=GeomVertexFormat.getV3t2() vdata=GeomVertexData('mountains', format, Geom.UHStatic) vertex=GeomVertexWriter(vdata, 'vertex') texcoord=GeomVertexWriter(vdata, 'texcoord') format2=GeomVertexFormat.getV3c4() vdata2=GeomVertexData('sky', format2, Geom.UHStatic) vertex2=GeomVertexWriter(vdata2, 'vertex') color2=GeomVertexWriter(vdata2, 'color') numQuads=32 angle=0 textureX=0 angleAdd=math.pi*2/numQuads textureXAdd=1.0/numQuads currentQuad=0 numVertices=0 while currentQuad<numQuads: if currentQuad==0: vertexX=math.sin(angle)*3000 vertexY=math.cos(angle)*3000 vertex.addData3f(vertexX,vertexY,0.0) vertex.addData3f(vertexX,vertexY,360.0) texcoord.addData2f(1.0, 0.0) texcoord.addData2f(1.0, 1.0) numVertices=numVertices+2 vertexX=math.sin(angle)*3000 vertexY=math.cos(angle)*3000 vertex.addData3f(vertexX,vertexY,0.0) vertex.addData3f(vertexX,vertexY,360.0) vertex2.addData3f(vertexX,vertexY,360.0) color2.addData4f(45.0/255.0,112.0/255.0,255.0/255.0,1.0) #color2.addData4f(1.0,1.0,1.0,1.0) #print('created vertex at '+str(vertexX)+','+str(vertexY)+',2') #print('created vertex at '+str(vertexX)+','+str(vertexY)+',0') texcoord.addData2f(textureX, 0.0) texcoord.addData2f(textureX, 1.0) #print('texturex is '+str(textureX)) #print('creating vertices v'+str(numVertices)+' and v'+str(numVertices+1)) numVertices=numVertices+2 currentQuad=currentQuad+1 textureX=textureX+textureXAdd angle=angle+angleAdd vertex2.addData3f(0.0,0.0,360.0) #color2.addData4f(1.0,1.0,1.0,1.0) color2.addData4f(45.0/255.0,112.0/255.0,255.0/255.0,1.0) currentQuad=0 currentOffset=2 tris=GeomTriangles(Geom.UHDynamic) #print('creating tris - numVertices is '+str(numVertices)) while currentQuad<numQuads: vertexOffsets=[] for innerIndex in range(4): offset=currentOffset+innerIndex #print('comparing '+str(offset)+' with '+str(numVertices-1)) if offset>numVertices-1: offset=offset-numVertices vertexOffsets.append(offset) #print('adding tri connecting v'+str(vertexOffsets[0])+', v'+str(vertexOffsets[1])+', v'+str(vertexOffsets[3])) #print('adding tri connecting v'+str(vertexOffsets[1])+', v'+str(vertexOffsets[3])+', v'+str(vertexOffsets[2])) tris.addVertex(vertexOffsets[0]) tris.addVertex(vertexOffsets[2]) tris.addVertex(vertexOffsets[1]) tris.closePrimitive() tris.addVertex(vertexOffsets[1]) tris.addVertex(vertexOffsets[2]) tris.addVertex(vertexOffsets[3]) tris.closePrimitive() currentOffset=currentOffset+2 currentQuad=currentQuad+1 tris2=GeomTriangles(Geom.UHDynamic) currentOffset=1 numTris=numQuads currentTri=0 while currentTri<numTris: tris2.addVertex(numTris) tris2.addVertex(currentTri) if currentTri==numTris-1: tris2.addVertex(0) else: tris2.addVertex(currentTri+1) tris2.closePrimitive() currentTri=currentTri+1 mountains=Geom(vdata) mountains.addPrimitive(tris) sky=Geom(vdata2) sky.addPrimitive(tris2) snode=GeomNode('mountains') snode.addGeom(mountains) snode2=GeomNode('sky') snode2.addGeom(sky) returnValues={"mountains":snode,"sky":snode2} return returnValues