示例#1
0
def main():
    init()

    window = glfw.create_window(800, 600, "Chapter10", None, None)
    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)
    glfw.set_input_mode(window, glfw.STICKY_KEYS, GL_TRUE)
    glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED)
    glEnable(GL_DEPTH_TEST)
    glDepthFunc(GL_LESS)
    glClearColor(0.3, 0.3, 0.3, 1)

    vertex_array_id = glGenVertexArrays(1)
    glBindVertexArray(vertex_array_id)

    res_x, res_y = glfw.get_window_size(window)
    cam = camera.Camera(0,0,2,res_x,res_y)

    grid = Grid(0,0,0,10,10,10,10)

    eye = Sphere()
    eye.set_shaders('res/glsl/chapter10.vs', 'res/glsl/chapter10.fs')

    while not glfw.window_should_close(window) and glfw.get_key(window, glfw.KEY_ESCAPE) != glfw.PRESS:
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        cam.controller(window)

        grid.projection = cam.projection
        grid.view = cam.view
        grid.render()

        eye.projection = cam.projection
        eye.view = cam.view
        eye.render()

        glfw.swap_buffers(window)
        glfw.poll_events()

    glfw.terminate()
示例#2
0
"""
Example of camera matrix factorization.
"""

import numpy as np

from common import camera

K = np.array([[1000, 0, 500], [0, 1000, 300], [0, 0, 1]])
tmp = camera.rotation_matrix([0, 0, 1])[:3, :3]
Rt = np.hstack((tmp, np.array([[50], [40], [30]])))
cam = camera.Camera(np.dot(K, Rt))
cam_K, cam_R, cam_t = cam.factor()
print('K:')
print(K)
print('Rt:')
print(Rt)
print('Camera Factor K:')
print(cam_K)
print('Camera Factor R:')
print(cam_R)
print('Camera Factor t:')
print(cam_t)
print('Camera P:')
print(cam.P)
示例#3
0
def main():
    init()

    window = glfw.create_window(800, 600, "Chapter5", None, None)
    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)
    glfw.set_input_mode(window, glfw.STICKY_KEYS, GL_TRUE)
    glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED)
    glfw.set_key_callback(window, key_callback)
    glfw.set_cursor_pos(window, 400, 300)
    glEnable(GL_DEPTH_TEST)
    glDepthFunc(GL_LESS)
    glClearColor(0.3, 0.3, 0.3, 1)

    vertex_array_id = glGenVertexArrays(1)
    glBindVertexArray(vertex_array_id)
    program_id = load_shaders('res/glsl/chapter8.vs', 'res/glsl/chapter8.fs')
    tex = texture.load('res/texture/eye.bmp')
    texture_id = glGetUniformLocation(program_id, 'TextureSampler')

    res_x, res_y = glfw.get_window_size(window)
    cam = camera.Camera(0, 0, 2, res_x, res_y)
    model = np.matrix(np.identity(4), dtype=np.float32)

    projection_id = glGetUniformLocation(program_id, 'projection')
    view_id = glGetUniformLocation(program_id, 'view')
    model_id = glGetUniformLocation(program_id, 'model')

    scene = load('res/model/sphere.obj')
    mesh = scene.meshes[0]
    vertex = mesh.vertices
    uv = mesh.texturecoords
    normal = mesh.normals
    index = mesh.faces

    vertex_buffer = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer)
    glBufferData(GL_ARRAY_BUFFER, vertex.nbytes, vertex, GL_STATIC_DRAW)

    uv_buffer = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, uv_buffer)
    glBufferData(GL_ARRAY_BUFFER, uv.nbytes, uv, GL_STATIC_DRAW)

    normal_buffer = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, normal_buffer)
    glBufferData(GL_ARRAY_BUFFER, normal.nbytes, normal, GL_STATIC_DRAW)

    element_buffer = glGenBuffers(1)
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, element_buffer)
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, index.nbytes, index, GL_STATIC_DRAW)

    while not glfw.window_should_close(window) and glfw.get_key(
            window, glfw.KEY_ESCAPE) != glfw.PRESS:
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        cam.controller(window)

        glUseProgram(program_id)
        glUniformMatrix4fv(projection_id, 1, GL_FALSE, cam.projection)
        glUniformMatrix4fv(view_id, 1, GL_FALSE, cam.view)
        glUniformMatrix4fv(model_id, 1, GL_FALSE, model)

        glActiveTexture(GL_TEXTURE0)
        glBindTexture(GL_TEXTURE_2D, tex)
        glUniform1i(texture_id, 0)

        glEnableVertexAttribArray(0)
        glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer)
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)

        glEnableVertexAttribArray(1)
        glBindBuffer(GL_ARRAY_BUFFER, uv_buffer)
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, None)

        glEnableVertexAttribArray(2)
        glBindBuffer(GL_ARRAY_BUFFER, normal_buffer)
        glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, None)

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, element_buffer)
        glDrawElements(GL_TRIANGLES, index.nbytes, GL_UNSIGNED_INT, None)

        glDisableVertexAttribArray(0)
        glDisableVertexAttribArray(1)
        glDisableVertexAttribArray(2)

        glfw.swap_buffers(window)
        glfw.poll_events()

    glfw.terminate()
"""
Projection of points from house.p3d example.
"""
import numpy as np
import pylab as plt

from common import camera

# load points
points = np.loadtxt('data/house.p3d').T
points = np.vstack((points, np.ones(points.shape[1])))

# setup camera
P = np.hstack((np.eye(3), np.array([[0], [0], [-10]])))
cam = camera.Camera(P)
x = cam.project(points)

# plot projection
plt.figure('House Projection')
plt.subplot(1, 2, 1)
plt.title('Projected Points')
plt.plot(x[0], x[1], 'k.')

# create transformation
r = 0.05 * np.random.rand(3)
rot = camera.rotation_matrix(r)

# rotate camera and project
plt.subplot(1, 2, 2)
plt.title('Camera Rotation')
for t in range(20):
# This differs from the book: Swap coordinates in the from/to points because they are row/col
# whereas the Camera plots are x/y. If they aren't swapped before goint into the
# H_from_ransac method, H will not be computed correctly for the camera translation.
fp_xy = np.array([fp[1, :], fp[0, :], fp[2, :]])
tp_xy = np.array([tp[1, :], tp[0, :], tp[2, :]])

model = homography.RansacModel()
H = homography.H_from_ransac(fp_xy, tp_xy, model)[0]
# camera calibration
K = camera.book_calibration((747, 1000))

# 3D points at plane z=0 with sides of length 0.2
box = objects3d.cube_points([0, 0, 0.1], 0.1)

# project bottom square in first image
cam1 = camera.Camera(np.hstack((K, np.dot(K, np.array([[0], [0], [-1]])))))

# first points are the bottom square
box_cam1 = cam1.project(homography.make_homog(box[:, :5]))

# use H to transfer points to the second image
box_trans = homography.normalize(np.dot(H, box_cam1))

# compute second camera matrix from cam1 and H
cam2 = camera.Camera(np.dot(H, cam1.P))
A = np.dot(np.linalg.inv(K), cam2.P[:, :3])
A = np.array([A[:, 0], A[:, 1], np.cross(A[:, 0], A[:, 1])]).T
cam2.P[:, :3] = np.dot(K, A)

# project with the second camera
box_cam2 = cam2.project(homography.make_homog(box))