Ejemplo n.º 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()
Ejemplo n.º 2
0
    def GetMatrix(self, bRotationOnly=False):
        matrix = M3DMatrix44f()

        # Calculate the right side (x) vector, drop it right into the matrix
        vXAxis = m3dCrossProduct(self.vUp, self.vForward)

        # m3dSetMatrixColum44 does not fill in the fourth value...
        # X Column
        m3dSetMatrixColumn44(matrix, vXAxis, 0)
        matrix[3] = 0.0

        # Y Column
        m3dSetMatrixColumn44(matrix, self.vUp, 1)
        matrix[7] = 0.0

        # Z Column
        m3dSetMatrixColumn44(matrix, self.vForward, 2)
        matrix[11] = 0.0

        # Translation (already done)
        if (bRotationOnly == True):
            matrix[12] = 0.0
            matrix[13] = 0.0
            matrix[14] = 0.0
        else:
            m3dSetMatrixColumn44(matrix, self.vOrigin, 3)

        matrix[15] = 1.0
        return matrix
Ejemplo n.º 3
0
def DrawGLScene():
    # Clear the window
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    glPushMatrix()

    frameCamera.ApplyCameraTransform()  # Move the camera about

    # Sky Box is manually textured
    glDisable(GL_TEXTURE_GEN_S)
    glDisable(GL_TEXTURE_GEN_T)
    glDisable(GL_TEXTURE_GEN_R)
    DrawSkyBox()

    # Use texgen to apply cube map
    glEnable(GL_TEXTURE_GEN_S)
    glEnable(GL_TEXTURE_GEN_T)
    glEnable(GL_TEXTURE_GEN_R)

    glPushMatrix()
    glTranslatef(0.0, 0.0, -3.0)

    glMatrixMode(GL_TEXTURE)
    glPushMatrix()

    # Invert camera matrix (rotation only) and apply to
    # texture coordinates
    m = M3DMatrix44f()
    invert = M3DMatrix44f()

    m = frameCamera.GetCameraOrientation()
    m3dInvertMatrix44(invert, m)
    glMultMatrixf(invert)

    gltDrawSphere(0.75, 41, 41)

    glPopMatrix()
    glMatrixMode(GL_MODELVIEW)
    glPopMatrix()

    glPopMatrix()

    glutSwapBuffers()
Ejemplo n.º 4
0
 def on_draw(self):
     # Clear the window with the current clearing color
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
     
     transformationMatrix = M3DMatrix44f()
     # Build a rotation matrix
     m3dRotationMatrix44(transformationMatrix, m3dDegToRad(yRot), 0.0, 1.0, 0.0)
     transformationMatrix[12] = 0.0
     transformationMatrix[13] = 0.0
     transformationMatrix[14] = -2.5
         
     glLoadMatrixf(transformationMatrix)
     gltDrawTorus(0.35, 0.15, 40, 20)
Ejemplo n.º 5
0
    def on_draw(self):
        # Clear the window
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        glPushMatrix()
        frameCamera.ApplyCameraTransform()  # Move the camera about

        # Sky Box is manually textured
        glActiveTexture(GL_TEXTURE0)
        glDisable(GL_TEXTURE_2D)
        glActiveTexture(GL_TEXTURE1)

        glEnable(GL_TEXTURE_CUBE_MAP)
        glDisable(GL_TEXTURE_GEN_S)
        glDisable(GL_TEXTURE_GEN_T)
        glDisable(GL_TEXTURE_GEN_R)
        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
        #        DrawSkyBox()

        # Use texgen to apply cube map
        glEnable(GL_TEXTURE_GEN_S)
        glEnable(GL_TEXTURE_GEN_T)
        glEnable(GL_TEXTURE_GEN_R)
        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)

        glActiveTexture(GL_TEXTURE0)
        glEnable(GL_TEXTURE_2D)

        glPushMatrix()
        glTranslatef(0.0, 0.0, -3.0)

        glActiveTexture(GL_TEXTURE1)
        glMatrixMode(GL_TEXTURE)
        glPushMatrix()

        # Invert camera matrix (rotation only) and apply to
        # texture coordinates
        invert = M3DMatrix44f()

        m = frameCamera.GetCameraOrientation()
        m3dInvertMatrix44(invert, m)
        glMultMatrixf(invert)

        glColor3f(1.0, 1.0, 1.0)
        gltDrawSphere(0.75, 41, 41)

        glPopMatrix()
        glMatrixMode(GL_MODELVIEW)
        glPopMatrix()

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

    transformationMatrix = M3DMatrix44f()
    # Build a rotation matrix
    m3dRotationMatrix44(transformationMatrix, m3dDegToRad(yRot), 0.0, 1.0, 0.0)
    transformationMatrix[12] = 0.0
    transformationMatrix[13] = 0.0
    transformationMatrix[14] = -2.5

    DrawTorus(transformationMatrix)

    glutSwapBuffers()
