Exemple #1
0
    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)
Exemple #2
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 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)
Exemple #4
0
    def apply(self, face=GL_FRONT_AND_BACK):
        '''Apply the material on current context'''
        if self.texture:
            self.texture.enable()
            self.texture.bind()
            glEnable(GL_COLOR_MATERIAL)

        glMaterialfv(face, GL_DIFFUSE, self.diffuse + [self.opacity])
        glMaterialfv(face, GL_AMBIENT, self.ambient + [self.opacity])
        glMaterialfv(face, GL_SPECULAR, self.specular + [self.opacity])
        glMaterialfv(face, GL_EMISSION, self.emission + [self.opacity])
        glMaterialf(face, GL_SHININESS, self.shininess)
        glColorMaterial(face, GL_AMBIENT_AND_DIFFUSE)
Exemple #5
0
    def apply(self, face=GL_FRONT_AND_BACK):
        '''Apply the material on current context'''
        if self.texture:
            self.texture.enable()
            self.texture.bind()
            glEnable(GL_COLOR_MATERIAL)

        glMaterialfv(face, GL_DIFFUSE, self.diffuse + [self.opacity])
        glMaterialfv(face, GL_AMBIENT, self.ambient + [self.opacity])
        glMaterialfv(face, GL_SPECULAR, self.specular + [self.opacity])
        glMaterialfv(face, GL_EMISSION, self.emission + [self.opacity])
        glMaterialf(face, GL_SHININESS, self.shininess)
        glColorMaterial(face, GL_AMBIENT_AND_DIFFUSE)
Exemple #6
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(1.0, 1.0, 1.0, 0.0)
    def init_opengl(self) -> bool:
        """Initialize and set OpenGL capabilities.

        Returns:
            True if initialized without error, False otherwise.
        """
        if self._gl_initialized:
            return True

        if self._context is None:
            return False

        glClearColor(*self.background_color)
        glClearDepth(1.0)

        glDepthFunc(GL_LESS)

        glEnable(GL_DEPTH_TEST)
        glEnable(GL_CULL_FACE)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

        # set antialiasing
        glEnable(GL_LINE_SMOOTH)

        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
        glEnable(GL_COLOR_MATERIAL)
        glEnable(GL_MULTISAMPLE)
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)

        self.create_vaos()

        # compile shader programs
        self._shaders['default'] = shaderlib.compile_shader(*shaderlib.default)
        self._shaders['single_color'] = shaderlib.compile_shader(
            *shaderlib.single_color)
        self._shaders['instanced_model_color'] = shaderlib.compile_shader(
            *shaderlib.instanced_model_color)
        self._shaders['instanced_picking'] = shaderlib.compile_shader(
            *shaderlib.instanced_picking)
        self._shaders['diffuse'] = shaderlib.compile_shader(*shaderlib.diffuse)
        self._shaders['solid'] = shaderlib.compile_shader(*shaderlib.solid)

        self._gl_initialized = True
        return True
Exemple #8
0
    def draw(self):
        glPushMatrix()
        glTranslated(self.pos.x, self.pos.y, self.pos.z)

        glEnable(GL_LIGHTING)
        color_r = 0.5
        color_g = 0.0
        color_b = 0.0
        glColor3f(color_r, color_g, color_b)
        glEnable(GL_COLOR_MATERIAL)
        glColorMaterial(GL_FRONT, GL_DIFFUSE)
        #glDisable(GL_TEXTURE_2D)
        glutSolidSphere(self.radius, 64, 64)
        glDisable(GL_COLOR_MATERIAL)

        glDisable(GL_LIGHTING)

        glPopMatrix()
Exemple #9
0
    def initializeGL(self):
        print("initializeGL")
        self.texNumeros, self.texDecor = self.load_textures()
        glEnable(GL_TEXTURE_2D)
        glShadeModel(GL_SMOOTH)
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_CULL_FACE)

        glFrontFace(GL_CCW)

        light_ambient = [0.3, 0.3, 0.3, 0.1]

        glEnable(GL_LIGHTING)
        lightpos = (0, 0, 50)
        glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient)
        glLightfv(GL_LIGHT0, GL_POSITION, lightpos)
        glEnable(GL_LIGHT0)
        glEnable(GL_COLOR_MATERIAL)
        glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)

        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
