2012-11-07 92 views

回答

1

使用固定管道(< OpenGL的3.x的)周圍的網狀創建輪廓或剪影的一個直接的方法在下面給出:

  1. 禁用照明:glDisable(GL_LIGHTING)
  2. 選擇顏色爲輪廓:glColor(R,G,B)
  3. 打開正面剔除:glCullFace(GL_FRONT)
  4. 量表的目(球體)由一些小百分比:glScale(SX,SY,SZ)
  5. 像以前一樣渲染球體:glutSolidSphere

使用核心配置文件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程序渲染球體的輪廓,在GLUT例如鹼發現在http://code.activestate.com/recipes/325391-open-a-glut-window-and-draw-a-sphere-using-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()