Example #1
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()

    cylinderGeom = Geom(vdata)

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

    cylinderGeom.decomposeInPlace()
    cylinderGeom.unifyInPlace()
    return cylinderGeom
Example #2
0
 def primitives(self, vdata):
     vertex = GeomVertexWriter(vdata, 'vertex')
     color = GeomVertexWriter(vdata, 'color')
     n = len(self.border)
     # Points
     for p in self.border:
         vertex.addData3f(p.x, p.y, p.z)
         color.addData4f(0.5, 0.5, 0.5, 0.0)
     for p in self.border:
         vertex.addData3f(p.x, p.y, p.z + self.top)
         color.addData4f(1.0, 1.0, 1.0, 0.0)
     # Wall
     wall = GeomTristrips(Geom.UHStatic)
     for i in range(n):
         wall.addVertices(i, i + n)
     wall.addVertices(0, n)
     wall.closePrimitive()
     yield wall
     # Ceiling
     if self.cover:
         ceil = GeomTristrips(Geom.UHStatic)
         ceil.addConsecutiveVertices(n, n)
         ceil.addVertex(n)
         ceil.closePrimitive()
         yield ceil
Example #3
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()

    cylinderGeom = Geom(vdata)

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

    cylinderGeom.decomposeInPlace()
    cylinderGeom.unifyInPlace()
    return cylinderGeom
 def createTristrips(self, pointCount, startPoint):
     prim = GeomTristrips(Geom.UHStatic)
     for i in xrange(pointCount):
         prim.addVertex(i+startPoint)
         prim.addVertex(i+startPoint+pointCount)         
     prim.closePrimitive() 
     return prim
    def moveForwardTask(self, task):
        change = 0.7
        self.counter = self.counter + change
        x, y, z = self.camera.get_pos()
        self.camera.set_pos(x, y + change, z)
        for i in range(0, len(self.plights)):
            x, y, z = self.plights[i].get_pos()
            self.plights[i].set_pos(x, y + change, z)
            self.render.set_light(self.plights[i])

        if y > self.rows1:
            self.nodeTrisPath.removeNode()

        if self.counter >= 1:
            if y > self.rows1:
                self.newNodePath[0].removeNode()
                del self.newNodePath[0]
            self.counter = self.counter - 1
            for x in range(0, self.cols):
                nz1 = random.uniform(self.pz[x] - 1, self.pz[x] + 1)
                nz2 = random.uniform(self.pz[x - 1] - 1, self.pz[x - 1] + 1)
                nz3 = random.uniform(self.pz[x + 1] - 1, self.pz[x + 1] + 1)
                nz = (nz1 + nz2 + nz3) / 3
                self.vertex.add_data3f((x, self.rows + 1, nz))
                self.vertex.add_data3f((x, self.rows, self.pz[x]))
                if nz < -5:
                    self.color.add_data4f(0.2, 0.1, 0, 1)
                elif nz < -3:
                    self.color.add_data4f(0, 0.2, 0.1, 1)
                elif nz < 0:
                    self.color.add_data4f(0, 0.4, 0.2, 1)
                elif nz < 4:
                    self.color.add_data4f(0.4, 0.4, 0.4, 1)
                else:
                    self.color.add_data4f(1, 1, 1, 1)
                if nz < -5:

                    self.color.add_data4f(0.2, 0.1, 0, 1)
                elif nz < -3:
                    self.color.add_data4f(0, 0.2, 0.1, 1)
                elif self.pz[x] < 0:
                    self.color.add_data4f(0, 0.4, 0.2, 1)
                elif self.pz[x] < 4:
                    self.color.add_data4f(0.4, 0.4, 0.4, 1)
                else:
                    self.color.add_data4f(1, 1, 1, 1)
                self.pz[x] = nz
                #print (nz)
            geom = Geom(self.vdata)
            prim = GeomTristrips(Geom.UH_static)
            prim.addVertex(self.cols * 2 * self.rows)
            prim.add_next_vertices((self.cols * 2) - 1)
            prim.close_primitive()
            geom.addPrimitive(prim)
            node = GeomNode("TriStrips")
            node.addGeom(geom)
            self.newNodePath.append(self.render.attachNewNode(node))
            self.rows = self.rows + 1
        return Task.cont
