Re: [pygame] Old pygame tutorial not working

2008-08-05 Thread Alistair Buxton
2008/8/5 Ian Mallett <[EMAIL PROTECTED]>:

> we're doing now).  I may be mistaken, but the speed issue is related to the
> number of OpenGL calls made and glBegin/End aren't especially long.

You could optimize the code you posted earlier by removing the
divisions by fwidth and fheight and instead using glMatrixMode and
glScalef to scale texture coordinates before you start drawing. That
would save two divisions per vertex.

-- 
Alistair Buxton
[EMAIL PROTECTED]


Re: [pygame] PyOpenGL Screenshots

2008-05-07 Thread Alistair Buxton
Here it is. I didn't spend any time making this code nice, but you can
just rip out the parts you need.

2008/5/8 Alistair Buxton <[EMAIL PROTECTED]>:
> I have a full example of doing exactly this task using python to
>  render to a series of png images (which you can load into something
>  like adobe premier), or a raw RV24 file (which you can transcode under
>  linux using... transcode). Will post it as soon as I've dug it out and
>  tested it still works. Includes examples of using transcode too.
>
>  2008/5/8 Ian Mallett <[EMAIL PROTECTED]>:
>
>
> > I figured out a way to do it in my program--Animation Shop Pro.  It would
>  > still be cool to have examples of how to do this without Animation Shop.  I
>  > was thinking ffmpeg or pil for doing so.  I looked around, but couldn't 
> find
>  > anything.
>  >  Thanks everyone,
>  > Ian
>  >
>
>
>
>  --
>  Alistair Buxton
>  [EMAIL PROTECTED]
>



-- 
Alistair Buxton
[EMAIL PROTECTED]
#!/usr/bin/env python

# Python OpenGL -> Video file
# (c) 2008 Alistair Buxton <[EMAIL PROTECTED]>
# Handy for doing procedural animations for compositing with other 
# videos in 

import os, random, math

from OpenGL.GL import *
from OpenGL.GLU import *
import pygame, pygame.image, pygame.key
from pygame.locals import *
from PIL import Image

##

# render settings

# 1 = best
QUALITY = 16

# whether to actually write output files or just preview
render_on = True

# If you don't want to write separate png files, you can just dump the output 
# from glReadPixels() into one huge file and encode it with transcode:

# transcode --use_rgb -i whatever.rv24 -x raw=RGB,null -y xvid4 -o output.xvid.avi -k -z  -f 25
# (for xvid)

# But beware that whatever.rv24 will be HUGE as it is uncompressed (not even RLE). 
# the size will be width*height*numframes*3 bytes, that's 31mb/sec for standard PAL video.

# Also worth mentioning: RV24 export is RGB, but png export is RGBA - thus if you use
# RV24 method, you will loose the alpha channel of your video.

# render type
# if true, write one big RV24 raw video file
# if false, write a png sequence
render_rv24 = False

if render_on:
# size of final render
w = 720
h = 576
else:
# size for preview
w = 720
h = 576

# how many frames to render before exiting
max_frames = 25

# framerate of rendered video
fps = 25.0

if render_rv24:
framefile = file("whatever.rv24", "wb")

##

# the snowflake animation stuff

flakes = []

class SnowFlake(object):
def __init__(self):
self.x = random.randrange(0,720)
self.y = random.randrange(0,576)

self.z = 3 + (random.randrange(3,20) / random.randrange(3,25))

self.dy = random.randrange(1,10)
self.drift = random.randrange(1,100)/1000.0 
self.rad = random.randrange(1,100)/100.0

def draw(self):
glColor4f(1.0,1.0,1.0,1.0)
if self.z < 0:
return
glBegin(GL_POLYGON)
r = math.floor(self.z * 4)
for i in range(int(r)):
glVertex2f( self.x+(self.z*math.sin(2*math.pi*(i/r))), self.y+(self.z*math.cos(2*math.pi*(i/r))) )
glVertex2f( self.x, self.y+self.z )
glEnd()


def update(self, ftime):

self.rad += ((ftime*0.5)+self.drift)
self.x += math.sin(self.rad)

