def main(): glutInit(sys.argv) glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH) glutInitWindowSize(640, 480) glutCreateWindow('GLSrf - Press MB and drag to rotate') glutMotionFunc(on_motion) glutDisplayFunc(on_display) glutReshapeFunc(on_reshape) nurb = gluNewNurbsRenderer() gluNurbsProperty(nurb, GLU_SAMPLING_TOLERANCE, 50.) gluNurbsProperty(nurb, GLU_DISPLAY_MODE, GLU_FILL) glMatrixMode(GL_MODELVIEW) glNewList(1, GL_COMPILE) glMaterialfv(GL_FRONT, GL_SPECULAR, (1.0, 1.0, 1.0, 1.0)) glMaterialfv(GL_FRONT, GL_SHININESS, 100.0) glMaterialfv(GL_FRONT, GL_DIFFUSE, (0.7, 0.0, 0.1, 1.0)) glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glEnable(GL_DEPTH_TEST) glEnable(GL_AUTO_NORMAL) glEnable(GL_NORMALIZE) pnts = [[0., 3., 4.5, 6.5, 8., 10.], [0., 0., 0., 0., 0., 0.], [2., 2., 7., 4., 6., 4.]] crv1 = Crv.Crv(pnts, [0., 0., 0., 1. / 3., 0.5, 2. / 3., 1., 1., 1.]) pnts = [[0., 3., 5., 8., 10.], [10., 10., 10., 10., 10.], [3., 6., 3., 6., 10.]] crv2 = Crv.Crv(pnts, [0., 0., 0., 1. / 3., 2. / 3., 1., 1., 1.]) srf = Srf.Ruled(crv1, crv2) gluBeginSurface(nurb) gluNurbsSurface(nurb, srf.uknots, srf.vknots, Numeric.transpose(srf.cntrl, (1, 2, 0)), GL_MAP2_VERTEX_4) gluEndSurface(nurb) glEndList()
def on_display(): import time global lasttime thistime = time.time() print (1.0/(thistime-lasttime)) lasttime = thistime glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glCallList(1) global lastx, lasty, width, height, lastkey cam_range = (-15,15) camx = maprange(lastx, (0,width), cam_range) camy = maprange(lasty, (0,height), cam_range) glTranslate(5, 5, 0.0) gluLookAt(0.0, 5.0, -2.5, 0, 0, 0, 0, 1, 0) glRotatef(-60, 1.0, 0.0, 0.0) glScale(3.0, 3.0, 3.0) scale_a = maprange(lastx, (0, width), (1, 10), clip=False) cntrl = np.zeros((4,4,4), np.float) for u in range(4): for v in range(4): cntrl[0][u][v] = 2.*(u - 1.5) cntrl[1][u][v] = 2.*(v - 1.5) if (u == 1 or u == 2) and (v == 1 or v == 2): cntrl[2][u][v] = scale_a # used to be 2.0 else: cntrl[2][u][v] = -2.0 # used to be -2.0 cntrl[3][u][v] = 1. cntrl_color = np.zeros_like(cntrl) cntrl_color[0:3,:,:] = cntrl[2,:,:] cntrl_color[3,:,:] = 1.0 knots = [0.,0.,0.,0.,1.,1.,1.,1.] srf = Srf.Srf(cntrl, knots, knots) nurb2 = gluNewNurbsRenderer() gluNurbsProperty(nurb2, GLU_SAMPLING_TOLERANCE, 50.) #gluNurbsProperty(nurb2, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON) gluNurbsProperty(nurb2, GLU_DISPLAY_MODE, GLU_FILL) gluBeginSurface(nurb2) # gluNurbsSurface(nurb2, srf.uknots, srf.vknots, np.transpose(cntrl_color, (1,2,0)), type=GL_MAP2_COLOR_4) gluNurbsSurface(nurb2, srf.uknots, srf.vknots, np.transpose(srf.cntrl, (1,2,0)), type=GL_MAP2_VERTEX_4) gluEndSurface(nurb2) # nurb1 = nurb2 # # nurb1 = gluNewNurbsRenderer() # gluBeginSurface(nurb1) # gluEndSurface(nurb1) glutSwapBuffers() data = glReadPixels(0,0,width,height, GL_RGB, GL_FLOAT) print "Maxval: %f" % data.max() print data.shape
# Demonstration of a surface import numpy as np from Nurbs import Srf, Crv cntrl = np.zeros((4, 4, 4), np.float) for u in range(4): for v in range(4): cntrl[0][u][v] = 2. * (u - 1.5) cntrl[1][u][v] = 2. * (v - 1.5) if (u == 1 or u == 2) and (v == 1 or v == 2): cntrl[2][u][v] = 2. else: cntrl[2][u][v] = -2. cntrl[3][u][v] = 1. knots = [0., 0., 0., 0., 1., 1., 1., 1.] srf = Srf.Srf(cntrl, knots, knots) srf.plot()
# Demonstration of a Revolve surface from Nurbs import Srf, Crv crv = Crv.Crv([[0, 30, 6, 90], [0, 0, 50, 50]], [0, 0, 0, 0, 1, 1, 1, 1]) srf = Srf.Revolve(crv) srf.plot()
# Demonstration of a Coons surface from Nurbs import Srf, Crv pnts = [[0., 3., 4.5, 6.5, 8., 10.], [0., 0., 0., 0., 0., 0.], [2., 2., 7., 4., 7., 9.]] crv1 = Crv.Crv(pnts, [0., 0., 0., 1./3., 0.5, 2./3., 1., 1., 1.]) pnts= [[0., 3., 5., 8., 10.], [10., 10., 10., 10., 10.], [3., 5., 8., 6., 10.]] crv2 = Crv.Crv(pnts, [0., 0., 0., 1./3., 2./3., 1., 1., 1.]) pnts= [[0.,0., 0., 0.], [0., 3., 8., 10.], [2., 0., 5., 3.]] crv3 = Crv.Crv(pnts, [0., 0., 0., 0.5, 1., 1., 1.]) pnts= [[10., 10., 10., 10., 10.], [0., 3., 5., 8., 10.], [9., 7., 7., 10., 10.]] crv4 = Crv.Crv(pnts, [0., 0., 0., 0.25, 0.75, 1., 1., 1.]) srf = Srf.Coons(crv1, crv2, crv3, crv4) srf.plot()
# Demonstration of a bilinear surface from Nurbs import Srf srf = Srf.Bilinear([0, 0, .25], [2, 0, 0], [0, 1, 0], [2, 1, 1.8]) srf.plot()
# Demonstration of a Ruled surface from Nurbs import Srf, Crv pnts = [[0., 3., 4.5, 6.5, 8., 10.], [0., 0., 0., 0., 0., 0.], [2., 2., 7., 4., 6., 4.]] crv1 = Crv.Crv(pnts, [0., 0., 0., 1. / 3., 0.5, 2. / 3., 1., 1., 1.]) pnts = [[0., 3., 5., 8., 10.], [10., 10., 10., 10., 10.], [3., 6., 3., 6., 10.]] crv2 = Crv.Crv(pnts, [0., 0., 0., 1. / 3., 2. / 3., 1., 1., 1.]) srf = Srf.Ruled(crv1, crv2) srf.plot()
# Demonstration of a Extrude surface from Nurbs import Srf, Crv cntrl = [[-50., -75., 25., 0., -25., 75., 50.], [25., 50., 50., 0., -50., -50., 25.]] knots = [0., 0., 0., .2, .4, .6, .8, 1., 1., 1.] crv = Crv.Crv(cntrl, knots) srf = Srf.Extrude(crv, [0, 0, 5]) srf.plot()