1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
import OpenGL
from OpenGL.GL import *
from OpenGL.GLU import *
from sfml import sf
# create the main window
window = sf.RenderWindow(sf.VideoMode(800, 600), "pySFML - OpenGL", sf.Style.DEFAULT, sf.ContextSettings(32))
window.vertical_synchronization = True
# load a font for drawing some text
font = sf.Font.from_file("resources/sansation.ttf")
# create a sprite for the background
background_texture = sf.Texture.from_file("resources/background.jpg")
background = sf.Sprite(background_texture)
# load an OpenGL texture.
# we could directly use a sf.Texture as an OpenGL texture (with its bind() member function),
# but here we want more control on it (generate mipmaps, ...) so we create a new one from an image
texture = 0
image = sf.Image.from_file("resources/texture.jpg")
glGenTextures(1, texture)
glBindTexture(GL_TEXTURE_2D, texture)
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image.size.x, image.size.y, GL_RGBA, GL_UNSIGNED_BYTE, image.pixels.data)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
# enable Z-buffer read and write
glEnable(GL_DEPTH_TEST)
glDepthMask(GL_TRUE)
glClearDepth(1.)
# setup a perspective projection
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(90., 1., 1., 500.)
# bind our texture
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, texture)
glColor4f(1., 1., 1., 1.)
# create a clock for measuring the time elapsed
clock = sf.Clock()
# start game loop
while window.is_open:
# process events
for event in window.events:
# close window : exit
if event == sf.CloseEvent:
window.close()
# escape key : exit
if event == sf.KeyEvent and event.code == sf.Keyboard.ESCAPE:
window.close()
# adjust the viewport when the window is resized
if event == sf.ResizeEvent:
glViewport(0, 0, event.width, event.height)
# draw the background
window.push_GL_states()
window.draw(background)
window.pop_GL_states()
# activate the window before using OpenGL commands.
# this is useless here because we have only one window which is
# always the active one, but don't forget it if you use multiple windows
window.active = True
# clear the depth buffer
glClear(GL_DEPTH_BUFFER_BIT);
# we get the position of the mouse cursor, so that we can move the box accordingly
x = sf.Mouse.get_position(window).x * 200. / window.size.x - 100.
y = -sf.Mouse.get_position(window).y * 200. / window.size.y + 100.
# apply some transformations
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(x, y, -100.)
glRotatef(clock.elapsed_time.seconds * 50., 1., 0., 0.)
glRotatef(clock.elapsed_time.seconds * 30., 0., 1., 0.)
glRotatef(clock.elapsed_time.seconds * 90., 0., 0., 1.)
# draw a cube
size = 20.
glBegin(GL_QUADS)
glTexCoord2f(0, 0)
glVertex3f(-size, -size, -size)
glTexCoord2f(0, 1)
glVertex3f(-size, size, -size)
glTexCoord2f(1, 1)
glVertex3f( size, size, -size)
glTexCoord2f(1, 0)
glVertex3f( size, -size, -size)
glTexCoord2f(0, 0)
glVertex3f(-size, -size, size)
glTexCoord2f(0, 1)
glVertex3f(-size, size, size)
glTexCoord2f(1, 1)
glVertex3f( size, size, size)
glTexCoord2f(1, 0)
glVertex3f( size, -size, size)
glTexCoord2f(0, 0)
glVertex3f(-size, -size, -size)
glTexCoord2f(0, 1)
glVertex3f(-size, size, -size)
glTexCoord2f(1, 1)
glVertex3f(-size, size, size)
glTexCoord2f(1, 0)
glVertex3f(-size, -size, size)
glTexCoord2f(0, 0)
glVertex3f(size, -size, -size)
glTexCoord2f(0, 1)
glVertex3f(size, size, -size)
glTexCoord2f(1, 1)
glVertex3f(size, size, size)
glTexCoord2f(1, 0)
glVertex3f(size, -size, size)
glTexCoord2f(0, 1)
glVertex3f(-size, -size, size)
glTexCoord2f(0, 0)
glVertex3f(-size, -size, -size)
glTexCoord2f(1, 0)
glVertex3f( size, -size, -size)
glTexCoord2f(1, 1)
glVertex3f( size, -size, size)
glTexCoord2f(0, 1)
glVertex3f(-size, size, size)
glTexCoord2f(0, 0)
glVertex3f(-size, size, -size)
glTexCoord2f(1, 0)
glVertex3f( size, size, -size)
glTexCoord2f(1, 1)
glVertex3f( size, size, size)
glEnd()
# draw some text on top of our OpenGL object
window.push_GL_states()
text = sf.Text("pySFML / OpenGL demo", font)
text.color = sf.Color(255, 255, 255, 170)
text.position = (230, 450)
window.draw(text)
window.pop_GL_states()
# finally, display the rendered frame on screen
window.display()
# don't forget to destroy our texture
glDeleteTextures(1, texture)
|