运行此代码时,我遇到了一个非常奇怪的问题。我也不知道它应该如何与OpenGL Api发生冲突。下面是代码:
import sys
sys.path.append("..\Blocks")
print sys.path
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import random
try:
import BlockModel
except:
print "Cant Find Block Model"
def createBlock():
block = BlockModel.Block()
blockVertices = block.returnVertices()
blockEdges = block.returnEdges()
blockSurface = block.returnSurface()
glBegin(GL_QUADS)
for surface in blockSurface:
for faceVertex in surface:
glVertex3fv(blockVertices[faceVertex])
glEnd
glBegin(GL_LINES)
for edge in blockEdges:
for vertex in edge:
glVertex3fv(blockVertices[vertex])
glEnd()
def main():
pygame.init()
display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
gluPerspective(15, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(random.randrange(-5,5),random.randrange(-5,5), -40)
exit = False
while not exit:
pygame.time.wait(10)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
createBlock()
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
main()
尝试运行程序时出现此错误:
C:\Users\Haavard\Desktop\MinecraftPythonProject\framework>python main.py
['C:\\Users\\Haavard\\Desktop\\MinecraftPythonProject\\framework', 'C:\\Windows\
\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python
27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\
lib\\site-packages', 'C:\\Python27\\lib\\site-packages\\PIL', '..\\Blocks']
Traceback (most recent call last):
File "main.py", line 60, in <module>
main()
File "main.py", line 52, in main
createBlock()
File "main.py", line 37, in createBlock
glEnd()
File "latebind.pyx", line 44, in OpenGL_accelerate.latebind.Curry.__call__ (c:
\Users\mcfletch\OpenGL-dev\OpenGL-ctypes\OpenGL_accelerate\src\latebind.c:1201)
File "C:\Python27\lib\site-packages\OpenGL\GL\exceptional.py", line 46, in glE
nd
return baseFunction( )
File "C:\Python27\lib\site-packages\OpenGL\platform\baseplatform.py", line 402
, in __call__
return self( *args, **named )
File "errorchecker.pyx", line 53, in OpenGL_accelerate.errorchecker._ErrorChec
ker.glCheckError (c:\Users\mcfletch\OpenGL-dev\OpenGL-ctypes\OpenGL_accelerate\s
rc\errorchecker.c:1218)
OpenGL.error.GLError: GLError(
err = 1282,
description = 'invalid operation',
baseOperation = glEnd,
cArguments = ()
)
我在惠普机器上的Windows 7上运行。
块模型模块如下所示:
class Block:
# initializing the basic functions of a block
def __init__(self, blockID = "0", blockType = "stone", verticesCords = ((1,-1,-1),(1,1,-1),(-1,1,-1),(-1,-1,-1),(1,-1,1),(1,1,1),(-1,-1,1),(-1,1,1)), edges = ((0,1),(0,3),(0,4),(2,1),(2,3),(2,7),(6,3),(6,4),(6,7),(5,1),(5,4),(5,7)), surfaces = (((0,1,2,3),(3,2,7,6),(6,7,5,4),(4,5,1,0),(1,5,7,2),(4,0,3,6)))):
# Block Placement
self.PLACEMENT = verticesCords
# Block identity in the world
self.EDGES = edges
self.SURFACE = surfaces
self.BLOCKID = blockID
# The block type
self.BLOCKTYPE = blockType
# A function letting the framework know its placement.
def returnVertices(self):
return self.PLACEMENT
def returnEdges(self):
return self.EDGES
def returnSurface(self):
return self.SURFACE
# A function to make the block fetch its own texture.
def defineTexture():
pass
感谢您的回复! :)
答案 0 :(得分:0)
您可能已经解决了这个问题,但我的猜测是您的边缘可能有奇数个顶点。 glEnd()上的1282错误只是意味着整个操作出现了问题。 GL_LINES期望给出偶数个顶点,因为GL_LINES在成对的点中工作以定义每个线段,而不是用于形成大折线的连续点串。仔细检查每条边有两个点。
答案 1 :(得分:0)
您似乎没有正确结束GL_QUADS进程。您正在调用glEnd,而不是glEnd()。我不知道这是不是问题,但这绝对是错误的。如果您有多次尝试,可能需要指定要结束的过程,例如如果当前有多个准备,则使用glEnd(GL_LINES)或glEnd(GL_QUADS),这就是为什么成功调用glEnd()时会发生错误的原因;您并没有告诉它应该结束。
希望这会有所帮助
答案 2 :(得分:0)
您应该从()
行第37行中删除glEnd()
,代码应该可以正常工作。