Ejemplo n.º 1
0
def init(filename):
    """ Initialize aspects of the GL scene rendering.  """
    global trackball, flashlight, vertex_buffer, normal_buffer, color_buffer, colors, vertices, normals

    # initialize quaternions for the light and trackball
    flashlight = quat.for_rotation(0.0, vector(1.0, 0.0, 0.0))
    trackball = quat.for_rotation(0.0, vector(1.0, 0.0, 0.0))

    # read the .OBJ file into VBOs
    scene.read(filename)
    vertices, normals, colors = scene.compile()

    vertex_buffer = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer)
    glBufferData(GL_ARRAY_BUFFER,
                 len(vertices) * 4, (c_float * len(vertices))(*vertices),
                 GL_STATIC_DRAW)

    normal_buffer = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, normal_buffer)
    glBufferData(GL_ARRAY_BUFFER,
                 len(normals) * 4, (c_float * len(normals))(*normals),
                 GL_STATIC_DRAW)

    color_buffer = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, color_buffer)
    glBufferData(GL_ARRAY_BUFFER,
                 len(colors) * 4, (c_float * len(colors))(*colors),
                 GL_STATIC_DRAW)

    # set up the object shaders
    init_shaders()

    glEnable(GL_DEPTH_TEST)
Ejemplo n.º 2
0
def init(filename):
    """ Initialize aspects of the GL object rendering.  """
    global \
    trackball, flashlight, \
    models, buffers, \
    surface_shader, flat_shader

    # Initialize quaternions for the light and trackball
    flashlight = quat.for_rotation(0.0, vector(1.0, 0.0, 0.0))
    trackball = quat.for_rotation(0.0, vector(1.0, 0.0, 0.0))

    # Read the .OBJ/.PGM file.
    if filename[-3:] == 'obj':
        object = surface.from_obj(filename)
    elif filename[-3:] == 'pgm':
        object = surface.from_pgm(filename)
    else:
        print("Unknown file format. Should be a .obj or .pgm file.\n")
        quit()

    models = [object]
    buffers = [buffers_for_model(object)]

    # Set up the shader(s).
    surface_shader = init_shaders('shaders/vs-mesh.c', 'shaders/fs-mesh.c')

    # Set up OpenGL state.
    glEnable(GL_DEPTH_TEST)
Ejemplo n.º 3
0
def init(filename):
    """ Initialize aspects of the GL object rendering.  """
    global \
    trackball, flashlight, \
    vertex_buffer, normal_buffer, bary_buffer, floor_buffer, \
    floor_shader, mesh_shader

    # Initialize quaternions for the light and trackball
    flashlight = quat.for_rotation(0.0, vector(1.0, 0.0, 0.0))
    trackball = quat.for_rotation(0.0, vector(1.0, 0.0, 0.0))

    # Read the .OBJ file into VBOs.
    object.read(filename)
    vertices, normals = object.compile()
    barys = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0] * len(face.instances)
    floor = [
        -2.0, -0.01, -2.0, -2.0, -0.01, +2.0, +2.0, -0.01, +2.0, +2.0, -0.01,
        +2.0, +2.0, -0.01, -2.0, -2.0, -0.01, -2.0
    ]

    # Scene vertices, both floor and object.
    floor_buffer = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, floor_buffer)
    glBufferData(GL_ARRAY_BUFFER,
                 len(floor) * 4, (c_float * len(floor))(*floor),
                 GL_STATIC_DRAW)

    vertex_buffer = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer)
    glBufferData(GL_ARRAY_BUFFER,
                 len(vertices) * 4, (c_float * len(vertices))(*vertices),
                 GL_STATIC_DRAW)

    # Object normals.
    normal_buffer = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, normal_buffer)
    glBufferData(GL_ARRAY_BUFFER,
                 len(normals) * 4, (c_float * len(normals))(*normals),
                 GL_STATIC_DRAW)

    # Object face barycenter determination.
    bary_buffer = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, bary_buffer)
    glBufferData(GL_ARRAY_BUFFER,
                 len(barys) * 4, (c_float * len(barys))(*barys),
                 GL_STATIC_DRAW)

    # Set up the shaders.
    floor_shader = init_shaders('shaders/vs-floor.c', 'shaders/fs-floor.c')
    mesh_shader = init_shaders('shaders/vs-mesh.c', 'shaders/fs-mesh.c')

    # Set up OpenGL state.
    glEnable(GL_DEPTH_TEST)
