Frage

I'm pretty sure that this is specifically my error in Python's looping syntax, but I can't for the life of me figure out what's wrong.

I'm using Python on OSX with PyOpenGL and GLUT for a very basic program: it's intended to draw a 3D grid around (0,0,0). I've managed to produce the 2D grid at a given elevation (so there's a grid at y=10 from x=-10 to x=10 and from z=-10 to z=10), but I can't make it iterate through the y-coordinate.

This screenshot should help to show what I mean. My intended scenario is that there would be ten of these flat grids above y=0, and ten below, forming a cube. Later on, too, I'll extend it to lines in the y-axis, but I've semi-achieved that before (same problem as I'm having now) so no big deal.

Would someone please let me know what the problem is with my code? It seems like a fairly simple issue, but I've wasted a few hours on this and would really appreciate some help.

A couple of disclaimers:

I'm aware that GLUT is old tech and that most of this code is deprecated, but this project isn't about that.

I'll need to go through and change the "-10 to 10" constants in the z- and x-coords at some point, but that method should be straightforward once I've got this y thing working.

from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *

grid_range = 10

def DrawGLScene( ):

    i = 0
    j = 0
    fl_range = float(grid_range)

    while (j <= (grid_range*2) ): 
        while (i <= (grid_range*2) ):
            # x grid
            glColor3f( 0.0, 1.0, 0.0 )
            glBegin( GL_LINES )
            glVertex3f( grid_range - i, grid_range - j, -10 )
            glVertex3f( grid_range - i, grid_range - j, 10 )
            glEnd( )

            # z grid
            glColor3f( 0.0, 0.0, 1.0 )
            glBegin( GL_LINES )
            glVertex3f( -10, grid_range - j, grid_range - i )
            glVertex3f( 10, grid_range - j, grid_range - i )
            glEnd( )

        i += 1
    j += 1
War es hilfreich?

Lösung

It looks like you just need to reset your 'i' variable to zero within your loop. It's also possible your i+=1 and j+=1 are not indented enough, but that might just be a copy/paste issue. To debug problems like this, I often add prints and simplify the code as much as possible.

Here's a minimal version of your code that prints i and j instead of drawing things.
(The 'print' below works with python 2.x. Add parentheses on the print line if you're using Python 3)

grid_range = 10

i = 0 
j = 0 

while (j <= (grid_range * 2)):

    # Reset i to 0 after completing each loop below.
    i = 0 

    while (i <= (grid_range * 2)):

        # OpenGL calls go here.

        print j, i 

        i += 1
    j += 1


Also, for the looping in your example, it's simpler to use a 'for' loop. Something like this should work:

grid_range = 10

for j in range(grid_range * 2 + 1):

    for i in range(grid_range * 2 + 1):

        # OpenGL calls go here.

        print j, i


Here is some extra background on python looping in case it helps:

http://docs.python.org/2/tutorial/controlflow.html
http://wiki.python.org/moin/ForLoop

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top