Visual studio 未在视口中显示的OpenGL图形

Visual studio 未在视口中显示的OpenGL图形,visual-studio,python-3.x,opengl,rendering,Visual Studio,Python 3.x,Opengl,Rendering,我对openGL相当陌生,(通过研究)我认为我理解了整个视口与平截头体的区别。。。但我不确定我是否正确地配置/设置了东西——第二(或第十)组眼睛来查看我的代码会有所帮助。我正在Visual Studio 2015(社区版)中使用Python 3.5和PyOpenGL(3.1.0) 我试图只渲染一个简单的球体: 使用值: self._left = 0. self._right = 400. self._bottom = 0. self._top = 400. self._width = self.

我对openGL相当陌生,(通过研究)我认为我理解了整个视口与平截头体的区别。。。但我不确定我是否正确地配置/设置了东西——第二(或第十)组眼睛来查看我的代码会有所帮助。我正在Visual Studio 2015(社区版)中使用Python 3.5和PyOpenGL(3.1.0)

我试图只渲染一个简单的球体:

使用值:

self._left = 0.
self._right = 400.
self._bottom = 0.
self._top = 400.
self._width = self._right - self._left (400.)
self._height = self._top - self._bottom (400.)
self._ratio = self._width / self._height (1.)
self._spheres = []
将单个球体对象附加到_spheres列表中,其中心位于(200,200,0),半径为20

我配置openGL:

glut.glutInit(sys.argv)
glut.glutInitDisplayMode(glut.GLUT_RGBA | glut.GLUT_DOUBLE | glut.GLUT_DEPTH)
glut.glutInitWindowSize(int(self._width), int(self._height))
glut.glutInitWindowPosition(int((1920 - self._width) * .05), int((1080 - self._height) * .80))
glut.glutCreateWindow(b'WindowName')
gl.glClearColor(0., 0., 0., 1.) # Use solid black when clearing the buffer (results in black background)
gl.glShadeModel(gl.GL_SMOOTH) # Have the coloring of faces be smooth (gradient between lighting values at the verticies) vs a solid color
gl.glEnable(gl.GL_CULL_FACE) # Don't render triangles and other shapes (?) which are not visible to the camera
gl.glEnable(gl.GL_DEPTH_TEST) # Enable depth testing for objects .. enables 3-dimensional drawing?..
gl.glEnable(gl.GL_LIGHTING) # Use lights to determine what color objects are (if no vertex shader is active)
# Configure Lighting
gl.lightZeroPosition = [self._width / 2, self._height / 2, -500., 1.] # create a variable ... (python specific) associate it with gl dictionary in case you want to access it somewhere else later..
gl.lightZeroColor = [0.8, 1.0, 0.8, 1.0] # create a variable ... (python specific) associate it with gl dictionary in case you want to access it somewhere else later.. green tinged light
gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, gl.lightZeroPosition) # set configuration for light0 (position)
gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, gl.lightZeroColor) # set configuration for light0 (color of light)
gl.glLightf(gl.GL_LIGHT0, gl.GL_CONSTANT_ATTENUATION, 0.01) # set configuration for light0 (light gets less bright farther away)
gl.glLightf(gl.GL_LIGHT0, gl.GL_LINEAR_ATTENUATION, 0.005) # set configuration for light0 (light gets less bright farther away)
gl.glEnable(gl.GL_LIGHT0) # Include light0 in the GL_LIGHTING calculation
# Configure visual area values
# Make viewport dimensions the same as the window
gl.glViewport(0, 0, int(self._width), int(self._height))
gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()
# frustum has near points same as window (so shapes don't get warped), but far points to go well beyond sphere location (which sphere should exist in)
gl.glFrustum(self._left, self._right, self._bottom, self._top, 100, 700)
# Configure rendering values
gl.glMatrixMode(gl.GL_MODELVIEW)
self._eyePosition = Point3D("Camera Eye Location").translate(int(self._width / 2), int(self._height / 2), -400)
self._watchingPosition = Point3D("Camera Watching Position").translate(int(self._width / 2), int(self._height / 2), 0)
self._upVector = Point3D("Camera Up Vector").translate(0, 1, 0)
glu.gluLookAt(self._eyePosition.x, self._eyePosition.y, self._eyePosition.z,
              self._watchingPosition.x, self._watchingPosition.y, self._watchingPosition.z,
              self._upVector.x, self._upVector.y, self._upVector.z)
gl.glPushMatrix()
我注册我的回拨:

glut.glutDisplayFunc(self.paint)
glut.glutIdleFunc(self.repaint)
最后,我启动主循环:

glut.glutMainLoop()
窗口显示,但视口中未绘制任何内容:/

以下是我的绘画方法:

def paint(self):
    elapsedTime = 0
    currentTime = time.clock()
    if self._lastTime is not None:
        elapsedTime = currentTime - self._lastTime
    self._lastTime = currentTime
    gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
    self._drawSpheres(elapsedTime)
    glut.glutSwapBuffers()
    return
def repaint(self):
    glut.glutPostRedisplay()
    return
我的重新绘制方法:

def paint(self):
    elapsedTime = 0
    currentTime = time.clock()
    if self._lastTime is not None:
        elapsedTime = currentTime - self._lastTime
    self._lastTime = currentTime
    gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
    self._drawSpheres(elapsedTime)
    glut.glutSwapBuffers()
    return
def repaint(self):
    glut.glutPostRedisplay()
    return
还有我的画圈法:

def _drawSpheres(self, elapsedTime):
    if self._spheres is not None:
        for sphere in self._spheres:
            # Remove any changes to matrix from previous drawing by pop.. preserve stack, so push immediately
            # This should be fine, since I pushed to stack in my initialization to save my visual and rendering area values.
            gl.glPopMatrix()
            gl.glPushMatrix()
            sphereCenter = sphere.currentLocation # As stated above - this is (200, 200, 0)
            sphereRadius = sphere.radius # As stated above - this is 20
            sphereColor = sphere.color # Not stated above, but this is (1., 0., 0., 1.)
            gl.glTranslate(sphereCenter.x, sphereCenter.y, sphereCenter.z)
            gl.glMaterialfv(gl.GL_FRONT, gl.GL_DIFFUSE, [sphereColor.getRedAsPercent(), sphereColor.getGreenAsPercent(), sphereColor.getBlueAsPercent(), sphereColor.getAlphaAsPercent()])
            glut.glutSolidSphere(sphereRadius, 15, 15)
            sphere.move(elapsedTime)
    return

感谢您的帮助。:)

可能是因为透视矩阵设置不当

请参阅下面的链接:


您可以看到透视图的实现是基于FoV角度的。研究这一解释。使用glFrustum可以设置不对称透视投影,这种情况下不经常使用

建议:缺少球坐标;如果堆栈为空,则不要弹出矩阵;禁用照明和其他功能以简化调试。嗯。。。我将glFrustum()更改为GLUPPERCEPTION(),现在一切都神奇地工作了。>><代码>透视图(30,自宽/自高,100700)。。为什么这和我直接声明截锥体有这么大的不同?(我的印象是,gluPerspective只是自己制作平截体的捷径,FoV/aspect用于计算平截体的左/右/上/下值…)研究我提供的链接。你的印象是正确的,但你必须做一些数学来理解它背后的魔力。您可以从研究透视图实现开始。