Example #6
0
    def create_model(self):
        vdata = GeomVertexData('name', Diagram.gformat, Geom.UHStatic)
        vdata.setNumRows(len(self.values)*2)

        vertex = GeomVertexWriter(vdata, 'vertex')
        color = GeomVertexWriter(vdata, 'color')
        prim = GeomTristrips(Geom.UHStatic)

        i = 0
        for x, z in self.values:
            vertex.addData3(0, x, 0)
            color.addData4(0, 0, 1, 1)
            prim.addVertex(i)

            vertex.addData3(0, x, z/100)
            color.addData4(0, 0, 1, 1)
            prim.addVertex(i+1)

            i += 2

        prim.closePrimitive()

        diagram_geom = Geom(vdata)
        diagram_geom.addPrimitive(prim)

        node = GeomNode('gnode')
        node.addGeom(diagram_geom)

        node.setTag('entity_type', self.__class__.__name__)
        node.setTag('entity_id', self.entity_id)

        model_parent = self.parent.geom[0]
        parent_scale = model_parent.getScale()

        nodePath = render.attachNewNode(node)
        nodePath.reparentTo(model_parent)
        nodePath.set_two_sided(True)
        nodePath.setLightOff()

        node_parent = self.parent.start.geom[0]



        L = self.parent.longitude()
        h = 1
        nodePath.setScale(h / parent_scale[2], 1 / parent_scale[1], 1 / parent_scale[0])

        nodePath.wrtReparentTo(node_parent)
        """
        model = app.base.loader.loadModel("data/geom/plate")
        model.set_two_sided(True)
        model.setTag('entity_type', self.__class__.__name__)
        model.setTag('entity_id', self.entity_id)
        self.geom = [model]"""

        self.update_model()
Example #7
0
    def drawBody(self, pos, quat, radius=1, keepDrawing=True, numVertices=16):
        """
        this draws the body of the tree. This draws a ring of vertices and
        connects the rings with triangles to from the body.

        the keepDrawing parameter tells the function whether or not we're
        at an end
        if the vertices before were an end, don't draw branches to it
        """
        vdata = self.bodydata
        circleGeom = Geom(vdata)
        vertWriter = GeomVertexWriter(vdata, "vertex")
        normalWriter = GeomVertexWriter(vdata, "normal")
        texReWriter = GeomVertexRewriter(vdata, "texcoord")
        startRow = vdata.getNumRows()
        vertWriter.setRow(startRow)
        normalWriter.setRow(startRow)
        sCoord = 0
        if (startRow != 0):
            texReWriter.setRow(startRow - numVertices)
            sCoord = texReWriter.getData2f().getX() + 1
            draw = (startRow - numVertices) in self.drawFlags
            if not draw:
                sCoord -= 1
        drawIndex = startRow
        texReWriter.setRow(startRow)

        angleSlice = 2 * math.pi / numVertices
        currAngle = 0
        perp1 = quat.getRight()
        perp2 = quat.getForward()
        #vertex information is written here
        for i in xrange(numVertices + 1):
            #doubles the last vertex to fix UV seam
            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(1.0 * i / numVertices, sCoord)
            if keepDrawing:
                self.drawFlags.add(drawIndex)
            drawIndex += 1
            currAngle += angleSlice
        draw = (startRow - numVertices) in self.drawFlags
        #we cant draw quads directly so we use Tristrips
        if (startRow != 0) and draw:
            lines = GeomTristrips(Geom.UHStatic)
            for i in xrange(numVertices + 1):
                lines.addVertex(i + startRow)
                lines.addVertex(i + startRow - numVertices - 1)
            lines.addVertex(startRow)
            lines.addVertex(startRow - numVertices)
            lines.closePrimitive()
            #lines.decompose()
            circleGeom.addPrimitive(lines)
            circleGeomNode = GeomNode("Debug")
            circleGeomNode.addGeom(circleGeom)
            self.numPrimitives += numVertices * 2
            self.bodies.attachNewNode(circleGeomNode)
