Example #1
0
class FloorSpots:
    def __init__(self, floor_color, background_color, y=0):
        self._floor_color = floor_color
        self._y = y
        self._display_list_id = None
        self._shader = Shader(
            radius = FLOOR_SPOT_RADIUS * 2 * FLOOR_GRID_SIZE * 1.5,
            background_color = background_color)

    def render(self, center_x, center_z, camera_x, camera_z):
        self._draw_floor_spots(
            center_x,
            center_z)
        self._shader.render(
            x=-camera_x,
            y=0,
            z=-camera_z)

    def _draw_floor_spots(self, center_x, center_z):
        cell_size = FLOOR_SPOT_RADIUS * 2
        quantified_center_x = int(center_x / cell_size / 2) * cell_size * 2
        quantified_center_z = int(center_z / cell_size) * cell_size
        for nx in range(FLOOR_GRID_SIZE):
            x = quantified_center_x + (nx - float(FLOOR_GRID_SIZE)/2 + 0.5) * cell_size
            if nx % 2 == 0:
                offset_z = 0
            else:
                offset_z = FLOOR_SPOT_RADIUS
            for nz in range(FLOOR_GRID_SIZE):
                z = offset_z + quantified_center_z + (nz - float(FLOOR_GRID_SIZE)/2 + 0.5) * cell_size
                self._draw_floor_spot(x=x, z=z)

    def _draw_floor_spot(self, x, z, y=0):
        if self._display_list_id is None:
            self._create_floor_spot_display_list()
        glPushMatrix()
        glTranslatef(x, y, z)
        glCallList(self._display_list_id)
        glPopMatrix()

    def _create_floor_spot_display_list(self, resolution=15):
        color_r, color_g, color_b, color_a = self._floor_color
        angle_increment = (float) (2 * math.pi / resolution);
        self._display_list_id = glGenLists(1)
        glNewList(self._display_list_id, GL_COMPILE)
        glBegin(GL_TRIANGLE_FAN)
        glColor4f(*self._floor_color)
        glVertex3f(0, 0, 0)
        glColor4f(color_r, color_g, color_b, 0)
        for i in range(resolution):
            angle1 = angle_increment * i
            angle2 = angle_increment * (i+1)
            glVertex3f(math.cos(angle1) * FLOOR_SPOT_RADIUS, 0, math.sin(angle1) * FLOOR_SPOT_RADIUS)
            glVertex3f(math.cos(angle2) * FLOOR_SPOT_RADIUS, 0, math.sin(angle2) * FLOOR_SPOT_RADIUS)
        glEnd()
        glEndList()
Example #2
0
class FloorGrid:
    def __init__(self, num_cells, size, floor_color, background_color, y=0):
        self._num_cells = num_cells
        self._size = size
        self._floor_color = floor_color
        self._y = y
        self._cell_size = float(self._size) / self._num_cells
        self._hypotenuse = math.sqrt(self._cell_size * self._cell_size * 2)
        self._shader = Shader(
            radius = math.sqrt(self._size * self._size * 2),
            background_color = background_color)

    def render(self, center_x, center_z, camera_x, camera_z):
        self._draw_grid(center_x, center_z)
        self._shader.render(-camera_x, 0, -camera_z)

    def _draw_grid(self, center_x, center_z):
        z1 = - self._size/2
        z2 = + self._size/2
        x1 = - self._size/2
        x2 = + self._size/2

        glColor4f(*self._floor_color)
        glLineWidth(1.4)

        quantified_center_x = int(center_x / self._hypotenuse) * self._hypotenuse
        quantified_center_z = int(center_z / self._hypotenuse) * self._hypotenuse
        glPushMatrix()
        glTranslatef(quantified_center_x, 0, quantified_center_z)
        glRotatef(45, 0, 1, 0)

        for n in range(self._num_cells):
            glBegin(GL_LINES)
            x = x1 + float(n) * self._cell_size
            glVertex3f(x, self._y, z1)
            glVertex3f(x, self._y, z2)
            glEnd()

        for n in range(self._num_cells):
            glBegin(GL_LINES)
            z = z1 + float(n) * self._cell_size
            glVertex3f(x1, self._y, z)
            glVertex3f(x2, self._y, z)
            glEnd()

        glPopMatrix()
Example #3
0
class FloorCheckerboard:
    def __init__(self, num_cells, size, floor_color, background_color,
                 board_color1, board_color2, y=0):
        self._num_cells = num_cells
        self._size = size
        self._board_color1 = board_color1
        self._board_color2 = board_color2
        self._y = y
        self._cell_size = float(self._size) / self._num_cells
        self._hypotenuse = math.sqrt(self._cell_size * self._cell_size * 2)
        self._shader = Shader(
            radius = math.sqrt(self._size * self._size * 2) * 0.55,
            background_color = background_color)
        self._grid_display_list_id = None

    def render(self, center_x, center_z, camera_x, camera_z):
        self._draw_grid(center_x, center_z)
        self._shader.render(-camera_x, 0, -camera_z)

    def _draw_grid(self, center_x, center_z):
        quantified_center_x = int(center_x / self._hypotenuse) * self._hypotenuse
        quantified_center_z = int(center_z / self._hypotenuse) * self._hypotenuse
        glPushMatrix()
        glTranslatef(quantified_center_x, 0, quantified_center_z)
        glRotatef(45, 0, 1, 0)

        if self._grid_display_list_id is None:
            self._grid_display_list_id = self._create_grid_display_list()
        glCallList(self._grid_display_list_id)

        glPopMatrix()

    def _create_grid_display_list(self):
        display_list_id = glGenLists(1)
        glEnable(GL_POLYGON_SMOOTH)
        glNewList(display_list_id, GL_COMPILE)

        radius = float(self._size) / 2
        glColor4f(*self._board_color1)
        angle_increment = 2 * math.pi / RESOLUTION
        glBegin(GL_TRIANGLE_FAN)
        glVertex3f(0, 0, 0)
        for i in range(RESOLUTION):
            angle1 = angle_increment * i
            angle2 = angle_increment * (i+1)
            glVertex3f(math.cos(angle1) * radius, 0, math.sin(angle1) * radius)
            glVertex3f(math.cos(angle2) * radius, 0, math.sin(angle2) * radius)
        glEnd()
        
        glColor4f(*self._board_color2)
        n = 0
        for nx in range(self._num_cells - 1):
            x1 = -self._size/2 + float(nx)   * self._cell_size
            x2 = -self._size/2 + float(nx+1) * self._cell_size
            for nz in range(self._num_cells - 1):
                cell_distance = math.sqrt(
                    pow(nx - float(self._num_cells)/2, 2) +
                    pow(nz - float(self._num_cells)/2, 2))
                if(n % 2 == 1 and cell_distance < float(self._num_cells)/2):
                    z1 = -self._size/2 + float(nz)   * self._cell_size
                    z2 = -self._size/2 + float(nz+1) * self._cell_size
                    glBegin(GL_QUADS)
                    glVertex3f(x1, self._y, z1)
                    glVertex3f(x2, self._y, z1)
                    glVertex3f(x2, self._y, z2)
                    glVertex3f(x1, self._y, z2)
                    glVertex3f(x1, self._y, z1)
                    glEnd()
                n += 1

        glEndList()
        return display_list_id