1

I'm on Mac OSX, and am running on Mountain Lion. A year or so ago, I installed PyGame so I could program in Eclipse. I was pretty satified, although the lack of code completion threw me off a bit. I plowed through it, and have managed to put up with everything so far. Then, today, I decided to try some PyOpenGL. I installed it using pip, and I copied and pasted he following code into my windows (I'm going to continue here as the actual code isnt the problem). Almost everything gives a warning. Almost everything. The sidebar is a solid wall of yellow.

Lines of code like

glBegin(GL_TRIANGLES)

give me errors like

undefined variable: GL_TRIANGLES
undefined variable: glBegin

Even going into Eclipse>Preferences>Pydev>Editor>Code Analysis and telling Eclipse to ignore undefined variables doesnt help. Other modules like Pygame do this for me too.

This bothers me. A lot. And I really don't want to be forced back into LWJGL and java.

Is there any way I can fix this problem once and for all?

#!/usr/bin/env python

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

import pygame

def pyramid():

   glBegin(GL_TRIANGLES)

   glColor3f(1.0, 0.0, 0.0)      #   // Red
   glVertex3f(0.0, 1.0, 0.0)      #   // Top Of Triangle (Front)
   glColor3f(0.0, 1.0, 0.0)      #   // Green
   glVertex3f(-1.0, -1.0, 1.0)      #      // Left Of Triangle (Front)
   glColor3f(0.0, 0.0, 1.0)      #   // Blue
   glVertex3f(1.0, -1.0, 1.0)      #   // Right Of Triangle (Front)

   glColor3f(1.0, 0.0, 0.0)      #   // Red
   glVertex3f(0.0, 1.0, 0.0)      #   // Top Of Triangle (Right)
   glColor3f(0.0,0.0, 1.0)         #   // Blue
   glVertex3f(1.0, -1.0, 1.0)      #   // Left Of Triangle (Right)
   glColor3f(0.0, 1.0, 0.0)      #   // Green
   glVertex3f(1.0, -1.0, -1.0)      #      // Right Of Triangle (Right)

   glColor3f(1.0, 0.0, 0.0)      #   // Red
   glVertex3f(0.0, 1.0, 0.0)      #   // Top Of Triangle (Back)
   glColor3f(0.0,1.0, 0.0)         #   // Green
   glVertex3f(1.0, -1.0, -1.0)      #   // Left Of Triangle (Back)
   glColor3f(0.0, 0.0, 1.0)      #   // Blue
   glVertex3f(-1.0, -1.0, -1.0)   #      // Right Of Triangle (Back)

   glColor3f(1.0, 0.0, 0.0)      #   // Red
   glVertex3f(0.0, 1.0, 0.0)      #   // Top Of Triangle (Left)
   glColor3f(0.0, 0.0, 1.0)      #   // Blue
   glVertex3f(-1.0, -1.0, -1.0)   #      // Left Of Triangle (Left)
   glColor3f(0.0, 1.0, 0.0)      #   // Green
   glVertex3f(-1.0, -1.0, 1.0)      #      // Right Of Triangle (Left)

   glEnd()

   glBegin(GL_QUADS)

   glColor3f(0.0, 0.0, 1.0)
   glVertex3f(-1.0, -1.0, -1.0)
   glColor3f(0.0, 1.0, 0.0)
   glVertex3f(1.0, -1.0, -1.0)
   glColor3f(0.0, 0.0, 1.0)
   glVertex3f(1.0, -1.0, 1.0)
   glColor3f(0.0, 1.0, 0.0)
   glVertex3f(-1.0, -1.0, 1.0)

   glEnd()

