Пример #1
0
 def draw(self, *args, **kwargs):
     Shader.upload_model(Matrix.identity())
     glActiveTexture(GL_TEXTURE1)
     self.normal[0].texture_bind()
     glActiveTexture(GL_TEXTURE0)
     self.texture[0].texture_bind()
     self.vbo.draw(*args, **kwargs)
Пример #2
0
    def render_world(self, camera):
        view = Matrix.lookat(
            camera.x + 19, camera.y, 15,
            camera.x + 19, camera.y, 0,
            0,1,0)

        with self.fbo as frame:
            frame.clear(0,0.03,0.15,1)

            Shader.upload_projection_view(self.projection, view)
            Shader.upload_game(None)
            Shader.upload_light(self.ambient_light, self.cull_lights())

            # parallax background
            pm = Matrix.transform(
                0.75 * camera.x, camera.y * 0.5 - 12, 0,
                (19*4) * self.parallax_rep, 19, 0
            )
            self.shader_hud.bind()
            self.parallax.texture_bind()
            Shader.upload_model(pm)
            self.repquad.draw()

            Shader.upload_projection_view(self.projection, view)

            self.shader.bind()
            self.map.draw()

            # entities
            for obj in self.map.obj:
                obj.draw()
            if not self.is_over:
                self.projectile.draw()
Пример #3
0
    def __init__(self, name, x, y, properties={}, **kwargs):
        self.name = name
        self.pos = Vector2f(x,-y) * (1.0 / 8)
        self.mat = self.model_matrix()
        self.killed = False

        if 'texture' in properties:
            self.diffuse = properties['texture']

        if 'shader' in properties:
            self.shader_name = properties['shader']

        self.load_sprite(self.diffuse, self.normal)
        self.shader = Shader.load(self.shader_name)

        if self.shader is None:
            raise AttributeError, 'Failed to load shader %s' % self.shader_name
Пример #4
0
    def __init__(self,vs: str,fs: str,MaxText=8):
        self.MaxQuadCount = 2000
        self.MaxVertexCount = self.MaxQuadCount * 4
        self.MaxIndexCount = self.MaxQuadCount * 6
        self.MaxTextures = MaxText
        self.QuadVA = 0
        self.QuadVB = 0
        self.QuadIB = 0
        self.WhiteTexture = 0 #The first texture unit is reserv for a 1x1 white texture
        self.IndexCount = 0
        self.QuadBuffer = [Vertex()]
        self.QuadBufferPtr = 0
        self.TextureSlots = [0] * self.MaxTextures        
        self.TextureSlotIndex = 1
        self.TexCoords = [vec2(0,0), vec2(1,0), vec2(1,1), vec2(0,1)]
        self.shader = Shader(vs,fs)

        print("Texture Units: " + str(self.MaxTextures))

        self.QuadVA = glGenVertexArrays(1)
        glBindVertexArray(self.QuadVA)

        self.QuadVB = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, self.QuadVB)
        glBufferData(GL_ARRAY_BUFFER, self.MaxVertexCount * 10 * sizeOfFloat, None, GL_DYNAMIC_DRAW)

        #Layout Position
        glEnableVertexAttribArray(0)
        glVertexAttribPointer(0,3,GL_FLOAT, GL_FALSE, 10*sizeOfFloat,ctypes.c_void_p(0)) #The last parm is the offset, can't find a offset function
        
        #Layout Color
        glEnableVertexAttribArray(1)
        glVertexAttribPointer(1,4,GL_FLOAT, GL_FALSE, 10*sizeOfFloat,ctypes.c_void_p(12))

        #Layout TexCoords
        glEnableVertexAttribArray(2)
        glVertexAttribPointer(2,2,GL_FLOAT, GL_FALSE, 10*sizeOfFloat,ctypes.c_void_p(28))

        #Layout TexIndex
        glEnableVertexAttribArray(3)
        glVertexAttribPointer(3,1,GL_FLOAT, GL_FALSE, 10*sizeOfFloat,ctypes.c_void_p(36))
        
        self.shader.Bind()
        loc = self.shader.GetUniformLocation("u_Textures")
        samplers = []
        for i in range(0,self.MaxTextures,1):
            samplers.append(i)
        glUniform1iv(loc, self.MaxTextures ,numpy.array(samplers,dtype=numpy.int32))

        indices = [0] * self.MaxIndexCount
        offset = 0
        for i in range(0,self.MaxIndexCount,6):
            indices[i + 0] = 0 + offset
            indices[i + 1] = 1 + offset
            indices[i + 2] = 2 + offset

            indices[i + 3] = 2 + offset
            indices[i + 4] = 3 + offset
            indices[i + 5] = 0 + offset

            offset += 4
        
        self.QuadIB = glGenBuffers(1)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,self.QuadIB)
        glBufferData(GL_ELEMENT_ARRAY_BUFFER,size=None,data=numpy.array(indices,dtype=numpy.uint32),usage=GL_STATIC_DRAW)

        self.WhiteTexture = glGenTextures(1)
        glBindTexture(GL_TEXTURE_2D, self.WhiteTexture)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
        
        white_color = [[255,255,255,255]]
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, white_color)

        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)

        self.TextureSlots[0] = self.WhiteTexture
        for i in range(1,self.MaxTextures,1):
            self.TextureSlots[i] = 0
