Lesson48.py :  » Development » PyObjC » trunk » pyobjc » PyOpenGL-2.0.2.01 » OpenGL » Demo » NeHe » lesson48 » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » PyObjC 
PyObjC » trunk » pyobjc » PyOpenGL 2.0.2.01 » OpenGL » Demo » NeHe » lesson48 » Lesson48.py

from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import sys
import copy
from math import cos,sin

from ArcBall import *# ArcBallT and this tutorials set of points/vectors/matrix types

PI2 = 2.0*3.1415926535      # 2 * PI (not squared!)     // PI Squared

# *********************** Globals *********************** 
# Python 2.2 defines these directly
try:
  True
except NameError:
  True = 1==1
  False = 1==0

g_Transform = Matrix4fT ()
g_LastRot = Matrix3fT ()
g_ThisRot = Matrix3fT ()

g_ArcBall = ArcBallT (640, 480)
g_isDragging = False
g_quadratic = None


# A general OpenGL initialization function.  Sets all of the initial parameters. 
def Initialize (Width, Height):        # We call this right after our OpenGL window is created.
  global g_quadratic

  glClearColor(0.0, 0.0, 0.0, 1.0)          # This Will Clear The Background Color To Black
  glClearDepth(1.0)                  # Enables Clearing Of The Depth Buffer
  glDepthFunc(GL_LEQUAL)                # The Type Of Depth Test To Do
  glEnable(GL_DEPTH_TEST)                # Enables Depth Testing
  glShadeModel (GL_FLAT);                # Select Flat Shading (Nice Definition Of Objects)
  glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)   # Really Nice Perspective Calculations

  g_quadratic = gluNewQuadric();
  gluQuadricNormals(g_quadratic, GLU_SMOOTH);
  gluQuadricDrawStyle(g_quadratic, GLU_FILL); 
  # Why? this tutorial never maps any textures?! ? 
  # gluQuadricTexture(g_quadratic, GL_TRUE);      # // Create Texture Coords

  glEnable (GL_LIGHT0)
  glEnable (GL_LIGHTING)

  glEnable (GL_COLOR_MATERIAL)

  return True




def Upon_Drag (cursor_x, cursor_y):
  """ Mouse cursor is moving
    Glut calls this function (when mouse button is down)
    and pases the mouse cursor postion in window coords as the mouse moves.
  """
  global g_isDragging, g_LastRot, g_Transform, g_ThisRot

  if (g_isDragging):
    mouse_pt = Point2fT (cursor_x, cursor_y)
    ThisQuat = g_ArcBall.drag (mouse_pt)            # // Update End Vector And Get Rotation As Quaternion
    g_ThisRot = Matrix3fSetRotationFromQuat4f (ThisQuat)    # // Convert Quaternion Into Matrix3fT
    # Use correct Linear Algebra matrix multiplication C = A * B
    g_ThisRot = Matrix3fMulMatrix3f (g_LastRot, g_ThisRot)    # // Accumulate Last Rotation Into This One
    g_Transform = Matrix4fSetRotationFromMatrix3f (g_Transform, g_ThisRot)  # // Set Our Final Transform's Rotation From This One
  return

def Upon_Click (button, button_state, cursor_x, cursor_y):
  """ Mouse button clicked.
    Glut calls this function when a mouse button is
    clicked or released.
  """
  global g_isDragging, g_LastRot, g_Transform, g_ThisRot

  g_isDragging = False
  if (button == GLUT_RIGHT_BUTTON and button_state == GLUT_UP):
    # Right button click
    g_LastRot = Matrix3fSetIdentity ();              # // Reset Rotation
    g_ThisRot = Matrix3fSetIdentity ();              # // Reset Rotation
    g_Transform = Matrix4fSetRotationFromMatrix3f (g_Transform, g_ThisRot);  # // Reset Rotation
  elif (button == GLUT_LEFT_BUTTON and button_state == GLUT_UP):
    # Left button released
    g_LastRot = copy.copy (g_ThisRot);              # // Set Last Static Rotation To Last Dynamic One
  elif (button == GLUT_LEFT_BUTTON and button_state == GLUT_DOWN):
    # Left button clicked down
    g_LastRot = copy.copy (g_ThisRot);              # // Set Last Static Rotation To Last Dynamic One
    g_isDragging = True                      # // Prepare For Dragging
    mouse_pt = Point2fT (cursor_x, cursor_y)
    g_ArcBall.click (mouse_pt);                # // Update Start Vector And Prepare For Dragging

  return



def Torus(MinorRadius, MajorRadius):    
  # // Draw A Torus With Normals
  glBegin( GL_TRIANGLE_STRIP );                  # // Start A Triangle Strip
  for i in xrange (20):                       # // Stacks
    for j in xrange (-1, 20):                     # // Slices
      # NOTE, python's definition of modulus for negative numbers returns
      # results different than C's
      #       (a / d)*d  +  a % d = a
      if (j < 0):
        wrapFrac = (-j%20)/20.0
        wrapFrac *= -1.0
      else:
        wrapFrac = (j%20)/20.0;
      phi = PI2*wrapFrac;
      sinphi = sin(phi);
      cosphi = cos(phi);

      r = MajorRadius + MinorRadius*cosphi;

      glNormal3f (sin(PI2*(i%20+wrapFrac)/20.0)*cosphi, sinphi, cos(PI2*(i%20+wrapFrac)/20.0)*cosphi);
      glVertex3f (sin(PI2*(i%20+wrapFrac)/20.0)*r, MinorRadius*sinphi, cos(PI2*(i%20+wrapFrac)/20.0)*r);

      glNormal3f (sin(PI2*(i+1%20+wrapFrac)/20.0)*cosphi, sinphi, cos(PI2*(i+1%20+wrapFrac)/20.0)*cosphi);
      glVertex3f (sin(PI2*(i+1%20+wrapFrac)/20.0)*r, MinorRadius*sinphi, cos(PI2*(i+1%20+wrapFrac)/20.0)*r);
  glEnd();                            # // Done Torus
  return

def Draw ():
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        # // Clear Screen And Depth Buffer
  glLoadIdentity();                        # // Reset The Current Modelview Matrix
  glTranslatef(-1.5,0.0,-6.0);                  # // Move Left 1.5 Units And Into The Screen 6.0

  glPushMatrix();                          # // NEW: Prepare Dynamic Transform
  glMultMatrixf(g_Transform);                    # // NEW: Apply Dynamic Transform
  glColor3f(0.75,0.75,1.0);
  Torus(0.30,1.00);
  glPopMatrix();                          # // NEW: Unapply Dynamic Transform

  glLoadIdentity();                        # // Reset The Current Modelview Matrix
  glTranslatef(1.5,0.0,-6.0);                    # // Move Right 1.5 Units And Into The Screen 7.0

  glPushMatrix();                          # // NEW: Prepare Dynamic Transform
  glMultMatrixf(g_Transform);                    # // NEW: Apply Dynamic Transform
  glColor3f(1.0,0.75,0.75);
  gluSphere(g_quadratic,1.3,20,20);
  glPopMatrix();                          # // NEW: Unapply Dynamic Transform

  glFlush ();                            # // Flush The GL Rendering Pipeline
  glutSwapBuffers()
  return

w___ww___.__j_ava___2_s_._c__o__m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.