Example #8
0
def make_cube(x, y, z):  # FIXME make prism
    """ make x, y, z sized cube (ints pls) """
    colors = [[1,1,1,0] for i in range(8)]
    #colors[0] = np.array((1,1,1,1))
    #colors[1] = np.array((1,0,0,0))
    #colors[2] = np.array((0,1,0,0))
    #colors[5] = np.array((0,0,1,0))
    points = (
        (0,0,0),
        (0,0,z),
        (0,y,0),
        (0,y,z),
        (x,0,0),
        (x,0,z),
        (x,y,0),
        (x,y,z),
    )
    order = [0, 5, 1, 7, 3, 2, 1, 0, 5, 4, 7, 6, 2, 4, 0]  # perfect for clockwise
    #order = [2, 6, 3, 7, 5, 6, 4, 2, 0, 3, 1, 4, 0, 4]
    #order.reverse()
    #order = [4, 3, 7, 8, 5, 3, 1, 4, 2, 7, 6, 5, 2, 1]


    fmt = GeomVertexFormat.getV3c4()
    vertexData = GeomVertexData('points', fmt, Geom.UHStatic)
    verts = GeomVertexWriter(vertexData, 'vertex')
    color = GeomVertexWriter(vertexData, 'color')

    for p,c in zip(points,colors):
        verts.addData3f(*p)
        color.addData4f(*c)

    targetTris = GeomTristrips(Geom.UHStatic)
    targetTris.addConsecutiveVertices(0,8)
    for i in order:
        targetTris.addVertex(i)#-1)
    targetTris.closePrimitive()

    target = Geom(vertexData)
    target.addPrimitive(targetTris)
    return target
Example #9
0
    def drawBody(self, pos, quat, radius=1,UVcoord=(1,1), numVertices=_polySize):
#        if isRoot:
#            self.bodydata = GeomVertexData("body vertices", GeomVertexFormat.getV3n3t2(), Geom.UHStatic)
        vdata = self.bodydata
        circleGeom = Geom(vdata) # this was originally a copy of all previous geom in 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)       
        texReWriter.setRow(startRow)   
       
        #axisAdj=Mat4.rotateMat(45, axis)*Mat4.scaleMat(radius)*Mat4.translateMat(pos)
        perp1 = quat.getRight()
        perp2 = quat.getForward()   
        
#TODO: PROPERLY IMPLEMENT RADIAL NOISE        
        #vertex information is written here
        angleSlice = 2 * pi / numVertices
        currAngle = 0
        for i in xrange(numVertices+1): 
            adjCircle = pos + (perp1 * cos(currAngle) + perp2 * sin(currAngle)) * radius * (.5+bNodeRadNoise*random.random())
            normal = perp1 * cos(currAngle) + perp2 * sin(currAngle)       

            normalWriter.addData3f(normal)
            vertWriter.addData3f(adjCircle)
            texReWriter.addData2f(float(UVcoord[0]*i) / numVertices,UVcoord[1])            # UV SCALE HERE!
            #colorWriter.addData4f(0.5, 0.5, 0.5, 1)
            currAngle += angleSlice 
        
        #we cant draw quads directly so we use Tristrips
        if (startRow != 0):
            lines = GeomTristrips(Geom.UHStatic)         
            for i in xrange(numVertices+1):
                lines.addVertex(i + startRow)
                lines.addVertex(i + startRow - numVertices-1)
            lines.addVertex(startRow)
            lines.addVertex(startRow - numVertices)
            lines.closePrimitive()
            #lines.decompose()
            circleGeom.addPrimitive(lines)           
            circleGeomNode = GeomNode("Debug")
            circleGeomNode.addGeom(circleGeom)   
            self.numPrimitives += numVertices * 2
            self.bodies.attachNewNode(circleGeomNode)
            return circleGeomNode
