hi all,
i'm new on this list. i write small ' engine ' for dungeon like game
[DM, EOB or simmilar].
pyglet using for rendering textures instead of making pseudo 3d
images.
whats the problem. i cannot set right FOV for display.
there is small example of my work.
please kick me to the right way coz im new in OGL.
how i set projection for displaying blocks like in Dungeon Master.
thanks.
<pre><code>
#!/usr/bin/python
#encoding: utf-8
from pyglet.gl import *
from pyglet import window
from pyglet.window import key
from pyglet import image
import pyglet.clock
import os
D_East, D_South, D_West, D_North = range(4)
M_Front, M_Back, M_Left, M_Right, M_Up, M_Down = range(6)
matrix = [[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,1,1,0,0,1,1,0,0,1],
[1,0,0,0,0,0,0,0,1,0,0,0,1,2,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,1,2,0,1],
[1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1],
[1,0,0,1,0,0,0,0,0,0,0,0,1,2,0,1],
[1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1],
[1,0,0,1,0,0,0,0,0,0,0,0,1,2,0,1],
[1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1],
[1,0,0,2,0,2,0,2,0,2,0,2,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]]
class Player(object):
def __init__(self, mapa):
self.dir = D_North
self.mapa = mapa
def set_pos(self, pos):
self.pos = pos
self.posx = pos[1]
self.posy = pos[0]
def TurnLeft(self):
if self.mapa.dir == D_East: self.mapa.dir = D_North
elif self.mapa.dir == D_South: self.mapa.dir = D_East
elif self.mapa.dir == D_West: self.mapa.dir = D_South
elif self.mapa.dir == D_North: self.mapa.dir = D_West
def TurnRight(self):
if self.mapa.dir == D_East: self.mapa.dir = D_South
elif self.mapa.dir == D_South: self.mapa.dir = D_West
elif self.mapa.dir == D_West: self.mapa.dir = D_North
elif self.mapa.dir == D_North: self.mapa.dir = D_East
def Move(self, where):
mapa = self.mapa
xx = mapa.mapx
yy = mapa.mapy
if where == M_Front:
if mapa.dir == D_East: xx += 1
elif mapa.dir == D_South: yy += 1
elif mapa.dir == D_West: xx -= 1
elif mapa.dir == D_North: yy -= 1
elif where == M_Back:
if mapa.dir == D_East: xx -= 1
elif mapa.dir == D_South: yy -= 1
elif mapa.dir == D_West: xx += 1
elif mapa.dir == D_North: yy += 1
elif where == M_Left:
if mapa.dir == D_East: yy -= 1
elif mapa.dir == D_South: xx += 1
elif mapa.dir == D_West: yy += 1
elif mapa.dir == D_North: xx -= 1
elif where == M_Right:
if mapa.dir == D_East: yy += 1
elif mapa.dir == D_South: xx -= 1
elif mapa.dir == D_West: yy -= 1
elif mapa.dir == D_North: xx += 1
if mapa.get_map(xx, yy) <> 1:
mapa.mapx = xx
mapa.mapy = yy
class Mapa(object):
def __init__(self, mapa):
self.width = 16
self.height = 16
self.levels = 1
self.mapx = 5
self.mapy = 5
self.dir = D_North
self.blocks = mapa
def get_map(self, x, y):
if (x < 0) or (y < 0):
return 0
if (x >= self.width) or (y >= self.height):
return 0
return self.blocks[y][x]
def set_block_rel(self, x, y):
id, x, y = self.get_map_rel_xy(x, y)
print id, x, y
if id == 0:
self.blocks[y][x] = 1
else:
self.blocks[y][x] = 0
def get_map_rel_xy(self, x, y):
if self.dir == D_East:
xx = self.mapx - y
yy = self.mapy + x
if self.dir == D_North:
xx = self.mapx + x
yy = self.mapy + y
if self.dir == D_West:
xx = self.mapx + y
yy = self.mapy - x
if self.dir == D_South:
xx = self.mapx - x
yy = self.mapy - y
return self.get_map(xx, yy),xx, yy
def get_map_rel(self, x, y):
if self.dir == D_East:
xx = self.mapx - y
yy = self.mapy + x
if self.dir == D_North:
xx = self.mapx + x
yy = self.mapy + y
if self.dir == D_West:
xx = self.mapx + y
yy = self.mapy - x
if self.dir == D_South:
xx = self.mapx - x
yy = self.mapy - y
return self.get_map(xx, yy)
class Game:
def __init__(self):
self.win = window.Window(width=640, height=480, visible=False)
self.win.on_resize = self.on_resize
self.win.on_key_press = self.on_key_press
self.win.on_draw = self.on_draw
self.clock = pyglet.clock.Clock()
self.load_textures()
glEnable(GL_TEXTURE_2D)
glShadeModel(GL_SMOOTH)
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
# quick dirty light
glEnable(GL_LIGHT0)
glEnable(GL_LIGHTING)
glEnable(GL_COLOR_MATERIAL)
# mlha
self.fogMode = [GL_EXP, GL_EXP2, GL_LINEAR]
self.fogFilter = 0
fogColor = (GLfloat*4)(0.5, 0.5, 0.5, 1.0)
glClearColor(0.5, 0.5, 0.5, 1.0)
glFogi(GL_FOG_MODE, self.fogMode[self.fogFilter])
glFogfv(GL_FOG_COLOR, fogColor)
glFogf(GL_FOG_DENSITY, 0.35)
glHint(GL_FOG_HINT, GL_DONT_CARE)
glFogf(GL_FOG_START, 1.0)
glFogf(GL_FOG_END, -5.0)
glEnable(GL_FOG)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
self.win.set_visible()
self.mapa = Mapa(matrix)
self.player = Player(self.mapa)
def load_textures(self):
texturefile = os.path.join('gfx','crate2.bmp')
textureSurface = image.load(texturefile)
self.texture = textureSurface.texture
glBindTexture(GL_TEXTURE_2D, self.texture.id)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
self.paste_cube()
def paste_cube(self):
self.box = glGenLists(3) # 3 listy, kostky a podlaha a strop
glNewList(self.box, GL_COMPILE)
glBegin(GL_QUADS)
# Front Face (note that the texture's corners have to match the
quad's corners)
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 1.0) # Bottom Left Of
The Texture and Quad
glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, 1.0) # Bottom Right
Of The Texture and Quad
glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 1.0) # Top Right Of
The Texture and Quad
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 1.0) # Top Left Of
The Texture and Quad
# Back Face
#glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0) # Bottom Right
Of The Texture and Quad
#glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, -1.0) # Top Right Of
The Texture and Quad
#glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, 1.0, -1.0) # Top Left Of
The Texture and Quad
#glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, -1.0) # Bottom Left
Of The Texture and Quad
# Top Face
#glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0) # Top Left Of
The Texture and Quad
#glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, 1.0, 1.0) # Bottom Left
Of The Texture and Quad
#glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, 1.0, 1.0) # Bottom Right
Of The Texture and Quad
#glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, -1.0) # Top Right Of
The Texture and Quad
# Bottom Face
#glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, -1.0, -1.0) # Top Right Of
The Texture and Quad
#glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, -1.0, -1.0) # Top Left Of
The Texture and Quad
#glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, 1.0) # Bottom Left
Of The Texture and Quad
#glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, 1.0) # Bottom Right
Of The Texture and Quad
# Right face
glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.0) # Bottom Right
Of The Texture and Quad
glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, -1.0) # Top Right Of
The Texture and Quad
glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, 1.0, 1.0) # Top Left Of
The Texture and Quad
glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, 1.0) # Bottom Left Of
The Texture and Quad
# Left Face
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0) # Bottom Left Of
The Texture and Quad
glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, 1.0) # Bottom Right
Of The Texture and Quad
glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, 1.0) # Top Right Of
The Texture and Quad
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0) # Top Left Of
The Texture and Quad
glEnd()
glEndList()
self.ground = self.box + 1
glNewList(self.ground, GL_COMPILE)
glBegin(GL_QUADS)
# Top Face
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0) # Top Left Of
The Texture and Quad
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, 1.0, 1.0) # Bottom Left Of
The Texture and Quad
glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, 1.0, 1.0) # Bottom Right
Of The Texture and Quad
glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, -1.0) # Top Right Of
The Texture and Quad
glEnd()
glEndList()
self.top = self.ground + 1
glNewList(self.top, GL_COMPILE)
glBegin(GL_QUADS)
# Bottom Face
glTexCoord2f(1.0, 1.0); glVertex3f(-1, -1, -1) # Top Right Of The
Texture and Quad
glTexCoord2f(0.0, 1.0); glVertex3f( 1, -1, -1) # Top Left Of The
Texture and Quad
glTexCoord2f(0.0, 0.0); glVertex3f( 1, -1, 1) # Bottom Left Of The
Texture and Quad
glTexCoord2f(1.0, 0.0); glVertex3f(-1, -1, 1) # Bottom Right Of The
Texture and Quad
glEnd()
glEndList()
def on_draw(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
coord = self.mapa.get_map_rel
for y in [-5,-4, -3, -2, -1]:
for x in [-3, 3, -2, 2, -1, 1, 0]:
ident = coord(x, y)
if ident > 0:
glLoadIdentity()
glTranslatef(x*2,0.0,y*2)
glCallList(self.box)
glLoadIdentity()
glTranslatef(x*2, -2.0, y*2)
glCallList(self.ground)
glLoadIdentity()
glTranslatef(x*2, 2.0, y*2)
glCallList(self.top)
def on_resize(self, width, height):
if height==0:
height=1
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(90, 1.0*width/height, 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def on_key_press(self, sym, mod):
if sym == key.ESCAPE:
self.win.has_exit = True
if sym == key.UP:
self.player.Move(M_Front)
if sym == key.DOWN:
self.player.Move(M_Back)
if sym == key.LEFT:
self.player.TurnLeft()
if sym == key.RIGHT:
self.player.TurnRight()
if sym == key.SPACE:
self.mapa.set_block_rel(0, -1)
if sym == key.F:
self.fogFilter += 1
if self.fogFilter > len(self.fogMode):
self.fogFilter = 0
glFogi(GL_FOG_MODE, self.fogMode[self.fogFilter])
if sym == key.D:
glDisable(GL_FOG)
if __name__ == '__main__':
app = Game()
pyglet.app.run()
</code></pre>
--
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pyglet-users?hl=en.