|
| 1 | +from math import radians |
| 2 | + |
| 3 | +from OpenGL.GL import * |
| 4 | +from OpenGL.GLU import * |
| 5 | + |
| 6 | +import pygame |
| 7 | +from pygame.locals import * |
| 8 | + |
| 9 | +SCREEN_SIZE = (800, 600) |
| 10 | + |
| 11 | +def resize(width, height): |
| 12 | + |
| 13 | + glViewport(0, 0, width, height) |
| 14 | + glMatrixMode(GL_PROJECTION) |
| 15 | + glLoadIdentity() |
| 16 | + gluPerspective(60.0, float(width)/height, .1, 1000.) |
| 17 | + glMatrixMode(GL_MODELVIEW) |
| 18 | + glLoadIdentity() |
| 19 | + |
| 20 | +def init(): |
| 21 | + |
| 22 | + glEnable(GL_TEXTURE_2D) |
| 23 | + glClearColor(1.0, 1.0, 1.0, 0.0) |
| 24 | + |
| 25 | +def run(): |
| 26 | + |
| 27 | + pygame.init() |
| 28 | + screen = pygame.display.set_mode(SCREEN_SIZE, HWSURFACE|OPENGL|DOUBLEBUF) |
| 29 | + |
| 30 | + resize(*SCREEN_SIZE) |
| 31 | + init() |
| 32 | + |
| 33 | + # Load the textures |
| 34 | + texture_surface = pygame.image.load("sushitex.png") |
| 35 | + # Retrieve the texture data |
| 36 | + texture_data = pygame.image.tostring(texture_surface, 'RGB', True) |
| 37 | + |
| 38 | + # Generate a texture id |
| 39 | + texture_id = glGenTextures(1) |
| 40 | + # Tell OpenGL we will be using this texture id for texture operations |
| 41 | + glBindTexture(GL_TEXTURE_2D, texture_id) |
| 42 | + |
| 43 | + # Tell OpenGL how to scale images |
| 44 | + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ) |
| 45 | + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ) |
| 46 | + |
| 47 | + # Tell OpenGL that data is aligned to byte boundries |
| 48 | + glPixelStorei(GL_UNPACK_ALIGNMENT, 1) |
| 49 | + |
| 50 | + # Get the dimensions of the image |
| 51 | + width, height = texture_surface.get_rect().size |
| 52 | + |
| 53 | + # Upload the image to OpenGL |
| 54 | + glTexImage2D( GL_TEXTURE_2D, |
| 55 | + 0, |
| 56 | + 3, |
| 57 | + width, |
| 58 | + height, |
| 59 | + 0, |
| 60 | + GL_RGB, |
| 61 | + GL_UNSIGNED_BYTE, |
| 62 | + texture_data) |
| 63 | + |
| 64 | + |
| 65 | + clock = pygame.time.Clock() |
| 66 | + |
| 67 | + tex_rotation = 0.0 |
| 68 | + |
| 69 | + while True: |
| 70 | + |
| 71 | + for event in pygame.event.get(): |
| 72 | + if event.type == QUIT: |
| 73 | + pygame.quit() |
| 74 | + quit() |
| 75 | + |
| 76 | + |
| 77 | + time_passed = clock.tick() |
| 78 | + time_passed_seconds = time_passed / 1000. |
| 79 | + tex_rotation += time_passed_seconds * 360.0 / 8.0 |
| 80 | + |
| 81 | + |
| 82 | + # Clear the screen (similar to fill) |
| 83 | + glClear(GL_COLOR_BUFFER_BIT) |
| 84 | + |
| 85 | + # Clear the model-view matrix |
| 86 | + glLoadIdentity() |
| 87 | + |
| 88 | + # Set the modelview matrix |
| 89 | + glTranslatef(0.0, 0.0, -600.0) |
| 90 | + glRotate(tex_rotation, 1, 0, 0) |
| 91 | + |
| 92 | + # Draw a quad (4 vertices, 4 texture coords) |
| 93 | + glBegin(GL_QUADS) |
| 94 | + |
| 95 | + glTexCoord2f(0, 1) |
| 96 | + glVertex3f(-300, 300, 0) |
| 97 | + |
| 98 | + glTexCoord2f(1, 1) |
| 99 | + glVertex3f(300, 300, 0) |
| 100 | + |
| 101 | + glTexCoord2f(1, 0) |
| 102 | + glVertex3f(300, -300, 0) |
| 103 | + |
| 104 | + glTexCoord2f(0, 0) |
| 105 | + glVertex3f(-300, -300, 0) |
| 106 | + |
| 107 | + glEnd() |
| 108 | + |
| 109 | + pygame.display.flip() |
| 110 | + |
| 111 | + glDeleteTextures(texture_id) |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + run() |
0 commit comments