Exemple #10
0
    def initializeGL(self):
        # We call this right after our OpenGL window is created.
        glClearColor(1.0, 1.0, 1.0, 1.0)
        glClearDepth(1.0)
        glDepthFunc(GL_LESS)
        glEnable(GL_DEPTH_TEST)
        glShadeModel(GL_SMOOTH)

        glEnable(GL_NORMALIZE)
        light_position = (0., 0., 1., 0.)
        white_light = (1., 1., 1., 0.501)
        d_light = (1., 1., 1., 0.01)
        spec = (1., 1., 1., 0.08)
        glLightfv(GL_LIGHT0, GL_POSITION, light_position)
        glLightfv(GL_LIGHT0, GL_AMBIENT, white_light)
        #glLightfv(GL_LIGHT0, GL_DIFFUSE,  d_light)
        glLightfv(GL_LIGHT0, GL_SPECULAR, spec)

        glEnable(GL_LIGHTING)
        glEnable(GL_LIGHT0)

        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        #glBlendFunc(GL_SRC_ALPHA,GL_ONE)
        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
        glEnable(GL_COLOR_MATERIAL)
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()

        gluPerspective(
            45.0,
            float(self.size().height()) / float(self.size().width()), 0.1,
            1000000.0)

        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        self.rotation = [0.0, 0.0]
        self.mesh.prepDraw()
Exemple #11
0
def _draw_compass_geometry():
    """
    Do GL state changes and draw constant geometry for compass.
    Doesn't depend on any outside state, so can be compiled
    into an unchanging display list. 
    """
    glEnable(GL_COLOR_MATERIAL)
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
    glDisable(GL_CULL_FACE)
    glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)

    #ninad 070122 - parametrized the compass drawing. (also added some doc).
    #Also reduced the overall size of the compass.

    p1 = -1  # ? start point of arrow cyl?
    p2 = 3.25  #end point of the arrow cylindrical portion
    p3 = 2.5  #arrow head start point
    p5 = 4.5  # cone tip

    r1 = 0.2  #cylinder radius
    r2 = 0.2
    r3 = 0.2
    r4 = 0.60  #cone base radius

    glePolyCone([[p1, 0, 0], [0, 0, 0], [p2, 0, 0], [p3, 0, 0], [_P4, 0, 0],
                 [p5, 0, 0]], [[0, 0, 0], [1, 0, 0], [1, 0, 0], [.5, 0, 0],
                               [.5, 0, 0], [0, 0, 0]], [r1, r2, r3, r4, 0, 0])

    glePolyCone([[0, p1, 0], [0, 0, 0], [0, p2, 0], [0, p3, 0], [0, _P4, 0],
                 [0, p5, 0]], [[0, 0, 0], [0, .9, 0], [0, .9, 0], [0, .4, 0],
                               [0, .4, 0], [0, 0, 0]], [r1, r2, r3, r4, 0, 0])

    glePolyCone([[0, 0, p1], [0, 0, 0], [0, 0, p2], [0, 0, p3], [0, 0, _P4],
                 [0, 0, p5]], [[0, 0, 0], [0, 0, 1], [0, 0, 1], [0, 0, .4],
                               [0, 0, .4], [0, 0, 0]], [r1, r2, r3, r4, 0, 0])

    glEnable(GL_CULL_FACE)
    glDisable(GL_COLOR_MATERIAL)

    return
Exemple #12
0
def _draw_compass_geometry():
    """
    Do GL state changes and draw constant geometry for compass.
    Doesn't depend on any outside state, so can be compiled
    into an unchanging display list. 
    """
    glEnable(GL_COLOR_MATERIAL)
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
    glDisable(GL_CULL_FACE)
    glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)

    #ninad 070122 - parametrized the compass drawing. (also added some doc). 
    #Also reduced the overall size of the compass. 

    p1 = -1 # ? start point of arrow cyl? 
    p2 = 3.25 #end point of the arrow cylindrical portion
    p3 = 2.5 #arrow head start point
    p5 = 4.5 # cone tip

    r1 = 0.2 #cylinder radius 
    r2 =0.2
    r3 = 0.2
    r4 = 0.60 #cone base radius

    glePolyCone([[p1,0,0], [0,0,0], [p2,0,0], [p3,0,0], [_P4,0,0], [p5,0,0]],
                [[0,0,0], [1,0,0], [1,0,0], [.5,0,0], [.5,0,0], [0,0,0]],
                [r1,r2,r3,r4,0,0])

    glePolyCone([[0,p1,0], [0,0,0], [0,p2,0], [0,p3,0], [0,_P4,0], [0,p5,0]],
                [[0,0,0], [0,.9,0], [0,.9,0], [0,.4,0], [0,.4,0], [0,0,0]],
                [r1,r2,r3,r4,0,0])

    glePolyCone([[0,0,p1], [0,0,0], [0,0,p2], [0,0,p3], [0,0,_P4], [0,0,p5]],
                [[0,0,0], [0,0,1], [0,0,1], [0,0,.4], [0,0,.4], [0,0,0]],
                [r1,r2,r3,r4,0,0])

    glEnable(GL_CULL_FACE)
    glDisable(GL_COLOR_MATERIAL)

    return
