Example #1
0
def toonDrawTorus(majorRadius, minorRadius, numMajor, numMinor, lightDir):

    mModelViewMatrix = M3DMatrix44f()
    mInvertedLight = M3DMatrix44f()
    vNewLight = M3DVector3f()
    vNormal = M3DVector3f()
    majorStep = 2.0 * M3D_PI / numMajor
    minorStep = 2.0 * M3D_PI / numMinor

    # Get the modelview matrix
    glGetFloatv(GL_MODELVIEW_MATRIX, mModelViewMatrix)

    # Instead of transforming every normal and then dotting it with
    # the light vector, we will transform the light into object
    # space by multiplying it by the inverse of the modelview matrix
    m3dInvertMatrix44(mInvertedLight, mModelViewMatrix)
    m3dTransformVector3(vNewLight, vLightDir, mInvertedLight)
    vNewLight[0] -= mInvertedLight[12]
    vNewLight[1] -= mInvertedLight[13]
    vNewLight[2] -= mInvertedLight[14]
    m3dNormalizeVector(vNewLight)

    # Draw torus as a series of triangle strips
    for i in range(0, numMajor):
        a0 = i * majorStep
        a1 = a0 + majorStep
        x0 = cos(a0)
        y0 = sin(a0)
        x1 = cos(a1)
        y1 = sin(a1)

        glBegin(GL_TRIANGLE_STRIP)
        for j in range(0, numMinor + 1):

            b = j * minorStep
            c = cos(b)
            r = minorRadius * c + majorRadius
            z = minorRadius * sin(b)

            # First point
            vNormal[0] = x0 * c
            vNormal[1] = y0 * c
            vNormal[2] = z / minorRadius
            m3dNormalizeVector(vNormal)

            # Texture coordinate is set by intensity of light
            glTexCoord1f(m3dDotProduct(vNewLight, vNormal))
            glVertex3f(x0 * r, y0 * r, z)

            # Second point
            vNormal[0] = x1 * c
            vNormal[1] = y1 * c
            vNormal[2] = z / minorRadius
            m3dNormalizeVector(vNormal)

            # Texture coordinate is set by intensity of light
            glTexCoord1f(m3dDotProduct(vNewLight, vNormal))
            glVertex3f(x1 * r, y1 * r, z)

        glEnd()
Example #2
0
    def __init__(self):
        # Default position and orientation.  At the origin, looking
        # down the positive Z axis (right handed coordinate system).

        # At origin
        self.vOrigin = M3DVector3f(0.0, 0.0, 0.0)  # Where am I?
        # Forward is -Z (default OpenGL)
        self.vForward = M3DVector3f(0.0, 0.0, -1.0)  # Where am I going?
        self.vUp = M3DVector3f(0.0, 1.0, 0.0)  # Which way is up?
Example #3
0
    def __init__(self, *args, **kwargs):
        global shadowMat
        window.Window.__init__(self, *args, **kwargs)

        # Any three points on the ground (counter clockwise order)
        points = [
            M3DVector3f(-30.0, -149.0, -20.0),
            M3DVector3f(-30.0, -149.0, 20.0),
            M3DVector3f(40.0, -149.0, 20.0)
        ]

        glEnable(GL_DEPTH_TEST)
        glEnable(GL_CULL_FACE)  # Do not calculate inside of jet
        glFrontFace(GL_CCW)  # Counter clock-wise polygons face out

        # Enable Lighting
        glEnable(GL_LIGHTING)

        # Setup and enable light 0
        glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight)
        glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight)
        glLightfv(GL_LIGHT0, GL_SPECULAR, specular)
        glLightfv(GL_LIGHT0, GL_POSITION, lightPos)
        glEnable(GL_LIGHT0)

        # Enable color tracking
        glEnable(GL_COLOR_MATERIAL)

        # Set Material properties to follow glColor values
        glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)

        # All materials hereafter have full specular reflectivity
        # with a high shine
        glMaterialfv(GL_FRONT, GL_SPECULAR, specref)
        glMateriali(GL_FRONT, GL_SHININESS, 128)

        # light blue background
        glClearColor(0.0, 0.0, 1.0, 1.0)

        # Get the plane equation from three points on the ground

        vPlaneEquation = m3dGetPlaneEquation(points[0], points[1], points[2])

        # Calculate projection matrix to draw shadow on the ground
        shadowMat = m3dMakePlanarShadowMatrix(vPlaneEquation, lightPos)
        glEnable(GL_NORMALIZE)
