This is pretty simple do, but the implementation would vary depending
on how you want to move the camera. For a side-scroller, I generally
do one of the following
1. Keep the camera's position (X) always in sync with the player's position
2. Only move the camera as the player reaches the edge of the screen.
Either way, there is no camera in OpenGL, just a world matrix
transformation. You can model the position of the `camera` with a
Camera class and just take the inverse of it's position when applying
the transformation:
glTranslatef(-camera.pos.x, -camera.pos.y, -camera.pos.z)
So in your on_update for the sprite, you might do something like:
player.pos.x = newpos
camera.pos.x = player.pos.x
I've actually developed some abstractions for camera (and lights) in
pyglet-based library I'm hacking on for use in 3D games
(http://miru.enterthefoo.com/tip). This might be useful for your
application as you can define a side-scroller track (2) over a camera
and renderable object, which is described here:
http://miru.enterthefoo.com/tip/#camera-tracks
--
\\\\\/\"/\\\\\\\\\\\
\\\\/ // //\/\\\\\\\
\\\/ \\// /\ \/\\\\
\\/ /\/ / /\/ /\ \\\
\/ / /\/ /\ /\\\ \\
/ /\\\ /\\\ \\\\\/\
\/\\\\\/\\\\\/\\\\\\
d.p.s
Because it doesn't.
> What is wrong with the gluLookAt(camera_x,
> camera_y, camera_z, look_x, look_y, look_z, up_x, up_y, up_z)
> command?
Nothing. It's just more complex than glTranslatef for something like an RTS.
> Doesn't that do exactly what the op is asking for by letting
> you draw your world ignorant of what the viewpoint is (if you want)
> and simply position a camera within it? I use this in a 3d game of
> mine and it works just fine.
>
Ok.
No, you are not missing anything, quite the opposite in fact. The
concept of "camera" is in your head, and you use low-level operations in
OpenGL to implement it.
Your code probably looks something like this: You represent the camera
(in your mind and your program) with several parameters, like position,
direction of view, angle of view, and up vector. Then you pass those
various parameters to OpenGL via calls to gluLookAt and glFrustum, or
gluPerspective and so forth, and OpenGL computes and stores a couple of
4x4 matrices in response.
Whether or not you store those parameters in an object called a "camera"
is a programming decision you make, but certainly you can't call the 4x4
matrices built by OpenGL a "camera". OpenGL just provides you with
all the low-level matrix manipulation tools needed to implement a camera
in your code.
Gary Herron
> >
>
Generally, you should only have to set the matrix mode in on_resize.
I generally use glLoadIdentity on each draw, otherwise this adds onto
previous transformations - which is certainly not what you want.
If you are just writing a 2d game, the base window class takes care of
most of this for you, so you might want to just subclass like:
class MyWindow(pyglet.window.Window):
def on_draw(self):
glLoadIdentity()
glTranslatef(-camera_x, -camera_y, -camera_z)
> EDIT: just using glTranslatef on the camera seems to make sprites be
> drawn in the right place, even with batches, I don't seem to need to
> push and pop matrices for each image. Is this correct?
The textures are blitted to an absolute position, so you should not
need to push/pop matrices for each image.
You'll need to provide a more complete example.
Alex.
http://pyglet.org/doc/1.1/programming_guide/opengl_imaging.html
You're not supplying any texture coordinates.
Alex.
That's rude. I think you've gotten plenty of help with your questions
on this thread.
> I know that it's what I
> need to do but how do I do such a thing using the TextureGroup class
> pyglet provides?
What you're asking is really not a question specific to pyglet, but to
OpenGL. And it sounds like you're really trying to jump into advanced
usage, when for your use case could likely be covered by using higher
level abstractions provided by pyglet:
http://pyglet.org/doc/1.1/programming_guide/displaying_images.html#sprites
Just an update: I worked out the heightmap stuff and used a grid to
display height differences.
Screenshot: http://i30.tinypic.com/1zmcdac.png
I'd still very much like to know how to map a texture onto the batch-
stored terrain, however!