Я довольно новичок в openGL, и (от исследования) Я ДУМАЮ, Я понимаю всю видимость vs frustum ... но я не уверен, что у меня есть настроенные/настроенные настройки - поможет второй (или 10-й) набор глаз для моего кода. Я использую Python 3.5 в Visual Studio 2015 (Community Edition) с PyOpenGL (3.1.0).OpenGL Формы, отсутствующие в ViewPort
Я пытаюсь оказать только простую сферу:
Использования значений:
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
Любая помощь приветствуется. :)
Некоторые советы: недостающие сферы координат; не поп-матрица, если стек пуст; отключить освещение и другие вещи, чтобы упростить отладку. – Luca