self.y -= 0.7*ftime*(40.0+self.dy)*self.z;

if (self.y < -10):
self.y = 586

if (self.x < -10):
self.x = 730

if (self.x > 730):
self.x = -10

# standard opengl stuff init/draw/update/resize

def resize((width, height)):
if height==0:
height=1.0
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0, 720, 0, 576)
#gluOrtho2D(-(width/(2*height)), width/(2*height), 0, 1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()

def init():
glShadeModel(GL_SMOOTH)
glClearColor(0.3,0.5,0.7,0.0)
glClearDepth(1.0)
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
glDisable(GL_DEPTH_TEST)

for i in range(100):
flakes.append(SnowFlake())

def update(ftime):
for f in flakes:
f.update(ftime)

def draw():
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )

glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glDisable(GL_TEXTURE_2D)

for f in flakes:
f.draw()

###

# write a png file from GL framebuffer data

def p

Re: [pygame] PyOpenGL Screenshots

2008-05-07 Thread Alistair Buxton
I have a full example of doing exactly this task using python to
render to a series of png images (which you can load into something
like adobe premier), or a raw RV24 file (which you can transcode under
linux using... transcode). Will post it as soon as I've dug it out and
tested it still works. Includes examples of using transcode too.

2008/5/8 Ian Mallett <[EMAIL PROTECTED]>:
> I figured out a way to do it in my program--Animation Shop Pro.  It would
> still be cool to have examples of how to do this without Animation Shop.  I
> was thinking ffmpeg or pil for doing so.  I looked around, but couldn't find
> anything.
>  Thanks everyone,
> Ian
>



-- 
Alistair Buxton
[EMAIL PROTECTED]


Re: [pygame] hoe to create the RoundRect ?

2008-03-28 Thread Alistair Buxton
On 28/03/2008, Ian Mallett <[EMAIL PROTECTED]> wrote:
> Interesting.  I like the better boundaries--mine weren't perfect.  I'm
> getting little pixel artifacts inside the rect near the rounded sections,
> though...
>

There's a bug in pygame.draw.ellipse() (and also arc()) where it gets
moire holes when width is larger than about 5. I'm not sure if it has
been fixed in the very latest version or not. I'm also not sure if the
bug is actually in pygame, or in some other library pygame just uses.

Also there is some descrepancy between rect and ellipse - while they
both take exactly the same arguments, a rect with width > 1 is larger
than it's input rect, while an ellipse is always bounded by it's input
rect, with the width being used to shrink the inner radius. It seems
strange, you would think they would behave the same.

-- 
Alistair Buxton
[EMAIL PROTECTED]


Re: [pygame] hoe to create the RoundRect ?

2008-03-28 Thread Alistair Buxton
Here is another way to do it. This method using clipping to draw the
rounded rectangle in six drawing operations: left and right sides (1
rect), top and bottom sides (1 rect), then each corner (4 ellipses.)
It can draw unfilled round rects without trashing the background, and
it's arguments are very similar to those of rect and ellipse, with
just 2 extra parameters: xr and yr, which are the radius of the corner
ellipses in x and y respectively. It should respect the current clip
region if there is one, and restore it afterwards - but I have not
tested this.

-- 
Alistair Buxton
[EMAIL PROTECTED]
import pygame
from pygame.locals import *
import sys, os
if sys.platform == 'win32' or sys.platform == 'win64':
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()

Screen = (800,600)

icon = pygame.Surface((1,1)); icon.set_alpha(0); pygame.display.set_icon(icon)
pygame.display.set_caption("Rounded Rectangle Demo - Ian Mallett - 2008")
surface = pygame.display.set_mode(Screen)

def GetInput():
key = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == QUIT or key[K_ESCAPE]:
pygame.quit(); sys.exit()

def DrawRoundRect(surface, color, rect, width, xr, yr):
clip = surface.get_clip()

# left and right
surface.set_clip(clip.clip(rect.inflate(0, -yr*2)))
pygame.draw.rect(surface, color, rect.inflate(1-width,0), width)

# top and bottom
surface.set_clip(clip.clip(rect.inflate(-xr*2, 0)))
pygame.draw.rect(surface, color, rect.inflate(0,1-width), width)