def DrawTorus(mTransform):
    majorRadius = 0.35
    minorRadius = 0.15
    numMajor = 40
    numMinor = 20
    objectVertex = M3DVector3f()  # Vertex in object/eye space
    transformedVertex = M3DVector3f()  # New Transformed vertex
    majorStep = 2.0
    majorStep = 2.0 * M3D_PI / float(numMajor)
    minorStep = 2.0 * M3D_PI / float(numMinor)

    i = 0
    while (i < numMajor):
        a0 = i * majorStep
        a1 = a0 + majorStep
        x0 = cos(a0)
        y0 = sin(a0)
        x1 = cos(a1)
        y1 = sin(a1)

        glBegin(GL_TRIANGLE_STRIP)
        j = 0
        # BS some sort of rounding error keeps a strip from being drawn
        while (j < numMinor + 1):
            b = j * minorStep
            c = cos(b)
            r = minorRadius * c + majorRadius
            z = minorRadius * sin(b)

            # First point
            objectVertex[0] = x0 * r
            objectVertex[1] = y0 * r
            objectVertex[2] = z
            m3dTransformVector3(transformedVertex, objectVertex, mTransform)
            glVertex3fv(transformedVertex)

            # Second point
            objectVertex[0] = x1 * r
            objectVertex[1] = y1 * r
            objectVertex[2] = z
            m3dTransformVector3(transformedVertex, objectVertex, mTransform)
            glVertex3fv(transformedVertex)
            j += 1
        glEnd()
        i += 1
Example #5
0
    def GetCameraOrientation(self):
        m = M3DMatrix44f()
        x = M3DVector3f()
        z = M3DVector3f()
        # Make rotation matrix
        # Z vector is reversed
        z[0] = -self.vForward[0]
        z[1] = -self.vForward[1]
        z[2] = -self.vForward[2]

        # X vector = Y cross Z
        x = m3dCrossProduct(self.vUp, z)

        # Matrix has no translation information and is
        # transposed.... (rows instead of columns)
        m[0] = x[0]
        m[1] = x[1]
        m[2] = x[2]
        m[3] = 0.0

        m[4] = self.vUp[0]
        m[5] = self.vUp[1]
        m[6] = self.vUp[2]
        m[7] = 0.0

        m[8] = z[0]
        m[9] = z[1]
        m[10] = z[2]
        m[11] = 0.0

        m[12] = 0.0
        m[13] = 0.0
        m[14] = 0.0
        m[15] = 1.0

        return m
Example #6
0
def gltDrawTorus(majorRadius, minorRadius, numMajor, numMinor):
    vNormal = M3DVector3f()
    majorStep = 2.0 * M3D_PI / numMajor
    minorStep = 2.0 * M3D_PI / numMinor

    i = j = 0
    while i < numMajor:
        a0 = i * majorStep
        a1 = a0 + majorStep
        x0 = cos(a0)
        y0 = sin(a0)
        x1 = cos(a1)
        y1 = sin(a1)

        glBegin(GL_TRIANGLE_STRIP)
        j = 0
        while j <= numMinor:
            b = j * minorStep
            c = cos(b)
            r = minorRadius * c + majorRadius
            z = minorRadius * sin(b)

            # First point
            glTexCoord2f(
                float(i) / float(numMajor),
                float(j) / float(numMinor))
            vNormal[0] = x0 * c
            vNormal[1] = y0 * c
            vNormal[2] = z / minorRadius
            m3dNormalizeVector(vNormal)
            glNormal3fv(vNormal)
            glVertex3f(x0 * r, y0 * r, z)

            glTexCoord2f(
                float(i + 1) / float(numMajor),
                float(j) / float(numMinor))
            vNormal[0] = x1 * c
            vNormal[1] = y1 * c
            vNormal[2] = z / minorRadius
            m3dNormalizeVector(vNormal)
            glNormal3fv(vNormal)
            glVertex3f(x1 * r, y1 * r, z)

            j += 1

        glEnd()
        i += 1
Example #7
0
    def RotateLocalY(self, fAngle):
        # Just Rotate around the up vector
        # Create a rotation matrix around my Up (Y) vector
        rotMat = M3DMatrix44f()
        m3dRotationMatrix44(rotMat, fAngle, self.vUp[0], self.vUp[1],
                            self.vUp[2])

        newVect = M3DVector3f()

        # Rotate forward pointing vector (inlined 3x3 transform)
        newVect[0] = rotMat[0] * self.vForward[0] + rotMat[4] * self.vForward[
            1] + rotMat[8] * self.vForward[2]
        newVect[1] = rotMat[1] * self.vForward[0] + rotMat[5] * self.vForward[
            1] + rotMat[9] * self.vForward[2]
        newVect[2] = rotMat[2] * self.vForward[0] + rotMat[6] * self.vForward[
            1] + rotMat[10] * self.vForward[2]
        self.vForward[0] = newVect[0]
        self.vForward[1] = newVect[1]
        self.vForward[2] = newVect[2]
