示例#1
0
def render_cb():
    state.frame += 1
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    projection = Transform().perspective(45.0, state.aspect, 0.1, 10.0)
    view = Transform().lookat(0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
    model = Transform().rotate(state.azimuth, 0.0, 1.0,
                               0.0).rotate(state.elevation, 1.0, 0.0, 0.0)
    cam_pos = view.inverse() * (0.0, 0.0, 0.0)

    # enable the shader
    state.shader.use()

    # view and projection matricies
    state.shader['view'] = view.matrix()
    state.shader['projection'] = projection.matrix()
    state.shader['camera_pos'] = cam_pos

    # world-space lighting
    state.shader['light_pos'] = (1.0, 10.0, 4.0)

    # per object/material stuff
    state.shader['model'] = model.matrix()
    state.shader['in_normal'] = state.mesh.normals
    state.shader['in_position'] = state.mesh.vertices

    # draw each material individually with its
    # specific (hacked a bit here) materials
    for matname, (start, end) in state.mesh.material_triangles.items():
        mat = state.materials[matname]
        state.shader['diffuse'] = mat.diffuse
        state.shader['ambient'] = mat.diffuse * 0.25
        state.shader['specular'] = (1.0, 1.0, 1.0)
        state.shader['spec_exp'] = 70.0
        glDrawArrays(GL_TRIANGLES, start * 3, (end - start) * 3)
示例#2
0
    def test_mul(self):
        pos = numpy.random.randn(3)
        deg = numpy.random.uniform(-180, 180)
        axis = numpy.random.randn(3)
        T = Transform().translate(*pos)
        R = Transform().rotate(deg, *axis)

        M1 = Transform().translate(*pos).rotate(deg, *axis)
        M2 = R * T
        M3 = R * T.matrix()
        self.assertTrue(numpy.allclose(M1.matrix(), M2.matrix()))
        self.assertTrue(numpy.allclose(M1.matrix(), M3))
示例#3
0
def render_cb():
    # camera stuff
    projection = Transform().perspective(45.0, state.aspect, 0.1, 10.0)
    modelview = Transform().lookat(4.0, 4.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)

    ## Selection code.
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    state.select_shader.use()
    state.select_shader['modelview'] = modelview.matrix()
    state.select_shader['projection'] = projection.matrix()

    state.select_shader['position'] = state.positions
    state.select_shader['id'] = state.ids

    # draw scene here
    glPointSize(10.0)
    glDrawArrays(GL_POINTS, 0, state.positions.shape[0])
    glFlush()

    # read the pixel under the mouse and convert to an integer,
    # if the result is in the map of ids, mark as selected
    rgb = glReadPixels(state.mouse[0], state.mouse[1], 1, 1, GL_RGBA,
                       GL_UNSIGNED_BYTE, None)
    idx = int(rgb[0]) + int(rgb[1]) * 256 + int(rgb[2]) * 65536
    if idx in state.id_map:
        selected = state.id_map[idx]
    else:
        selected = -1

    # Rendering code
    glClearColor(0.7, 0.7, 1.0, 0.0)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    # highlight the selected point in yellow
    tcolors = state.colors.copy()
    if selected >= 0:
        tcolors[selected] = (1.0, 1.0, 0.0)

    state.render_shader.use()
    state.render_shader['modelview'] = modelview.matrix()
    state.render_shader['projection'] = projection.matrix()

    state.render_shader['position'] = state.positions
    state.render_shader['color'] = tcolors

    glPointSize(10.0)
    glDrawArrays(GL_POINTS, 0, state.positions.shape[0])
    glFlush()