# top left corner
surface.set_clip(clip.clip(rect.left, rect.top, xr, yr))
pygame.draw.ellipse(surface, color, pygame.Rect(rect.left, rect.top, 2*xr, 2*yr), width)

# top right corner
surface.set_clip(clip.clip(rect.right-xr, rect.top, xr, yr))
pygame.draw.ellipse(surface, color, pygame.Rect(rect.right-2*xr, rect.top, 2*xr, 2*yr), width)

# bottom left
surface.set_clip(clip.clip(rect.left, rect.bottom-yr, xr, yr))
pygame.draw.ellipse(surface, color, pygame.Rect(rect.left, rect.bottom-2*yr, 2*xr, 2*yr), width)

# bottom right
surface.set_clip(clip.clip(rect.right-xr, rect.bottom-yr, xr, yr))
pygame.draw.ellipse(surface, color, pygame.Rect(rect.right-2*xr, rect.bottom-2*yr, 2*xr, 2*yr), width)

surface.set_clip(clip)

def main():
while True:
GetInput()
surface.fill((0,0,0))
DrawRoundRect(surface, (255,0,0), pygame.Rect(100, 100, 200, 200), 0, 32, 32)
DrawRoundRect(surface, (0,255,0), pygame.Rect(100, 100, 200, 200), 1, 32, 32)

DrawRoundRect(surface, (0,0,255), pygame.Rect(450,100,250,300), 2, 32, 32)

DrawRoundRect(surface, (0,0,255), pygame.Rect(150,350,200,200), 0, 32, 32)
DrawRoundRect(surface, (0,255,0), pygame.Rect(150,350,200,200), 16, 32, 32)

DrawRoundRect(surface, (0,255,0), pygame.Rect(400,450,350,64), 3, 32, 32)
pygame.display.flip()

if __name__ == '__main__': main()


Re: [pygame] shadow demo

2008-03-23 Thread Alistair Buxton
>  On Mon, Mar 24, 2008 at 4:05 PM, Ian Mallett <[EMAIL PROTECTED]> wrote:
>  > So at this resolution, the shadows can never be any higher resolution?  As
>  > they are, they are really pixel-y, which is unacceptable.  How can the
>  > shadows be more detailed?
>  >

A side note: If you only want to cast shadows onto a flat surface, ie
the ground plane, you don't need to use a texture at all. Just draw
the surface, then draw all your meshes "squashed" into the plane by a
transformation.

With this method there will be no loss of resolution at all, but there
will be no self-shadowing, and the shadows will not grow larger with
distance because you are simulating shadows from a directional source
rather than a point source. Even so, it can give reasonable results
for scenes with high levels of ambient lighting and a flat ground
plane, and where the light source is very far away, ie outdoor daytime
scenes.

-- 
Alistair Buxton
[EMAIL PROTECTED]


Re: [pygame] shadow demo

2008-03-23 Thread Alistair Buxton
On 24/03/2008, Ian Mallett <[EMAIL PROTECTED]> wrote:
> So at this resolution, the shadows can never be any higher resolution?  As
> they are, they are really pixel-y, which is unacceptable.  How can the
> shadows be more detailed?

Decrease the light FOV, and you decrease the area over which the
shadow texture will be stretched. Turning on filtering for the shadow
texture should also help a bit.


-- 
Alistair Buxton
[EMAIL PROTECTED]


Re: [pygame] shadow demo

2008-03-23 Thread Alistair Buxton
On 24/03/2008, Ian Mallett <[EMAIL PROTECTED]> wrote:
> The demo, then, should change the size of the framebuffer, or the demo is
> incomplete.
>

The framebuffer is literally the display surface. It's always the same
size as the window, or the screen resolution if you are running
fullscreen.

-- 
Alistair Buxton
[EMAIL PROTECTED]


Re: [pygame] shadow demo

2008-03-23 Thread Alistair Buxton
On 24/03/2008, Ian Mallett <[EMAIL PROTECTED]> wrote:
> but nowhere else.  Seeing as how this is a variable, (and of course, a
> shadow implementation should be adjustable to different platform
> capabilities), it seems illogical that it cannot, in effect, be changed.

