I can't seem to get lighting to work right with glDrawElements. I have
been working on this for quite some time now and can't seem to figure
out what I'm doing wrong.  After googling, testing, logging, and
beating my head against the keyboard, I'm hoping someone on this board
can point out my folly :)

I'm loading a very simple .obj file I exported from blender.  When I
draw the object in immediate mode it lights/renders perfectly fine.
Immediate mode is using the same root data (vertices, face indices,
and calculated normals) as glDrawElements minus the pointers, so I
know my normal calculations are correct.  When I use glDrawElements
the geometry is right, but the lighting is very wrong (maybe almost
random).  It makes me think that the normals pointer isn't right, but
after double checking the code against examples, I'm pretty sure it's
right.

Anyway here are the relevant portions of my code.  My gl Init
settings, and portions of the mesh class.  Maybe a fresh (more
experienced) set of eyes will see something I'm glazing over.

Many thanks for any help you can offer :)

# ----- My Init settings
        glShadeModel(GL_SMOOTH)
        glClearColor(0.0,0.0,0.0,0.0)

        # Depth
        glClearDepth(1.0)
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_CULL_FACE)
        glDepthFunc(GL_LEQUAL)

        # Blending
        glEnable( GL_BLEND )
        glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA )

        # Vertex Arrays
        glEnableClientState( GL_VERTEX_ARRAY )
        glEnableClientState( GL_NORMAL_ARRAY )

        glPointSize( 3.0 )

        glFrontFace( GL_CCW )
        # Hints
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

        glEnable(GL_LIGHTING)
        glEnable(GL_LIGHT0)

        # Define a simple function to create ctypes arrays of floats:
        def vec(*args):
            return (GLfloat * len(args))(*args)

        glLightfv(GL_LIGHT0, GL_POSITION, vec(.5, .5, .5, 1))
        glLightfv(GL_LIGHT0, GL_DIFFUSE, vec(1, 1, 1, 1))

        glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, vec(0.5, 0.5,
0.5, 1))



# --- Some code from my mesh class:

class Mesh( Entity3D ):
    def __init__(self):
        Entity3D.__init__(self)
        self.vertices = []          # Vertices
        self.tri_normals = []       # Tri Normals
        self.quad_normals = []      # Quad Normals
        self.quads = []             # Quad Indices
        self.tris =[]               # Tri Indices

        self.c_verts = None         # Vertex Array
        self.c_quad_indices = None  # Quad Indices pointer
        self.c_tri_indices = None   # Tri Indices pointer
        self.c_tri_normal = None    # Tri normals
        self.c_quad_normal = None   # Quad normals

        self.rotx = 0.0
        self.roty = 0.0
        self.rotz = 0.0

    def load_obj(self, path):
        fin = open( path, 'rt' )
        for line in fin:
            temp = line.split(' ')
            if temp[0] == 'v':
                self.vertices.extend( [float(temp[1]),
                                       float(temp[2]),
                                       float(temp[3]) ] )

            if temp[0] == 'f':
                # Read triangle
                if len(temp) == 4:
                    self.tris.extend( [int(temp[1])-1,
                                       int(temp[2])-1,
                                       int(temp[3])-1 ] )
                # Read quad
                elif len(temp) == 5:
                    self.quads.extend( [int(temp[1])-1,
                                        int(temp[2])-1,
                                        int(temp[3])-1,
                                        int(temp[4])-1 ] )
        fin.close()

        # Calculate Normals
        for i in range(0, len(self.tris), 3):
            p1 =Vector3( self.vertices[ (self.tris[i]*3)   ],
                         self.vertices[ (self.tris[i]*3)+1 ],
                         self.vertices[ (self.tris[i]*3)+2 ] )

            p2 =Vector3( self.vertices[ (self.tris[i+1]*3)   ],
                         self.vertices[ (self.tris[i+1]*3)+1 ],
                         self.vertices[ (self.tris[i+1]*3)+2 ] )

            p3 =Vector3( self.vertices[ (self.tris[i+2]*3)   ],
                         self.vertices[ (self.tris[i+2]*3)+1 ],
                         self.vertices[ (self.tris[i+2]*3)+2 ] )

            v1 = p1 - p2
            v2 = p3 - p2
            v1.normalize()
            v2.normalize()

            cross = v2.cross(v1)
            cross.normalize()

            self.tri_normals.append( cross.x )
            self.tri_normals.append( cross.y )
            self.tri_normals.append( cross.z )

        for i in range(0, len(self.quads), 4):
            p1 =Vector3( self.vertices[ (self.quads[i] * 3 )   ],
                         self.vertices[ (self.quads[i] * 3 )+1 ],
                         self.vertices[ (self.quads[i] * 3 )+2 ] )

            p2 =Vector3( self.vertices[ (self.quads[i+1]*3)   ],
                         self.vertices[ (self.quads[i+1]*3)+1 ],
                         self.vertices[ (self.quads[i+1]*3)+2 ] )

            p3 =Vector3( self.vertices[ (self.quads[i+2]*3)   ],
                         self.vertices[ (self.quads[i+2]*3)+1 ],
                         self.vertices[ (self.quads[i+2]*3)+2 ] )

            v1 = p1 - p2
            v2 = p2 - p3
            v1.normalize()
            v2.normalize()

            cross = v1.cross(v2)
            cross.normalize()

            self.quad_normals.extend( [ cross.x, cross.y, cross.z ] )

        self.c_verts = (GLfloat * len(self.vertices))(*self.vertices)
        self.c_tri_indices = (GLuint * len(self.tris))(*self.tris)
        self.c_quad_indices = (GLuint * len(self.quads))(*self.quads)
        self.c_tri_normal = (GLfloat * len(self.tri_normals))
