0

我有一个问题,我在 PyOpenGL 的场景中有很多球体,但我无法区分一个球体和另一个球体。

如何在形状上创建轮廓?

4

1 回答 1

1

下面给出了使用固定管道 (< OpenGL-3.x) 在网格周围创建轮廓或轮廓的简单方法:

  1. 禁用照明:glDisable(GL_LIGHTING)
  2. 为轮廓选择一种颜色:glColor(R, G, B)
  3. 开启正面剔除:glCullFace(GL_FRONT)
  4. 将网格(球体)缩放一小部分:glScale(sx, sy, sz)
  5. 像往常一样渲染球体: glutSolidSphere(radius, slices, stacks)

使用核心配置文件 OpenGL 3.x 或更高版本,您将执行完全相同的操作,但改为使用顶点着色器。

使用固定管道实现这一点所需的代码很简单:

    # Render silhouette around object
    glPushMatrix()
    glCullFace(GL_FRONT) # Front face culling makes us render only the inside of the sphere, which gives the illusion of a silhouette
    glScale(1.04, 1.04, 1.04) # This makes the silhouette show up around the object
    glDisable(GL_LIGHTING) # We only want a plain single colour silhouette
    glColor(0., 1., 0.) # Silhouette colour
    glutSolidSphere(2,20,20) # This can be any object, not limited to spheres (even non-convex objects)
    glPopMatrix()

下面是一个简单的 PyOpenGL 程序示例,它基于http://code.activestate.com/recipes/325391-open-a-glut-window-and-draw-a-sphere中的 GLUT 示例渲染一个带有轮廓的球体-使用-pythono/

from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
import sys

name = 'ball_glut'

def main():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
    glutInitWindowSize(400,400)
    glutCreateWindow(name)

    glClearColor(0.,0.,1.,1.)
    glShadeModel(GL_SMOOTH)
    glEnable(GL_CULL_FACE)
    glEnable(GL_DEPTH_TEST)
    glEnable(GL_LIGHTING)
    lightZeroPosition = [10.,4.,10.,1.]
    lightZeroColor = [1.0,1.0,1.0,1.0]
    glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition)
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor)
    glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1)
    glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05)
    glEnable(GL_LIGHT0)
    glutDisplayFunc(display)
    glMatrixMode(GL_PROJECTION)
    gluPerspective(40.,1.,1.,40.)
    glMatrixMode(GL_MODELVIEW)
    gluLookAt(0,0,10,
              0,0,0,
              0,1,0)
    glPushMatrix()
    glutMainLoop()
    return

def display():
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glMatrixMode(GL_MODELVIEW)

    # Render sphere normally
    glPushMatrix()
    color = [1.0,0.,0.,1.]
    glMaterialfv(GL_FRONT,GL_DIFFUSE,color)
    glCullFace(GL_BACK)
    glEnable(GL_LIGHTING)
    glutSolidSphere(2,20,20)

    # Render silhouette around object
    glPushMatrix()
    glCullFace(GL_FRONT) # Front face culling makes us render only the inside of the sphere, which gives the illusion of a silhouette
    glScale(1.04, 1.04, 1.04) # This makes the silhouette show up around the object
    glDisable(GL_LIGHTING) # We only want a plain fixed colour silhouette
    glColor(0., 1., 0.) # Silhouette colour
    glutSolidSphere(2,20,20) # This can be any object, not limited to spheres (even non-convex objects)
    glPopMatrix()

    glPopMatrix()
    glutSwapBuffers() # Display results.
    return

if __name__ == '__main__': main()
于 2012-11-07T13:39:04.167 回答