def OnInit(self): """Load the image on initial load of the application""" self.imageIDs = self.loadImages() self.currentFilter = 0 # index into imageIDs self.lightsOn = 0 # boolean self.blendOn = 1 self.currentZOffset = -6 self.rotationCycle = 8.0 # note that these are different bindings from the tutorial, # as you can wander around with the arrow keys already... self.addEventHandler('keypress', name='f', function=self.OnFilter) self.addEventHandler('keypress', name='l', function=self.OnLightToggle) self.addEventHandler( 'keyboard', name='<pageup>', function=self.OnSpeedUp, state=0, ) self.addEventHandler( 'keyboard', name='<pagedown>', function=self.OnSlowDown, state=0, ) '''Our first change from the NeHe7 tutorial code, and the only one in OnInit is to bind a handler for changing the blending functions.''' self.addEventHandler('keypress', name='b', function=self.OnBlendToggle) print self.usage '''The Lights setup and the Lights method are identical to the code from the NeHe7 translation''' glLightfv(GL_LIGHT1, GL_AMBIENT, GLfloat_4(0.2, .2, .2, 1.0)) glLightfv(GL_LIGHT1, GL_DIFFUSE, GLfloat_3(.8, .8, .8)) glLightfv(GL_LIGHT1, GL_POSITION, GLfloat_4(-2, 0, 3, 1))
def init_opengl(self): #model view matrix self.inverseModelView = numpy.identity(4) #its anti-matrix self.modelView = numpy.identity(4) #open tichu effect glEnable(GL_CULL_FACE) #to not rend invisible part glCullFace(GL_BACK) #open depth test glEnable(GL_DEPTH_TEST) #objects being covered ot to rend glDepthFunc(GL_LESS) #open light 0 glEnable(GL_LIGHT0) #to set the position of light glLightfv(GL_LIGHT0, GL_POSITION, GLfloat_4(0, 0, 1, 0)) #to set the direction that light sheds at glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, GLfloat_3(0, 0, -1)) #to set material color glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE) glEnable(GL_COLOR_MATERIAL) #set the color of a clear-screen glClearColor(0.4, 0.4, 0.4, 0.0)
def init_opengl(self): """ 初始化opengl的配置 """ #模型视图矩阵 self.inverseModelView = numpy.identity(4) #模型视图矩阵的逆矩阵 self.modelView = numpy.identity(4) #开启剔除操作效果 glEnable(GL_CULL_FACE) #取消对多边形背面进行渲染的计算(看不到的部分不渲染) glCullFace(GL_BACK) #开启深度测试 glEnable(GL_DEPTH_TEST) #测试是否被遮挡,被遮挡的物体不予渲染 glDepthFunc(GL_LESS) #启用0号光源 glEnable(GL_LIGHT0) #设置光源的位置 glLightfv(GL_LIGHT0, GL_POSITION, GLfloat_4(0, 0, 1, 0)) #设置光源的照射方向 glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, GLfloat_3(0, 0, -1)) #设置材质颜色 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE) glEnable(GL_COLOR_MATERIAL) #设置清屏的颜色 glClearColor(0.4, 0.4, 0.4, 0.0)
def init_opengl(self): """ initialize the opengl settings to render the scene """ self.inverseModelView = numpy.identity(4) self.modelView = numpy.identity(4) glEnable(GL_CULL_FACE) glCullFace(GL_BACK) glEnable(GL_DEPTH_TEST) glDepthFunc(GL_LESS) glEnable(GL_LIGHT0) glLightfv(GL_LIGHT0, GL_POSITION, GLfloat_4(0, 0, 1, 0)) glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, GLfloat_3(0, 0, -1)) glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE) glEnable(GL_COLOR_MATERIAL) glClearColor(0.4, 0.4, 0.4, 0.0)
def OnInit(self): """Load the image on initial load of the application""" '''As required by the tutorial, we load a single image as 3 different textures, see the loadImages method below for the new code. The OnInit handler loads the textures and stores their textureIDs as a list of integers. It then does some basic instance setup bookkeeping.''' self.imageIDs = self.loadImages() self.currentFilter = 0 # index into imageIDs self.lightsOn = 1 # boolean self.currentZOffset = -6 self.rotationCycle = 8.0 '''Adds event handlers for the given keys Note that these are different bindings from the tutorial, as you can wander around with the arrow keys already... ''' self.addEventHandler('keypress', name='f', function=self.OnFilter) self.addEventHandler('keypress', name='l', function=self.OnLightToggle) self.addEventHandler( 'keyboard', name='<pageup>', function=self.OnSpeedUp, state=0, ) self.addEventHandler( 'keyboard', name='<pagedown>', function=self.OnSlowDown, state=0, ) print(self.usage) '''Here we are setting the lighting parameters during init, as they do not change for the entire run of the application, normally code would set these values every time they change, often once per render-pass.''' glLightfv(GL_LIGHT1, GL_AMBIENT, GLfloat_4(0.2, .2, .2, 1.0)) glLightfv(GL_LIGHT1, GL_DIFFUSE, GLfloat_3(.8, .8, .8)) glLightfv(GL_LIGHT1, GL_POSITION, GLfloat_4(-2, 0, 3, 1))
def set_lighting(self): v1 = Vector3((0.3, -0.7, -0.6)).normalized light1_position = GLfloat_4(v1.x, v1.y, v1.z, 0.0) diffuse = 0.6 light_diffuse = GLfloat_4(diffuse, diffuse, diffuse, 1.0) light_ambient = GLfloat_4(0.0, 0.0, 0.0, 1.0) specular = 0.0 light_specular = GLfloat_4(specular, specular, specular, 1.0) glLightfv(GL_LIGHT0, GL_POSITION, light1_position) glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse) glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient) glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular) v2 = Vector3((-0.3, 0.7, -0.6)).normalized light2_position = GLfloat_4(v2.x, v2.y, v2.z, 0.0) glLightfv(GL_LIGHT1, GL_POSITION, light2_position) glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse) glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient) glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular) f = 0.6 light_model = GLfloat_4(f, f, f, 1.0) glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_model)