(*self.tri_normals)
        self.c_quad_normal = (GLfloat * len(self.quad_normals))
(*self.quad_normals)

    def draw_immediate(self):
        glBegin( GL_QUADS )

        counter = 0
        for i in range(0, len(self.quads), 4):
            norm = self.get_quad_normal(counter)

            glNormal3f( norm.x, norm.y, norm.z )
            glVertex3f( self.vertices[ (self.quads[i] * 3 )   ],
                        self.vertices[ (self.quads[i] * 3 )+1 ],
                        self.vertices[ (self.quads[i] * 3 )+2 ] )

            glVertex3f( self.vertices[ (self.quads[i+1]*3)   ],
                        self.vertices[ (self.quads[i+1]*3)+1 ],
                        self.vertices[ (self.quads[i+1]*3)+2 ] )

            glVertex3f( self.vertices[ (self.quads[i+2]*3)   ],
                        self.vertices[ (self.quads[i+2]*3)+1 ],
                        self.vertices[ (self.quads[i+2]*3)+2 ] )

            glVertex3f( self.vertices[ (self.quads[i+3]*3)   ],
                        self.vertices[ (self.quads[i+3]*3)+1 ],
                        self.vertices[ (self.quads[i+3]*3)+2 ] )
            counter += 1
        glEnd()


    def draw(self):
        glColor3ub(255,255,255)
        glPushMatrix()

        glTranslatef( self.position.x, self.position.y,
self.position.z )

        glRotatef(self.euler.z, 0.0, 0.0, 1.0)
        glRotatef(self.euler.y, 0.0, 1.0, 0.0)
        glRotatef(self.euler.x, 1.0, 0.0, 0.0)

        glVertexPointer(3, GL_FLOAT, 0, self.c_verts )

        glNormalPointer( GL_FLOAT, 0, self.c_quad_normal )

        #self.draw_intermediate()
        glDrawElements( GL_QUADS, len(self.quads), GL_UNSIGNED_INT,
self.c_quad_indices )
        #self.draw_normals()

        glPopMatrix()



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to