Пример #5
0
class Render2D:
    def __init__(self,vs: str,fs: str,MaxText=8):
        self.MaxQuadCount = 2000
        self.MaxVertexCount = self.MaxQuadCount * 4
        self.MaxIndexCount = self.MaxQuadCount * 6
        self.MaxTextures = MaxText
        self.QuadVA = 0
        self.QuadVB = 0
        self.QuadIB = 0
        self.WhiteTexture = 0 #The first texture unit is reserv for a 1x1 white texture
        self.IndexCount = 0
        self.QuadBuffer = [Vertex()]
        self.QuadBufferPtr = 0
        self.TextureSlots = [0] * self.MaxTextures        
        self.TextureSlotIndex = 1
        self.TexCoords = [vec2(0,0), vec2(1,0), vec2(1,1), vec2(0,1)]
        self.shader = Shader(vs,fs)

        print("Texture Units: " + str(self.MaxTextures))

        self.QuadVA = glGenVertexArrays(1)
        glBindVertexArray(self.QuadVA)

        self.QuadVB = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, self.QuadVB)
        glBufferData(GL_ARRAY_BUFFER, self.MaxVertexCount * 10 * sizeOfFloat, None, GL_DYNAMIC_DRAW)

        #Layout Position
        glEnableVertexAttribArray(0)
        glVertexAttribPointer(0,3,GL_FLOAT, GL_FALSE, 10*sizeOfFloat,ctypes.c_void_p(0)) #The last parm is the offset, can't find a offset function
        
        #Layout Color
        glEnableVertexAttribArray(1)
        glVertexAttribPointer(1,4,GL_FLOAT, GL_FALSE, 10*sizeOfFloat,ctypes.c_void_p(12))

        #Layout TexCoords
        glEnableVertexAttribArray(2)
        glVertexAttribPointer(2,2,GL_FLOAT, GL_FALSE, 10*sizeOfFloat,ctypes.c_void_p(28))

        #Layout TexIndex
        glEnableVertexAttribArray(3)
        glVertexAttribPointer(3,1,GL_FLOAT, GL_FALSE, 10*sizeOfFloat,ctypes.c_void_p(36))
        
        self.shader.Bind()
        loc = self.shader.GetUniformLocation("u_Textures")
        samplers = []
        for i in range(0,self.MaxTextures,1):
            samplers.append(i)
        glUniform1iv(loc, self.MaxTextures ,numpy.array(samplers,dtype=numpy.int32))

        indices = [0] * self.MaxIndexCount
        offset = 0
        for i in range(0,self.MaxIndexCount,6):
            indices[i + 0] = 0 + offset
            indices[i + 1] = 1 + offset
            indices[i + 2] = 2 + offset

            indices[i + 3] = 2 + offset
            indices[i + 4] = 3 + offset
            indices[i + 5] = 0 + offset

            offset += 4
        
        self.QuadIB = glGenBuffers(1)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,self.QuadIB)
        glBufferData(GL_ELEMENT_ARRAY_BUFFER,size=None,data=numpy.array(indices,dtype=numpy.uint32),usage=GL_STATIC_DRAW)

        self.WhiteTexture = glGenTextures(1)
        glBindTexture(GL_TEXTURE_2D, self.WhiteTexture)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
        
        white_color = [[255,255,255,255]]
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, white_color)

        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)

        self.TextureSlots[0] = self.WhiteTexture
        for i in range(1,self.MaxTextures,1):
            self.TextureSlots[i] = 0
        
    def ShutDown(self):
        glDeleteVertexArrays(1,self.QuadVA)
        glDeleteBuffers(1,self.QuadVB)
        glDeleteBuffers(1,self.QuadIB)
        glDeleteTextures(1,self.WhiteTexture)
    
    def BeginBatch(self):
        self.QuadBuffer = []
        self.QuadBufferPtr = 0

    def ToRaw(self):
        ret = []
        for i in range(0,self.QuadBufferPtr,1):
            ret.extend(self.QuadBuffer[i].Raw())
        return ret
    
    def EndBatch(self):
        glBindBuffer(GL_ARRAY_BUFFER,self.QuadVB)
        data = numpy.array(self.ToRaw(),dtype=numpy.float32)
        glBufferSubData(GL_ARRAY_BUFFER,0,size=None,data=data)
    
    def BindShader(self):
        self.shader.Bind()
    
    def UnbindShader(self):
        self.shader.Unbind()

    def Flush(self):
        self.shader.Bind()
        for i in range(0,self.TextureSlotIndex,1):
            glActiveTexture(GL_TEXTURE0 + i)
            glBindTexture(GL_TEXTURE_2D,self.TextureSlots[i])
        
        glBindVertexArray(self.QuadVA)
        glDrawElements(GL_TRIANGLES,self.IndexCount,GL_UNSIGNED_INT,None)

        self.IndexCount = 0
        self.TextureSlotIndex = 1
    
    def FillVertices(self,position: vec2,size: vec2,color: vec4,texCoords: list,texIndex: float, rotation=None, axis=None):
        
        quadVertices = [
            vec3(position.x,position.y,0.0),
            vec3(position.x + size.x,position.y,0.0),
            vec3(position.x + size.x,position.y + size.y,0.0),
            vec3(position.x,position.y + size.y,0.0)
        ]
        
        if rotation != None:
            if axis == None:
                axis = vec3(0.0,0.0,1.0)
            quadVertices = self.RotateVertices(quadVertices,rotation,vec3(position.x + (size.x / 2), position.y + (size.y / 2), 0.0), axis)

        v = Vertex()
        v.Position = quadVertices[0]
        v.Color = color
        v.TexCoords = texCoords[0]
        v.TexIndex = texIndex
        self.QuadBuffer.append(v)
        self.QuadBufferPtr += 1

        f = Vertex()
        f.Position = quadVertices[1]
        f.Color = color
        f.TexCoords = texCoords[1]
        f.TexIndex = texIndex
        self.QuadBuffer.append(f)
        self.QuadBufferPtr += 1

        g = Vertex()
        g.Position = quadVertices[2]
        g.Color = color
        g.TexCoords = texCoords[2]
        g.TexIndex = texIndex
        self.QuadBuffer.append(g)
        self.QuadBufferPtr += 1

        h = Vertex()
        h.Position = quadVertices[3]
        h.Color = color
        h.TexCoords = texCoords[3]
        h.TexIndex = texIndex
        self.QuadBuffer.append(h)
        self.QuadBufferPtr += 1
    
    def RotateVertices(self,vertices: list,angle: float,rotationCenter: vec3,axis: vec3):
        
        translationMatrix = translate(identity(mat4), rotationCenter - (rotationCenter*2))
        rotationMatrix = rotate(identity(mat4),angle,axis)
        reverseTranslationMatrix = translate(identity(mat4),rotationCenter)

        for i in range(0,4,1):
            vertices[i] = vec3(reverseTranslationMatrix * rotationMatrix * translationMatrix * vec4(vertices[i],1.0))
        
        return vertices
    
    def DrawQuad(self, position: vec2, size: vec2, color: vec4,rotation=None,axis=None):
        
        if self.IndexCount >= self.MaxIndexCount:
            self.EndBatch()
            self.Flush()
            self.BeginBatch()
        
        textureIndex = 0.0

        self.FillVertices(position,size,color,self.TexCoords,textureIndex,rotation,axis)
        self.IndexCount += 6
    
    def DrawQuadTexture(self,position: vec2,size: vec2,texId: int,color=vec4(1),rotation=None,axis=None):
        if self.IndexCount >= self.MaxIndexCount or self.TextureSlotIndex > self.MaxTextures - 1:
            self.EndBatch()
            self.Flush()
            self.BeginBatch()
        
        tx = self.TexCoords

        #if it's a Texture get texID
        if isinstance(texId,Texture):
            texId = texId.TexID
        
        #if it's a SubTexture get TexCoords and texID
        if isinstance(texId,SubTexture):
            tx = texId.TexCoords
            texId = texId.TexID

        textureIndex = 0

        #Look if this texture already exist in the current batch
        for i in range(1,self.TextureSlotIndex,1):
            if self.TextureSlots[i] == texId:
                textureIndex = float(i)
                break
        
        if textureIndex == 0:
            textureIndex = float(self.TextureSlotIndex)
            self.TextureSlots[self.TextureSlotIndex] = texId
            self.TextureSlotIndex += 1
        
        self.FillVertices(position,size,color,tx,textureIndex,rotation,axis)
        self.IndexCount += 6
