Example #1
0
class myApp(Snowball):

    def setup(self):

        self.shader = Shader("vertex.vs", "fragment.fs")

        self.object = OpenGLObject("box")

        self.object.vbo.data = [ 0.5, 0.5, 0.0, -0.5, 0.5, 0.0, -0.5, -0.5, 0.0, 0.5, -0.5, 0.0 ]
        
        self.object.ebo.data = [ 0, 1, 2, 0, 2, 3 ]

        self.object.loadData()

    def loop(self, dt):

        glClearColor(0.2, 0.3, 0.8, 1.0)

        glClear(GL_COLOR_BUFFER_BIT)

        self.shader.use()

        self.object.draw()
    elapsed_seconds = current_second - previous_second
    if elapsed_seconds > 1.0:
        previous_second = current_second
        fps = float(frame_count) / float(elapsed_seconds)
        glfw.SetWindowTitle(window, '%s @ FPS: %.2f' % (window_title, fps))
        frame_count = 0.0
    frame_count += 1.0

glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)
glDisable(GL_STENCIL_TEST)
glDisable(GL_BLEND)
glDisable(GL_CULL_FACE)
glClearColor(0.0, 0.0, 0.0, 0.0)

screen_shader = Shader('shader/screen_shader.vs', 'shader/screen_shader.frag')
screen_shader.compile(); screen_shader.use()

color_shader = Shader('shader/color_shader.vs', 'shader/color_shader.frag')
color_shader.compile(); color_shader.use()

raw_im = np.zeros((480,640,3), dtype=np.uint8)

def createFramebuffer(W, H, samples):
    tex = np.empty(3, dtype=np.uint32)
    if samples == 1:
        glCreateTextures(GL_TEXTURE_2D, len(tex), tex)
        glTextureStorage2D(tex[0], 1, GL_RGB8, W, H)
        glTextureParameteri(tex[0], GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glTextureParameteri(tex[0], GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        glTextureStorage2D(tex[1], 1, GL_DEPTH_COMPONENT32F, W, H)
        glTextureParameteri(tex[1], GL_TEXTURE_MAG_FILTER, GL_NEAREST)
Example #3
0
    glfw.PollEvents()
    t = glfw.GetTime()

    sceneData = packSceneData()
    glNamedBufferSubData(sceneUBO, 0, sceneData.nbytes, sceneData)

    objectData = packObjectData()
    glNamedBufferSubData(objectUBO, 0, objectData.nbytes, objectData)

    window.update_fps_counter()
    tex.update(webcam_image)

    glViewport(0, 0, window.framebuf_width, window.framebuf_height)
    glClearColor(0.0, 0.0, 0.0, 1.0)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    screen_shader.use()
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)
    color_shader.use()

    quad_shader.use()
    T = np.dot(np.dot(camera.T_proj_world(), T_world_prime), T_world_board)
    glUniformMatrix4fv(0, 1, GL_TRUE, T)
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
    glUniform4fv(1, 1, np.array([0.3, 0.4, 0.7, 0.4], dtype=np.float32))
    glDrawArrays(GL_TRIANGLE_FAN, 0, 4)
    glDisable(GL_BLEND)
    glLineWidth(5.0)
    glUniform4fv(1, 1, np.array([0.3, 0.4, 0.7, 1.0], dtype=np.float32))
    glDrawArrays(GL_LINE_LOOP, 0, 4)
    glLineWidth(1.0)
Example #4
0
def main():
    res = (2600,1800)
    n_particles = 1500                                      # Number of bodies. My computer runs 2000 pretty comfortably.
    cam = Camera(fov=80,
                 aspect_ratio=res[0]/res[1],
                 xyz=(0,0,-10))
    torch_nbody = torch_NBody(n_particles,
                              damping=0.10,                 # Prevent bodies from hitting lightspeed when they get too close
                              G=.05,                        # Gravity strength
                              spread=3.0,                   # Place bodies in a normal distribution with this standard deviation
                              mass_pareto=1.9,              # The masses of the bodies follow this Pareto distribution
                              velocity_spread=0.5,          # Initial velocity vectors are normally distributed with this standard deviation
                              dtype=torch.cuda.FloatTensor)

    if not glfw.init():
        return
    window = glfw.create_window(res[0], res[1], "", None, None)
    if not window:
        glfw.terminate()
        return
    glfw.make_context_current(window)
    glEnable(GL_DEPTH_TEST)
    glClearColor(0.0,0.0,0.0,1.0)

    shader = Shader()
    shader.use()
    shader.view = cam.matrix

    model = Model("sphere.obj", center=True)
    model.use(shader.position)
    m = model.n_indices

    buffer_translate  = glGenBuffers(1)
    buffer_scale = glGenBuffers(1)
    buffer_brightness = glGenBuffers(1)

    glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer_scale)
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, shader.obj_scale, buffer_scale)
    scale = np.array(0.03 * (torch_nbody.mass.cpu().view(-1) * .75 / np.pi).pow(1/3).to(torch.float32).numpy())
    glBufferData(GL_SHADER_STORAGE_BUFFER, 4 * n_particles, scale, GL_DYNAMIC_DRAW)

    glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer_translate)
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, shader.obj_translate, buffer_translate)

    exposure = 200.0
    while not glfw.window_should_close(window):
        
        glfw.poll_events()

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        torch_nbody.step(0.01)

        glBindBufferBase(GL_SHADER_STORAGE_BUFFER, shader.obj_translate, buffer_translate)
        translate = torch_nbody.position.cpu().view(-1).to(torch.float32).numpy()
        glBufferData(GL_SHADER_STORAGE_BUFFER, 4 * len(translate), translate, GL_DYNAMIC_DRAW)

        # Example visualization
        brightness = torch_nbody.a.norm(dim=1)
        exposure = exposure * 0.98 + brightness.max() * 0.02
        brightness /= exposure
        brightness = 0.2 + 0.9 * brightness
        brightness = brightness.cpu().view(-1).to(torch.float32).numpy()
        glBindBufferBase(GL_SHADER_STORAGE_BUFFER, shader.obj_brightness, buffer_brightness)
        glBufferData(GL_SHADER_STORAGE_BUFFER, 4 * len(brightness), brightness, GL_DYNAMIC_DRAW)

        glDrawElementsInstanced(GL_TRIANGLES, 3 * m, GL_UNSIGNED_INT, None, n_particles)

        glfw.swap_buffers(window)
    glfw.terminate()