Ejemplo n.º 4
0
def main(argc, argv):
    """ The main procedure, sets up GL and GLUT. """
    global trackball, facets

    trackball = quat.for_rotation(0.0, vector(1.0, 0.0, 0.0))
    facets = makeObject()

    glutInit(argv)
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH)
    glutInitWindowPosition(0, 20)
    glutInitWindowSize(360, 360)
    glutCreateWindow(b'object showcase - Press ESC to quit')
    initRendering()

    # Register interaction callbacks.
    glutKeyboardFunc(myKeyFunc)
    glutSpecialFunc(myArrowFunc)
    glutReshapeFunc(resizeWindow)
    glutDisplayFunc(drawScene)

    print()
    print('Press the arrow keys to rotate the object.')
    print('Press ESC to quit.\n')
    print()

    glutMainLoop()

    return 0
Ejemplo n.º 5
0
def init(filename):
    """ Initialize aspects of the GL object rendering.  """
    global \
    trackball, flashlight, \
    num_vertices, vertex_buffer, normal_buffer, \
    object, \
    surface_shader, flat_shader

    # Initialize quaternions for the light and trackball
    flashlight = quat.for_rotation(0.0, vector(1.0, 0.0, 0.0))
    trackball = quat.for_rotation(0.0, vector(1.0, 0.0, 0.0))

    # Read the .OBJ/.PGM file.
    if filename[-3:] == 'obj':
        object = surface.from_obj(filename)
    elif filename[-3:] == 'pgm':
        object = surface.from_pgm(filename)
    else:
        print("Unknown file format. Should be a .obj or .pgm file.\n")
        quit()

    # Get the information from the surface for the VBOs just below.
    num_vertices, vertices, normals = object.compile()

    # Surface vertices.
    vertex_buffer = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer)
    glBufferData(GL_ARRAY_BUFFER,
                 len(vertices) * 4, (c_float * len(vertices))(*vertices),
                 GL_STATIC_DRAW)

    # Surface normals.
    normal_buffer = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, normal_buffer)
    glBufferData(GL_ARRAY_BUFFER,
                 len(normals) * 4, (c_float * len(normals))(*normals),
                 GL_STATIC_DRAW)

    # Set up the shader(s).
    surface_shader = init_shaders('shaders/vs-surface.c',
                                  'shaders/fs-surface.c')
    flat_shader = init_shaders('shaders/vs-flat.c', 'shaders/fs-flat.c')

    # Set up OpenGL state.
    glEnable(GL_DEPTH_TEST)
Ejemplo n.º 6
0
def arrow(key, x, y):
    """ Handle a "special" keypress. """
    global trackball, flashlight

    x_axis = trackball.recip().rotate(vector(1.0, 0.0, 0.0))
    y_axis = trackball.recip().rotate(vector(0.0, 1.0, 0.0))

    # Apply an adjustment to the overall rotation.
    if key == GLUT_KEY_DOWN:
        flashlight = quat.for_rotation(pi / 12.0, x_axis) * flashlight
    if key == GLUT_KEY_UP:
        flashlight = quat.for_rotation(-pi / 12.0, x_axis) * flashlight
    if key == GLUT_KEY_LEFT:
        flashlight = quat.for_rotation(-pi / 12.0, y_axis) * flashlight
    if key == GLUT_KEY_RIGHT:
        flashlight = quat.for_rotation(pi / 12.0, y_axis) * flashlight

    # Redraw.
    glutPostRedisplay()