Example #10
0
def makeClickableGeom():
    vdata = GeomVertexData('handleData', GeomVertexFormat.getV3(),
            Geom.UHStatic)
    v = GeomVertexWriter(vdata, 'vertex')
    length = 1.0
    cylRad = 0.10
    circRes = 8

    # Add vertices cylinder.
    for z in [length/2.0, -length/2.0]:
        for i in xrange(circRes):
            theta = i*(2*pi/circRes)
            v.addData3f(cylRad*sin(theta), cylRad*cos(theta), z)

    geom = Geom(vdata)
    # Make polys for the circles
    CCW = 1
    CW = 0
    for i, wind in ((0, CCW), (circRes, CW)):
        circle = GeomTristrips(Geom.UHStatic)
        l = range(i, i+circRes)
        for i in xrange(1-wind, (len(l)-wind)/2):
            l.insert(2*i+wind, l.pop())
        for v in l:
            circle.addVertex(v)
        circle.closePrimitive()
        geom.addPrimitive(circle)

    # Make polys for the cylinder.
    cyl = GeomTristrips(Geom.UHStatic)
    for i in xrange(circRes):
        cyl.addVertex(i + circRes)
        cyl.addVertex(i)
    cyl.addVertex(circRes)
    cyl.addVertex(0)
    cyl.closePrimitive()
    geom.addPrimitive(cyl)
    node = GeomNode('geomnode')
    node.addGeom(geom)
    return node
Example #11
0
def makeClickableGeom():
    vdata = GeomVertexData('handleData', GeomVertexFormat.getV3(),
                           Geom.UHStatic)
    v = GeomVertexWriter(vdata, 'vertex')
    length = 1.0
    cylRad = 0.10
    circRes = 8

    # Add vertices cylinder.
    for z in [length / 2.0, -length / 2.0]:
        for i in xrange(circRes):
            theta = i * (2 * pi / circRes)
            v.addData3f(cylRad * sin(theta), cylRad * cos(theta), z)

    geom = Geom(vdata)
    # Make polys for the circles
    CCW = 1
    CW = 0
    for i, wind in ((0, CCW), (circRes, CW)):
        circle = GeomTristrips(Geom.UHStatic)
        l = range(i, i + circRes)
        for i in xrange(1 - wind, (len(l) - wind) / 2):
            l.insert(2 * i + wind, l.pop())
        for v in l:
            circle.addVertex(v)
        circle.closePrimitive()
        geom.addPrimitive(circle)

    # Make polys for the cylinder.
    cyl = GeomTristrips(Geom.UHStatic)
    for i in xrange(circRes):
        cyl.addVertex(i + circRes)
        cyl.addVertex(i)
    cyl.addVertex(circRes)
    cyl.addVertex(0)
    cyl.closePrimitive()
    geom.addPrimitive(cyl)
    node = GeomNode('geomnode')
    node.addGeom(geom)
    return node
Example #12
0
def makeEquiTri():
    colors = (
        (1,1,1,1),
        (1,0,0,1),
        (0,0,1,1),
        (0,1,0,1),
        (1,1,1,1),
        (1,0,0,1),
    )
    points = (
        (-1,0,0),
        (1,0,0),
        (0,3**.5/2,3**.5),
        (0,3**.5,0),
        #(-1,0,0),
        #(1,0,0),
    )

    fmt = GeomVertexFormat.getV3c4() #3 component vertex, w/ 4 comp color
    #fmt = GeomVertexFormat.getV3() #3 component vertex, w/ 4 comp color
    vertexData = GeomVertexData('points', fmt, Geom.UHStatic)

    verts = GeomVertexWriter(vertexData, 'vertex')
    color = GeomVertexWriter(vertexData, 'color')

    for p,c in zip(points,colors):
        verts.addData3f(*p)
        color.addData4f(*c)

    targetTris = GeomTristrips(Geom.UHStatic)
    targetTris.addConsecutiveVertices(0,4)
    targetTris.addVertex(0)
    targetTris.addVertex(1)
    targetTris.closePrimitive()

    target = Geom(vertexData)
    target.addPrimitive(targetTris)
    return target