Ejemplo n.º 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]
Ejemplo n.º 8
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
Ejemplo n.º 9
0
spheres = [GLFrame() for i in range(NUM_SPHERES)]
frameCamera = GLFrame()

# Light and material data

# pyglet reverses y direction
fLightPos = (GLfloat * 4)(-100.0, 100.0, 50.0, 1.0)

lightArrayType = GLfloat * 4
fNoLight = lightArrayType(0.0, 0.0, 0.0, 0.0)
fLowLight = lightArrayType(0.25, 0.25, 0.25, 1.0)
fBrightLight = lightArrayType(1.0, 1.0, 1.0, 1.0)

yRot = 0.0 # Rotation angle for animation

mShadowMatrix = M3DMatrix44f()

# Draw a gridded ground
def DrawGround():
    fExtent = 20.0
    fStep = 1.0
    y = -0.4
    
    iStrip = -fExtent
    
    while (iStrip <= fExtent):
        t = 0.0
        glBegin(GL_TRIANGLE_STRIP)
        
        glNormal3f(0.0, 1.0, 0.0) # All point up
        iRun = fExtent
Ejemplo n.º 10
0
sys.path.append("../shared")

from math3d import M3DVector3f, m3dFindNormal, M3DMatrix44f, m3dGetPlaneEquation, m3dMakePlanarShadowMatrix
from fakeglut import glutSolidSphere

xRot = 0.0
yRot = 0.0

lightArrayType = GLfloat * 4
ambientLight = lightArrayType(0.3, 0.3, 0.3, 1.0)
diffuseLight = lightArrayType(0.7, 0.7, 0.7, 1.0)
specular = lightArrayType(1.0, 1.0, 1.0, 1.0)
lightPos = (GLfloat * 4)(-75.0, 150.0, -50.0, 0.0)
specref = (GLfloat * 4)(1.0, 1.0, 1.0, 1.0)

shadowMat = M3DMatrix44f()

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)
def RegenerateShadowMap():
    global textureMatrix, strips
    lightModelview = (GLfloat * 16)()
    lightProjection = (GLfloat * 16)()
    sceneBoundingRadius = 95.0  # based on objects in scene

    # Save the depth precision for where it's useful
    lightToSceneDistance = sqrt(lightPos[0] * lightPos[0] +
                                lightPos[1] * lightPos[1] +
                                lightPos[2] * lightPos[2])
    nearPlane = lightToSceneDistance - sceneBoundingRadius

    # Keep the scene filling the depth texture
    fieldOfView = m3dRadToDeg(2.0 *
                              atan(sceneBoundingRadius / lightToSceneDistance))

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(fieldOfView, 1.0, nearPlane,
                   nearPlane + (2.0 * sceneBoundingRadius))
    glGetFloatv(GL_PROJECTION_MATRIX, lightProjection)

    # Switch to light's point of view
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    gluLookAt(lightPos[0], lightPos[1], lightPos[2], 0.0, 0.0, 0.0, 0.0, 1.0,
              0.0)
    glGetFloatv(GL_MODELVIEW_MATRIX, lightModelview)
    glViewport(0, 0, shadowWidth, shadowHeight)

    # Clear the depth buffer only
    glClear(GL_DEPTH_BUFFER_BIT)

    # All we care about here is resulting depth values
    glShadeModel(GL_FLAT)
    glDisable(GL_LIGHTING)
    glDisable(GL_COLOR_MATERIAL)
    glDisable(GL_NORMALIZE)
    glColorMask(0, 0, 0, 0)

    # Overcome imprecision
    glEnable(GL_POLYGON_OFFSET_FILL)

    # Draw objects in the scene except base plane
    # which never shadows anything
    DrawModels(False)

    # Copy depth values into depth texture
    glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, shadowWidth,
                     shadowHeight, 0)

    # Restore normal drawing state
    glShadeModel(GL_SMOOTH)
    glEnable(GL_LIGHTING)
    glEnable(GL_COLOR_MATERIAL)
    glEnable(GL_NORMALIZE)
    glColorMask(1, 1, 1, 1)
    glDisable(GL_POLYGON_OFFSET_FILL)

    # Set up texture matrix for shadow map projection,
    # which will be rolled into the eye linear
    # texture coordinate generation plane equations
    tempMatrix = M3DMatrix44f()
    m3dLoadIdentity44(tempMatrix)
    m3dTranslateMatrix44(tempMatrix, 0.5, 0.5, 0.5)
    m3dScaleMatrix44(tempMatrix, 0.5, 0.5, 0.5)
    m3dMatrixMultiply44(textureMatrix, tempMatrix, lightProjection)
    m3dMatrixMultiply44(tempMatrix, textureMatrix, lightModelview)

    # transpose to get the s, t, r, and q rows for plane equations
    m3dTransposeMatrix44(textureMatrix, tempMatrix)
    # this seems sorta awkward, but I haven't hit on a better way to index into the array in a fashion that
    # pyglet will work with.
    strips = [
        (GLfloat * 4)(textureMatrix[4], textureMatrix[5], textureMatrix[6],
                      textureMatrix[7]),
        (GLfloat * 4)(textureMatrix[8], textureMatrix[9], textureMatrix[10],
                      textureMatrix[11]),
        (GLfloat * 4)(textureMatrix[12], textureMatrix[13], textureMatrix[14],
                      textureMatrix[15]),
    ]
