Mixing 2D and 3D

89 views
Skip to first unread message

jotham

unread,
Feb 11, 2008, 6:36:43 AM2/11/08
to pyglet-users
Hi,

I'm sure this has been asked before, but I cannot find any mention of
it.

I've been using pyglet with ImageGrid to place a number of animated 2D
sprites. I want to now draw lines between them using OpenGL. Can I
do this without having to first move my 2D sprites to OpenGL textured
quads (or similar?) Is this possible?

I am new to OpenGL and am working through NeHe and the Red Book.

Regards,
Jotham

Txema Vicente

unread,
Feb 11, 2008, 8:17:16 AM2/11/08
to pyglet-users
> I've been using pyglet with ImageGrid to place a number of animated 2D
> sprites. I want to now draw lines between them using OpenGL. Can I
> do this without having to first move my 2D sprites to OpenGL textured
> quads (or similar?) Is this possible?

Yes.

jotham

unread,
Feb 11, 2008, 4:51:18 PM2/11/08
to pyglet-users
Well, I guess I should have said. I had a play around before posting
here and could not figure out how to do it. Could you provide a
simple test case?

Drew Smathers

unread,
Feb 11, 2008, 5:11:09 PM2/11/08
to pyglet...@googlegroups.com
On Feb 11, 2008 4:51 PM, jotham <jotha...@gmail.com> wrote:

Well, I guess I should have said.  I had a play around before posting
here and could not figure out how to do it.  Could you provide a
simple test case?


How about we make a compromise.  Try to get it done first on your own by:

1. Learning some OpenGL basics
2. Reading the documentation for pyglet

If you're still lost, post your broken code and we'll be glad to make further suggestions.  If you want me to write code for you, that's fine as well - just contact me for an estimate (I accept paypal or money orders).

-Drew


dio...@gmail.com

unread,
Feb 11, 2008, 5:25:11 PM2/11/08
to pyglet-users
You guys are remarkably caustic. I imagine you'll only respond to this
opinion with more of the same, but I thought I'd throw that out there.
If it only takes you thirty seconds to give a quick overview, then why
not? Is it some sort of trade secret? If it's more involved, then
there is certainly no harm in telling him so in nicer terms. He is
already making the effort to learn.

Unfortunately, I don't have the answer myself, because I am in a
similar position. I only started reading the Red Book a couple of
weeks ago and my demanding class schedule has kept me from getting
past Chapter 2 so far. But I'm trying.

As a possible hint to the original poster: I speculate that it
involves getting out of orthographic mode, drawing all that 3D stuff
with OpenGL, getting back into orthographic mode, and then drawing all
the regular stuff the normal pyglet way, with images, etc.

On Feb 11, 5:11 pm, "Drew Smathers" <drew.smath...@gmail.com> wrote:

dio...@gmail.com

unread,
Feb 11, 2008, 5:25:20 PM2/11/08
to pyglet-users
You guys are remarkably caustic. I imagine you'll only respond to this
opinion with more of the same, but I thought I'd throw that out there.
If it only takes you thirty seconds to give a quick overview, then why
not? Is it some sort of trade secret? If it's more involved, then
there is certainly no harm in telling him so in nicer terms. He is
already making the effort to learn.

Unfortunately, I don't have the answer myself, because I am in a
similar position. I only started reading the Red Book a couple of
weeks ago and my demanding class schedule has kept me from getting
past Chapter 2 so far. But I'm trying.

As a possible hint to the original poster: I speculate that it
involves getting out of orthographic mode, drawing all that 3D stuff
with OpenGL, getting back into orthographic mode, and then drawing all
the regular stuff the normal pyglet way, with images, etc.

On Feb 11, 5:11 pm, "Drew Smathers" <drew.smath...@gmail.com> wrote:

Mark Melvin

unread,
Feb 11, 2008, 5:34:54 PM2/11/08
to pyglet...@googlegroups.com
Well, everything except the last sentence was reasonable (and I
thought it a bit blunt too and set the tone of the response).

I am just beginning (with OpenGL) as well or I would chip in. I think
the point was if the original poster could include at least a snippet
of what he/she tried to do, people would be more willing to respond
with code suggestions. It is hard to come up with sample code for
everyone asking. It would be much easier to point to a sample or some
docs, but I don't think there are any on this subject (yet).

Maybe a wiki or some other form of code samples that people could
contribute to would help here? Then us noobs could post solutions to
these sorts of problems as we conquered them and save some strife.

M.

Drew Smathers

unread,
Feb 11, 2008, 5:46:21 PM2/11/08
to pyglet...@googlegroups.com
On Feb 11, 2008 5:25 PM, dio...@gmail.com <dio...@gmail.com> wrote:

You guys are remarkably caustic. I imagine you'll only respond to this
opinion with more of the same, but I thought I'd throw that out there.
If it only takes you thirty seconds to give a quick overview, then why
not? Is it some sort of trade secret? If it's more involved, then
there is certainly no harm in telling him so in nicer terms. He is
already making the effort to learn.