Example #13
0
    def _get_primitive(cls, layer: Layer) -> GeomTristrips:
        """Generate GeomTristrips primitive for provided layer."""
        primitive = GeomTristrips(Geom.UHStatic)

        cur_point_num_in_layer = 0

        # Iterate over paths in layer
        for path in layer.paths:
            segment_count = len(path) - 1
            path_start_point_index = (
                cur_point_num_in_layer * 4
            )  # Relative to first point in first path in layer

            # Start cap
            primitive.addVertices(
                *[path_start_point_index + i for i in [0, 1, 3, 2]])
            primitive.closePrimitive()

            # Segment walls
            for segment_index in range(segment_count):
                v_start_index = path_start_point_index + segment_index * 4

                # Zigzag around segment walls: 0, 4, 1, 5, 2, 6, 3, 7, 0, 4
                for i in range(v_start_index, v_start_index + 4):
                    primitive.addVertex(i)
                    primitive.addVertex(i + 4)

                primitive.addVertex(v_start_index)
                primitive.addVertex(v_start_index + 4)

                primitive.closePrimitive()

            # End cap
            end_wall_start_index = (cur_point_num_in_layer + segment_count) * 4
            primitive.addVertices(
                *[end_wall_start_index + i for i in [0, 3, 1, 2]])
            primitive.closePrimitive()

            cur_point_num_in_layer += len(path)

        return primitive
Example #14
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=LMatrix4.rotateMat(45, axis)*LMatrix4.scaleMat(radius)*LMatrix4.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

    if startRow == 0:
        return

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

    # we cant draw quads directly so we use Tristrips
    if drawReader.getData1i() != 0:
        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)
Example #15
0
    def drawBody(self,
                 pos,
                 quat,
                 radius=1,
                 UVcoord=(1, 1),
                 numVertices=_polySize):
        #        if isRoot:
        #            self.bodydata = GeomVertexData("body vertices", GeomVertexFormat.getV3n3t2(), Geom.UHStatic)
        vdata = self.bodydata
        circleGeom = Geom(
            vdata
        )  # this was originally a copy of all previous geom in 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)
        texReWriter.setRow(startRow)

        #axisAdj=Mat4.rotateMat(45, axis)*Mat4.scaleMat(radius)*Mat4.translateMat(pos)
        perp1 = quat.getRight()
        perp2 = quat.getForward()

        #TODO: PROPERLY IMPLEMENT RADIAL NOISE
        #vertex information is written here
        angleSlice = 2 * pi / numVertices
        currAngle = 0
        for i in xrange(numVertices + 1):
            adjCircle = pos + (perp1 * cos(currAngle) +
                               perp2 * sin(currAngle)) * radius * (
                                   .5 + bNodeRadNoise * random.random())
            normal = perp1 * cos(currAngle) + perp2 * sin(currAngle)

            normalWriter.addData3f(normal)
            vertWriter.addData3f(adjCircle)
            texReWriter.addData2f(
                float(UVcoord[0] * i) / numVertices,
                UVcoord[1])  # UV SCALE HERE!
            #colorWriter.addData4f(0.5, 0.5, 0.5, 1)
            currAngle += angleSlice

        #we cant draw quads directly so we use Tristrips
        if (startRow != 0):
            lines = GeomTristrips(Geom.UHStatic)
            for i in xrange(numVertices + 1):
                lines.addVertex(i + startRow)
                lines.addVertex(i + startRow - numVertices - 1)
            lines.addVertex(startRow)
            lines.addVertex(startRow - numVertices)
            lines.closePrimitive()
            #lines.decompose()
            circleGeom.addPrimitive(lines)
            circleGeomNode = GeomNode("Debug")
            circleGeomNode.addGeom(circleGeom)
            self.numPrimitives += numVertices * 2
            self.bodies.attachNewNode(circleGeomNode)
            return circleGeomNode