shadowWidth = 1024  # set based on window size
shadowHeight = 512
shadowTextureID = GLuint(0)

maxTexSize = GLint(0)  # maximum allowed size for 1D/2D texture

ambientLight = (GLfloat * 4)(0.2, 0.2, 0.2, 1.0)
diffuseLight = (GLfloat * 4)(0.7, 0.7, 0.7, 1.0)
noLight = (GLfloat * 4)(0.0, 0.0, 0.0, 1.0)
lightPos = (GLfloat * 4)(100.0, 300.0, 100.0, 1.0)

cameraPos = (GLfloat * 4)(100.0, 150.0, 200.0, 1.0)
cameraZoom = 0.3

textureMatrix = M3DMatrix44f()
global strips


# Called to draw scene objects
def DrawModels(drawBasePlane):
    if drawBasePlane:
        # Draw plane that the objects rest on
        glColor3f(0.0, 0.0, 0.90)  # Blue
        glNormal3f(0.0, 1.0, 0.0)
        glBegin(GL_QUADS)
        glVertex3f(-100.0, -25.0, -100.0)
        glVertex3f(-100.0, -25.0, 100.0)
        glVertex3f(100.0, -25.0, 100.0)
        glVertex3f(100.0, -25.0, -100.0)
        glEnd()