Пример #6
0
    def init(self, size, fullscreen=False):
        flags = OPENGL|DOUBLEBUF
        if fullscreen:
            flags |= FULLSCREEN

        pygame.display.set_mode(size.xy, flags)
        pygame.display.set_caption('Ancient Earth')

        i = pygame.display.Info()
        self.size = Vector2i(i.current_w, i.current_h)

        glMatrixMode(GL_MODELVIEW)
        glEnable(GL_TEXTURE_2D)
        glDisable(GL_CULL_FACE)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)

        image.setup()

        self.stage = 1
        self.projection = Matrix.perspective(75, self.size, 0.1, 100)
        self.ortho = Matrix.ortho(self.size)

        v = np.array([
                0,0,0, 0,0,
                1,0,0, 1,0,
                1,1,0, 1,1,
                0,1,0, 0,1,
                ], np.float32)
        i = np.array([0,1,2,3], np.uint32)
        self.quad = VBO(GL_QUADS, v, i)

        # parallax
        self.parallax_rep = 10
        v = np.array([
                0,0,0, 0,1,
                1,0,0, self.parallax_rep, 1,
                1,1,0, self.parallax_rep, 0,
                0,1,0, 0,0,
                ], np.float32)
        i = np.array([0,1,2,3], np.uint32)
        self.repquad = VBO(GL_QUADS, v, i)
        self.parallax = Image('texture/sky.png', wrap=GL_REPEAT)
        self.hudbg = Image('texture/hud_bottom.png')

        self.fbo = FBO(self.size, format=GL_RGB8, depth=True)

        self.shader = Shader.load('default')
        self.shader_hud = Shader.load('hud')
        self.post = Shader.load('post')
        self.windmax = 1.0
        self.random_wind()

        self.ambient_light = (1.0, 1.0, 1.0)

        fontsize = 16 + int(self.res_hack() * 14)

        self.map = Map('map.json')
        self.clock = pygame.time.Clock()
        self.hud_msgbox = HUD(Vector2i(500,100), 'msgbox')
        self.hud_ui = HUD(Vector2i(self.size.x, self.size.x * (160./800)), 'ui')
        self.scrollbar = HUD(Vector2i(self.size.x,28), 'scrollbar')
        self.font = self.hud_msgbox.create_font(size=fontsize)
        self.font_ui = self.hud_ui.create_font(size=fontsize, font='Comic Sans MS')
        self.camera_max = self.map.width - 38
        self.catapults = [self.map.get_named_item('Catapult 1'), self.map.get_named_item('Catapult 2')]

        self.reset()

        with self.hud_msgbox:
            self.hud_msgbox.clear((0,1,1,1))