Example #16
0
    def __init__(self):
        ShowBase.__init__(self)

        self.cols = 100
        self.rows = 100

        base.disableMouse()
        base.setFrameRateMeter(True)
        self.cameraHeight = 13
        self.camera.set_pos(self.cols / 2, -30, self.cameraHeight)
        self.camera.look_at(self.cols / 2, 300, 0)

        plights = []

        for i in range(0, int(self.cols / 5), 2):
            plight = PointLight("plight")
            plight.setColor(VBase4(1, 1, 1, 1))
            plights.append(plight)
            plights[i] = self.render.attachNewNode(plight)
            x, y, z = self.camera.get_pos()
            plights[i].setPos(self.cols / 2 + ((i - int(i / 2)) * 10), y + 20,
                              5)
            self.render.set_light(plights[i])

            plight = PointLight("plight")
            plight.setColor(VBase4(1, 1, 1, 1))
            plights.append(plight)
            plights[i + 1] = self.render.attachNewNode(plight)
            x, y, z = self.camera.get_pos()
            plights[i + 1].setPos(self.cols / 2 + ((i - int(i / 2)) * 10),
                                  y + 20, 10)
            self.render.set_light(plights[i + 1])

        self.plights = plights

        format = GeomVertexFormat.getV3c4()
        vdata = GeomVertexData('name', format, Geom.UHStatic)
        vdata.setNumRows(self.cols * self.rows)
        self.vertex = GeomVertexWriter(vdata, 'vertex')
        self.color = GeomVertexWriter(vdata, 'color')

        pz = [random.uniform(-1, 1)]
        for i in range(self.rows):
            pz.append(random.uniform(pz[i - 1] - 1, pz[i] + 1))
        for y in range(0, self.rows):
            for x in range(0, self.cols):
                nz1 = random.uniform(pz[x] - 1, pz[x] + 1)
                nz2 = random.uniform(pz[x - 1] - 1, pz[x - 1] + 1)
                nz3 = random.uniform(pz[x + 1] - 1, pz[x + 1] + 1)
                nz = (nz1 + nz2 + nz3) / 3
                self.vertex.add_data3f((x, y + 1, nz))
                self.vertex.add_data3f((x, y, pz[x]))
                if nz < -5:
                    self.color.add_data4f(0.2, 0.1, 0, 1)
                elif nz < -3:
                    self.color.add_data4f(0, 0.2, 0.1, 1)
                elif nz < 0:
                    self.color.add_data4f(0, 0.4, 0.2, 1)
                elif nz < 2:
                    self.color.add_data4f(0.4, 0.4, 0.4, 1)
                else:
                    self.color.add_data4f(1, 1, 1, 1)
                if nz < -5:
                    self.color.add_data4f(0.2, 0.1, 0, 1)
                elif nz < -3:
                    self.color.add_data4f(0, 0.2, 0.1, 1)
                elif pz[x] < 0:
                    self.color.add_data4f(0, 0.4, 0.2, 1)
                elif pz[x] < 2:
                    self.color.add_data4f(0.4, 0.4, 0.4, 1)
                else:
                    self.color.add_data4f(1, 1, 1, 1)
                pz[x] = nz
                #print (nz)
        self.pz = pz

        geom = Geom(vdata)
        for y in range(0, self.rows):
            prim = GeomTristrips(Geom.UH_static)
            prim.addVertex(y * self.cols * 2)
            prim.add_next_vertices((self.cols * 2) - 1)
            prim.close_primitive()
            geom.addPrimitive(prim)

        nodeTris = GeomNode("TriStrips")
        nodeTris.addGeom(geom)
        self.nodeTrisPath = self.render.attachNewNode(nodeTris)
        self.task_mgr.add(self.moveForwardTask, "moveForwardTask")

        self.vdata = vdata
        self.newNodePath = []
        self.counter = 0
        self.rows1 = self.rows

        skybox = self.loader.loadModel("models/skybox.bam")
        skybox.reparent_to(self.render)
        skybox.set_scale(20000)

        skybox_texture = self.loader.loadTexture("textures/dayfair.jpg")
        skybox_texture.set_minfilter(SamplerState.FT_linear)
        skybox_texture.set_magfilter(SamplerState.FT_linear)
        skybox_texture.set_wrap_u(SamplerState.WM_repeat)
        skybox_texture.set_wrap_v(SamplerState.WM_mirror)
        skybox_texture.set_anisotropic_degree(16)
        skybox.set_texture(skybox_texture)

        skybox_shader = Shader.load(Shader.SL_GLSL, "skybox.vert.glsl",
                                    "skybox.frag.glsl")
        skybox.set_shader(skybox_shader)
