By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,002 Members | 2,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,002 IT Pros & Developers. It's quick & easy.

Want connect planes to eachother in OpenGl example, but can't pass a tuple as vertex.

P: 3
Basically, I want to connect the tip of each plane to the other. So I thought I might try creating a variable specifically for the points I want to connect: The tip of each plane.

Then, for each plane in the planes list, I would get the point of that specified tuple of each plane, add it to a list of points, and draw a line to each.

But it's not working. When I try to pass planes[n].tip: glVertex3f(planes[n].tip), I get the following error;

exceptions.EOFError: [Errno 10054] An existing connection was forcibly closed by the remote host

I don't NEED this...I'm just messing around, but I'd still like to know what I am doing wrong.
Also, if there is a more efficient way I could go about drawing the lines, please let me know about it. :P
Expand|Select|Wrap|Line Numbers
  1.  
  2. import sys
  3.  
  4. from OpenGL.GLUT import *
  5. from OpenGL.GLU import *
  6. from OpenGL.GL import *
  7. from math import *
  8. from random import random, choice, randint, getrandbits
  9.  
  10. """
  11.    Copyright (c) Mark J. Kilgard, 1994.
  12.  
  13.    This program is freely distributable without licensing fees
  14.    and is provided without guarantee or warrantee expressed or
  15.    implied. This program is -not- in the public domain.
  16. """
  17.  
  18. M_PI   = pi
  19. M_PI_2 = pi / 2.0
  20.  
  21. moving = False
  22. MAX_PLANES = 15
  23.  
  24. # define plane object#
  25.  
  26. class plane(object):
  27.     def __init__(self, speed, red, green, blue, theta, x, y, z, angle):
  28.         self.speed = speed
  29.         self.red   = red
  30.         self.green = green
  31.         self.blue  = blue
  32.         self.theta = theta
  33.         self.angle = angle
  34.         self.x = x
  35.         self.y = y
  36.         self.z = z
  37.         self.tip = (0.0, 8.0, 0.0)#I want to pass this  
  38.               # variable as a vertex3f tuple, but it  
  39.               # stops the program.
  40.  
  41. # create list of planes#
  42.  
  43. planes = []
  44. for n in range(MAX_PLANES) :
  45.     p = plane(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
  46.     planes.append(p)
  47.  
  48.  
  49. # define the GLUT display function#
  50.  
  51. def draw():
  52.     glClear(GL_DEPTH_BUFFER_BIT)
  53.     # paint black to blue smooth shaded polygon for background
  54.     glDisable(GL_DEPTH_TEST)
  55.     glShadeModel(GL_SMOOTH)
  56.     glBegin(GL_POLYGON);
  57.     glColor3f(0.0, 0.0, 0.0);
  58.     glVertex3f(-20.0, 20.0, -19.0)
  59.     glVertex3f( 20.0, 20.0, -19.0)
  60.     glColor3f(0.0, 0.0, 1.0)
  61.     glVertex3f( 20.0, -20.0, -19.0)
  62.     glVertex3f(-20.0, -20.0, -10.0)
  63.     glEnd()
  64.     # paint planes
  65.     glEnable(GL_DEPTH_TEST)
  66.     glShadeModel(GL_FLAT)
  67.     for n in range(MAX_PLANES) :
  68.         if (planes[n].speed != 0.0) :
  69.             glPushMatrix()
  70.             glTranslatef(planes[n].x, planes[n].y, planes[n].z)
  71.             glRotatef(290.0, 1.0, 0.0, 0.0)
  72.             glRotatef(planes[n].angle, 0.0, 0.0, 1.9)
  73.             glScalef(1.0 / 10.0, 1.0 / 4.0, 1.0 / 10.0)
  74.             glTranslatef(0.0, -4.0, -1.5)
  75.             glBegin(GL_TRIANGLE_STRIP)
  76.             # left wing
  77.             glVertex3f(-7.0, 0.0, 2.0)
  78.             glVertex3f(-1.0, 0.0, 3.0)
  79.             red   = planes[n].red
  80.             green = planes[n].green
  81.             blue  = planes[n].blue
  82.             glColor3f(red, green, blue)
  83.             glVertex3f(-1.0, 7.0, 3.0)
  84.             # left side
  85.             glColor3f(0.6 * red, 0.6 * green, 0.6 * blue)
  86.             glVertex3f(0.0, 0.0, 0.0)
  87.             glVertex3f(planes[n].tip)
  88.             # right side
  89.             glVertex3f(1.0, 0.0, 3.0)
  90.             glVertex3f(1.0, 7.0, 3.0)
  91.             # final tip of right wing */
  92.             glColor3f(red, green, blue)
  93.             glVertex3f(7.0, 0.0, 2.0)
  94.             glEnd()
  95.             glPopMatrix()
  96.             print planes[n].x
  97.  
  98.  
  99.     glutSwapBuffers()
  100.     return
  101.  
  102. # define the plane position and speed incrementor#
  103.  
  104. def tick_per_plane(i):
  105.     planes[i].theta += planes[i].speed
  106.     theta = planes[i].theta
  107.     planes[i].z = -10 + 4 * cos(theta)
  108.     planes[i].x = 5 * sin(2 * theta)
  109.     planes[i].y = sin(theta / 3.4) * 3
  110.     planes[i].angle = ((atan(2.0) + M_PI_2) * sin(theta) - M_PI_2) * 180 / M_PI
  111.     if (planes[i].speed < 0.0) :
  112.         planes[i].angle += 180.0
  113.     return
  114.  
  115. #define the list of rgb tuples for setting plane colours by random choice#
  116.  
  117. rgblist = [(1.0, 0.0, 0.0),  # red
  118.            (1.0, 1.0, 1.0),  # white
  119.            (0.0, 1.0, 0.0),  # green
  120.            (1.0, 0.0, 1.0),  # magenta
  121.            (1.0, 1.0, 0.0),  # yellow
  122.            (0.0, 1.0, 1.0)   # cyan
  123.           ]
  124.  
  125. # define add planes to display of planes#
  126.  
  127. def add_plane():
  128.     for i in range(MAX_PLANES) :
  129.         if (planes[i].speed == 0.0) :
  130.             planes[i].red, planes[i].green, planes[i].blue = choice(rgblist)
  131.             planes[i].speed = (float(randint(0, 19)) * 0.001) + 0.02
  132.             if (getrandbits(32) & 0x1) :
  133.                 planes[i].speed *= -1
  134.             planes[i].theta = float(randint(0, 256)) * 0.1111
  135.             tick_per_plane(i)
  136.             if (not moving) :
  137.                 glutPostRedisplay()
  138.             return
  139.     return
  140.  
  141.  
  142. # define remove a plane from display of planes#
  143.  
  144. def remove_plane():
  145.     for i in range(MAX_PLANES-1, -1, -1) :
  146.       if (planes[i].speed != 0) :
  147.           planes[i].speed = 0
  148.           if (not moving) :
  149.               glutPostRedisplay()
  150.           return
  151.     return
  152.  
  153. #define choice of planes to animate#
  154.  
  155. def tick():
  156.     for i in range(MAX_PLANES) :
  157.         if (planes[i].speed != 0.0) :
  158.             tick_per_plane(i)
  159.     return
  160.  
  161. # define animator so that motion can be started#
  162.  
  163. def animate():
  164.     tick()
  165.     glutPostRedisplay()
  166.     return
  167.  
  168.  
  169. def visible(state):
  170.     if (state == GLUT_VISIBLE) :
  171.         if (moving) :
  172.             glutIdleFunc(animate)
  173.     else :
  174.         if (moving) :
  175.             glutIdleFunc(None)
  176.     return
  177.  
  178.  
  179. # ARGSUSED1 #
  180.  
  181. def keyboard( ch,  x,  y):
  182.     if(ch == ' ') :
  183.         if (not moving) :
  184.             tick()
  185.             glutPostRedisplay()
  186.     elif (ch == chr(27)) :
  187.         sys.exit(0)
  188.     return 0
  189.  
  190. VOID, ADD_PLANE, REMOVE_PLANE, MOTION_ON, MOTION_OFF, QUIT = range(6)
  191.  
  192. def domotion_on():
  193.     moving = GL_TRUE
  194.     glutChangeToMenuEntry(3, "Motion off", MOTION_OFF)
  195.     glutIdleFunc(animate)
  196.     return
  197.  
  198. def domotion_off():
  199.     moving = GL_FALSE
  200.     glutChangeToMenuEntry(3, "Motion", MOTION_ON)
  201.     glutIdleFunc(None)
  202.     return
  203.  
  204. def doquit():
  205.     sys.exit(0)
  206.     return
  207.  
  208. menudict ={ADD_PLANE : add_plane,
  209.            REMOVE_PLANE : remove_plane,
  210.            MOTION_ON : domotion_on,
  211.            MOTION_OFF: domotion_off,
  212.            QUIT : doquit}
  213.  
  214. def dmenu(item):
  215.     menudict[item]()
  216.     return 0
  217.  
  218. if __name__ == "__main__":
  219.     glutInit(['glutplane'])
  220.     glutInitWindowPosition(112, 84)
  221.     glutInitWindowSize(800, 600)
  222.     # use multisampling if available
  223.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_MULTISAMPLE)
  224.     wintitle = "glutplane  Copyright (c) Mark J. Kilgard, 1994. RIGHT-CLICK for menu"
  225.     glutCreateWindow(wintitle)
  226.     glutDisplayFunc(draw)
  227.     glutKeyboardFunc(keyboard)
  228.     glutVisibilityFunc(visible)
  229.     #
  230.     # This program fails if PyOpenGL-3.0.0b1-py2.5.egg\OpenGL\GLUT\special.py
  231.     # is not corrected at line 158 to read :
  232.     # callbackType = ctypes.CFUNCTYPE( None, ctypes.c_int )
  233.     # instead of :
  234.     # callbackType = ctypes.CFUNCTYPE( ctypes.c_int, ctypes.c_int )
  235.     #
  236.     # RIGHT-CLICK to display the menu
  237.     #
  238.     glutCreateMenu(dmenu)
  239.     glutAddMenuEntry("Add plane", ADD_PLANE)
  240.     glutAddMenuEntry("Remove plane", REMOVE_PLANE)
  241.     glutAddMenuEntry("Motion", MOTION_ON)
  242.     glutAddMenuEntry("Quit", QUIT)
  243.     glutAttachMenu(GLUT_RIGHT_BUTTON)
  244.  
  245.     # setup OpenGL state
  246.     glClearDepth(1.0)
  247.     glClearColor(0.0, 0.0, 0.0, 0.0)
  248.     glMatrixMode(GL_PROJECTION)
  249.     glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 30)
  250.     glMatrixMode(GL_MODELVIEW)
  251.     # add three initial random planes
  252.     add_plane()
  253.     add_plane()
  254.     add_plane()
  255.     # start event processing */
  256.     print 'RIGHT-CLICK to display the menu.'
  257.     glutMainLoop()
  258.  
  259.  
  260.  
Apr 4 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.