I did feel bad for the sarcasm in my response, but it is the hard truth.  If you answer someone's question with working code that solves their problem, then who does that help?  No one.  First, the person responding has wasted time because the example might not even work in the context of the asker's application. Second, the asker misses an opportunity to learn by doing - through trial and error or other means.

Cheers,
Drew

Luke Paireepinart

unread,
Feb 12, 2008, 4:14:42 AM2/12/08
to pyglet...@googlegroups.com
Drew Smathers wrote:
> On Feb 11, 2008 5:25 PM, dio...@gmail.com <mailto:dio...@gmail.com>
diordna, have you read http://catb.org/~esr/faqs/smart-questions.html ?
It goes into great detail specifically about what you just mentioned.
Here are some excerpts:

"Open-ended questions tend to be perceived as open-ended time sinks.
Those people most likely to be able to give you a useful answer are also
the busiest people (if only because they take on the most work
themselves). People like that are allergic to open-ended time sinks,
thus they tend to be allergic to open-ended questions."

"To understand the world the experts live in, think of expertise as an
abundant resource and time to respond as a scarce one. The less of a
time commitment you implicitly ask for, the more likely you are to get
an answer from someone really good and really busy."

"In general, asking yes-or-no questions is a good thing to avoid unless
you want a yes-or-no answer
<http://homepages.tesco.net/%7EJ.deBoynePollard/FGA/questions-with-yes-or-no-answers.html>."

"Much of what looks like rudeness in hacker circles is not intended to
give offense. Rather, it's the product of the direct,
cut-through-the-bullshit communications style that is natural to people
who are more concerned about solving problems than making others feel
warm and fuzzy.

"When you perceive rudeness, try to react calmly. If someone is really
acting out, it is very likely a senior person on the list or newsgroup
or forum will call him or her on it. If that /doesn't/ happen and you
lose your temper, it is likely that the person you lose it at was
behaving within the hacker community's norms and /you/ will be
considered at fault. This will hurt your chances of getting the
information or help you want."

I feel the replies so far have been very reasonable.
If Jotham has tried code, he should include it.
"If you have some malfunctioning code, it is usually smarter to ask for
someone to explain what's wrong with it than it is to ask someone to fix
it."
Or in this case, ask for someone to rewrite it from scratch.
-Luke

Txema Vicente

unread,
Feb 12, 2008, 10:43:25 AM2/12/08
to pyglet...@googlegroups.com
> I feel the replies so far have been very reasonable.
Me too.
Try harder.


# OpenGL Projections
#---------------------------------
from pyglet import window,image
from pyglet.window import key
from pyglet.gl import *

def opengl_init():
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glDepthFunc(GL_LEQUAL)

def your_test_code():
glColor4f(1,1,1,1)
glBegin(GL_LINES)
glVertex3f(0,0,0)
glVertex3f(640,480,0)
glEnd()

class camera():
mode=1
x,y,z=0,0,512
rx,ry,rz=30,-45,0
w,h=640,480
far=8192
fov=60

def view(self,width,height):
self.w,self.h=width,height
glViewport(0, 0, width, height)
print "Viewport "+str(width)+"x"+str(height)
if self.mode==2: self.isometric()
elif self.mode==3: self.perspective()
else: self.default()

def default(self):
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, self.w, 0, self.h, -1, 1)
glMatrixMode(GL_MODELVIEW)

def isometric(self):
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(-self.w/2.,self.w/2.,-self.h/2.,self.h/2.,0,self.far)
glMatrixMode(GL_MODELVIEW)

def perspective(self):
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(self.fov, float(self.w)/self.h, 0.1, self.far)
glMatrixMode(GL_MODELVIEW)

def key(self, symbol, modifiers):
if symbol==key.F1:
self.mode=1
self.default()
print "Projection: Pyglet default"
elif symbol==key.F2:
print "Projection: 3D Isometric"
self.mode=2
self.isometric()
elif symbol==key.F3:
print "Projection: 3D Perspective"
self.mode=3
self.perspective()
elif self.mode==3 and symbol==key.NUM_SUBTRACT:
self.fov-=1
self.perspective()
elif self.mode==3 and symbol==key.NUM_ADD:
self.fov+=1
self.perspective()
else: print "KEY "+key.symbol_string(symbol)

def drag(self, x, y, dx, dy, button, modifiers):
if button==1:
self.x-=dx*2
self.y-=dy*2
elif button==2:
self.x-=dx*2
self.z-=dy*2
elif button==4:
self.ry+=dx/4.
self.rx-=dy/4.

def apply(self):
glLoadIdentity()
if self.mode==1: return
glTranslatef(-self.x,-self.y,-self.z)
glRotatef(self.rx,1,0,0)
glRotatef(self.ry,0,1,0)
glRotatef(self.rz,0,0,1)

def x_array(list):
return (GLfloat * len(list))(*list)