Exemple #13
0
	def initializeGL(self):
		# We call this right after our OpenGL window is created.
		glClearColor(1.0, 1.0, 1.0, 1.0)
		glClearDepth(1.0)
		glDepthFunc(GL_LESS)
		glEnable(GL_DEPTH_TEST)
		glShadeModel(GL_SMOOTH)

		glEnable(GL_NORMALIZE)
		light_position = (0., 0., 1., 0.)
		white_light = (1., 1., 1., 0.501)
		d_light = (1., 1., 1., 0.01)
		spec = (1., 1., 1., 0.08)
		glLightfv(GL_LIGHT0, GL_POSITION, light_position)
		glLightfv(GL_LIGHT0, GL_AMBIENT,  white_light)
		#glLightfv(GL_LIGHT0, GL_DIFFUSE,  d_light)
		glLightfv(GL_LIGHT0, GL_SPECULAR, spec)

		glEnable(GL_LIGHTING)
		glEnable(GL_LIGHT0)

		glEnable(GL_BLEND)
		glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
		#glBlendFunc(GL_SRC_ALPHA,GL_ONE)
		glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
		glEnable(GL_COLOR_MATERIAL)
		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

		glMatrixMode(GL_PROJECTION)
		glLoadIdentity()

		gluPerspective(45.0, float(self.size().height())/
						float(self.size().width()), 0.1, 1000000.0)

		glMatrixMode(GL_MODELVIEW)
		glLoadIdentity()
		self.rotation = [0.0, 0.0]
		self.mesh.prepDraw()