Example #8
0
def DrawJet(nShadow):
    
    # Set material color, note we only have to set to black
    # for the shadow once
    if nShadow == 0:
        glColor3ub(128, 128, 128)
    else:
        glColor3ub(0,0,0)

    # Nose Cone - Points straight down
    # Set material color
    glBegin(GL_TRIANGLES)
    
    glNormal3f(0.0, -1.0, 0.0)
    glNormal3f(0.0, -1.0, 0.0)
    glVertex3f(0.0, 0.0, 60.0)
    glVertex3f(-15.0, 0.0, 30.0)
    glVertex3f(15.0,0.0,30.0)
            

    # Verticies for this panel
    vPoints = [ M3DVector3f(15.0, 0.0, 30.0),
                M3DVector3f(0.0, 15.0, 30.0),
                M3DVector3f(0.0, 0.0, 60.0)]
                
    # Calculate the normal for the plane
    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])
    
    vPoints = [ M3DVector3f(0.0, 0.0, 60.0),
                M3DVector3f(0.0, 15.0, 30.0),
                M3DVector3f(-15.0, 0.0, 30.0)]
                
    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])
    


    # Body of the Plane ############
    vPoints = [ M3DVector3f(-15.0, 0.0, 30.0),
                M3DVector3f(0.0, 15.0, 30.0),
                M3DVector3f(0.0, 0.0, -56.0)]
                
    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])
        
    vPoints = [ M3DVector3f(0.0, 0.0, -56.0),
                M3DVector3f(0.0, 15.0, 30.0),
                M3DVector3f(15.0, 0.0, 30.0)]
                
    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])
    
    glNormal3f(0.0, -1.0, 0.0)
    glVertex3f(15.0,0.0,30.0)
    glVertex3f(-15.0, 0.0, 30.0)
    glVertex3f(0.0, 0.0, -56.0)

    #######################
    # Left wing
    # Large triangle for bottom of wing
    
    vPoints = [ M3DVector3f(0.0, 2.0, 27.0),
                M3DVector3f(-60.0, 2.0, -8.0),
                M3DVector3f(60, 2.0, -8.0)]
                
    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])
    
    vPoints = [ M3DVector3f(60.0, 2.0, -8.0),
                M3DVector3f(0.0, 7.0, -8.0),
                M3DVector3f(0.0, 2.0, 27.0)]
                
    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])
    
    vPoints = [ M3DVector3f(60.0, 2.0, -8.0),
                M3DVector3f(-60.0, 2.0, -8.0),
                M3DVector3f(0.0, 7.0, -8.0)]

    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])
    
    vPoints = [ M3DVector3f(0.0, 2.0, 27.0),
                M3DVector3f(0.0, 7.0, -8.0),
                M3DVector3f(-60.0, 2.0, -8.0)]
    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])
    
            
                    
    # Tail section###############
    # Bottom of back fin
    glNormal3f(0.0, -1.0, 0.0)
    glVertex3f(-30.0, -0.50, -57.0)
    glVertex3f(30.0, -0.50, -57.0)
    glVertex3f(0.0,-0.50,-40.0)

    vPoints = [ M3DVector3f(0.0, -0.5, -40.0),
                M3DVector3f(30.0, -0.5, -57.0),
                M3DVector3f(0.0, 4.0, -57.0)]

    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])
    
    vPoints = [ M3DVector3f(0.0, 4.0, -57.0),
                M3DVector3f(-30.0, -0.5, -57.0),
                M3DVector3f(0.0, -0.5, -40.0)]
    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])
    
    vPoints = [ M3DVector3f(30.0, -0.5, -57.0),
                M3DVector3f(-30.0, -0.5, -57.0),
                M3DVector3f(0.0, 4.0, -57.0)]
    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])
    

    vPoints = [ M3DVector3f(0.0, 0.5, -40.0),
                M3DVector3f(3.0, 0.5, -57.0),
                M3DVector3f(0.0, 25.0, -65.0)]

    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])
    
                 
    vPoints = [ M3DVector3f(0.0, 25.0, -65.0),
                M3DVector3f(-3.0, 0.5, -57.0),
                M3DVector3f(0.0, 0.5, -40.0)]

    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])
    
    vPoints = [ M3DVector3f(3.0, 0.5, -57.0),
                M3DVector3f(-3.0, 0.5, -57.0),
                M3DVector3f(0.0, 25.0, -65.0)]

    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])


    glEnd()