Ejemplo n.º 7
0
def arrow(key, x, y):
    """ Handle a "special" keypress. """
    global trackball,flashlight

    x_axis = trackball.recip().rotate(vector(1.0,0.0,0.0))
    y_axis = trackball.recip().rotate(vector(0.0,1.0,0.0))

    # Apply an adjustment to the overall rotation.
    if key == GLUT_KEY_DOWN:
        flashlight = quat.for_rotation( pi/12.0,x_axis) * flashlight
    if key == GLUT_KEY_UP:
        flashlight = quat.for_rotation(-pi/12.0,x_axis) * flashlight
    if key == GLUT_KEY_LEFT:
        flashlight = quat.for_rotation(-pi/12.0,y_axis) * flashlight
    if key == GLUT_KEY_RIGHT:
        flashlight = quat.for_rotation( pi/12.0,y_axis) * flashlight

    # Redraw.
    glutPostRedisplay()
Ejemplo n.º 8
0
def myArrowFunc(key, x, y):
    """ Handle a "special" keypress. """
    global trackball

    x_axis = vector(1.0, 0.0, 0.0)
    y_axis = vector(0.0, 1.0, 0.0)

    # Apply an adjustment to the overall rotation.
    if key == GLUT_KEY_DOWN:
        trackball = quat.for_rotation(pi / 12.0, x_axis) * trackball
    if key == GLUT_KEY_UP:
        trackball = quat.for_rotation(-pi / 12.0, x_axis) * trackball
    if key == GLUT_KEY_LEFT:
        trackball = quat.for_rotation(-pi / 12.0, y_axis) * trackball
    if key == GLUT_KEY_RIGHT:
        trackball = quat.for_rotation(pi / 12.0, y_axis) * trackball

    # Redraw.
    glutPostRedisplay()
Ejemplo n.º 9
0
def motion(x, y):
    global trackball, xStart, yStart
    xNow, yNow = world(x,y)
    dx = xNow-xStart
    dy = yNow-yStart
    axis = vector(-dy,dx,0.0).unit()
    angle = asin(min(sqrt(dx*dx+dy*dy),1.0))
    trackball = quat.for_rotation(angle,axis) * trackball
    xStart = xNow
    yStart = yNow
    glutPostRedisplay()
Ejemplo n.º 10
0
def main(argc, argv):
	global trackball, facets, cam, radius

	#read in input
	if argc is 3:
		shape = argv[1]
		smoothness = argv[2]
	elif argc is 2:
		shape = argv[1]
		smoothness = 10
	else:
		shape = "bunny"
		smoothness = 15

	#generate the .obj shapefile, and read it
	shapeFile = shape + str(smoothness) + ".obj"
	writeObj(str(shape), smoothness)
	s = surface()
	s.readObjFile(shapeFile)
	s.createHalfEdges()

	facets = s.faces
	radius = 2.0
	cam = camera()

	#you can even extend facets to see the shapes overlaid
	#facets.extend(readObjFile'torus.obj')

	trackball = quat.for_rotation(0.0,vector(1.0,0.0,0.0))

	glutInit(argv)
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH)
	glutInitWindowPosition(0, 20)
	glutInitWindowSize(width, height)
	glutCreateWindow( 'shapes - Press ESC to quit' )
	initRendering()

	# Register interaction callbacks.
	glutKeyboardFunc(myKeyFunc)
	glutSpecialFunc(myArrowFunc)
	glutMouseFunc(facetSelect)
	glutMotionFunc(sliceDir)
	glutReshapeFunc(resize)
	glutDisplayFunc(drawScene)

	print()
	print('Press the arrow keys rotate the object.')
	print('Press ESC to quit.\n')
	print()

	glutMainLoop()

	return 0
Ejemplo n.º 11
0
def motion(x, y):
    global trackball, xStart, yStart
    xNow = (x - width/2) * scale
    yNow = (height/2 - y) * scale
    change = point(xNow,yNow,0.0) - point(xStart,yStart,0.0)
    axis = vector(-change.dy,change.dx,0.0)
    sin_angle = change.norm()/radius
    sin_angle = max(min(sin_angle,1.0),-1.0) # clip
    angle = asin(sin_angle)
    trackball = quat.for_rotation(angle,axis) * trackball
    xStart,yStart = xNow, yNow

    glutPostRedisplay()