Ejemplo n.º 13
0
    def on_draw(self):
        mCubeTransform = M3DMatrix44f()
        # Clear the window with current clearing color
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glShadeModel(GL_SMOOTH)
        glEnable(GL_NORMALIZE)

        glPushMatrix()

        # Draw plane that the cube rests on
        glDisable(GL_LIGHTING)
        if nStep == 5:
            glColor3ub(255, 255, 255)
            glEnable(GL_TEXTURE_2D)
            glBindTexture(GL_TEXTURE_2D, textures[0])
            glBegin(GL_QUADS)

            glTexCoord2f(0.0, 0.0)
            glVertex3f(-100.0, -25.3, -100.0)
            glTexCoord2f(0.0, 1.0)
            glVertex3f(-100.0, -25.3, 100.0)
            glTexCoord2f(1.0, 1.0)
            glVertex3f(100.0, -25.3, 100.0)
            glTexCoord2f(1.0, 0.0)
            glVertex3f(100.0, -25.3, -100.0)
            glEnd()

        else:
            glColor3f(0.0, 0.0, 0.90)  # Blue
            glBegin(GL_QUADS)
            glVertex3f(-100.0, -25.3, -100.0)
            glVertex3f(-100.0, -25.3, 100.0)
            glVertex3f(100.0, -25.3, 100.0)
            glVertex3f(100.0, -25.3, -100.0)
            glEnd()

        # Set drawing color to Red
        glColor3f(1.0, 0.0, 0.0)

        # Enable, disable lighting
        if nStep > 2:
            glEnable(GL_DEPTH_TEST)
            glDepthFunc(GL_LEQUAL)
            glEnable(GL_COLOR_MATERIAL)

            glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient)
            glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse)
            glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular)
            glEnable(GL_LIGHTING)
            glEnable(GL_LIGHT0)
            glMaterialfv(GL_FRONT, GL_SPECULAR, lightSpecular)
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, materialColor)
            glMateriali(GL_FRONT, GL_SHININESS, 128)

        # Move the cube slightly forward and to the left
        glTranslatef(-10.0, 0.0, 10.0)

        if nStep == 0:
            # Just draw the wire framed cube
            glutWireCube(50.0)

            # Same wire cube with hidden line removal simulated
        elif nStep == 1:
            # Front Face (before rotation)
            glBegin(GL_LINES)
            glVertex3f(25.0, 25.0, 25.0)
            glVertex3f(25.0, -25.0, 25.0)

            glVertex3f(25.0, -25.0, 25.0)
            glVertex3f(-25.0, -25.0, 25.0)

            glVertex3f(-25.0, -25.0, 25.0)
            glVertex3f(-25.0, 25.0, 25.0)

            glVertex3f(-25.0, 25.0, 25.0)
            glVertex3f(25.0, 25.0, 25.0)
            glEnd()

            # Top of cube
            glBegin(GL_LINES)
            # Front Face
            glVertex3f(25.0, 25.0, 25.0)
            glVertex3f(25.0, 25.0, -25.0)

            glVertex3f(25.0, 25.0, -25.0)
            glVertex3f(-25.0, 25.0, -25.0)

            glVertex3f(-25.0, 25.0, -25.0)
            glVertex3f(-25.0, 25.0, 25.0)

            glVertex3f(-25.0, 25.0, 25.0)
            glVertex3f(25.0, 25.0, 25.0)
            glEnd()

            # Last two segments for effect
            glBegin(GL_LINES)
            glVertex3f(25.0, 25.0, -25.0)
            glVertex3f(25.0, -25.0, -25.0)

            glVertex3f(25.0, -25.0, -25.0)
            glVertex3f(25.0, -25.0, 25.0)
            glEnd()

        # Uniform colored surface, looks 2D and goofey
        elif nStep == 2:
            glutSolidCube(50.0)

        elif nStep == 3:
            glutSolidCube(50.0)

        # Draw a shadow with some lighting
        elif nStep == 4:
            glGetFloatv(GL_MODELVIEW_MATRIX, mCubeTransform)
            glutSolidCube(50.0)
            glPopMatrix()

            # Disable lighting, we'll just draw the shadow as black
            glDisable(GL_LIGHTING)

            glPushMatrix()

            pPlane = m3dGetPlaneEquation(ground[0], ground[1], ground[2])
            mCubeTransform = m3dMakePlanarShadowMatrix(pPlane, vLightPos)
            #MakeShadowMatrix(ground, lightpos, cubeXform)
            glMultMatrixf(mCubeTransform)

            glTranslatef(-10.0, 0.0, 10.0)

            # Set drawing color to Black
            glColor3f(0.0, 0.0, 0.0)

            glutSolidCube(50.0)

        elif nStep == 5:
            glColor3ub(255, 255, 255)
            glGetFloatv(GL_MODELVIEW_MATRIX, mCubeTransform)

            # Front Face (before rotation)
            glBindTexture(GL_TEXTURE_2D, textures[1])
            glBegin(GL_QUADS)
            glTexCoord2f(1.0, 1.0)
            glVertex3f(25.0, 25.0, 25.0)
            glTexCoord2f(1.0, 0.0)
            glVertex3f(25.0, -25.0, 25.0)
            glTexCoord2f(0.0, 0.0)
            glVertex3f(-25.0, -25.0, 25.0)
            glTexCoord2f(0.0, 1.0)
            glVertex3f(-25.0, 25.0, 25.0)
            glEnd()

            # Top of cube
            glBindTexture(GL_TEXTURE_2D, textures[2])
            glBegin(GL_QUADS)
            # Front Face
            glTexCoord2f(0.0, 0.0)
            glVertex3f(25.0, 25.0, 25.0)
            glTexCoord2f(1.0, 0.0)
            glVertex3f(25.0, 25.0, -25.0)
            glTexCoord2f(1.0, 1.0)
            glVertex3f(-25.0, 25.0, -25.0)
            glTexCoord2f(0.0, 1.0)
            glVertex3f(-25.0, 25.0, 25.0)
            glEnd()

            # Last two segments for effect
            glBindTexture(GL_TEXTURE_2D, textures[3])
            glBegin(GL_QUADS)
            glTexCoord2f(1.0, 1.0)
            glVertex3f(25.0, 25.0, -25.0)
            glTexCoord2f(1.0, 0.0)
            glVertex3f(25.0, -25.0, -25.0)
            glTexCoord2f(0.0, 0.0)
            glVertex3f(25.0, -25.0, 25.0)
            glTexCoord2f(0.0, 1.0)
            glVertex3f(25.0, 25.0, 25.0)
            glEnd()

            glPopMatrix()

            # Disable lighting, we'll just draw the shadow as black
            glDisable(GL_LIGHTING)
            glDisable(GL_TEXTURE_2D)

            glPushMatrix()

            pPlane = m3dGetPlaneEquation(ground[0], ground[1], ground[2])
            mCubeTransform = m3dMakePlanarShadowMatrix(pPlane, vLightPos)
            glMultMatrixf(mCubeTransform)

            glTranslatef(-10.0, 0.0, 10.0)

            # Set drawing color to Black
            glColor3f(0.0, 0.0, 0.0)
            glutSolidCube(50.0)

        glPopMatrix()