Пример #1
0
def init():
    global theMeshes, theLight, theCamera, theScreen, skyColor
    initializeVAO()
    glEnable(GL_DEPTH_TEST)

    # Add our objects

    # LIGHT
    theLight = N.array((0.577, 0.577, 0.577, 0.0),dtype=N.float32)

    # OBJECTS
    islandHeight = 5.0
    surroundingLandHeight = 1.0
    moatDepth = -10.0
    islandRadius = 50.0
    moatInnerRadius = 65.0
    moatOuterRadius = 90.0
    outerShoreRadius = 100.0
    terrainXZMax = 400.0
    terrainNumSamples = 400

    tRadius = 6.0
    tThickness = 1.0
    height = 15.0
    crenHeight = 2.0

    wThickness = 1.0

    castleRadius = islandRadius - tRadius + 1
    
    pi = N.pi
    qtrpi = N.pi/4

    skyColor = (0.0, 0.47, 0.67, 0.0)
    
    towerList = [
        #radius, height, numArcs, crenWidth, crenHeight, moveBackUnits, moveRightUnits
        (tRadius, height, 128, tThickness, 8, crenHeight,
         castleRadius*(-N.sin(pi*19/12)), castleRadius*(N.cos(pi*19/12))),

        (tRadius, height, 128, tThickness, 8, crenHeight,
         castleRadius*(-N.sin(pi*17/12)), castleRadius*(N.cos(pi*17/12))),

        (tRadius, height, 128, tThickness, 8, crenHeight,
         castleRadius*(-N.sin(pi*7/6)), castleRadius*(N.cos(pi*7/6))),

        (tRadius, height, 128, tThickness, 8, crenHeight,
         castleRadius*(-N.sin(pi*5/6)), castleRadius*(N.cos(pi*5/6))),

        (tRadius, height, 128, tThickness, 8, crenHeight,
         castleRadius*(-N.sin(pi*2/3)), castleRadius*(N.cos(pi*2/3))),

        (tRadius, height, 128, tThickness, 8, crenHeight,
         castleRadius*(-N.sin(pi/3)), castleRadius*(N.cos(pi/3))),

        (tRadius, height, 128, tThickness, 8, crenHeight,
         castleRadius*(-N.sin(pi/6)), castleRadius*(N.cos(pi/6))),

        (tRadius, height, 128, tThickness, 8, crenHeight,
         castleRadius*(-N.sin(pi*11/6)), castleRadius*(N.cos(pi*11/6))),

        (12.0, 35.0, 256, tThickness*2, 16, crenHeight*2, 0.0, 0.0)
        ]

    wallList = [
        #wallWidth, wallHeight, wallThickness,
        #numCren, crenHeight, moveBackUnits, moveRightUnits, yawUnits
        (castleRadius*(N.cos(pi*19/12)-N.cos(pi*17/12))-tRadius*2,
         height, wThickness, 3, crenHeight,
         (-N.sin(pi*17/12)), 0.0, 0.0),
        
        (castleRadius*(2*N.sin((pi*17/12-pi*7/6)/2))-tRadius*2,
         height, wThickness, 4, crenHeight,
         ((-N.sin(pi*17/12)-N.sin(pi*7/6))/2),
         ((N.cos(pi*17/12)+N.cos(pi*7/6))/2),
         ((pi*3/2) - ((pi*17/12)-(((pi*17/12)-(pi*7/6))/2)))/-qtrpi),

        (castleRadius*(2*N.sin((pi*7/6-pi*5/6)/2))-tRadius*2,
         height, wThickness, 5, crenHeight,
         ((-N.sin(pi*7/6)-N.sin(pi*5/6))/2),
         ((N.cos(pi*7/6)+N.cos(pi*5/6))/2),
         ((pi*3/2) - ((pi*7/6)-(((pi*7/6)-(pi*5/6))/2)))/-qtrpi),

        (castleRadius*(2*N.sin((pi*5/6-pi*2/3)/2))-tRadius*2,
         height, wThickness, 3, crenHeight,
         ((-N.sin(pi*5/6)-N.sin(pi*2/3))/2),
         ((N.cos(pi*5/6)+N.cos(pi*2/3))/2),
         ((pi*3/2) - ((pi*5/6)-(((pi*5/6)-(pi*2/3))/2)))/-qtrpi),

        (castleRadius*(2*N.sin((pi*2/3-pi/3)/2))-tRadius*2,
         height, wThickness, 5, crenHeight,
         ((-N.sin(pi*2/3)-N.sin(pi/3))/2),
         ((N.cos(pi*2/3)+N.cos(pi/3))/2),
         ((pi*3/2) - ((pi*2/3)-(((pi*2/3)-(pi/3))/2)))/-qtrpi),

        (castleRadius*(2*N.sin((pi/3-pi/6)/2))-tRadius*2,
         height, wThickness, 3, crenHeight,
         ((-N.sin(pi/3)-N.sin(pi/6))/2),
         ((N.cos(pi/3)+N.cos(pi/6))/2),
         ((pi*3/2) - ((pi/3)-(((pi/3)-(pi/6))/2)))/-qtrpi),

        (castleRadius*(1.0)-tRadius*2,
         height, wThickness, 5, crenHeight,
         ((-N.sin(pi/6)-N.sin(pi*11/6))/2),
         ((N.cos(pi/6)+N.cos(pi*11/6))/2),
         -6.0),

        (castleRadius*(2*N.sin((pi*11/6-pi*19/12)/2))-tRadius*2,
         height, wThickness, 4, crenHeight,
         ((-N.sin(pi*11/6)-N.sin(pi*19/12))/2),
         ((N.cos(pi*11/6)+N.cos(pi*19/12))/2),
         -6.0 - (2.0-((pi*3/2) - ((pi*11/6)-(((pi*11/6)-(pi*19/12))/2)))/-qtrpi))
        ]
    
    theMeshes = []

    #towers
    for tower in towerList:
        (towerRadius, towerHeight, nArcs,
         towerThickness, crenWidth, crenHeight,
         moveBackUnits, moveRightUnits) = tower

        #create tower
        verts,elements = cylinder(towerRadius,
                                  towerHeight,
                                  nArcs)
        theMeshes.append(proceduralMesh(N.array((0.373,0.361,0.314,1.0),dtype=N.float32),
                                        N.array((0.475,0.475,0.475,1.0),dtype=N.float32),
                                        100.0,
                                        getArrayBuffer(verts),
                                        getElementBuffer(elements),
                                        len(elements),
                                        makeShader("worley.vert", "worley.frag")
                                        ))
        theMeshes[-1].moveBack(moveBackUnits)
        theMeshes[-1].moveRight(moveRightUnits)
        theMeshes[-1].moveUp(islandHeight)
        
        verts,elements = reflectedSurface(verts,
                                          elements)
        theMeshes.append(proceduralMesh(N.array((0.373,0.361,0.314,1.0),dtype=N.float32),
                                        N.array((0.475,0.475,0.475,1.0),dtype=N.float32),
                                        100.0,
                                        getArrayBuffer(verts),
                                        getElementBuffer(elements),
                                        len(elements),
                                        makeShader("worley.vert", "worleyinverted.frag")
                                        ))
        theMeshes[-1].moveBack(moveBackUnits)
        theMeshes[-1].moveRight(moveRightUnits)
        theMeshes[-1].moveUp(-islandHeight)

        #create inside wall of tower
        verts,elements = cylinder(-towerRadius+towerThickness,
                                  towerHeight,
                                  nArcs)
        theMeshes.append(proceduralMesh(N.array((0.373,0.361,0.314,1.0),dtype=N.float32),
                                        N.array((0.475,0.475,0.475,1.0),dtype=N.float32),
                                        100.0,
                                        getArrayBuffer(verts),
                                        getElementBuffer(elements),
                                        len(elements),
                                        makeShader("worley.vert", "worley.frag")
                                        ))
        theMeshes[-1].moveBack(moveBackUnits)
        theMeshes[-1].moveRight(moveRightUnits)
        theMeshes[-1].moveUp(islandHeight)
        
        verts,elements = reflectedSurface(verts,
                                          elements)
        theMeshes.append(proceduralMesh(N.array((0.373,0.361,0.314,1.0),dtype=N.float32),
                                        N.array((0.475,0.475,0.475,1.0),dtype=N.float32),
                                        100.0,
                                        getArrayBuffer(verts),
                                        getElementBuffer(elements),
                                        len(elements),
                                        makeShader("worley.vert", "worleyinverted.frag")
                                        ))
        theMeshes[-1].moveBack(moveBackUnits)
        theMeshes[-1].moveRight(moveRightUnits)
        theMeshes[-1].moveUp(-islandHeight)

        #create top for tower
        verts,elements = cylinderTop(towerRadius, towerThickness, nArcs)
        theMeshes.append(proceduralMesh(N.array((0.373,0.361,0.314,1.0),dtype=N.float32),
                                        N.array((0.475,0.475,0.475,1.0),dtype=N.float32),
                                        100.0,
                                        getArrayBuffer(verts),
                                        getElementBuffer(elements),
                                        len(elements),
                                        makeShader("worley.vert", "worley.frag")
                                        ))
        theMeshes[-1].moveBack(moveBackUnits)
        theMeshes[-1].moveRight(moveRightUnits)
        theMeshes[-1].moveUp(towerHeight)
        theMeshes[-1].moveUp(islandHeight)

        verts,elements = reflectedSurface(verts, elements)
        theMeshes.append(proceduralMesh(N.array((0.373,0.361,0.314,1.0),dtype=N.float32),
                                        N.array((0.475,0.475,0.475,1.0),dtype=N.float32),
                                        100.0,
                                        getArrayBuffer(verts),
                                        getElementBuffer(elements),
                                        len(elements),
                                        makeShader("worley.vert", "worleyinverted.frag")
                                        ))
        theMeshes[-1].moveBack(moveBackUnits)
        theMeshes[-1].moveRight(moveRightUnits)
        theMeshes[-1].moveUp(-towerHeight)
        theMeshes[-1].moveUp(-islandHeight)
        
        #add crenellations to tower
        vertsList, elementsList = cylinderCrenellations(towerRadius,
                                                        towerHeight,
                                                        nArcs,
                                                        crenWidth,
                                                        crenHeight)
        for verts,elements in zip(vertsList,elementsList):
            theMeshes.append(proceduralMesh(N.array((0.373,0.361,0.314,1.0),dtype=N.float32),
                                            N.array((0.475,0.475,0.475,1.0),dtype=N.float32),
                                            100.0,
                                            getArrayBuffer(verts),
                                            getElementBuffer(elements),
                                            len(elements),
                                            makeShader("worley.vert", "worley.frag")
                                            ))
            theMeshes[-1].moveBack(moveBackUnits)
            theMeshes[-1].moveRight(moveRightUnits)
            theMeshes[-1].moveUp(islandHeight)
            theMeshes[-1].moveUp(towerHeight-0.01)
            
            verts,elements = reflectedSurface(verts, elements)
            theMeshes.append(proceduralMesh(N.array((0.373,0.361,0.314,1.0),dtype=N.float32),
                                            N.array((0.475,0.475,0.475,1.0),dtype=N.float32),
                                            100.0,
                                            getArrayBuffer(verts),
                                            getElementBuffer(elements),
                                            len(elements),
                                            makeShader("worley.vert", "worleyinverted.frag")
                                            ))
            theMeshes[-1].moveBack(moveBackUnits)
            theMeshes[-1].moveRight(moveRightUnits)
            theMeshes[-1].moveUp(-islandHeight)
            theMeshes[-1].moveUp(-towerHeight+0.01)

    #walls
    for wall in wallList:
        (wallWidth, wallHeight, wallThickness, numCren, crenHeight,
         moveBackUnits, moveRightUnits, yawUnits) = wall

        #create outside wall
        verts,elements = rectangle(wallWidth, wallHeight)
        theMeshes.append(proceduralMesh(N.array((0.373,0.361,0.314,1.0),dtype=N.float32),
                                        N.array((0.475,0.475,0.475,1.0),dtype=N.float32),
                                        100.0,
                                        getArrayBuffer(verts),
                                        getElementBuffer(elements),
                                        len(elements),
                                        makeShader("worley.vert", "worley.frag")
                                        ))
        theMeshes[-1].moveBack((moveBackUnits) * (castleRadius+(wallThickness/2)))
        theMeshes[-1].moveRight((moveRightUnits) * (castleRadius+(wallThickness/2)))
        theMeshes[-1].moveUp(wallHeight/2)
        theMeshes[-1].moveUp(islandHeight)
        yawCopy = yawUnits
        while (yawCopy < -1.0):
            theMeshes[-1].yaw(-1.0)
            yawCopy += 1.0
        theMeshes[-1].yaw(yawCopy)
        
        verts,elements = reflectedSurface(verts, elements)
        theMeshes.append(proceduralMesh(N.array((0.373,0.361,0.314,1.0),dtype=N.float32),
                                        N.array((0.475,0.475,0.475,1.0),dtype=N.float32),
                                        100.0,
                                        getArrayBuffer(verts),
                                        getElementBuffer(elements),
                                        len(elements),
                                        makeShader("worley.vert", "worleyinverted.frag")
                                        ))
        theMeshes[-1].moveBack((moveBackUnits) * (castleRadius+(wallThickness/2)))
        theMeshes[-1].moveRight((moveRightUnits) * (castleRadius+(wallThickness/2)))
        theMeshes[-1].moveUp(-wallHeight/2)
        theMeshes[-1].moveUp(-islandHeight)
        yawCopy = yawUnits
        while (yawCopy < -1.0):
            theMeshes[-1].yaw(-1.0)
            yawCopy += 1.0
        theMeshes[-1].yaw(yawCopy)

        #add crenellations to wall
        verts,elements,xcenters = rectangleCrenellation(wallWidth, numCren, crenHeight)
        rverts,relements = reflectedSurface(verts, elements)
        for i in range(numCren):
            theMeshes.append(proceduralMesh(N.array((0.373,0.361,0.314,1.0),dtype=N.float32),
                                        N.array((0.475,0.475,0.475,1.0),dtype=N.float32),
                                        100.0,
                                        getArrayBuffer(verts),
                                        getElementBuffer(elements),
                                        len(elements),
                                        makeShader("worley.vert", "worley.frag")
                                        ))
            theMeshes[-1].moveBack((moveBackUnits) * (castleRadius+(wallThickness/2)))
            theMeshes[-1].moveRight((moveRightUnits) * (castleRadius+(wallThickness/2)))
            theMeshes[-1].moveUp(crenHeight/2 + wallHeight)
            theMeshes[-1].moveUp(islandHeight)
            yawCopy = yawUnits
            while (yawCopy < -1.0):
                theMeshes[-1].yaw(-1.0)
                yawCopy += 1.0
            theMeshes[-1].yaw(yawCopy)
            theMeshes[-1].moveRight(xcenters[i] - wallWidth/2)
        
            theMeshes.append(proceduralMesh(N.array((0.373,0.361,0.314,1.0),dtype=N.float32),
                                        N.array((0.475,0.475,0.475,1.0),dtype=N.float32),
                                        100.0,
                                        getArrayBuffer(rverts),
                                        getElementBuffer(relements),
                                        len(relements),
                                        makeShader("worley.vert", "worleyinverted.frag")
                                        ))
            theMeshes[-1].moveBack((moveBackUnits) * (castleRadius+(wallThickness/2)))
            theMeshes[-1].moveRight((moveRightUnits) * (castleRadius+(wallThickness/2)))
            theMeshes[-1].moveUp(-crenHeight/2 - wallHeight)
            theMeshes[-1].moveUp(-islandHeight)
            yawCopy = yawUnits
            while (yawCopy < -1.0):
                theMeshes[-1].yaw(-1.0)
                yawCopy += 1.0
            theMeshes[-1].yaw(yawCopy)
            theMeshes[-1].moveRight(xcenters[i] - wallWidth/2)

        #create top of wall
        yout = N.square(((moveBackUnits) * (castleRadius+(wallThickness/2))))
        xout = N.square(((moveRightUnits) * (castleRadius+(wallThickness/2))))
        rout = N.sqrt(yout + xout)
        yin  = N.square(((moveBackUnits) * (castleRadius-(wallThickness/2))))
        xin  = N.square(((moveRightUnits) * (castleRadius-(wallThickness/2))))
        rin  = N.sqrt(yin + xin)
        wallTopThickness = rout - rin
        verts,elements = rectangle(wallWidth, wallTopThickness)
        theMeshes.append(proceduralMesh(N.array((0.373,0.361,0.314,1.0),dtype=N.float32),
                                        N.array((0.475,0.475,0.475,1.0),dtype=N.float32),
                                        100.0,
                                        getArrayBuffer(verts),
                                        getElementBuffer(elements),
                                        len(elements),
                                        makeShader("worley.vert", "worley.frag")
                                        ))
        theMeshes[-1].moveBack(moveBackUnits*castleRadius)
        theMeshes[-1].moveRight(moveRightUnits*castleRadius)
        theMeshes[-1].moveUp(wallHeight)
        theMeshes[-1].moveUp(islandHeight)
        yawCopy = yawUnits
        while (yawCopy < -1.0):
            theMeshes[-1].yaw(-1.0)
            yawCopy += 1.0
        theMeshes[-1].yaw(yawCopy)
        for i in range(2):
            theMeshes[-1].pitch(1.0)
        
        verts,elements = reflectedSurface(verts, elements)
        theMeshes.append(proceduralMesh(N.array((0.373,0.361,0.314,1.0),dtype=N.float32),
                                        N.array((0.475,0.475,0.475,1.0),dtype=N.float32),
                                        100.0,
                                        getArrayBuffer(verts),
                                        getElementBuffer(elements),
                                        len(elements),
                                        makeShader("worley.vert", "worleyinverted.frag")
                                        ))
        theMeshes[-1].moveBack(moveBackUnits*castleRadius)
        theMeshes[-1].moveRight(moveRightUnits*castleRadius)
        theMeshes[-1].moveUp(-wallHeight)
        theMeshes[-1].moveUp(-islandHeight)
        yawCopy = yawUnits
        while (yawCopy < -1.0):
            theMeshes[-1].yaw(-1.0)
            yawCopy += 1.0
        theMeshes[-1].yaw(yawCopy)
        for i in range(2):
            theMeshes[-1].pitch(1.0)
        
        #create inside wall
        verts,elements = rectangle(wallWidth, wallHeight)
        theMeshes.append(proceduralMesh(N.array((0.373,0.361,0.314,1.0),dtype=N.float32),
                                        N.array((0.475,0.475,0.475,1.0),dtype=N.float32),
                                        100.0,
                                        getArrayBuffer(verts),
                                        getElementBuffer(elements),
                                        len(elements),
                                        makeShader("worley.vert", "worley.frag")
                                        ))
        theMeshes[-1].moveBack((moveBackUnits) * (castleRadius-(wallThickness/2)))
        theMeshes[-1].moveRight((moveRightUnits) * (castleRadius-(wallThickness/2)))
        theMeshes[-1].moveUp(wallHeight/2)
        theMeshes[-1].moveUp(islandHeight)
        yawCopy = yawUnits
        while (yawCopy < -1.0):
            theMeshes[-1].yaw(-1.0)
            yawCopy += 1.0
        theMeshes[-1].yaw(yawCopy)
        for i in range(4):
            theMeshes[-1].yaw(-1.0)
        
        verts,elements = reflectedSurface(verts, elements)
        theMeshes.append(proceduralMesh(N.array((0.373,0.361,0.314,1.0),dtype=N.float32),
                                        N.array((0.475,0.475,0.475,1.0),dtype=N.float32),
                                        100.0,
                                        getArrayBuffer(verts),
                                        getElementBuffer(elements),
                                        len(elements),
                                        makeShader("worley.vert", "worleyinverted.frag")
                                        ))
        theMeshes[-1].moveBack((moveBackUnits) * (castleRadius-(wallThickness/2)))
        theMeshes[-1].moveRight((moveRightUnits) * (castleRadius-(wallThickness/2)))
        theMeshes[-1].moveUp(-wallHeight/2)
        theMeshes[-1].moveUp(-islandHeight)
        yawCopy = yawUnits
        while (yawCopy < -1.0):
            theMeshes[-1].yaw(-1.0)
            yawCopy += 1.0
        theMeshes[-1].yaw(yawCopy)
        for i in range(4):
            theMeshes[-1].yaw(-1.0)
       
    #add terrain
    verts,elements = islandAndMoat(islandHeight, surroundingLandHeight,
                                   moatDepth, islandRadius,
                                   moatInnerRadius, moatOuterRadius, outerShoreRadius,
                                   terrainXZMax, terrainNumSamples,
                                   noiseY = True, noiseVariance = 2.0, noiseScale = 0.2)
    theMeshes.append(flatTexturedMesh(loadTexture("grass.jpg", magFilter=GL_LINEAR),
                                      getArrayBuffer(verts),
                                      getElementBuffer(elements),
                                      len(elements),
                                      makeShader("flattextured.vert", "flattextured.frag"),
                                      scaleuv = N.array((32.0,32.0),dtype=N.float32),
                                      useFog = True,
                                      fogColor = N.array(skyColor,dtype=N.float32),
                                      fogStart = 300.0,
                                      fogEnd = 325.0
                                      ))
    verts,elements = reflectedSurface(verts, elements)
    theMeshes.append(flatTexturedMesh(loadTexture("grass.jpg", magFilter=GL_LINEAR),
                                      getArrayBuffer(verts),
                                      getElementBuffer(elements),
                                      len(elements),
                                      makeShader("flattextured.vert", "flattexturedinverted.frag"),
                                      scaleuv = N.array((32.0,32.0),dtype=N.float32),
                                      useFog = True,
                                      fogColor = N.array(skyColor,dtype=N.float32),
                                      fogStart = 300.0,
                                      fogEnd = 325.0
                                      ))
    
    # CAMERA
    width, height = theScreen.get_size()
    aspectRatio = float(width)/float(height)
    near = 0.01
    far = 325.0
    lens = 4.0  # "longer" lenses mean more telephoto
    theCamera = Camera(lens, near, far, aspectRatio)
    
    theCamera.moveBack(180.0)
    theCamera.moveUp(40.0)
    theCamera.moveRight(-40.0)
    theCamera.yaw(-.25)
    theCamera.pitch(.20)