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?
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.
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.
"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
# 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()
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.
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.