OpenGL Shapes没有出现在ViewPort中

时间:2015-12-09 20:43:22

标签: visual-studio python-3.x opengl rendering

我对openGL相当新,而且(来自研究)我认为我理解整个视口与视锥体的差异......但我不确定我是否正确配置/设置了一些东西 - 一个第2(或第10)组眼睛看我的代码会有所帮助。我在Visual Studio 2015(Community Edition)中使用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)

最后我启动了mainLoop:

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

我的drawSpheres方法:

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

感谢任何帮助。 :)

1 个答案:

答案 0 :(得分:0)

可能是因为您严重设置了透视矩阵。

请参阅以下链接:

https://unspecified.wordpress.com/2012/06/21/calculating-the-gluperspective-matrix-and-other-opengl-matrix-maths/

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