The demo implementation draws the shadows into the framebuffer, then
copies the result to a texture. This means that if the shadow texture
is larger than the framebuffer, some parts of it won't be drawn.

-- 
Alistair Buxton
[EMAIL PROTECTED]


Re: [pygame] shadow demo

2008-03-23 Thread Alistair Buxton
On 22/03/2008, Ian Mallett <[EMAIL PROTECTED]> wrote:
> Hasn't anyone figured this out?  One would think that it can't be too
> difficult.  One quad over another--how hard could it be?

It's pretty easy really, when you consider that opengl is a system
designed around the idea of projecting 3d coordinates into 2d
coordinates in a plane, based on a transformation matrix.

When you draw your world, you project the 3d geometry into the screen
plane based on the eye point.

When you draw the shadows, you project the 3d geometry into the
shadowed surface plane, based on the light source point.

So, you see, it's exactly the same thing.

This has the following implications:

* A shadow can no more have a soft edge than a polygon can have a soft
edge. You can fake it by doing multiple samples, but then you have to
draw everything multiple times. Doing this for your shadow rendering
gives you the penumbra(1) effect, doing it for your world view gives
you a depth of field(2) effect.

* If every triangle can shadow every other triangle, then rendering
the shadows requires drawing n*n*l triangles where n = number of
triangles and l = number of light sources. You have to do this for
every single sample you do, if you are trying to do soft shadows.

Of course, you can optimize using depth tests, bounding boxes etc. But
then you are getting into the kind of complex algorithms(3) that take
people years to perfect...

(1) http://en.wikipedia.org/wiki/Umbra
(2) http://en.wikipedia.org/wiki/Depth_of_field
(3) http://graphics.cs.lth.se/research/shadows/

-- 
Alistair Buxton
[EMAIL PROTECTED]


Re: [pygame] Sound to String

2008-02-29 Thread Alistair Buxton
On windows, you must open binary files as binary: f = open("sound.wav", "rb")

If you don't do this, windows will truncate the file and give no
error, though it will work on linux.