Exemple #14
0
    def initializeGL(self):
        glClearColor(0.85, 0.85, 0.85, 1.0)
        glClearDepth(1.0)
        glDepthFunc(GL_LESS)
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_CULL_FACE)

        glShadeModel(GL_SMOOTH)
        glEnable(GL_NORMALIZE)
        glEnable(GL_COLOR_MATERIAL)
        glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)
        glLightfv(GL_LIGHT0, GL_POSITION, (0.0, 0.0, 1.0, 0.0))
        glEnable(GL_LIGHT0)

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()

        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

        self.display_list = glGenLists(1)
        glNewList(self.display_list, GL_COMPILE)
        glScalef(0.5, 0.5, 0.5)

        glEnable(GL_LIGHTING)

        # board
        glColor3f(0.0, 0.0, 0.0)
        self.draw_cuboid(4.0, 4.0, 0.16)

        # USB connector
        glPushMatrix()
        glColor3f(0.5, 0.51, 0.58)
        glTranslatef(0.0, -1.6, 0.28)
        self.draw_cuboid(0.75, 0.9, 0.4)
        glPopMatrix()

        # right button
        glPushMatrix()
        glColor3f(0.5, 0.51, 0.58)
        glTranslatef(1.15, -1.85, 0.16)
        self.draw_cuboid(0.4, 0.3, 0.16)
        glColor3f(0.0, 0.0, 0.0)
        glTranslatef(0.0, -0.155, 0.025)
        self.draw_cuboid(0.18, 0.1, 0.08)
        glPopMatrix()

        # left button
        glPushMatrix()
        glColor3f(0.5, 0.51, 0.58)
        glTranslatef(-1.15, -1.85, 0.16)
        self.draw_cuboid(0.4, 0.3, 0.16)
        glColor3f(0.0, 0.0, 0.0)
        glTranslatef(0.0, -0.155, 0.025)
        self.draw_cuboid(0.18, 0.1, 0.08)
        glPopMatrix()

        # left btb top
        glPushMatrix()
        glColor3f(1.0, 1.0, 1.0)
        glTranslatef(-1.65, 0.0, 0.38)
        self.draw_cuboid(0.5, 1.4, 0.6)
        glPopMatrix()

        # right btb top
        glPushMatrix()
        glColor3f(1.0, 1.0, 1.0)
        glTranslatef(1.65, 0.0, 0.38)
        self.draw_cuboid(0.5, 1.4, 0.6)
        glPopMatrix()

        # left btb bottom
        glPushMatrix()
        glColor3f(1.0, 1.0, 1.0)
        glTranslatef(-1.65, 0.0, -0.33)
        self.draw_cuboid(0.5, 1.4, 0.5)
        glPopMatrix()

        # right btb bottom
        glPushMatrix()
        glColor3f(1.0, 1.0, 1.0)
        glTranslatef(1.65, 0.0, -0.33)
        self.draw_cuboid(0.5, 1.4, 0.5)
        glPopMatrix()

        # left bricklet port
        glPushMatrix()
        glColor3f(1.0, 1.0, 1.0)
        glTranslatef(-0.85, 1.8, -0.23)
        self.draw_cuboid(1.2, 0.4, 0.3)
        glPopMatrix()

        # right bricklet port
        glPushMatrix()
        glColor3f(1.0, 1.0, 1.0)
        glTranslatef(0.85, 1.8, -0.23)
        self.draw_cuboid(1.2, 0.4, 0.3)
        glPopMatrix()

        # left direction LED
        glPushMatrix()
        glColor3f(0.0, 0.5, 0.0)
        glTranslatef(-1.05, 1.425, 0.115)
        self.draw_cuboid(0.1, 0.2, 0.07)
        glPopMatrix()

        # top direction LED
        glPushMatrix()
        glColor3f(0.0, 0.5, 0.0)
        glTranslatef(-0.675, 1.8, 0.115)
        self.draw_cuboid(0.2, 0.1, 0.07)
        glPopMatrix()

        # right direction LED
        glPushMatrix()
        glColor3f(0.0, 0.5, 0.0)
        glTranslatef(-0.3, 1.425, 0.115)
        self.draw_cuboid(0.1, 0.2, 0.07)
        glPopMatrix()

        # bottom direction LED
        glPushMatrix()
        glColor3f(0.0, 0.5, 0.0)
        glTranslatef(-0.675, 1.05, 0.115)
        self.draw_cuboid(0.2, 0.1, 0.07)
        glPopMatrix()

        # left y orientation LED
        glPushMatrix()
        glColor3f(0.0, 0.0, 1.0)
        glTranslatef(0.275, 1.7, 0.115)
        self.draw_cuboid(0.1, 0.2, 0.07)
        glPopMatrix()

        # right y orientation LED
        glPushMatrix()
        glColor3f(1.0, 0.0, 0.0)
        glTranslatef(0.425, 1.7, 0.115)
        self.draw_cuboid(0.1, 0.2, 0.07)
        glPopMatrix()

        # top z orientation LED
        glPushMatrix()
        glColor3f(1.0, 0.0, 0.0)
        glTranslatef(0.35, 1.15, 0.115)
        self.draw_cuboid(0.2, 0.1, 0.07)
        glPopMatrix()

        # bottom z orientation LED
        glPushMatrix()
        glColor3f(0.0, 0.0, 1.0)
        glTranslatef(0.35, 1.0, 0.115)
        self.draw_cuboid(0.2, 0.1, 0.07)
        glPopMatrix()

        # top x orientation LED
        glPushMatrix()
        glColor3f(1.0, 0.0, 0.0)
        glTranslatef(1.0, 1.15, 0.115)
        self.draw_cuboid(0.2, 0.1, 0.07)
        glPopMatrix()

        # bottom x orientation LED
        glPushMatrix()
        glColor3f(0.0, 0.0, 1.0)
        glTranslatef(1.0, 1.0, 0.115)
        self.draw_cuboid(0.2, 0.1, 0.07)
        glPopMatrix()

        # top alignment corner
        glPushMatrix()
        glColor3f(1.0, 1.0, 1.0)
        glBegin(GL_TRIANGLES)
        glNormal3f(0.0, 0.0, 1.0)
        glVertex3f(-2.0, -2.0, 0.081)
        glVertex3f(-1.1, -2.0, 0.081)
        glVertex3f(-2.0, -1.1, 0.081)
        glEnd()
        glPopMatrix()

        # bottom alignment corner
        glPushMatrix()
        glColor3f(1.0, 1.0, 1.0)
        glBegin(GL_TRIANGLES)
        glNormal3f(0.0, 0.0, -1.0)
        glVertex3f(-2.0, -2.0, -0.081)
        glVertex3f(-2.0, -1.1, -0.081)
        glVertex3f(-1.1, -2.0, -0.081)
        glEnd()
        glPopMatrix()

        glDisable(GL_LIGHTING)

        # axis
        glPushMatrix()
        glTranslatef(-2.3, -2.3, -0.38)
        glLineWidth(3.0)
        glBegin(GL_LINES)
        glColor3f(1, 0, 0)  # x axis is red
        glVertex3f(0, 0, 0)
        glVertex3f(3, 0, 0)
        glColor3f(0, 0.5, 0)  # y axis is green
        glVertex3f(0, 0, 0)
        glVertex3f(0, 3, 0)
        glColor3f(0, 0, 1)  # z axis is blue
        glVertex3f(0, 0, 0)
        glVertex3f(0, 0, 3)
        glEnd()
        glLineWidth(1.0)
        glPopMatrix()

        glEndList()