Ejemplo n.º 12
0
def motion(x, y):
    global trackball, xStart, yStart
    xNow = (x - width / 2) * scale
    yNow = (height / 2 - y) * scale
    change = point(xNow, yNow, 0.0) - point(xStart, yStart, 0.0)
    axis = vector(-change.dy, change.dx, 0.0)
    sin_angle = change.norm() / radius
    sin_angle = max(min(sin_angle, 1.0), -1.0)  # clip
    angle = asin(sin_angle)
    trackball = quat.for_rotation(angle, axis) * trackball
    xStart, yStart = xNow, yNow

    glutPostRedisplay()
Ejemplo n.º 13
0
def init(filename):
    """ Initialize aspects of the GL scene rendering.  """
    global trackball, flashlight, \
           shaders, shadowers, mesh, mesh0

    # Initialize quaternions for the light and trackball
    flashlight = quat.for_rotation(0.0,vector(1.0,0.0,0.0))
    trackball = quat.for_rotation(0.0,vector(1.0,0.0,0.0))

    # Read the .OBJ file into VBOs.
    mesh0 = object()
    mesh0.read(filename)
    vbo_ify(mesh0)
    mesh = mesh0

    # Set up the shaders.
    shaders = init_shaders('shaders/vs-mesh.c',
                           'shaders/fs-mesh.c')
    shadowers = init_shaders('shaders/vs-shadow.c',
                             'shaders/fs-shadow.c')
                 
    # Set up OpenGL state.
    glEnable (GL_DEPTH_TEST)
Ejemplo n.º 14
0
def arrow(key, x, y):
    """ Handle a "special" keypress. """
    global trackball, flashlight

    if key == GLUT_KEY_DOWN or key == GLUT_KEY_UP:
        axis = trackball.recip().rotate(vector(1.0, 0.0, 0.0))
    if key == GLUT_KEY_LEFT or key == GLUT_KEY_RIGHT:
        axis = trackball.recip().rotate(vector(0.0, 1.0, 0.0))
    if key == GLUT_KEY_LEFT or key == GLUT_KEY_DOWN:
        angle = -pi / 12.0
    if key == GLUT_KEY_RIGHT or key == GLUT_KEY_UP:
        angle = pi / 12.0

    if key in {GLUT_KEY_LEFT, GLUT_KEY_RIGHT, GLUT_KEY_UP, GLUT_KEY_DOWN}:
        flashlight = quat.for_rotation(angle, axis) * flashlight
        glutPostRedisplay()
Ejemplo n.º 15
0
def arrow(key, x, y):
    """ Handle a "special" keypress. """
    global trackball,flashlight

    if key == GLUT_KEY_DOWN or key == GLUT_KEY_UP:
        axis = trackball.recip().rotate(vector(1.0,0.0,0.0))
    if key == GLUT_KEY_LEFT or key == GLUT_KEY_RIGHT:
        axis = trackball.recip().rotate(vector(0.0,1.0,0.0))
    if key == GLUT_KEY_LEFT or key == GLUT_KEY_DOWN:
        angle = -pi/12.0
    if key == GLUT_KEY_RIGHT or key == GLUT_KEY_UP:
        angle = pi/12.0

    if key in {GLUT_KEY_LEFT,GLUT_KEY_RIGHT,GLUT_KEY_UP,GLUT_KEY_DOWN}:
        # Apply an adjustment to the position of the light.
        flashlight = quat.for_rotation(angle,axis) * flashlight
        # Redraw.
        glutPostRedisplay()
Ejemplo n.º 16
0
def motion(x, y):
    global trackball, xStart, yStart

    # Capture mouse's position.
    xNow, yNow = world(x, y)

    # Update trackball orientation based on movement.
    dx = xNow - xStart
    dy = yNow - yStart
    axis = vector(-dy, dx, 0.0).unit()
    angle = asin(min(sqrt(dx * dx + dy * dy), 1.0))
    trackball = quat.for_rotation(angle, axis) * trackball

    # Ready state for next mouse move.
    xStart = xNow
    yStart = yNow

    # Update window.
    glutPostRedisplay()
