473,225 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

python... pyglet... opengl... scaling

hey, I was experimenting with pyglet, and was trying out their opengl, but cant figure out how to scale images...

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2.  
  3. # $Id:$
  4.  
  5.  
  6.  
  7. '''Demonstrates one way of fixing the display resolution to a certain
  8.  
  9. size, but rendering to the full screen.
  10.  
  11.  
  12.  
  13. The method used in this example is:
  14.  
  15.  
  16.  
  17. 1. Set the OpenGL viewport to the fixed resolution
  18.  
  19. 2. Render the scene using any OpenGL functions (here, just a polygon)
  20.  
  21. 3. Copy the framebuffer into a texture
  22.  
  23. 4. Reset the OpenGL viewport to the window (full screen) size
  24.  
  25. 5. Blit the texture to the framebuffer
  26.  
  27.  
  28.  
  29. Recent video cards could also render the scene directly to the texture
  30.  
  31. using EXT_framebuffer_object.  (This is not demonstrated in this example).
  32.  
  33. '''
  34.  
  35.  
  36.  
  37. from pyglet.gl import *
  38.  
  39. from pyglet import clock
  40.  
  41. from pyglet import image
  42.  
  43. from pyglet import window
  44.  
  45.  
  46.  
  47. # Create a fullscreen window using the user's desktop resolution.  You can
  48.  
  49. # also use this technique on ordinary resizable windows.
  50.  
  51. win = window.Window(fullscreen=True)
  52.  
  53.  
  54.  
  55. # Use 320x200 fixed resolution to make the effect completely obvious.  You
  56.  
  57. # can change this to a more reasonable value such as 800x600 here.
  58.  
  59. target_resolution = 1280, 800
  60.  
  61.  
  62.  
  63.  
  64. class FixedResolutionViewport(object):
  65.  
  66.     def __init__(self, win, width, height, filtered=False):
  67.  
  68.         self.window = win
  69.  
  70.         self.width = width
  71.  
  72.         self.height = height
  73.  
  74.         self.texture = image.Texture.create_for_size(GL_TEXTURE_2D, 
  75.  
  76.             width, height, GL_RGB)
  77.  
  78.         self.texture = self.texture.get_region(0, 0, width, height)
  79.  
  80.  
  81.  
  82.         if not filtered:
  83.  
  84.             # By default the texture will be bilinear filtered when scaled
  85.  
  86.             # up.  If requested, turn filtering off.  This makes the image
  87.  
  88.             # aliased, but is more suitable for pixel art.
  89.  
  90.             glTexParameteri(self.texture.target, 
  91.  
  92.                 GL_TEXTURE_MAG_FILTER, GL_NEAREST)
  93.  
  94.             glTexParameteri(self.texture.target, 
  95.  
  96.                 GL_TEXTURE_MIN_FILTER, GL_NEAREST)
  97.  
  98.  
  99.  
  100.     def begin(self):
  101.  
  102.         glViewport(0, 0, self.width, self.height)
  103.  
  104.         self.set_fixed_projection()
  105.  
  106.  
  107.  
  108.     def end(self):
  109.  
  110.         buffer = image.get_buffer_manager().get_color_buffer()
  111.  
  112.         self.texture.blit_into(buffer, 0, 0, 0)
  113.  
  114.  
  115.  
  116.         glViewport(0, 0, self.window.width, self.window.height)
  117.  
  118.         self.set_window_projection()
  119.  
  120.  
  121.  
  122.         aspect_width = self.window.width / float(self.width)
  123.  
  124.         aspect_height = self.window.height / float(self.height)
  125.  
  126.         if aspect_width > aspect_height:
  127.  
  128.             scale_width = aspect_height * self.width
  129.  
  130.             scale_height = aspect_height * self.height
  131.  
  132.         else:
  133.  
  134.             scale_width = aspect_width * self.width
  135.  
  136.             scale_height = aspect_width * self.height
  137.  
  138.         x = (self.window.width - scale_width) / 2
  139.  
  140.         y = (self.window.height - scale_height) / 2
  141.  
  142.  
  143.  
  144.         glClearColor(0, 0, 0, 1)
  145.  
  146.         glClear(GL_COLOR_BUFFER_BIT)
  147.  
  148.         glLoadIdentity()
  149.  
  150.         glColor3f(1, 1, 1)
  151.  
  152.         self.texture.blit(x, y, width=scale_width, height=scale_height)
  153.  
  154.  
  155.  
  156.     def set_fixed_projection(self):
  157.  
  158.         # Override this method if you need to change the projection of the
  159.  
  160.         # fixed resolution viewport.
  161.  
  162.         glMatrixMode(GL_PROJECTION)
  163.  
  164.         glLoadIdentity()
  165.  
  166.         glOrtho(0, self.width, 0, self.height, -1, 1)
  167.  
  168.         glMatrixMode(GL_MODELVIEW)
  169.  
  170.  
  171.  
  172.     def set_window_projection(self):
  173.  
  174.         # This is the same as the default window projection, reprinted here
  175.  
  176.         # for clarity.
  177.  
  178.         glMatrixMode(GL_PROJECTION)
  179.  
  180.         glLoadIdentity()
  181.  
  182.         glOrtho(0, self.window.width, 0, self.window.height, -1, 1)
  183.  
  184.         glMatrixMode(GL_MODELVIEW)
  185.  
  186.  
  187.  
  188. rotate = 0
  189. kitten = image.load('kitten.jpg')
  190.  
  191. def draw_scene():
  192.     global kitten
  193.  
  194.     # Draw the scene, assuming the fixed resolution viewport and projection
  195.  
  196.     # have been set up.
  197.  
  198.     glClearColor(0, 0, 0, 0)
  199.  
  200.     glClear(GL_COLOR_BUFFER_BIT)
  201.     f=kitten.width
  202.  
  203.     glLoadIdentity()
  204.     glEnable(GL_TEXTURE_2D)
  205.  
  206.     w, h = target_resolution
  207.  
  208.     glTranslatef(w/2, h/2, 0)
  209.  
  210.     glRotatef(rotate, 0, 0, 1)
  211.  
  212.     #glColor3f(1, 1, 0,)
  213.  
  214.     s = min(w, h) / 4
  215.  
  216.     glRectf(s, s, s, s)
  217.     kitten.blit(0,0)
  218.  
  219. viewport = FixedResolutionViewport(win, 
  220.  
  221.     target_width, target_height, filtered=False)
  222.  
  223. while not win.has_exit:
  224.  
  225.     dt = clock.tick()
  226.  
  227.     rotate += dt * 20
  228.  
  229.  
  230.  
  231.     win.dispatch_events()
  232.  
  233.  
  234.  
  235.     viewport.begin()
  236.  
  237.     win.clear()
  238.  
  239.     draw_scene()
  240.  
  241.     viewport.end()
  242.  
  243.  
  244.  
  245.     win.flip()