On 29/02/2008, PyMike <[EMAIL PROTECTED]> wrote:
> I tried some others and still get the same error. (error: Unrecognized sound
> file type)
>
>
> On Fri, Feb 29, 2008 at 8:00 AM, PyMike <[EMAIL PROTECTED]> wrote:
>
> > Hi Douglas
> >
> > Just to let you know, my sound will load and play with pygame without the
> string compression. It's a mere 0.18 seconds long. I tried some other sounds
> that were short and I got ~around~ the same results.
> >
> > After editing sound2.py,
> >
> > "RIFF\x16\xe9\x00\x00WAVEfmt
> \x10\x00\x00\x00\x01\x00\x01\x00D\xac\x00\x00\x88X\x01\x00\x02\x00\x10\x00data\xf2\xe8\x00\x00\xd2$\xcf(U\xf0r+\xc3\x16\x0c\xf6\xa7\xdc2\x0bH\xf8\x95\xdcy\xd6\x06!\xbc\x05\x17\xe5\xbf\xe4\xd5\xfa\n\xea'\xedN\xfb\xec!\x055\x88\xecy\xcb\xdf\xc8#\xd2x\x16\xf1\xd0|\xf9\x1f\x02\x86\xe2\x04\xf3\xa4'G\x1c\xfd\xfb7B\x86"
> >
> >
> > import binascii
> > from cStringIO import StringIO
> > from pygame import mixer
> >
> > snd = binascii.a2b_base64(__doc__)
> > f = StringIO(snd)
> >
> > mixer.init()
> > sound = mixer.Sound(f)
> > sound.play()
> >
> > I get this traceback:
> >
> > >>>
> > Traceback (most recent call last):
> >   File "C:\Documents and
> Settings\Michael\Desktop\sound2.py", line 8, in 
> >
> > snd = binascii.a2b_base64(__doc__)
> > Error: Incorrect padding
> > >>>
> >
> > Then I switched a2b_base64 to b2a_base64 and got this traceback:
> >
> > >>>
> > Traceback (most recent call last):
> >   File "C:\Documents and
> Settings\Michael\Desktop\sound2.py", line 12, in 
> > sound = mixer.Sound(f)
> > error: Unrecognized sound file type
> > >>>
> >
> > I'm going to try a few other sound files real quick.
> >
> >
> >
> >
> >
> >
> > On Fri, Feb 29, 2008 at 1:56 AM, Douglas Bagnall <[EMAIL PROTECTED]>
> wrote:
> >
> > > hi Mike
> > >
> > > This is your main problem:
> > >
> > > >> print len(s)
> > > >>
> > > >> 217
> > >
> > > Your wave file is broken. In 16 bit mono, 217 bytes leaves room for 94
> > > samples when you take away the headers. That's about 1/500 of a second
> > > at 44.1 kHz.  Your sound is not actually that short: the file is broken.
> > >
> > >
> > > Then later you wrote:
> > >
> > > > f = open("shoot.wav")
> > > > s = f.read()
> > > > f.close()
> > > > print len(s)
> > > > print len(repr(s))
> > > > print len(eval(repr(s)))
> > > >
> > >
> > > > b64 = binascii.b2a_base64(snd)
> > >
> > >
> > > You mean binascii.b2a_base64(s).
> > > snd is not defined yet.
> > >
> > >
> > > >
> > > > f = open("sound2.py", "wr").write(str(repr(b64)))
> > >
> > > repr() always returns a string -- there's no need for str().
> > > Also, f here will always be None, because it is the result of
> > > open().write() not open().
> > >
> > > Anyway, at this point sound2.py will contain a python representation of
> > > a sound, and nothing else, which incidentally makes it the module's
> > > __doc__ string.
> > >
> > > So then, if you modify sound2.py, adding the following lines:
> > >
> > >
> > > 'RIFFZB\x00\x00WAVEfmt \x10\x00[...]' #this is the existing line
> > >
> > > import binascii
> > > from cStringIO import StringIO
> > > from pygame import mixer
> > >
> > > snd = binascii.a2b_base64(__doc__)
> > >
> > > f = StringIO(snd)
> > >
> > > mixer.init()
> > > sound = mixer.Sound(f)
> > > sound.play()
> > >
> > >
> > > It will play. At least it would if you didn't have a corrupt wav file.
> > >
> > >
> > >
> > > >>>>>> I tried that out, and it got close to working. But I get an
> > > >>>>> "unrecognized file-type"' error
> > >
> > >
> > > It always helps if you say where an exception occurs. Pasting the
> > > traceback is good.
> > >
> > >
> > >
> > > douglas
> > >
> >
> >
> >
> > --
> > - PyMike
>
>
>
> --
> - PyMike


-- 
Alistair Buxton
[EMAIL PROTECTED]


Re: [pygame] How to check sound format of mixer?

2008-02-19 Thread Alistair Buxton
> Found it. The code never reaches the part that handles file objects,
> since mixer.c:881,882 return an error if the object has no buffer interface:
>
> if (PyObject_AsReadBuffer (file, &buf, &buflen) == -1)
> return -1;

You beat me to it. Also chunk isn't reset to NULL.

I think I totally understand this now, but we will see when I try to
actually use it. I don't need to modify the sound after loading it so
I should have everything I need.

-- 
Alistair Buxton
[EMAIL PROTECTED]


Re: [pygame] How to check sound format of mixer?

2008-02-19 Thread Alistair Buxton
> I'm an oblivious idiot :-). A buffer method's already available in the
> current SVN tree.  You can use pygame.Sound (your_buffer) safely, the
> buffer just has to match the mixer criteria.
>
> Due to the SDL_Mixer implementation however, the buffer will be copied,
> so a manipulation of the buffer object after creating the Sound will not
> have any impact on it.
>

I'm looking at that code right now. It looks like it tries to copy it,
and if it can't, it raises an exception. But then there is a block of
code right after which would load it in place, but can not be reached
currently, because the previous code returns on error. Correct? So I'm
guessing this is already WIP by somebody :)