def DrawGLScene():
    # Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    vNormal = M3DVector3f()

    # Save the matrix state and do the rotations
    glPushMatrix()
    glRotatef(xRot, 1.0, 0.0, 0.0)
    glRotatef(yRot, 0.0, 1.0, 0.0)

    # Nose Cone - Points straight down
    # Set material color
    glColor3ub(128, 128, 128)

    glBegin(GL_TRIANGLES)

    glNormal3f(0.0, -1.0, 0.0)
    glNormal3f(0.0, -1.0, 0.0)
    glVertex3f(0.0, 0.0, 60.0)
    glVertex3f(-15.0, 0.0, 30.0)
    glVertex3f(15.0, 0.0, 30.0)

    # Verticies for this panel
    vPoints = [
        M3DVector3f(15.0, 0.0, 30.0),
        M3DVector3f(0.0, 15.0, 30.0),
        M3DVector3f(0.0, 0.0, 60.0)
    ]

    # Calculate the normal for the plane
    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])

    vPoints = [
        M3DVector3f(0.0, 0.0, 60.0),
        M3DVector3f(0.0, 15.0, 30.0),
        M3DVector3f(-15.0, 0.0, 30.0)
    ]

    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])

    # Body of the Plane ############
    vPoints = [
        M3DVector3f(-15.0, 0.0, 30.0),
        M3DVector3f(0.0, 15.0, 30.0),
        M3DVector3f(0.0, 0.0, -56.0)
    ]

    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])

    vPoints = [
        M3DVector3f(0.0, 0.0, -56.0),
        M3DVector3f(0.0, 15.0, 30.0),
        M3DVector3f(15.0, 0.0, 30.0)
    ]

    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])

    glNormal3f(0.0, -1.0, 0.0)
    glVertex3f(15.0, 0.0, 30.0)
    glVertex3f(-15.0, 0.0, 30.0)
    glVertex3f(0.0, 0.0, -56.0)

    #######################
    # Left wing
    # Large triangle for bottom of wing

    vPoints = [
        M3DVector3f(0.0, 2.0, 27.0),
        M3DVector3f(-60.0, 2.0, -8.0),
        M3DVector3f(60, 2.0, -8.0)
    ]

    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])

    vPoints = [
        M3DVector3f(60.0, 2.0, -8.0),
        M3DVector3f(0.0, 7.0, -8.0),
        M3DVector3f(0.0, 2.0, 27.0)
    ]

    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])

    vPoints = [
        M3DVector3f(60.0, 2.0, -8.0),
        M3DVector3f(-60.0, 2.0, -8.0),
        M3DVector3f(0.0, 7.0, -8.0)
    ]

    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])

    vPoints = [
        M3DVector3f(0.0, 2.0, 27.0),
        M3DVector3f(0.0, 7.0, -8.0),
        M3DVector3f(-60.0, 2.0, -8.0)
    ]
    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])

    # Tail section###############
    # Bottom of back fin
    glNormal3f(0.0, -1.0, 0.0)
    glVertex3f(-30.0, -0.50, -57.0)
    glVertex3f(30.0, -0.50, -57.0)
    glVertex3f(0.0, -0.50, -40.0)

    vPoints = [
        M3DVector3f(0.0, -0.5, -40.0),
        M3DVector3f(30.0, -0.5, -57.0),
        M3DVector3f(0.0, 4.0, -57.0)
    ]

    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])

    vPoints = [
        M3DVector3f(0.0, 4.0, -57.0),
        M3DVector3f(-30.0, -0.5, -57.0),
        M3DVector3f(0.0, -0.5, -40.0)
    ]
    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])

    vPoints = [
        M3DVector3f(30.0, -0.5, -57.0),
        M3DVector3f(-30.0, -0.5, -57.0),
        M3DVector3f(0.0, 4.0, -57.0)
    ]
    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])

    vPoints = [
        M3DVector3f(0.0, 0.5, -40.0),
        M3DVector3f(3.0, 0.5, -57.0),
        M3DVector3f(0.0, 25.0, -65.0)
    ]

    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])

    vPoints = [
        M3DVector3f(0.0, 25.0, -65.0),
        M3DVector3f(-3.0, 0.5, -57.0),
        M3DVector3f(0.0, 0.5, -40.0)
    ]

    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])

    vPoints = [
        M3DVector3f(3.0, 0.5, -57.0),
        M3DVector3f(-3.0, 0.5, -57.0),
        M3DVector3f(0.0, 25.0, -65.0)
    ]

    vNormal = m3dFindNormal(vPoints[0], vPoints[1], vPoints[2])
    glNormal3fv(vNormal)
    glVertex3fv(vPoints[0])
    glVertex3fv(vPoints[1])
    glVertex3fv(vPoints[2])

    glEnd()

    # Restore the matrix state
    glPopMatrix()

    glutSwapBuffers()