that is the fixed_resolution.py file that came with it

basically i just want to make that kitten picture rotate instead of the red box. i got the picture to rotate, but I cant figure out how to scale it down to match the box size.

i searched for about 2-3 hours tonight for opengl refrences on how to do this but came up empty handed
Dec 24 '07 #1
1 4157
Oops, embarrassing... I put down the wrong code :p

Expand|Select|Wrap|Line Numbers
  1. kitten = image.load('kitten.jpg')
  2. texture=kitten.texture
  3.  
  4. def draw_scene():
  5.     global kitten, texture
  6.  
  7.     # Draw the scene, assuming the fixed resolution viewport and projection
  8.  
  9.     # have been set up.
  10.  
  11.     glClearColor(0, 0, 0, 0)
  12.  
  13.     glClear(GL_COLOR_BUFFER_BIT)
  14.     f=kitten.width
  15.  
  16.     glLoadIdentity()
  17.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
  18.     glEnable(GL_TEXTURE_2D)
  19.  
  20.     w, h = target_resolution
  21.  
  22.     glTranslatef(w/2, h/2, 0)
  23.  
  24.     #glRotatef(rotate, 0, 0, 1)
  25.  
  26.     glColor3f(1, 0, 1)
  27.  
  28.     s = min(w, h) / 4
  29.  
  30.     glRectf(-s, -s, s, s)
  31.     glBindTexture(GL_TEXTURE_2D, texture.id)
  32.     glBegin(GL_POLYGON)
  33.     glVertex3f (-s, -s, 0.0);
  34.     glVertex3f (s, -s, 0.0);
  35.     glVertex3f (s, s, 0.0);
  36.     glVertex3f (-s, s, 0.0);
  37.     glEnd()
that is what I changed, and all it gives is a gray square
Dec 24 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: jg.campbell.ng | last post by:
I'm beginning learning Python and OpenGL in Python. Python fine. But difficulties with OpenGL; presumably with the installation of OpenGL. OS = Linux FC5. Python program gl_test.py: from...
3
by: morris.slutsky | last post by:
So every now and then I like to mess around with hobby projects - I often end up trying to write an OpenGL video game. My last attempt aborted due to the difficulty of automating game elements and...
10
by: imx | last post by:
Hi there, I wonder whether python can be used to simulate a real user to do the following: 1) open a web site in a browser; 2) printscreen, so to copy the current active window image to...
4
by: PatrickMinnesota | last post by:
I like to do fun stuff when learning a new language. I've been working with Python for a little while on real world problems mostly fixing bugs and writing a simulator for work. I was thinking...
0
by: Facundo Batista | last post by:
Hi! This post is to tell you about a Python event we had the last weekend. It was a Python camping, in Los Cocos, Córdoba, Argentina. More than 20 Python developers, some experienced, some new...
0
by: _wolf | last post by:
cairo@cairographics.org] hi all, i've heard cairo has become the image scling library for firefox3. is that true? wonderful, i want to do that in python. there's a python interface for cairo,...
3
by: david | last post by:
You learn something new every day: On my ubuntu, update-manager is supposed to use the python2.5 installed on /usr/bin. Well, I had subsequently installed a whole bunch of stuff in /usr/local...
13
by: Thomas Troeger | last post by:
Hi, Sorry I've posted a similar question some weeks ago, but I got no answers. I want to embed a Python application on a device with limited resources, esp. storage limitations. Is there a way...
12
by: Ben Sizer | last post by:
Although the standard library in Python is great, there are undoubtedly some great packages available from 3rd parties, and I've encountered a few almost by accident. However, I don't know how a...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.