def axis(d=200):
vertices,colors=[],[]
#XZ RED
vertices.extend([-d, 0,-d, d, 0,-d, d, 0, d,-d, 0, d])
for i in range (0,4): colors.extend([1,0,0,0.5])
#YZ GREEN
vertices.extend([ 0,-d,-d, 0,-d, d, 0, d, d, 0, d,-d])
for i in range (0,4): colors.extend([0,1,0,0.5])
#XY BLUE
vertices.extend([-d,-d, 0, d,-d, 0, d, d, 0,-d, d, 0])
for i in range (0,4): colors.extend([0,0,1,0.5])
return x_array(vertices),x_array(colors)
AXIS_VERTICES,AXIS_COLORS=axis()

def draw_vertex_array(vertices,colors,mode=GL_LINES):
glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_COLOR_ARRAY)
glColorPointer(4, GL_FLOAT, 0, colors)
glVertexPointer(3, GL_FLOAT, 0, vertices)
glDrawArrays(GL_QUADS, 0, len(vertices)/3)
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_COLOR_ARRAY)

def draw_axis():
glEnable(GL_DEPTH_TEST)
draw_vertex_array(AXIS_VERTICES,AXIS_COLORS,GL_QUADS)
glDisable(GL_DEPTH_TEST)

print "OpenGL playground for newbies"
print "---------------------------------"
print "Projection matrix -> F1, F2, F3"
print "Camera -> Drag LMB,CMB,RMB"
print ""
cam=camera()
win = window.Window(resizable=True)
win.on_resize=cam.view
win.on_key_press=cam.key
win.on_mouse_drag=cam.drag
opengl_init()

while not win.has_exit:
win.dispatch_events()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
cam.apply()
draw_axis()
your_test_code()
win.flip()

jotham

unread,
Feb 12, 2008, 7:04:39 PM2/12/08
to pyglet-users
This project will go far with a community like this.

I thought my question was pretty specific. Clearly I have a lot to
learn. Other people will ask this question, let it be a lesson to
them.

Thanks for your help guys.


On Feb 13, 4:43 am, "Txema Vicente" <tx...@nabla.net> wrote:
> >> have you readhttp://catb.org/~esr/faqs/smart-questions.html?

Luke Paireepinart

unread,
Feb 12, 2008, 11:21:21 PM2/12/08
to pyglet...@googlegroups.com
jotham wrote:
> This project will go far with a community like this.
>
It's not the community as a whole. There are just a few people who are
annoyed when posts such as "I had a play around before posting here and
could not figure out how to do it." are not accompanied by said code.
All the other people don't care enough to reply.
There's no way for us to know what you've tried, and it's not really in
either of our interests for us to just hand you boilerplate code that
you won't understand fully.
So if you give us code that shows where you're stuck we'll try to help.
Just asking for a solution is not the right way to go about learning
anything.

> I thought my question was pretty specific. Clearly I have a lot to
> learn. Other people will ask this question, let it be a lesson to
> them.
>
> Thanks for your help guys.
>
Don't worry, we picked up on your sarcasm.
As I said, your question was specific, but from what we could see, you
were just asking someone to give you code. If you show that you've
tried (with code) people are much more likely to assist you.

If you begrudge this community for this thread, you will find many
others you're at odds with. Programming communities in general frown
upon people asking "how do I do x" without proving that they've tried to
do x themselves and they are stuck.

Txema Vicente

unread,
Feb 13, 2008, 5:40:05 AM2/13/08
to pyglet...@googlegroups.com
Subject: Re: Mixing 2D and 3D

> I thought my question was pretty specific. Clearly I have a lot to
> learn.Other people will ask this question, let it be a lesson to
them.

Yes, let it be.Your question was very clear. You want to use openGL and you
know there is a red book. Why does it not work? Give me a working example.

Really good people is not going to help you, they have better things to do
for us, like good libraries and documentation. We don't want them to waste
their time.

As opengl newbie, the most difficult part that I saw when I started few
weeks ago is to find the geometry that I was trying to draw, so I supposed
that you also had problems to understand the projection matrix.

I spend 2 hours to give you a fully working example, where you can find the
difference between pyglet's "2D"(sure?) default view and 3D, and the line
that you wanted to draw. All the opengl functions you need to know to start
working.

Have you ever run the example?
--------------------------------

Don't worry, I hope this will help to other people in your position, but
really interested in learning.
So is it, that I uploaded the code to the web.

> Thanks for your help guys.

>> Don't worry, we picked up on your sarcasm.

I did not, damn language lack. I'm a fool, I thought he was serious.

jotham

unread,
Feb 13, 2008, 5:50:44 AM2/13/08
to pyglet-users
Actually Txema, you were a great help, I thank you for that. The
conversation topics about the nature of helping, your [Txema] direct
help, and my obvious inability to ask questions that please everyone
lead me to believe my sarcasm was and is quite ambiguous.

My primary faults were that I did not understand anything about
pyglets role in the abstraction of OpenGL, and that I did not state I
was looking for an in-pyglet solution rather than getting into the
OpenGL side directly. Being talked down to by people didn't help me
clarify my position though.

I will post in this thread again when I have a clearer understanding
of the outcome and some code to show and be critiqued.

Thank you again guys!
Reply all
Reply to author
Forward
0 new messages