Example #10
0
# OpenGL SuperBible
# Program by Richard S. Wright Jr.

import pyglet
from pyglet.gl import *
from math import sin, cos
from pyglet import window

import sys
sys.path.append("../shared")

from math3d import M3DVector3f, M3DMatrix44f, M3D_PI, m3dInvertMatrix44, m3dNormalizeVector, m3dTransformVector3, m3dDotProduct

yRot = 0.0

vLightDir = M3DVector3f(-1.0, 1.0, 1.0)


# Draw a torus (doughnut), using the current 1D texture for light shading
def toonDrawTorus(majorRadius, minorRadius, numMajor, numMinor, lightDir):

    mModelViewMatrix = M3DMatrix44f()
    mInvertedLight = M3DMatrix44f()
    vNewLight = M3DVector3f()
    vNormal = M3DVector3f()
    majorStep = 2.0 * M3D_PI / numMajor
    minorStep = 2.0 * M3D_PI / numMinor

    # Get the modelview matrix
    glGetFloatv(GL_MODELVIEW_MATRIX, mModelViewMatrix)
def DrawGLScene():

    vNormal = M3DVector3f()
    vCorners = [
        M3DVector3f(0.0, 0.8, 0.0),
        M3DVector3f(-0.5, 0.0, -0.5),
        M3DVector3f(0.5, 0.0, -0.5),
        M3DVector3f(0.5, 0.0, 0.5),
        M3DVector3f(-0.5, 0.0, 0.5),
    ]

    # Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    # Save the matrix state and do the rotations
    glPushMatrix()
    # Move object back and do in place rotation
    glTranslatef(0.0, -0.25, -4.0)
    glRotatef(xRot, 1.0, 0.0, 0.0)
    glRotatef(yRot, 0.0, 1.0, 0.0)

    # Draw the Pyramid
    glColor3f(1.0, 1.0, 1.0)
    glBegin(GL_TRIANGLES)

    # Bottom section - two triangles
    glNormal3f(0.0, -1.0, 0.0)
    glTexCoord2f(1.0, 1.0)
    glVertex3fv(vCorners[2])

    glTexCoord2f(0.0, 0.0)
    glVertex3fv(vCorners[4])

    glTexCoord2f(0.0, 1.0)
    glVertex3fv(vCorners[1])

    glTexCoord2f(1.0, 1.0)
    glVertex3fv(vCorners[2])

    glTexCoord2f(1.0, 0.0)
    glVertex3fv(vCorners[3])

    glTexCoord2f(0.0, 0.0)
    glVertex3fv(vCorners[4])

    # Front Face
    vNormal = m3dFindNormal(vCorners[0], vCorners[4], vCorners[3])
    glNormal3fv(vNormal)
    glTexCoord2f(0.5, 1.0)
    glVertex3fv(vCorners[0])
    glTexCoord2f(0.0, 0.0)
    glVertex3fv(vCorners[4])
    glTexCoord2f(1.0, 0.0)
    glVertex3fv(vCorners[3])

    # Left Face
    vNormal = m3dFindNormal(vCorners[0], vCorners[1], vCorners[4])
    glNormal3fv(vNormal)
    glTexCoord2f(0.5, 1.0)
    glVertex3fv(vCorners[0])
    glTexCoord2f(0.0, 0.0)
    glVertex3fv(vCorners[1])
    glTexCoord2f(1.0, 0.0)
    glVertex3fv(vCorners[4])

    # Back Face
    vNormal = m3dFindNormal(vCorners[0], vCorners[2], vCorners[1])
    glNormal3fv(vNormal)
    glTexCoord2f(0.5, 1.0)
    glVertex3fv(vCorners[0])

    glTexCoord2f(0.0, 0.0)
    glVertex3fv(vCorners[2])

    glTexCoord2f(1.0, 0.0)
    glVertex3fv(vCorners[1])

    # Right Face
    vNormal = m3dFindNormal(vCorners[0], vCorners[3], vCorners[2])
    glNormal3fv(vNormal)
    glTexCoord2f(0.5, 1.0)
    glVertex3fv(vCorners[0])
    glTexCoord2f(0.0, 0.0)
    glVertex3fv(vCorners[3])
    glTexCoord2f(1.0, 0.0)
    glVertex3fv(vCorners[2])

    glEnd()

    # Restore the matrix state
    glPopMatrix()

    glutSwapBuffers()