-- 
Alistair Buxton
[EMAIL PROTECTED]


Re: [pygame] How to check sound format of mixer?

2008-02-19 Thread Alistair Buxton
Thanks for the quick reply.

I was confused because SDL_InitAudio directly returns the actual
format through a pointer, while SDL_mixer uses a separate function
Mix_QuerySpec which I didn't notice.

On 19/02/2008, Marcus von Appen <[EMAIL PROTECTED]> wrote:

> > Also it would be nice to have a pygame.mixer.Sound.from_buffer()
> > function similar to the Surface.from_buffer() in that it does not
> > require Numeric. I don't have anything against sndarray and surfarray
>
> True. I'll put it onto my TODO list.

How long is your TODO list? I might take a shot at it myself since I
have svn set up already but if I don't post a patch within a couple of
days it probably means I've given up...

-- 
Alistair Buxton
[EMAIL PROTECTED]


[pygame] How to check sound format of mixer?

2008-02-19 Thread Alistair Buxton
I can't seem to find out how you check the sound format that mixer is
using. For example, when I request 8 bit signed format, I get 8 bit
unsigned, and all my samples sound terrible (they are raw samples so I
have to load them into a sndarray.) But to find this out, I had to
write a test in C using SDL_OpenAudio because even SDL_mixer cannot
seem to tell you this. Checking the struct output by SDL_OpenAudio
seems to be the only way to know, so if you don't call it yourself,
you can't know, and Mix_OpenAudio hides the output from
SDL_OpenAudio...

Also it would be nice to have a pygame.mixer.Sound.from_buffer()
function similar to the Surface.from_buffer() in that it does not
require Numeric. I don't have anything against sndarray and surfarray
but if you only need to convert your samples/images once it seems
unnecessary to add a further dependency on Numeric. In my case I'm
loading 1 bit bitmaps and converting them to 8 bit, and
imageop.mono2grey is fine for this. Unfortunately it would be much
less useful for sound without the ability to check mixer format, so if
anyone can solve that problem, I'll try to code from_buffer for
sounds.

-- 
Alistair Buxton
[EMAIL PROTECTED]


[pygame] PATCH: bug in pygame.image.frombuffer

2008-02-15 Thread Alistair Buxton
Hi,

Found a problem with pygame.image.frombuffer and 8 bit images. The
call to SDL_CreateRGBSurfaceFrom uses incorrect pitch, which causes
the image to be be corrupted. pygame.image.fromstring does not have
this problem so you can use that instead as a workaround. The bug is
present in latest subversion code.

Demo script and patch are attached.

-- 
Alistair Buxton
[EMAIL PROTECTED]
from struct import unpack
from imageop import mono2grey, scale

import pygame
from pygame.locals import *

pygame.init()

sx,sy = 320,320

screen = pygame.display.set_mode((sx,sy))

# 8x8 checkerboard
pixels = "\x00\xff\x00\xff\x00\xff\x00\xff\xff\x00\xff\x00\xff\x00\xff\x00"*4
# scale to 320x320
newpixels = scale(pixels, 1, 8, 8, 320, 320)

# this produces an image which is warped horizontally:
surface = pygame.image.frombuffer(newpixels, (320, 320), "P")
# this works ok:
#surface = pygame.image.fromstring(newpixels, (320, 320), "P")
surface.set_palette([(0,0,0), (255,255,255)]*128)

running = True

while running:

screen.fill((0,0,0))
screen.blit(surface, (0,0))

for event in pygame.event.get():
if event.type == QUIT:
running = 0
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = 0

pygame.display.flip()
pygame.time.delay(10)


Index: src/image.c
===
--- src/image.c	(revision 1113)
+++ src/image.c	(working copy)
@@ -938,7 +938,7 @@
 (PyExc_ValueError,
  "Buffer length does not equal format and resolution size");
 
-surf = SDL_CreateRGBSurfaceFrom (data, w, h, 8, 3, 0, 0, 0, 0);
+surf = SDL_CreateRGBSurfaceFrom (data, w, h, 8, w, 0, 0, 0, 0);
 }
 else if (!strcmp (format, "RGB"))
 {