Hello all,
I have been away for some time but i am begining my quest for OpenGL-fu again. I hope you guy’s can see this post since the thread is almost a month old.
I can now successfully create some geometry and store the objects in a simplified cache. I have a much better understanding of how GL works but still have much to learn.
My current delimma is how will i create my geometry thru a command line interface. I tried grabbing std_io from within the main GLUT loop but that causes everything to freeze up. So currently all i can do is hardcode my geometry and then run the program. I really don’t want to use a thread for input and a thread for GLUT. There must be a way thru the “glutIdleFunc()” or something??
Below is a simplified version of my code so far. I have a class called “Entities” that is my cache. “Entity”, which is a base class for all primitives and two primitive classes, “Face” and “Edge”. I know this code is horrible at the moment so please go easy on me since i am just a beginner to OpenGL and Graphics in general. Eventually this whole thing will be wraped into a “Model” class but for now getting anything to work is good enough for me
class Entities(list):
def __init__(self):
list.__init__(self)
def draw(self):
for obj in self:
obj.draw()
def add_item(self, item):
self.append( item ) #just append a class object to entities
def add_face(self, coords):
self.append( Face(coords) )
def add_edge(self, coords):
self.append( Edge(coords) )
def remove(self, item):
super(Entities, self).remove(item)
class Entity():
def __init__(self):
pass
class Face(Entity):
def __init__(self, pts):
self.type = 'Face'
self.vertices = [Point3d(p) for p in pts]
v1 = self.vertices[0].vector_to(self.vertices[1])
v2 = self.vertices[1].vector_to(self.vertices[2])
self.normal = v1.cross(v2).normalized()
Entity.__init__(self)
def draw(self):
N = self.normal
glBegin(GL_QUADS)
glNormal3f(N.x, N.y, N.z)
for v in self.vertices:
glVertex3f(v.x, v.y, v.z)
glEnd()
def reverse(self):
self.normal = -self.normal
def extrude(self, amt, new=0):
f2 = [p.offset(self.normal*amt) for p in self.vertices]
f1 = self.vertices
l = [(f2[c], f1[c], f1[c-1], f2[c-1]) for c in range(len(f1))] + [tuple(f2)]
self.vertices = f2
return l
class Edge(Entity):
def __init__(self, pts):
self.type = 'Edge'
self.vertices = [Point3d(p) for p in pts]
Entity.__init__(self)
def draw(self):
glColor3f(0.0,1.0,0.0)
glBegin(GL_LINES)
for v in self.vertices:
glVertex3f(v.x, v.y, v.z)
glEnd()
#------------------#
#-- GL functions --#
#------------------#
def update_view():
glColor3f(1.0,0.0,0.0)#set square to red
ents.draw()
def init():#-- Lights --#
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
def display():#-- GL function --#
glClearDepth(1) #for lighting
glClearColor(0.0,0.0,0.0,1.0)
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
gluLookAt(5.0,5.0,5.0, #eye
0.0,0.0,0.0, #target
0.0,1.0,0.0) #up
update_view()
glutSwapBuffers()
def reshape(w, h):#-- GL window resize event -- #
glViewport(0,0,w,h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(60,w/h, 1.0, 100)
glMatrixMode(GL_MODELVIEW)
def keyboard(key, w, h):
print key, w, h
if key == ESCAPE:
sys.exit(0)
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA)
glutInitWindowSize(500,500)
glutInitWindowPosition(100,100)
glutCreateWindow("Blank Window")
init() #lighting
glutDisplayFunc(display) #--display--#
#glutIdleFunc(display) #change any idle values accordingly
glutReshapeFunc(reshape) #--reshape--#
glutKeyboardFunc(keyboard)
glutMainLoop()
#-- create a face and extrude it into 3D and create an edge--#
ents = Entities()
ents.add_face(((0.0,0.0,0.0),(1.0,0.0,0.0),(1.0,1.0,0.0),(0.0,1.0,0.0)))
ents.add_edge(((0.5,0.5,0.0),(0.5,0.5,5.0)))
coords = ents[0].extrude(1)
for tup in coords:
ents.add_face(tup)
print ents
main()
So the main issue here is how to interrupt the GLUT main function so i can read input from the user?