Example #17
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=LMatrix4.rotateMat(45, axis)*LMatrix4.scaleMat(radius)*LMatrix4.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)
Example #18
0
def add_button(self, text, label_id, pos_x, pos_y, width=0.0, hight=0.1):
    if width == 0.0:
        for c in range(len(text) / 2):
            width += 0.08
    ls = LineSegs("lines")
    ls.setColor(0, 1, 0, 1)
    ls.drawTo(-width / 2, 0, hight / 2)
    ls.drawTo(width / 2, 0, hight / 2)
    ls.drawTo(width / 2, 0, -hight / 2)
    ls.drawTo(-width / 2, 0, -hight / 2)
    ls.drawTo(-width / 2, 0, hight / 2)
    border = ls.create(False)
    border.setTag('back_ground', '1')

    array = GeomVertexArrayFormat()
    array.addColumn("vertex", 4, Geom.NTFloat32, Geom.CPoint)
    arr_format = GeomVertexFormat()
    arr_format.addArray(array)
    arr_format = GeomVertexFormat.registerFormat(arr_format)

    vdata = GeomVertexData('fill', arr_format, Geom.UHStatic)
    vdata.setNumRows(4)
    vertex = GeomVertexWriter(vdata, 'vertex')

    vertex.addData3f(-width / 2, 0, hight / 2)
    vertex.addData3f(width / 2, 0, hight / 2)
    vertex.addData3f(-width / 2, 0, -hight / 2)
    vertex.addData3f(width / 2, 0, -hight / 2)

    prim = GeomTristrips(Geom.UHStatic)
    prim.addVertex(0)
    prim.addVertex(1)
    prim.addVertex(2)
    prim.addVertex(3)

    geom = Geom(vdata)
    geom.addPrimitive(prim)
    node = GeomNode('gnode')
    node.addGeom(geom)
    nodePath = NodePath("button")
    nodePath.attachNewNode(node)
    nodePath.setPos(0, 0, 0)
    nodePath.setTag('button', '1')
    nodePath.setBin("unsorted", 0)
    nodePath.setDepthTest(False)
    nodePath.setColor(0, 0, 0, 1)
    nodePath.attachNewNode(border)

    nodePath1 = NodePath("button")
    nodePath1.attachNewNode(node)
    nodePath1.setPos(0, 0, 0)
    nodePath1.setTag('button1', '1')
    nodePath1.setBin("unsorted", 0)
    nodePath1.setDepthTest(False)
    nodePath1.setColor(0, 1, 0, 1)
    nodePath1.attachNewNode(border)

    button = DirectFrame(enableEdit=1,
                         text=text,
                         geom=nodePath,
                         text_scale=0.05,
                         text_fg=(0, 1, 0, 1),
                         borderWidth=(1, 1),
                         relief=None,
                         text_pos=(0, -0.01, 0),
                         textMayChange=1,
                         state=DGG.NORMAL,
                         parent=aspect2d)
    button.setPos(pos_x, 0, pos_y)
    button.bind(DGG.B1PRESS, button_click, [button])
    button.bind(DGG.WITHIN, button_hover, [button])
    button.bind(DGG.WITHOUT, button_no_hover, [button])
    # button.resetFrameSize()
    # self.button.bind(DGG.WITHIN, self.onMouseHoverInFunction, [button, some_value1])

    defines.ENTITIES[defines.ENTITY_ID] = {
        'CATEGORY': 'button',
        'BUTTON': button,
        'NODE': nodePath,
        'LABEL': label_id,
        'STATUS': 0
    }
    defines.ENTITY_ID += 1
