OpenGL 形状未出现在 ViewPort 中
OpenGL Shapes not appearing in ViewPort
我是 openGL 的新手,并且(通过研究)我认为我了解整个视口与截锥体的区别...但我不确定我是否 configured/set 设置正确 - 一个第二(或第十)双眼睛看我的代码会有所帮助。我在 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()
window 显示,但视口中未绘制任何内容。 :/
这是我的绘画方法:
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
如有任何帮助,我们将不胜感激。 :)
可能是你的透视矩阵设置不好。
参见下面的link:
您可以看到 gluPerspective 实现是基于 FoV 角度的。研究那个解释。使用 glFrustum 允许您设置不对称透视投影,在这种情况下不经常使用。
我是 openGL 的新手,并且(通过研究)我认为我了解整个视口与截锥体的区别...但我不确定我是否 configured/set 设置正确 - 一个第二(或第十)双眼睛看我的代码会有所帮助。我在 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()
window 显示,但视口中未绘制任何内容。 :/
这是我的绘画方法:
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
如有任何帮助,我们将不胜感激。 :)
可能是你的透视矩阵设置不好。
参见下面的link:
您可以看到 gluPerspective 实现是基于 FoV 角度的。研究那个解释。使用 glFrustum 允许您设置不对称透视投影,在这种情况下不经常使用。