Exemple #15
0
    def OnDraw(self):
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glFrustum(-50.0 * self.zoom, 50.0 * self.zoom, -50.0 * self.zoom,
                  50.0 * self.zoom, 200, 800.0)
        #glTranslatef(0.0, 0.0, -400.0)
        gluLookAt(200.0, -200.0, 400.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0)

        glMatrixMode(GL_MODELVIEW)
        if self.resetView:
            glLoadIdentity()
            self.lastx = self.x = 0
            self.lasty = self.y = 0
            self.anglex = self.angley = self.anglez = 0
            self.transx = self.transy = 0
            self.resetView = False

        if self.size is None:
            self.size = self.GetClientSize()
        w, h = self.size
        w = max(w, 1.0)
        h = max(h, 1.0)
        xScale = 180.0 / w
        yScale = 180.0 / h
        glRotatef(self.angley * yScale, 1.0, 0.0, 0.0)
        glRotatef(self.anglex * xScale, 0.0, 1.0, 0.0)
        glRotatef(self.anglez, 0.0, 0.0, 1.0)
        glTranslatef(self.transx, self.transy, 0.0)

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
        glEnable(GL_COLOR_MATERIAL)

        glEnable(GL_LIGHTING)
        glEnable(GL_LIGHT0)
        glEnable(GL_LIGHT1)

        if self.drawGrid:
            glBegin(GL_LINES)
            for i in range(len(self.gridVertices)):
                glColor(self.gridColors[i])
                p = self.gridVertices[i]
                glVertex3f(p[0], p[1], p[2])
                glVertex3f(p[3], p[4], p[5])
            glEnd()

        colors = {MT_RAPID: [0.0, 1.0, 1.0, 1], MT_NORMAL: [1.0, 1.0, 1.0, 1]}

        currentLineType = MT_RAPID
        glColor(colors[MT_RAPID])

        glBegin(GL_LINE_STRIP)
        for p in self.dataPoints:
            if p[3] != currentLineType:
                currentLineType = p[3]
                glColor(colors[currentLineType])
            glVertex3f(p[0], p[1], p[2])

        glEnd()

        self.SwapBuffers()

        glDisable(GL_LIGHT0)
        glDisable(GL_LIGHT1)
        glDisable(GL_LIGHTING)

        self.anglex = self.angley = self.anglez = 0
        self.transx = self.transy = 0
