from algorithms import * import sys import math import random import time from collections import deque from pyglet import image from pyglet.gl import * from pyglet.graphics import TextureGroup from pyglet.window import key, mouse class View(): def set_2d(game): """ Configure OpenGL to draw in 2d. """ width, height = game.get_size() glDisable(GL_DEPTH_TEST) viewport = game.get_viewport_size() glViewport(0, 0, max(1, viewport[0]), max(1, viewport[1])) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0, max(1, width), 0, max(1, height), -1, 1) glMatrixMode(GL_MODELVIEW) glLoadIdentity() def set_3d(game): """ Configure OpenGL to draw in 3d. """ width, height = game.get_size() glEnable(GL_DEPTH_TEST) viewport = game.get_viewport_size() glViewport(0, 0, max(1, viewport[0]), max(1, viewport[1])) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(65.0, width / float(height), 0.1, 60.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() x, y = game.rotation glRotatef(x, 0, 1, 0) glRotatef(-y, math.cos(math.radians(x)), 0, math.sin(math.radians(x))) x, y, z = game.position glTranslatef(-x, -y, -z) def draw_focused_block(game): """ Draw black edges around the block that is currently under the crosshairs. """ vector = game.get_sight_vector() block = game.world.hit_test(game.position, vector)[0] if block: x, y, z = block vertex_data = cube_vertices(x, y, z, 0.51) glColor3d(0, 0, 0) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) pyglet.graphics.draw(24, GL_QUADS, ('v3f/static', vertex_data)) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) def draw_label(game): """ Draw the label in the top left of the screen. """ x, y, z = game.position game.label.text = '%02d (%.2f, %.2f, %.2f) %d / %d' % ( pyglet.clock.get_fps(), x, y, z, len(game.world._shown), len(game.world.world)) game.label.draw() def draw_reticle(game): """ Draw the crosshairs in the center of the screen. """ glColor3d(0, 0, 0) game.reticle.draw(GL_LINES) def drawMenu(): pass def drawGame(game): View.setup() View.set_3d(game) glColor3d(1, 1, 1) game.world.batch.draw() View.draw_focused_block(game) View.set_2d(game) View.draw_label(game) View.draw_reticle(game) def drawSetting(): pass def setup_fog(): """ Configure the OpenGL fog properties. """ # Enable fog. Fog "blends a fog color with each rasterized pixel fragment's # post-texturing color." glEnable(GL_FOG) # Set the fog color. glFogfv(GL_FOG_COLOR, (GLfloat * 4)(0.5, 0.69, 1.0, 1)) # Say we have no preference between rendering speed and quality. glHint(GL_FOG_HINT, GL_DONT_CARE) # Specify the equation used to compute the blending factor. glFogi(GL_FOG_MODE, GL_LINEAR) # How close and far away fog starts and ends. The closer the start and end, # the denser the fog in the fog range. glFogf(GL_FOG_START, 20.0) glFogf(GL_FOG_END, 60.0) def setup(): """ Basic OpenGL configuration. """ # Set the color of "clear", i.e. the sky, in rgba. glClearColor(0.5, 0.69, 1.0, 1) # Enable culling (not rendering) of back-facing facets -- facets that aren't # visible to you. glEnable(GL_CULL_FACE) # Set the texture minification/magnification function to GL_NEAREST (nearest # in Manhattan distance) to the specified texture coordinates. GL_NEAREST # "is generally faster than GL_LINEAR, but it can produce textured images # with sharper edges because the transition between texture elements is not # as smooth." glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) View.setup_fog()