def add_button(self, text, label_id, pos_x, pos_y, width=0.0, hight=0.1):
    if width == 0.0:
        for c in range(len(text)/2):
            width += 0.08
    ls = LineSegs("lines")
    ls.setColor(0,1,0,1)
    ls.drawTo(-width/2, 0, hight/2)
    ls.drawTo(width/2, 0, hight/2)
    ls.drawTo(width/2, 0,-hight/2)
    ls.drawTo(-width/2, 0,-hight/2)
    ls.drawTo(-width/2, 0, hight/2)
    border = ls.create(False)
    border.setTag('back_ground', '1')
    
    array = GeomVertexArrayFormat()
    array.addColumn("vertex", 4, Geom.NTFloat32, Geom.CPoint)
    arr_format = GeomVertexFormat()
    arr_format.addArray(array)
    arr_format = GeomVertexFormat.registerFormat(arr_format)

    vdata = GeomVertexData('fill', arr_format, Geom.UHStatic)
    vdata.setNumRows(4)
    vertex = GeomVertexWriter(vdata, 'vertex')

    vertex.addData3f(-width/2, 0, hight/2)
    vertex.addData3f(width/2, 0, hight/2)
    vertex.addData3f(-width/2, 0,-hight/2)
    vertex.addData3f(width/2, 0,-hight/2)

    prim = GeomTristrips(Geom.UHStatic)
    prim.addVertex(0)
    prim.addVertex(1)
    prim.addVertex(2)
    prim.addVertex(3)

    geom = Geom(vdata)
    geom.addPrimitive(prim)
    node = GeomNode('gnode')
    node.addGeom(geom)
    nodePath = NodePath("button")
    nodePath.attachNewNode(node)
    nodePath.setPos(0,0,0)
    nodePath.setTag('button', '1')
    nodePath.setBin("unsorted", 0)
    nodePath.setDepthTest(False)
    nodePath.setColor(0,0,0,1)
    nodePath.attachNewNode(border)

    nodePath1 = NodePath("button")
    nodePath1.attachNewNode(node)
    nodePath1.setPos(0,0,0)
    nodePath1.setTag('button1', '1')
    nodePath1.setBin("unsorted", 0)
    nodePath1.setDepthTest(False)
    nodePath1.setColor(0,1,0,1)
    nodePath1.attachNewNode(border)


    button=DirectFrame( 
                        enableEdit=1,                      
                        text=text,
                        geom=nodePath,
                        text_scale=0.05,
                        text_fg=(0,1,0,1),
                        borderWidth=(1,1), 
                        relief = None,
                        text_pos=(0,-0.01,0),
                        textMayChange=1,
                        state=DGG.NORMAL,
                        parent=aspect2d
                        )
    button.setPos(pos_x,0,pos_y)
    button.bind(DGG.B1PRESS, button_click, [button])
    button.bind(DGG.WITHIN, button_hover, [button])
    button.bind(DGG.WITHOUT, button_no_hover, [button])
    # button.resetFrameSize()
    # self.button.bind(DGG.WITHIN, self.onMouseHoverInFunction, [button, some_value1])

    defines.ENTITIES[defines.ENTITY_ID] = {'CATEGORY':'button', 'BUTTON':button, 'NODE':nodePath, 'LABEL':label_id,'STATUS': 0}
    defines.ENTITY_ID += 1