Exemple #16
0
    def initializeGL(self):
        glClearColor(0.85, 0.85, 0.85, 1.0)
        glClearDepth(1.0)
        glDepthFunc(GL_LESS)
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_CULL_FACE)

        glShadeModel(GL_SMOOTH)
        glEnable(GL_NORMALIZE)
        glEnable(GL_COLOR_MATERIAL)
        glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)
        glLightfv(GL_LIGHT0, GL_POSITION, (0.0, 0.0, 1.0, 0.0))
        glEnable(GL_LIGHT0)

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()

        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

        self.display_list = glGenLists(1)
        glNewList(self.display_list, GL_COMPILE)
        glScalef(0.5, 0.5, 0.5)

        glEnable(GL_LIGHTING)

        # board
        glColor3f(0.0, 0.0, 0.0)
        self.draw_cuboid(4.0, 4.0, 0.16)

        # USB connector
        glPushMatrix()
        glColor3f(0.5, 0.51, 0.58)
        glTranslatef(0.0, -1.6, 0.28)
        self.draw_cuboid(0.75, 0.9, 0.4)
        glPopMatrix()

        # right button
        glPushMatrix()
        glColor3f(0.5, 0.51, 0.58)
        glTranslatef(1.15, -1.85, 0.16)
        self.draw_cuboid(0.4, 0.3, 0.16)
        glColor3f(0.0, 0.0, 0.0)
        glTranslatef(0.0, -0.155, 0.025)
        self.draw_cuboid(0.18, 0.1, 0.08)
        glPopMatrix()

        # left button
        glPushMatrix()
        glColor3f(0.5, 0.51, 0.58)
        glTranslatef(-1.15, -1.85, 0.16)
        self.draw_cuboid(0.4, 0.3, 0.16)
        glColor3f(0.0, 0.0, 0.0)
        glTranslatef(0.0, -0.155, 0.025)
        self.draw_cuboid(0.18, 0.1, 0.08)
        glPopMatrix()

        # left btb top
        glPushMatrix()
        glColor3f(1.0, 1.0, 1.0)
        glTranslatef(-1.65, 0.0, 0.38)
        self.draw_cuboid(0.5, 1.4, 0.9)
        glPopMatrix()

        # right btb top
        glPushMatrix()
        glColor3f(1.0, 1.0, 1.0)
        glTranslatef(1.65, 0.0, 0.38)
        self.draw_cuboid(0.5, 1.4, 0.9)
        glPopMatrix()

        # left btb bottom
        glPushMatrix()
        glColor3f(1.0, 1.0, 1.0)
        glTranslatef(-1.65, 0.0, -0.33)
        self.draw_cuboid(0.5, 1.4, 0.5)
        glPopMatrix()

        # right btb bottom
        glPushMatrix()
        glColor3f(1.0, 1.0, 1.0)
        glTranslatef(1.65, 0.0, -0.33)
        self.draw_cuboid(0.5, 1.4, 0.5)
        glPopMatrix()

        # left bricklet port
        glPushMatrix()
        glColor3f(1.0, 1.0, 1.0)
        glTranslatef(-0.85, 1.8, -0.23)
        self.draw_cuboid(1.2, 0.4, 0.3)
        glPopMatrix()

        # right bricklet port
        glPushMatrix()
        glColor3f(1.0, 1.0, 1.0)
        glTranslatef(0.85, 1.8, -0.23)
        self.draw_cuboid(1.2, 0.4, 0.3)
        glPopMatrix()

        # left direction LED
        glPushMatrix()
        glColor3f(0.0, 0.5, 0.0)
        glTranslatef(-1.05, 1.425, 0.115)
        self.draw_cuboid(0.1, 0.2, 0.07)
        glPopMatrix()

        # top direction LED
        glPushMatrix()
        glColor3f(0.0, 0.5, 0.0)
        glTranslatef(-0.675, 1.8, 0.115)
        self.draw_cuboid(0.2, 0.1, 0.07)
        glPopMatrix()

        # right direction LED
        glPushMatrix()
        glColor3f(0.0, 0.5, 0.0)
        glTranslatef(-0.3, 1.425, 0.115)
        self.draw_cuboid(0.1, 0.2, 0.07)
        glPopMatrix()

        # bottom direction LED
        glPushMatrix()
        glColor3f(0.0, 0.5, 0.0)
        glTranslatef(-0.675, 1.05, 0.115)
        self.draw_cuboid(0.2, 0.1, 0.07)
        glPopMatrix()

        # left y orientation LED
        glPushMatrix()
        glColor3f(0.0, 0.0, 1.0)
        glTranslatef(0.275, 1.7, 0.115)
        self.draw_cuboid(0.1, 0.2, 0.07)
        glPopMatrix()

        # right y orientation LED
        glPushMatrix()
        glColor3f(1.0, 0.0, 0.0)
        glTranslatef(0.425, 1.7, 0.115)
        self.draw_cuboid(0.1, 0.2, 0.07)
        glPopMatrix()

        # top z orientation LED
        glPushMatrix()
        glColor3f(1.0, 0.0, 0.0)
        glTranslatef(0.35, 1.15, 0.115)
        self.draw_cuboid(0.2, 0.1, 0.07)
        glPopMatrix()

        # bottom z orientation LED
        glPushMatrix()
        glColor3f(0.0, 0.0, 1.0)
        glTranslatef(0.35, 1.0, 0.115)
        self.draw_cuboid(0.2, 0.1, 0.07)
        glPopMatrix()

        # top x orientation LED
        glPushMatrix()
        glColor3f(1.0, 0.0, 0.0)
        glTranslatef(1.0, 1.15, 0.115)
        self.draw_cuboid(0.2, 0.1, 0.07)
        glPopMatrix()

        # bottom x orientation LED
        glPushMatrix()
        glColor3f(0.0, 0.0, 1.0)
        glTranslatef(1.0, 1.0, 0.115)
        self.draw_cuboid(0.2, 0.1, 0.07)
        glPopMatrix()

        # top alignment corner
        glPushMatrix()
        glColor3f(1.0, 1.0, 1.0)
        glBegin(GL_TRIANGLES)
        glNormal3f(0.0, 0.0, 1.0)
        glVertex3f(-2.0, -2.0, 0.09)
        glVertex3f(-1.1, -2.0, 0.09)
        glVertex3f(-2.0, -1.1, 0.09)
        glEnd()
        glPopMatrix()

        # bottom alignment corner
        glPushMatrix()
        glColor3f(1.0, 1.0, 1.0)
        glBegin(GL_TRIANGLES)
        glNormal3f(0.0, 0.0, -1.0)
        glVertex3f(-2.0, -2.0, -0.09)
        glVertex3f(-2.0, -1.1, -0.09)
        glVertex3f(-1.1, -2.0, -0.09)
        glEnd()
        glPopMatrix()

        glDisable(GL_LIGHTING)

        # axis
        glPushMatrix()
        glTranslatef(-2.3, -2.3, -0.38)
        glLineWidth(3.0)
        glBegin(GL_LINES)
        glColor3f(1,0,0) # x axis is red
        glVertex3f(0,0,0)
        glVertex3f(3,0,0)
        glColor3f(0,0.5,0) # y axis is green
        glVertex3f(0,0,0)
        glVertex3f(0,3,0)
        glColor3f(0,0,1) # z axis is blue
        glVertex3f(0,0,0)
        glVertex3f(0,0,3)
        glEnd()
        glLineWidth(1.0)
        glPopMatrix()

        glEndList()