def cube():
   glBegin(GL_QUADS)

   glColor3f(0.0, 1.0, 0.0)      #   // Set The Color To Green
   glVertex3f(1.0, 1.0, -1.0)      #   // Top Right Of The Quad (Top)
   glVertex3f(-1.0, 1.0, -1.0)      #   // Top Left Of The Quad (Top)
   glVertex3f(-1.0, 1.0, 1.0)      #   // Bottom Left Of The Quad (Top)
   glVertex3f(1.0, 1.0, 1.0)      #   // Bottom Right Of The Quad (Top)

   glColor3f(1.0, 0.5, 0.0)      #   // Set The Color To Orange
   glVertex3f(1.0, -1.0, 1.0)      #   // Top Right Of The Quad (Bottom)
   glVertex3f(-1.0, -1.0, 1.0)      #      // Top Left Of The Quad (Bottom)
   glVertex3f(-1.0, -1.0, -1.0)   #      // Bottom Left Of The Quad (Bottom)
   glVertex3f(1.0, -1.0, -1.0)      #      // Bottom Right Of The Quad (Bottom)

   glColor3f(1.0, 0.0, 0.0)      #   // Set The Color To Red
   glVertex3f(1.0, 1.0, 1.0)      #   // Top Right Of The Quad (Front)
   glVertex3f(-1.0, 1.0, 1.0)      #   // Top Left Of The Quad (Front)
   glVertex3f(-1.0, -1.0, 1.0)      #      // Bottom Left Of The Quad (Front)
   glVertex3f(1.0, -1.0, 1.0)      #   // Bottom Right Of The Quad (Front)

   glColor3f(1.0, 1.0, 0.0)      #   // Set The Color To Red
   glVertex3f(1.0, -1.0, -1.0)      #   // Top Right Of The Quad (Back)
   glVertex3f(-1.0, -1.0, -1.0)   #   // Top Left Of The Quad (Back)
   glVertex3f(-1.0, 1.0, -1.0)      #      // Bottom Left Of The Quad (Back)
   glVertex3f(1.0, 1.0, -1.0)      #   // Bottom Right Of The Quad (Back)

   glColor3f(0.0, 0.0, 1.0)      #   // Set The Color To Blue
   glVertex3f(-1.0, 1.0, 1.0)      #   // Top Right Of The Quad (Left)
   glVertex3f(-1.0, 1.0, -1.0)      #   // Top Left Of The Quad (Left)
   glVertex3f(-1.0, -1.0, -1.0)   #      // Bottom Left Of The Quad (Left)
   glVertex3f(-1.0, -1.0, 1.0)      #   // Bottom Right Of The Quad (Left)

   glColor3f(1.0, 0.0, 1.0)      #   // Set The Color To Violet
   glVertex3f( 1.0, 1.0, -1.0)      #   // Top Right Of The Quad (Right)
   glVertex3f( 1.0, 1.0, 1.0)      #   // Top Left Of The Quad (Right)
   glVertex3f( 1.0, -1.0, 1.0)      #   // Bottom Left Of The Quad (Right)
   glVertex3f( 1.0, -1.0, -1.0)   #      // Bottom Right Of The Quad (Right)


   glEnd()


if __name__ == "__main__":

   from sys import exit

   print ("Initalizing...")

   resolution = (400, 300)

   pygame.init()
   screen = pygame.display.set_mode(resolution, pygame.OPENGL|pygame.DOUBLEBUF)

   print ("Doing GL stuff...")
   glViewport(0, 0, resolution[0], resolution[1])

   glMatrixMode(GL_PROJECTION)
   glLoadIdentity()
   gluPerspective(45.0, float(resolution[0]) / resolution[1], 0.1, 1000.0)

   glMatrixMode(GL_MODELVIEW)
   glLoadIdentity()

   print ("More GL stuff...")
   glEnable(GL_DEPTH_TEST)

   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

   glShadeModel(GL_SMOOTH)
   glClearColor(0.0, 0.0, 0.0, 0.0)

   print ("Minor details...")
   clock = pygame.time.Clock()

   rot_tri = 0
   rot_quad = 0

   import random

   print ("Mainloop...")
   while True:

      for event in pygame.event.get():

         if event.type == pygame.QUIT:
            pygame.quit()
            exit()

      glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

      glLoadIdentity()
      glTranslatef(-1.5, 0.0, -6.0)
      glRotatef(rot_tri, 0.0, 1.0, 0.0)
      pyramid()

      glLoadIdentity()
      glTranslate(1.5, 0.0, -6.0)
      glRotatef(rot_quad, 1.0, 0.0, 0.0)
      cube()

      rot_tri += 2.0
      rot_quad -= 1.5

      pygame.display.set_caption("hello_opengl.py FPS: %i" % clock.get_fps())

      pygame.display.flip()
      clock.tick()
4

1 回答 1

0

我在 Eclipse 和 PyOpenGL 中使用 PyDev 没有任何问题。我安装了 OpenGL pip install pyopengl,然后进入 Window > Preferences > PyDev > Interpreter - Python。在不更改任何内容的情况下点击应用。出现提示时,选择适当的解释器进行刷新。完成后,关闭首选项,然后关闭文件 > 重新启动。这立即起作用:

from OpenGL.GL import GL_TRIANGLES
于 2013-02-20T23:11:35.620 回答