Пример #7
0
    def render(self):
        glClearColor(1,0,1,1)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

        camera = self.camera.copy()
        if self.projectile:
            camera.x = min(max(self.projectile.pos.x - 19, 0), self.camera_max)
            self.follow_cam = camera
        elif self.follow_cam:
            camera = self.follow_cam

        self.render_hud(camera)
        self.render_world(camera)

        mat = Matrix.scale(self.size.x, self.size.y)
        Shader.upload_projection_view(self.ortho, Matrix.identity())
        Shader.upload_model(mat)

        self.fbo.bind_texture()
        self.post.bind()
        self.quad.draw()

        y = self.size.x * (160./800)

        # hud background
        pm = Matrix.transform(
            0, y, 0,
            self.size.x, -y, 1
        )
        self.shader_hud.bind()
        self.hudbg.texture_bind()
        Shader.upload_model(pm)
        self.quad.draw()

        # ui
        pm = Matrix.transform(
            0, self.hud_ui.height, 0,
            self.hud_ui.width, -self.hud_ui.height, 1
        )
        self.hud_ui.draw()
        self.shader_hud.bind()
        Shader.upload_model(pm)
        self.quad.draw()

        # messagebox
        mat = Matrix.translate(self.size.x / 2 - self.hud_msgbox.width / 2, self.size.y - self.hud_msgbox.height)
        Shader.upload_model(mat)
        self.hud_msgbox.draw()

        # scrollbar
        mat = Matrix.translate(0, y-22)
        Shader.upload_model(mat)
        self.scrollbar.draw()

        pygame.display.flip()
Пример #8
0
 def draw(self):
     Shader.upload_model(self.mat)
     self.shader.bind()
     self.sprite.draw()