Exemple #1
0
def makeCircle(vdata, numVertices=40,offset=Vec3(0,0,0), direction=1):
  circleGeom=Geom(vdata)

  vertWriter=GeomVertexWriter(vdata, "vertex")
  normalWriter=GeomVertexWriter(vdata, "normal")
  colorWriter=GeomVertexWriter(vdata, "color")
  uvWriter=GeomVertexWriter(vdata, "texcoord")
  drawWriter=GeomVertexWriter(vdata, "drawFlag")

  #make sure we start at the end of the GeomVertexData so we dont overwrite anything
  #that might be there already
  startRow=vdata.getNumRows()

  vertWriter.setRow(startRow)
  colorWriter.setRow(startRow)
  uvWriter.setRow(startRow)
  normalWriter.setRow(startRow)
  drawWriter.setRow(startRow)

  angle=2*math.pi/numVertices
  currAngle=angle

  for i in range(numVertices):
    position=Vec3(math.cos(currAngle)+offset.getX(), math.sin(currAngle)+offset.getY(),offset.getZ())
    vertWriter.addData3f(position)
    uvWriter.addData2f(position.getX()/2.0+0.5,position.getY()/2.0+0.5)
    colorWriter.addData4f(1.0, 1.0, 1.0, 1.0)
    position.setZ(position.getZ()*direction)
    position.normalize()
    normalWriter.addData3f(position)

    #at default Opengl only draws "front faces" (all shapes whose vertices are arranged CCW). We
    #need direction so we can specify which side we want to be the front face
    currAngle+=angle*direction

  circle=GeomTrifans(Geom.UHStatic)
  circle.addConsecutiveVertices(startRow, numVertices)
  circle.closePrimitive()

  circleGeom.addPrimitive(circle)

  return circleGeom
Exemple #2
0
def makeCylinder(vdata,numVertices=40):
  topCircleGeom=makeCircle(vdata, numVertices,Vec3(0,0, 1))
  bottomCircleGeom=makeCircle(vdata, numVertices,Vec3(0,0,0),-1)


  body=GeomTristrips(Geom.UHStatic)

  j=40
  i=0
  while i < numVertices+1:
    body.addVertex(i)
    body.addVertex(j)
    i+=1
    if j==40:
      j=2*numVertices-1
    else:
      j-=1
    body.addVertex(i)
    body.addVertex(j)
    j-=1
    i+=1

  body.addVertex(numVertices-1)
  body.addVertex(0)
  body.addVertex(numVertices)
  body.closePrimitive()
  #print body



  cylinderGeom=Geom(vdata)

  cylinderGeom.addPrimitive(body)
  cylinderGeom.copyPrimitivesFrom(topCircleGeom)
  cylinderGeom.copyPrimitivesFrom(bottomCircleGeom)


  cylinderGeom.decomposeInPlace()
  cylinderGeom.unifyInPlace()
  return cylinderGeom
Exemple #3
0
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
Exemple #4
0
def drawBody(nodePath, vdata, pos, vecList, radius=1, keepDrawing=True,numVertices=8):

  circleGeom=Geom(vdata)

  vertWriter=GeomVertexWriter(vdata, "vertex")
  colorWriter=GeomVertexWriter(vdata, "color")
  normalWriter=GeomVertexWriter(vdata, "normal")
  drawReWriter=GeomVertexRewriter(vdata, "drawFlag")
  texReWriter=GeomVertexRewriter(vdata, "texcoord")


  startRow=vdata.getNumRows()
  vertWriter.setRow(startRow)
  colorWriter.setRow(startRow)
  normalWriter.setRow(startRow)

  sCoord=0

  if (startRow!=0):
    texReWriter.setRow(startRow-numVertices)
    sCoord=texReWriter.getData2f().getX()+1

    drawReWriter.setRow(startRow-numVertices)
    if(drawReWriter.getData1f()==False):
      sCoord-=1

  drawReWriter.setRow(startRow)
  texReWriter.setRow(startRow)

  angleSlice=2*math.pi/numVertices
  currAngle=0

  #axisAdj=Mat4.rotateMat(45, axis)*Mat4.scaleMat(radius)*Mat4.translateMat(pos)

  perp1=vecList[1]
  perp2=vecList[2]

  #vertex information is written here
  for i in range(numVertices):
    adjCircle=pos+(perp1*math.cos(currAngle)+perp2*math.sin(currAngle))*radius
    normal=perp1*math.cos(currAngle)+perp2*math.sin(currAngle)
    normalWriter.addData3f(normal)
    vertWriter.addData3f(adjCircle)
    texReWriter.addData2f(sCoord,(i+0.001)/(numVertices-1))
    colorWriter.addData4f(0.5,0.5,0.5,1)
    drawReWriter.addData1f(keepDrawing)
    currAngle+=angleSlice


  drawReader=GeomVertexReader(vdata, "drawFlag")
  drawReader.setRow(startRow-numVertices)

  #we cant draw quads directly so we use Tristrips
  if (startRow!=0) & (drawReader.getData1f()!=False):
    lines=GeomTristrips(Geom.UHStatic)
    half=int(numVertices*0.5)
    for i in range(numVertices):
      lines.addVertex(i+startRow)
      if i< half:
        lines.addVertex(i+startRow-half)
      else:
        lines.addVertex(i+startRow-half-numVertices)

    lines.addVertex(startRow)
    lines.addVertex(startRow-half)
    lines.closePrimitive()
    lines.decompose()
    circleGeom.addPrimitive(lines)


    circleGeomNode=GeomNode("Debug")
    circleGeomNode.addGeom(circleGeom)

    #I accidentally made the front-face face inwards. Make reverse makes the tree render properly and
      #should cause any surprises to any poor programmer that tries to use this code
    circleGeomNode.setAttrib(CullFaceAttrib.makeReverse(),1)
    global numPrimitives
    numPrimitives+=numVertices*2

    nodePath.attachNewNode(circleGeomNode)
Exemple #5
0
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