Ejemplo n.º 17
0
from constants import *
from airplane import *
from hud import *
from time import *



# global variables

width = 512 # window h
height = 512 # widow w
radius = 1.0 # window "radius" - how large things appear

xStart = 0
yStart = 0
trackball = quat.for_rotation(0.0,vector(1.0,0.0,0.0))
land = None
vertex_buffer = None
horizone_vertex_buffer = None
vertices = []

forward = 0.0
right = 0.0
up = 0.0

plane = airplane()
phud = hud(plane) # player's HUD

mouse_x = 0.0
mouse_y = 0.0
rudder = 0.0
Ejemplo n.º 18
0
from landscape import *

from constants import *
from airplane import *
from hud import *
from time import *

# global variables

width = 512  # window h
height = 512  # widow w
radius = 1.0  # window "radius" - how large things appear

xStart = 0
yStart = 0
trackball = quat.for_rotation(0.0, vector(1.0, 0.0, 0.0))
land = None
vertex_buffer = None
horizone_vertex_buffer = None
vertices = []

forward = 0.0
right = 0.0
up = 0.0

plane = airplane()
phud = hud(plane)  # player's HUD

mouse_x = 0.0
mouse_y = 0.0
rudder = 0.0
Ejemplo n.º 19
0
def init(argc, argv):
    """ Initialize aspects of the GL scene rendering.  """
    global trackball, flashlight, vertex_buffer, normal_buffer, color_buffer, colors, vertices, normals, surf, radius, phong_shader, shadow_shader, wireframe

    # initialize quaternions for the light and trackball
    flashlight = quat.for_rotation(0.0,vector(1.0,0.0,0.0))
    trackball = quat.for_rotation(0.0,vector(1.0,0.0,0.0))

    if argc < 2:
        print("No file specified. Use: python3.3 newview.py <PATH TO FILE> <Number of subdivisions> <flags> ")
        vertices = []
        normals = []
        colors = []
        shadows = []

    else:
        
        if argc >= 3:
            subdivisions = int(argv[2])
        else:
            subdivisions = 1

        if argc >= 4:
            if argv[3] == "-w":
                wireframe = True
            
        
        filename = argv[1]
        
        # read the .OBJ file into VBOs
        if(filename != None):
            print("Subdividing " + str(subdivisions) + " times.")
            surf.load(filename)
            if subdivisions > 0:
                for i in range(subdivisions):
                    surf = subdivide(surf)
                
            vertices,normals,colors = surf.compile()
        
        else:
            print("No file! \n")
            vertices = []
            normals = []
            colors = []
            shadows =  []
        
    vertex_buffer = glGenBuffers(1)
    glBindBuffer (GL_ARRAY_BUFFER, vertex_buffer)
    glBufferData (GL_ARRAY_BUFFER, len(vertices)*4, 
                  (c_float*len(vertices))(*vertices), GL_STATIC_DRAW)

    normal_buffer = glGenBuffers(1)
    glBindBuffer (GL_ARRAY_BUFFER, normal_buffer)
    glBufferData (GL_ARRAY_BUFFER, len(normals)*4, 
                  (c_float*len(normals))(*normals), GL_STATIC_DRAW)

    color_buffer = glGenBuffers(1)
    glBindBuffer (GL_ARRAY_BUFFER, color_buffer)
    glBufferData (GL_ARRAY_BUFFER, len(colors)*4, 
                  (c_float*len(colors))(*colors), GL_STATIC_DRAW)
    
    
    radius = surf.radius

    # set up the object shaders
    phong_shader = init_shaders('vs-phong-interp.c',
                 'fs-phong-interp.c')
    shadow_shader = init_shaders('vs-shadow.c',
                 'fs-shadow.c')
    

    glEnable (GL_DEPTH_TEST)