Exemple #17
0
def position_mars_3d(temp=None):
    '''
    This is a simple 3D window that shows a spacecraft in MSO coordinates.
    This tool will look for links to the tplot variable that are named either "x/y/z" or "mso_(x/y/z)"
    '''

    # Import 3D functionality from opengl
    try:
        import pyqtgraph.opengl as gl
        from pyqtgraph.Qt import QtGui
        from OpenGL.GL import glLightModelfv, glLightfv, GL_LIGHT0, GL_POSITION, \
            GL_LIGHT_MODEL_AMBIENT, GL_LIGHTING, glEnable, GL_COLOR_MATERIAL, \
            GL_AMBIENT, GL_SPECULAR, GL_DIFFUSE, glMaterial, GL_FRONT_AND_BACK, \
            GL_AMBIENT_AND_DIFFUSE, GL_FRONT, glColorMaterial, GL_PROJECTION, \
            glMatrixMode, glLoadIdentity, glTexParameterf
    except:
        raise (
            "In order to use the 3D position viewing tool you must pip install PyOpenGL"
        )

    # Tell Pytplot about new window
    window = pytplot.tplot_utilities.get_available_qt_window(name='3D_MARS')
    window.resize(1000, 600)
    window.setWindowTitle('3D Mars Window')

    # Defining a new class that keeps track of spacecraft position and moves the
    class PlanetView(gl.GLViewWidget):
        spacecraft_x = 0
        spacecraft_y = 0
        spacecraft_z = 0
        tvar_name = 'temp'  # Store the name of the tplot variable stored, so we know if we need to redraw the orbit

        def paintGL(self, region=None, viewport=None, useItemNames=False):
            glLightfv(GL_LIGHT0, GL_POSITION, [-100000, 0, 0, 0])
            super().paintGL(region=region,
                            viewport=viewport,
                            useItemNames=useItemNames)

    plot1 = PlanetView()

    # Set up the "sun"
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [.3, .3, .3, 1.0])
    light_position = [-100000, 0, 0, 0]
    glLightfv(GL_LIGHT0, GL_POSITION, light_position)
    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)
    glEnable(GL_COLOR_MATERIAL)
    glLightfv(GL_LIGHT0, GL_AMBIENT, [1, 1, 1, 0])
    glLightfv(GL_LIGHT0, GL_DIFFUSE, [1, 1, 1, 1])
    glLightfv(GL_LIGHT0, GL_SPECULAR, [1, 0, 0, 0])

    # Create Mars and spacecraft icons (assuming spherical spacecraft)
    md = gl.MeshData.sphere(rows=100, cols=220)
    mars = gl.GLMeshItem(meshdata=md,
                         smooth=True,
                         color=(.5, 0, 0, 1),
                         glOptions='opaque')
    mars.translate(0, 0, 0)
    mars.scale(3390, 3390, 3390)
    spacecraft = gl.GLMeshItem(meshdata=md, smooth=True, color=(1, 1, 1, 1))
    spacecraft.translate(plot1.spacecraft_x, plot1.spacecraft_y,
                         plot1.spacecraft_z)

    spacecraft.scale(200, 200, 200)
    orbit_path = gl.GLLinePlotItem()
    plot1.addItem(mars)
    plot1.addItem(spacecraft)
    plot1.addItem(orbit_path)
    glMaterial(GL_FRONT_AND_BACK, GL_SPECULAR, [.5, .5, .5, 1])
    glEnable(GL_COLOR_MATERIAL)
    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)

    # Put the planetview plot into the pytplot window
    window.setCentralWidget(plot1)

    # Move around the camera
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    plot1.setModelview()
    plot1.setCameraPosition(distance=30000, azimuth=0, elevation=0)

    # Turn on the window!
    window.show()

    # Define the function that is called on new hover_time updates
    def update(t, name):
        # Move spacecraft back to 0,0,0
        previous_x = plot1.spacecraft_x
        previous_y = plot1.spacecraft_y
        previous_z = plot1.spacecraft_z
        previous_tvar = plot1.tvar_name

        spacecraft.translate(-1 * previous_x, -1 * previous_y, -1 * previous_z)

        # Get the xarray for x/y/z positions of the spacecraft
        if 'x' in pytplot.data_quants[name].attrs['plot_options']['links']:
            x_tvar = pytplot.data_quants[
                pytplot.data_quants[name].attrs['plot_options']['links']['x']]
            y_tvar = pytplot.data_quants[
                pytplot.data_quants[name].attrs['plot_options']['links']['y']]
            z_tvar = pytplot.data_quants[
                pytplot.data_quants[name].attrs['plot_options']['links']['z']]
        elif 'mso_x' in pytplot.data_quants[name].attrs['plot_options'][
                'links']:
            x_tvar = pytplot.data_quants[pytplot.data_quants[name].attrs[
                'plot_options']['links']['mso_x']]
            y_tvar = pytplot.data_quants[pytplot.data_quants[name].attrs[
                'plot_options']['links']['mso_y']]
            z_tvar = pytplot.data_quants[pytplot.data_quants[name].attrs[
                'plot_options']['links']['mso_z']]
        else:
            return

        if name != previous_tvar:
            import numpy as np
            pathasdf = np.array((x_tvar.data, y_tvar.data, z_tvar.data),
                                dtype=float).T
            orbit_path.setData(pos=pathasdf)

        # Get the nearest x/y/z of the hover time
        new_x = x_tvar.sel(time=t, method='nearest').values
        new_y = y_tvar.sel(time=t, method='nearest').values
        new_z = z_tvar.sel(time=t, method='nearest').values

        # Move the spacecraft
        spacecraft.translate(new_x, new_y, new_z)
        plot1.spacecraft_x, plot1.spacecraft_y, plot1.spacecraft_z = new_x, new_y, new_z
        plot1.tvar_name = name
        plot1.paintGL()

    # Register the above update function to the called functions
    pytplot.hover_time.register_listener(update)

    return