def main():
    # initialize glfw
    if not glfw.init():
        return

    w_width, w_height = 1280, 720
    aspect_ratio = w_width / w_height

    window = glfw.create_window(w_width, w_height, "My OpenGL window", None, None)

    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)
    glfw.set_window_size_callback(window, window_resize)
    glfw.set_key_callback(window, key_callback)

    #        positions        texture_coords
    cube = [-0.5, -0.5, 0.5, 0.0, 0.0,
            0.5, -0.5, 0.5, 1.0, 0.0,
            0.5, 0.5, 0.5, 1.0, 1.0,
            -0.5, 0.5, 0.5, 0.0, 1.0,

            -0.5, -0.5, -0.5, 0.0, 0.0,
            0.5, -0.5, -0.5, 1.0, 0.0,
            0.5, 0.5, -0.5, 1.0, 1.0,
            -0.5, 0.5, -0.5, 0.0, 1.0,

            0.5, -0.5, -0.5, 0.0, 0.0,
            0.5, 0.5, -0.5, 1.0, 0.0,
            0.5, 0.5, 0.5, 1.0, 1.0,
            0.5, -0.5, 0.5, 0.0, 1.0,

            -0.5, 0.5, -0.5, 0.0, 0.0,
            -0.5, -0.5, -0.5, 1.0, 0.0,
            -0.5, -0.5, 0.5, 1.0, 1.0,
            -0.5, 0.5, 0.5, 0.0, 1.0,

            -0.5, -0.5, -0.5, 0.0, 0.0,
            0.5, -0.5, -0.5, 1.0, 0.0,
            0.5, -0.5, 0.5, 1.0, 1.0,
            -0.5, -0.5, 0.5, 0.0, 1.0,

            0.5, 0.5, -0.5, 0.0, 0.0,
            -0.5, 0.5, -0.5, 1.0, 0.0,
            -0.5, 0.5, 0.5, 1.0, 1.0,
            0.5, 0.5, 0.5, 0.0, 1.0]

    cube = numpy.array(cube, dtype=numpy.float32)

    indices = [0, 1, 2, 2, 3, 0,
               4, 5, 6, 6, 7, 4,
               8, 9, 10, 10, 11, 8,
               12, 13, 14, 14, 15, 12,
               16, 17, 18, 18, 19, 16,
               20, 21, 22, 22, 23, 20]

    indices = numpy.array(indices, dtype=numpy.uint32)

    vertex_shader = """
    #version 330
    in layout(location = 0) vec3 position;
    in layout(location = 1) vec2 texture_cords;

    uniform mat4 model;
    uniform mat4 view;
    uniform mat4 proj;

    out vec2 textures;

    void main()
    {
        gl_Position =  proj * view * model * vec4(position, 1.0f);
        textures = texture_cords;
    }
    """

    fragment_shader = """
    #version 330
    in vec2 textures;

    out vec4 color;
    uniform sampler2D tex_sampler;

    void main()
    {
        color = texture(tex_sampler, textures);
    }
    """
    shader = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
                                              OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER))

    VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, cube.itemsize * len(cube), cube, GL_STATIC_DRAW)

    EBO = glGenBuffers(1)
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.itemsize * len(indices), indices, GL_STATIC_DRAW)

    # position
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube.itemsize * 5, ctypes.c_void_p(0))
    glEnableVertexAttribArray(0)

    # textures
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, cube.itemsize * 5, ctypes.c_void_p(12))
    glEnableVertexAttribArray(1)

    crate = TextureLoader.load_texture("res/crate.jpg")
    metal = TextureLoader.load_texture("res/metal.jpg")
    brick = TextureLoader.load_texture("res/brick.jpg")

    glUseProgram(shader)

    glClearColor(0.2, 0.3, 0.2, 1.0)
    glEnable(GL_DEPTH_TEST)

    projection = matrix44.create_perspective_projection_matrix(45.0, aspect_ratio, 0.1, 100.0)

    model_loc = glGetUniformLocation(shader, "model")
    view_loc = glGetUniformLocation(shader, "view")
    proj_loc = glGetUniformLocation(shader, "proj")

    cube_positions = [(0.0, 0.0, 0.0), (2.0, 2.0, -5.0), (1.5, -1.2, -2.5), (8.8, -2.0, -12.3), (-2.0, 2.0, -5.5),
                      (-4.0, 2.0, -3.0)]

    glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection)

    while not glfw.window_should_close(window):
        glfw.poll_events()
        do_movement()

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        view = cam.get_view_matrix()
        glUniformMatrix4fv(view_loc, 1, GL_FALSE, view)

        for i in range(len(cube_positions)):
            model = matrix44.create_from_translation(cube_positions[i])
            glUniformMatrix4fv(model_loc, 1, GL_FALSE, model)
            if i < 2:
                glBindTexture(GL_TEXTURE_2D, crate)
            elif i == 2 or i == 3:
                glBindTexture(GL_TEXTURE_2D, metal)
            else:
                glBindTexture(GL_TEXTURE_2D, brick)

            glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, None)

        glfw.swap_buffers(window)

    glfw.terminate()
Esempio n. 2
0
def main():

    # comenzar glfw
    if not glfw.init():
        return

    aspect_ratio = wwidth / wheight
    #creamos la ventana
    window = glfw.create_window(wwidth, wheight, "ProyectoGraficas", None,
                                None)

    #terminar ventana
    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)

    glfw.set_window_size_callback(window, window_resize)

    glfw.set_key_callback(window, key_callback)

    glfw.set_cursor_pos_callback(window, mouse_callback)

    glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED)

    #carbamos el primer ObjLoader
    casa = ObjLoader()
    #buscamos el objeto en nuestras carpetas
    casa.load_model("obj/casa2.obj")
    #buscamos la textura en nuestras carpetas
    casa_tex = TextureLoader.load_texture("obj/pared.jpg")
    #calculamos
    casa_texture_offset = len(casa.vertex_index) * 12

    #cargamos el segundo objeto
    monster = ObjLoader()
    #buscamos el obj en nuestras carpetas
    monster.load_model("obj/monster.obj")
    #buscamos la textura en nuestras carpetas
    monster_tex = TextureLoader.load_texture("obj/monster.jpg")
    #calculamos
    monster_texture_offset = len(monster.vertex_index) * 12

    #obtenemos los shaders de nuestras carpetas.
    generic_shader = ShaderLoader.compile_shader(
        "shaders/generic_vertex_shader.vs",
        "shaders/generic_fragment_shader.fs")

    #------------------------------------casa--------------------------------------------------------------------------#
    #generamos nestras variaml
    casavao = glGenVertexArrays(1)

    glBindVertexArray(casavao)

    casavbo = glGenBuffers(1)

    glBindBuffer(GL_ARRAY_BUFFER, casavbo)

    glBufferData(GL_ARRAY_BUFFER, casa.model.itemsize * len(casa.model),
                 casa.model, GL_STATIC_DRAW)

    #posicion
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, casa.model.itemsize * 3,
                          ctypes.c_void_p(0))
    glEnableVertexAttribArray(0)

    #Texturas
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, casa.model.itemsize * 2,
                          ctypes.c_void_p(casa_texture_offset))
    glEnableVertexAttribArray(1)
    glBindVertexArray(0)

    #--------------------------------------------------------------------------------------------------------------------

    #-------------------------------------------------------monstruo------------------------------------------------------#
    monster_vao = glGenVertexArrays(1)

    glBindVertexArray(monster_vao)

    monster_vbo = glGenBuffers(1)

    glBindBuffer(GL_ARRAY_BUFFER, monster_vbo)

    glBufferData(GL_ARRAY_BUFFER, monster.model.itemsize * len(monster.model),
                 monster.model, GL_STATIC_DRAW)

    #position
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, monster.model.itemsize * 3,
                          ctypes.c_void_p(0))

    glEnableVertexAttribArray(0)
    #textures
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, monster.model.itemsize * 2,
                          ctypes.c_void_p(monster_texture_offset))
    glEnableVertexAttribArray(1)
    glBindVertexArray(0)
    #-----------------------------------------------------------------------------------------------------------------------#

    #colocamos el color negro

    glClearColor(0, 0, 0, 0)
    glEnable(GL_DEPTH_TEST)

    projection = matrix44.create_perspective_projection_matrix(
        45.0, aspect_ratio, 0.1, 100.0)

    #traslacion del vector a la casa2
    #colocar en el lugar
    casaModelo = matrix44.create_from_translation(Vector3([0.0, 0.0, -3.0]))

    #traslacion en el vectro3 al monstruo
    #colocar en el lugar
    monster_model = matrix44.create_from_translation(Vector3([0.0, 0.0,
                                                              -10.0]))

    #shaders
    glUseProgram(generic_shader)

    model_loc = glGetUniformLocation(generic_shader, "model")

    view_loc = glGetUniformLocation(generic_shader, "view")

    proj_loc = glGetUniformLocation(generic_shader, "proj")

    glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection)

    #mientras la ventana
    while not glfw.window_should_close(window):

        glfw.poll_events()

        do_movement()
        #colocar la ventada de un color especifico
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        view = cam.get_view_matrix()

        glUniformMatrix4fv(view_loc, 1, GL_FALSE, view)

        #rotaciones
        rot_y = Matrix44.from_y_rotation(glfw.get_time() * 0.5)

        #--------------------------------------casa--------------------------------------#
        glBindVertexArray(casavao)
        glBindTexture(GL_TEXTURE_2D, casa_tex)
        glUniformMatrix4fv(model_loc, 1, GL_FALSE, casaModelo)
        glDrawArrays(GL_TRIANGLES, 0, len(casa.vertex_index))
        glBindVertexArray(0)
        #--------------------------------------------------------------------------------#

        #---------------------------------------monstruo----------------------------------#
        glBindVertexArray(monster_vao)
        glBindTexture(GL_TEXTURE_2D, monster_tex)
        glUniformMatrix4fv(model_loc, 1, GL_FALSE, monster_model)
        glDrawArrays(GL_TRIANGLES, 0, len(monster.vertex_index))
        glBindVertexArray(0)
        #--------------------------------------------------------------------------------------

        #buffer de la ventana
        glfw.swap_buffers(window)

    #finalizamos
    glfw.terminate()
Esempio n. 3
0
def main():
    # initialize glfw
    if not glfw.init():
        return

    w_width, w_height = 1280, 720
    aspect_ratio = w_width / w_height

    window = glfw.create_window(w_width, w_height, "My OpenGL window", None, None)

    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)
    glfw.set_window_size_callback(window, window_resize)

    #        positions        texture_coords
    cube = [-0.5, -0.5, 0.5, 0.0, 0.0,
            0.5, -0.5, 0.5, 1.0, 0.0,
            0.5, 0.5, 0.5, 1.0, 1.0,
            -0.5, 0.5, 0.5, 0.0, 1.0,

            -0.5, -0.5, -0.5, 0.0, 0.0,
            0.5, -0.5, -0.5, 1.0, 0.0,
            0.5, 0.5, -0.5, 1.0, 1.0,
            -0.5, 0.5, -0.5, 0.0, 1.0,

            0.5, -0.5, -0.5, 0.0, 0.0,
            0.5, 0.5, -0.5, 1.0, 0.0,
            0.5, 0.5, 0.5, 1.0, 1.0,
            0.5, -0.5, 0.5, 0.0, 1.0,

            -0.5, 0.5, -0.5, 0.0, 0.0,
            -0.5, -0.5, -0.5, 1.0, 0.0,
            -0.5, -0.5, 0.5, 1.0, 1.0,
            -0.5, 0.5, 0.5, 0.0, 1.0,

            -0.5, -0.5, -0.5, 0.0, 0.0,
            0.5, -0.5, -0.5, 1.0, 0.0,
            0.5, -0.5, 0.5, 1.0, 1.0,
            -0.5, -0.5, 0.5, 0.0, 1.0,

            0.5, 0.5, -0.5, 0.0, 0.0,
            -0.5, 0.5, -0.5, 1.0, 0.0,
            -0.5, 0.5, 0.5, 1.0, 1.0,
            0.5, 0.5, 0.5, 0.0, 1.0]

    cube = numpy.array(cube, dtype=numpy.float32)

    indices = [0, 1, 2, 2, 3, 0,
               4, 5, 6, 6, 7, 4,
               8, 9, 10, 10, 11, 8,
               12, 13, 14, 14, 15, 12,
               16, 17, 18, 18, 19, 16,
               20, 21, 22, 22, 23, 20]

    indices = numpy.array(indices, dtype=numpy.uint32)

    vertex_shader = """
    #version 330
    in layout(location = 0) vec3 position;
    in layout(location = 1) vec2 texture_cords;
    uniform mat4 vp;
    uniform mat4 model;
    out vec2 textures;
    void main()
    {
        gl_Position =  vp * model * vec4(position, 1.0f);
        textures = texture_cords;
    }
    """

    fragment_shader = """
    #version 330
    in vec2 textures;
    out vec4 color;
    uniform sampler2D tex_sampler;
    void main()
    {
        color = texture(tex_sampler, textures);
    }
    """
    shader = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
                                              OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER))

    VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, cube.itemsize * len(cube), cube, GL_STATIC_DRAW)

    EBO = glGenBuffers(1)
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.itemsize * len(indices), indices, GL_STATIC_DRAW)

    # position
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube.itemsize * 5, ctypes.c_void_p(0))
    glEnableVertexAttribArray(0)

    # textures
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, cube.itemsize * 5, ctypes.c_void_p(12))
    glEnableVertexAttribArray(1)

    crate = TextureLoader.load_texture("resources/images/planks_brown_10_diff_1k.jpg")
    metal = TextureLoader.load_texture("resources/images/green_metal_rust_diff_1k.jpg")
    brick = TextureLoader.load_texture("resources/images/castle_brick_07_diff_1k.jpg")

    glUseProgram(shader)

    glClearColor(0.5, 0.1, 0.2, 1.0)
    glEnable(GL_DEPTH_TEST)

    view = matrix44.create_from_translation(Vector3([0.0, 0.0, -4.0]))
    projection = matrix44.create_perspective_projection_matrix(45.0, aspect_ratio, 0.1, 100.0)

    vp = matrix44.multiply(view, projection)

    vp_loc = glGetUniformLocation(shader, "vp")
    model_loc = glGetUniformLocation(shader, "model")

    # cube_positions = [(1.0, 0.0, 0.0), (2.0, 5.0, -15.0), (-1.5, -1.2, -2.5), (-8.8, -2.0, -12.3)]
    cube_positions = [(1.0, 0.0, 0.0), (2.0, 5.0, -15.0), (-1.5, -1.2, -2.5), (-8.8, -2.0, -12.3), (-2.0, 2.0, -5.5),
                      (-4.0, 2.0, -3.0)]
    # cube_positions = [(-1.5, 1.0, -0.5), (0.0, 1.0, -0.5), (1.5, 1.0, -0.5), (-1.5, -1.0, -0.5), (0.0, -1.0, -0.5), (1.5, -1.0, -0.5)]

    glUniformMatrix4fv(vp_loc, 1, GL_FALSE, vp)

    while not glfw.window_should_close(window):
        glfw.poll_events()

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        time = glfw.get_time()

        rot_x = matrix44.create_from_x_rotation(sin(time) * 2)
        rot_y = matrix44.create_from_y_rotation(time * 0.5)
        rot_z = matrix44.create_from_z_rotation(time)

        for i in range(len(cube_positions)):
            model = matrix44.create_from_translation(cube_positions[i])
            if i < 2:
                glBindTexture(GL_TEXTURE_2D, crate)
                rotX = matrix44.multiply(rot_x, model)
                glUniformMatrix4fv(model_loc, 1, GL_FALSE, rotX)
            elif i == 2 or i == 3:
                glBindTexture(GL_TEXTURE_2D, metal)
                rotY = matrix44.multiply(rot_y, model)
                glUniformMatrix4fv(model_loc, 1, GL_FALSE, rotY)
            else:
                glBindTexture(GL_TEXTURE_2D, brick)
                rotZ = matrix44.multiply(rot_z, model)
                glUniformMatrix4fv(model_loc, 1, GL_FALSE, rotZ)

            glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, None)

        glfw.swap_buffers(window)

    glfw.terminate()
Esempio n. 4
0
def main():
    if not glfw.init():
        return

    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 1)
    glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)
    glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
    w_width, w_height = 1440, 900

    window = glfw.create_window(w_width, w_height, "My OpenGL Window", None,
                                None)
    #window = glfw.create_window(w_width,w_height,"My OpenGL Window", glfw.get_primary_monitor(), None)

    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)
    glfw.set_key_callback(window, key_callback)
    glfw.set_cursor_pos_callback(window, mouse_callback)
    glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED)

    monkey_obj = ObjLoader()
    monkey_obj.load_model('monkey.obj')
    monkey_tex = TextureLoader.load_texture("res/monkey.png")
    monkey_tex_offset = len(monkey_obj.vertex_index) * 12
    monkey_norm_offset = (monkey_tex_offset +
                          len(monkey_obj.texture_index) * 8)

    sphere_obj = ObjLoader()
    sphere_obj.load_model('sphere2.obj')
    sphere_tex = TextureLoader.load_texture("res/yellow.png")
    sphere_tex_offset = len(sphere_obj.vertex_index) * 12
    sphere_norm_offset = (sphere_tex_offset +
                          len(sphere_obj.texture_index) * 8)

    shader = ShaderLoader.compile_shader("Shaders/vert.vs", "Shaders/frag.fs")

    sphere_vao = glGenVertexArrays(1)
    glBindVertexArray(sphere_vao)
    sphere_VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, sphere_VBO)
    glBufferData(GL_ARRAY_BUFFER,
                 sphere_obj.model.itemsize * len(sphere_obj.model),
                 sphere_obj.model, GL_STATIC_DRAW)
    #Position
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
                          sphere_obj.model.itemsize * 3, ctypes.c_void_p(0))
    glEnableVertexAttribArray(0)
    #TextureCoords
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
                          sphere_obj.model.itemsize * 2,
                          ctypes.c_void_p(sphere_tex_offset))
    glEnableVertexAttribArray(1)
    #Normals
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE,
                          sphere_obj.model.itemsize * 3,
                          ctypes.c_void_p(sphere_norm_offset))
    glEnableVertexAttribArray(2)
    glBindVertexArray(0)

    monkey_vao = glGenVertexArrays(1)
    glBindVertexArray(monkey_vao)
    monkey_VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, monkey_VBO)
    glBufferData(GL_ARRAY_BUFFER,
                 monkey_obj.model.itemsize * len(monkey_obj.model),
                 monkey_obj.model, GL_STATIC_DRAW)
    #Position
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
                          monkey_obj.model.itemsize * 3, ctypes.c_void_p(0))
    glEnableVertexAttribArray(0)
    #TextureCoords
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
                          monkey_obj.model.itemsize * 2,
                          ctypes.c_void_p(monkey_tex_offset))
    glEnableVertexAttribArray(1)
    #Normals
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE,
                          monkey_obj.model.itemsize * 3,
                          ctypes.c_void_p(monkey_norm_offset))
    glEnableVertexAttribArray(2)
    glBindVertexArray(0)

    glClearColor(0.2, 0.3, 0.2, 1.0)
    glEnable(GL_DEPTH_TEST)

    projection = pyrr.matrix44.create_perspective_projection_matrix(
        60.0, w_width / w_height, 0.1, 100.0)
    sphere_model = matrix44.create_from_translation(Vector3([-4.0, 0.0, -3.0]))
    monkey_model = matrix44.create_from_translation(Vector3([0.0, 0.0, -3.0]))

    glUseProgram(shader)
    model_loc = glGetUniformLocation(shader, "model")
    view_loc = glGetUniformLocation(shader, "view")
    proj_loc = glGetUniformLocation(shader, "proj")
    light_loc = glGetUniformLocation(shader, "light")

    glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection)

    while not glfw.window_should_close(window):
        glfw.poll_events()
        do_movement()

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        view = cam.get_view_matrix()
        glUniformMatrix4fv(view_loc, 1, GL_FALSE, view)

        light_y = Matrix44.from_y_rotation(0.5)
        rot_y = Matrix44.from_y_rotation(glfw.get_time() * 0.5)

        glUniformMatrix4fv(light_loc, 1, GL_FALSE, light_y)

        glBindVertexArray(sphere_vao)
        glBindTexture(GL_TEXTURE_2D, sphere_tex)
        glUniformMatrix4fv(model_loc, 1, GL_FALSE,
                           Matrix44(sphere_model) * rot_y)
        glDrawArrays(GL_TRIANGLES, 0, len(sphere_obj.vertex_index))
        glBindVertexArray(0)

        glBindVertexArray(monkey_vao)
        glBindTexture(GL_TEXTURE_2D, monkey_tex)
        glUniformMatrix4fv(model_loc, 1, GL_FALSE, monkey_model)
        glDrawArrays(GL_TRIANGLES, 0, len(monkey_obj.vertex_index))
        glBindVertexArray(0)

        glfw.swap_buffers(window)

    glfw.terminate()
Esempio n. 5
0
def main():
    if not opengl_init():
        return

    OpenglInit()

    load_gedung("itb_coordinate.txt")

    # Enable key events
    glfw.set_input_mode(window,glfw.STICKY_KEYS,GL_TRUE)
    glfw.set_cursor_pos(window, 1024/2, 768/2)

    # Set opengl clear color to something other than red (color used by the fragment shader)
    glClearColor(0.0,0.0,0.0,0.0)

    # Enable depth test
    glEnable(GL_DEPTH_TEST)
    # Accept fragment if it closer to the camera than the former one
    glDepthFunc(GL_LESS);
    glEnable(GL_CULL_FACE)

    vertex_array_id = glGenVertexArrays(1)
    glBindVertexArray( vertex_array_id )

    program_id = common.LoadShaders( ".\\shaders\\Tutorial6\\TransformVertexShader.vertexshader",
        ".\\shaders\\Tutorial6\\TextureFragmentShader.fragmentshader" )

    vertex_data = createAllBuilding()

    # Two UV coordinatesfor each vertex. They were created withe Blender.
    uv_data = createUVData()

    # Get a handle for our "MVP" uniform
    matrix_id = glGetUniformLocation(program_id, "MVP");

    texture = []
    texture_id = []

    texture_id.append(glGetUniformLocation(program_id, "myTextureSampler"))
    texture_id.append(glGetUniformLocation(program_id, "myTextureSampler2"))

    tex1 = TextureLoader.load_texture("res/crate.jpg")
    tex2 = TextureLoader.load_texture("res/metal.jpg")
    # Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
    # A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices



    vertex_buffer = glGenBuffers(1);
    array_type = GLfloat * len(vertex_data)
    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer)
    glBufferData(GL_ARRAY_BUFFER, len(vertex_data) * 4, array_type(*vertex_data), GL_STATIC_DRAW)

    uv_buffer = glGenBuffers(1);
    array_type = GLfloat * len(uv_data)
    glBindBuffer(GL_ARRAY_BUFFER, uv_buffer)
    glBufferData(GL_ARRAY_BUFFER, len(uv_data) * 4, array_type(*uv_data), GL_STATIC_DRAW)

    # vsync and glfw do not play nice.  when vsync is enabled mouse movement is jittery.
    common.disable_vsyc()

    glUseProgram(program_id)

    #1rst attribute buffer : vertices
    glEnableVertexAttribArray(0)
    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
    glVertexAttribPointer(
        0,                  # attribute 0. No particular reason for 0, but must match the layout in the shader.
        3,                  # len(vertex_data)
        GL_FLOAT,           # type
        GL_FALSE,           # ormalized?
        0,                  # stride
        null                # array buffer offset (c_type == void*)
        )

    # 2nd attribute buffer : colors
    glEnableVertexAttribArray(1)
    glBindBuffer(GL_ARRAY_BUFFER, uv_buffer);
    glVertexAttribPointer(
        1,                  # attribute 1. No particular reason for 1, but must match the layout in the shader.
        2,                  # len(vertex_data)
        GL_FLOAT,           # type
        GL_FALSE,           # ormalized?
        0,                  # stride
        null                # array buffer offset (c_type == void*)
        )

    glActiveTexture(GL_TEXTURE0);

    currentTexture = []
    for i in range(1, jumlah_tempat+1):
        currentTexture.append(TextureLoader.load_texture("img/building"+str(i)+".jpg"))

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

        controls.computeMatricesFromInputs(window)
        ProjectionMatrix = controls.getProjectionMatrix();
        ViewMatrix = controls.getViewMatrix();
        ModelMatrix = mat4.identity();
        mvp = ProjectionMatrix * ViewMatrix * ModelMatrix;

        Display()

        ##################################################################### SET TEXTURE 1

        # Send our transformation to the currently bound shader,
        # in the "MVP" uniform
        # draws Aula barat, timur ; CC barat, timur

        # glBindTexture(GL_TEXTURE_2D, tex1);
        # glUniformMatrix4fv(matrix_id, 1, GL_FALSE,mvp.data)

        # # Draw the shapes
        # glDrawArrays(GL_TRIANGLES, 0, 12*3*4) #3 indices starting at 0 -> 1 triangle


        # ####################################################################### SET TEXTURE 2
        # #draws 4 labtek kembar + perpus, pau

        # glBindTexture(GL_TEXTURE_2D, tex2);
        # glUniformMatrix4fv(matrix_id, 1, GL_FALSE, mvp.data)

        # # Draw the shapes
        # glDrawArrays(GL_TRIANGLES, 12*3*4, 12*3*6) #3 indices starting at 0 -> 1 triangle
        # print (jumlah_tempat)

        for i in range(0, jumlah_tempat):
            # Send our transformation to the currently bound shader,
            # in the "MVP" uniform
            # draws Aula barat, timur ; CC barat, timur

            glBindTexture(GL_TEXTURE_2D, currentTexture[i]);
            glUniformMatrix4fv(matrix_id, 1, GL_FALSE,mvp.data)

            # # Draw the shapes
            glDrawArrays(GL_TRIANGLES, 12*3*i, 12*3) #3 indices starting at 0 -> 1 triangle

        ################################################### FINALIZE

        # Swap front and back buffers
        glfw.swap_buffers(window)

        # Poll for and process events
        glfw.poll_events()

    # Not strictly necessary because we only have
    glDisableVertexAttribArray(0)
    glDisableVertexAttribArray(1)
    # !Note braces around vertex_buffer and uv_buffer.
    # glDeleteBuffers expects a list of buffers to delete
    glDeleteBuffers(1, [vertex_buffer])
    glDeleteBuffers(1, [uv_buffer])
    glDeleteProgram(program_id)
    glDeleteTextures([texture_id])
    glDeleteVertexArrays(1, [vertex_array_id])

    glfw.terminate()
Esempio n. 6
0
def main():
    # initialize glfw
    if not glfw.init():
        return

    w_width, w_height = 1920, 1080
    aspect_ratio = w_width / w_height

    window = glfw.create_window(w_width, w_height, "My OpenGL window", None,
                                None)
    #window = glfw.create_window(w_width, w_height, "My OpenGL window", glfw.get_primary_monitor(), None)

    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)
    glfw.set_window_size_callback(window, window_resize)
    glfw.set_key_callback(window, key_callback)
    glfw.set_cursor_pos_callback(window, mouse_callback)
    glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED)

    #        positions        texture_coords
    cube = [
        -0.5, -0.5, 0.5, 0.0, 0.0, 0.5, -0.5, 0.5, 1.0, 0.0, 0.5, 0.5, 0.5,
        1.0, 1.0, -0.5, 0.5, 0.5, 0.0, 1.0, -0.5, -0.5, -0.5, 0.0, 0.0, 0.5,
        -0.5, -0.5, 1.0, 0.0, 0.5, 0.5, -0.5, 1.0, 1.0, -0.5, 0.5, -0.5, 0.0,
        1.0, 0.5, -0.5, -0.5, 0.0, 0.0, 0.5, 0.5, -0.5, 1.0, 0.0, 0.5, 0.5,
        0.5, 1.0, 1.0, 0.5, -0.5, 0.5, 0.0, 1.0, -0.5, 0.5, -0.5, 0.0, 0.0,
        -0.5, -0.5, -0.5, 1.0, 0.0, -0.5, -0.5, 0.5, 1.0, 1.0, -0.5, 0.5, 0.5,
        0.0, 1.0, -0.5, -0.5, -0.5, 0.0, 0.0, 0.5, -0.5, -0.5, 1.0, 0.0, 0.5,
        -0.5, 0.5, 1.0, 1.0, -0.5, -0.5, 0.5, 0.0, 1.0, 0.5, 0.5, -0.5, 0.0,
        0.0, -0.5, 0.5, -0.5, 1.0, 0.0, -0.5, 0.5, 0.5, 1.0, 1.0, 0.5, 0.5,
        0.5, 0.0, 1.0
    ]

    cube = numpy.array(cube, dtype=numpy.float32)

    indices = [
        0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4, 8, 9, 10, 10, 11, 8, 12, 13, 14,
        14, 15, 12, 16, 17, 18, 18, 19, 16, 20, 21, 22, 22, 23, 20
    ]

    indices = numpy.array(indices, dtype=numpy.uint32)

    vertex_shader = """
    #version 330
    in layout(location = 0) vec3 position;
    in layout(location = 1) vec2 texture_cords;
    in layout(location = 2) vec3 offset;
    uniform mat4 model;
    uniform mat4 view;
    uniform mat4 proj;
    out vec2 textures;
    void main()
    {
        vec3 final_pos = vec3(position.x + offset.x, position.y + offset.y, position.z + offset.z);
        gl_Position =  proj * view * model * vec4(final_pos, 1.0f);
        textures = texture_cords;
    }
    """

    fragment_shader = """
    #version 330
    in vec2 textures;
    out vec4 color;
    uniform sampler2D tex_sampler;
    void main()
    {
        color = texture(tex_sampler, textures);
    }
    """
    shader = OpenGL.GL.shaders.compileProgram(
        OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
        OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER))

    VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, cube.itemsize * len(cube), cube,
                 GL_STATIC_DRAW)

    EBO = glGenBuffers(1)
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.itemsize * len(indices),
                 indices, GL_STATIC_DRAW)

    # position
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube.itemsize * 5,
                          ctypes.c_void_p(0))
    glEnableVertexAttribArray(0)

    # textures
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, cube.itemsize * 5,
                          ctypes.c_void_p(12))
    glEnableVertexAttribArray(1)

    # instances
    instance_array = []
    offset = 1

    for z in range(0, 100, 2):
        for y in range(0, 100, 2):
            for x in range(0, 100, 2):
                translation = Vector3([0.0, 0.0, 0.0])
                translation.x = x + offset
                translation.y = y + offset
                translation.z = z + offset
                instance_array.append(translation)

    instance_array = numpy.array(instance_array, numpy.float32).flatten()

    instanceVBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, instanceVBO)
    glBufferData(GL_ARRAY_BUFFER,
                 instance_array.itemsize * len(instance_array), instance_array,
                 GL_STATIC_DRAW)

    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
    glEnableVertexAttribArray(2)
    glVertexAttribDivisor(2, 1)

    crate = TextureLoader.load_texture(
        "resources/images/planks_brown_10_diff_1k.jpg")

    glUseProgram(shader)

    glClearColor(0.2, 0.3, 0.2, 1.0)
    glEnable(GL_DEPTH_TEST)

    model = matrix44.create_from_translation(Vector3([-14.0, -8.0, 0.0]))
    projection = matrix44.create_perspective_projection_matrix(
        45.0, aspect_ratio, 0.1, 100.0)

    model_loc = glGetUniformLocation(shader, "model")
    view_loc = glGetUniformLocation(shader, "view")
    proj_loc = glGetUniformLocation(shader, "proj")

    glUniformMatrix4fv(model_loc, 1, GL_FALSE, model)
    glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection)

    while not glfw.window_should_close(window):
        glfw.poll_events()
        do_movement()

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        view = cam.get_view_matrix()
        glUniformMatrix4fv(view_loc, 1, GL_FALSE, view)

        glDrawElementsInstanced(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT,
                                None, 125000)

        glfw.swap_buffers(window)

    glfw.terminate()
Esempio n. 7
0
def main():
    # initialize glfw
    if not glfw.init():
        return

    w_width, w_height = 1920, 1080
    aspect_ratio = w_width / w_height

    window = glfw.create_window(w_width, w_height, "My OpenGL window", None,
                                None)

    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)
    glfw.set_window_size_callback(window, window_resize)
    glfw.set_key_callback(window, key_callback)
    glfw.set_cursor_pos_callback(window, mouse_callback)
    glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED)

    cube = ObjLoader()
    cube.load_model("res/cube/cube.obj")
    cube_shader = ShaderLoader.compile_shader("shaders/video_09_cube.vs",
                                              "shaders/video_09_cube.fs")
    cube_tex = TextureLoader.load_texture("res/cube/cube_texture.jpg")
    cube_texture_offset = len(cube.vertex_index) * 12

    monkey = ObjLoader()
    monkey.load_model("res/monkey/monkey.obj")
    monkey_shader = ShaderLoader.compile_shader("shaders/video_09_monkey.vs",
                                                "shaders/video_09_monkey.fs")
    monkey_tex = TextureLoader.load_texture("res/monkey/monkey.jpg")
    monkey_texture_offset = len(monkey.vertex_index) * 12

    monster = ObjLoader()
    monster.load_model("res/monster/monster.obj")
    monster_shader = ShaderLoader.compile_shader(
        "shaders/video_09_monster.vs", "shaders/video_09_monster.fs")
    monster_tex = TextureLoader.load_texture("res/monster/monster.jpg")
    monster_texture_offset = len(monster.vertex_index) * 12

    cube_vao = glGenVertexArrays(1)
    glBindVertexArray(cube_vao)
    cube_vbo = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, cube_vbo)
    glBufferData(GL_ARRAY_BUFFER, cube.model.itemsize * len(cube.model),
                 cube.model, GL_STATIC_DRAW)
    #position
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube.model.itemsize * 3,
                          ctypes.c_void_p(0))
    glEnableVertexAttribArray(0)
    #textures
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, cube.model.itemsize * 2,
                          ctypes.c_void_p(cube_texture_offset))
    glEnableVertexAttribArray(1)
    glBindVertexArray(0)

    monkey_vao = glGenVertexArrays(1)
    glBindVertexArray(monkey_vao)
    monkey_vbo = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, monkey_vbo)
    glBufferData(GL_ARRAY_BUFFER, monkey.model.itemsize * len(monkey.model),
                 monkey.model, GL_STATIC_DRAW)
    #position
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, monkey.model.itemsize * 3,
                          ctypes.c_void_p(0))
    glEnableVertexAttribArray(0)
    #textures
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, monkey.model.itemsize * 2,
                          ctypes.c_void_p(monkey_texture_offset))
    glEnableVertexAttribArray(1)
    glBindVertexArray(0)

    monster_vao = glGenVertexArrays(1)
    glBindVertexArray(monster_vao)
    monster_vbo = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, monster_vbo)
    glBufferData(GL_ARRAY_BUFFER, monster.model.itemsize * len(monster.model),
                 monster.model, GL_STATIC_DRAW)
    #position
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, monster.model.itemsize * 3,
                          ctypes.c_void_p(0))
    glEnableVertexAttribArray(0)
    #textures
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, monster.model.itemsize * 2,
                          ctypes.c_void_p(monster_texture_offset))
    glEnableVertexAttribArray(1)
    glBindVertexArray(0)

    glClearColor(0.13, 0.2, 0.15, 1.0)
    glEnable(GL_DEPTH_TEST)

    projection = matrix44.create_perspective_projection_matrix(
        45.0, aspect_ratio, 0.1, 100.0)
    cube_model = matrix44.create_from_translation(Vector3([-4.0, 0.0, -3.0]))
    monkey_model = matrix44.create_from_translation(Vector3([0.0, 0.0, -3.0]))
    monster_model = matrix44.create_from_translation(Vector3([0.0, 0.0,
                                                              -10.0]))

    glUseProgram(cube_shader)
    cube_model_loc = glGetUniformLocation(cube_shader, "model")
    cube_view_loc = glGetUniformLocation(cube_shader, "view")
    cube_proj_loc = glGetUniformLocation(cube_shader, "proj")
    glUniformMatrix4fv(cube_model_loc, 1, GL_FALSE, cube_model)
    glUniformMatrix4fv(cube_proj_loc, 1, GL_FALSE, projection)
    glUseProgram(0)

    glUseProgram(monkey_shader)
    monkey_model_loc = glGetUniformLocation(monkey_shader, "model")
    monkey_view_loc = glGetUniformLocation(monkey_shader, "view")
    monkey_proj_loc = glGetUniformLocation(monkey_shader, "proj")
    glUniformMatrix4fv(monkey_model_loc, 1, GL_FALSE, monkey_model)
    glUniformMatrix4fv(monkey_proj_loc, 1, GL_FALSE, projection)
    glUseProgram(0)

    glUseProgram(monster_shader)
    monster_model_loc = glGetUniformLocation(monster_shader, "model")
    monster_view_loc = glGetUniformLocation(monster_shader, "view")
    monster_proj_loc = glGetUniformLocation(monster_shader, "proj")
    glUniformMatrix4fv(monster_model_loc, 1, GL_FALSE, monster_model)
    glUniformMatrix4fv(monster_proj_loc, 1, GL_FALSE, projection)
    glUseProgram(0)

    while not glfw.window_should_close(window):
        glfw.poll_events()
        do_movement()

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        view = cam.get_view_matrix()

        glBindVertexArray(cube_vao)
        glUseProgram(cube_shader)
        glBindTexture(GL_TEXTURE_2D, cube_tex)
        glUniformMatrix4fv(cube_view_loc, 1, GL_FALSE, view)
        glDrawArrays(GL_TRIANGLES, 0, len(cube.vertex_index))
        glUseProgram(0)
        glBindVertexArray(0)

        glBindVertexArray(monkey_vao)
        glBindTexture(GL_TEXTURE_2D, monkey_tex)
        glUseProgram(monkey_shader)
        glUniformMatrix4fv(monkey_view_loc, 1, GL_FALSE, view)
        glDrawArrays(GL_TRIANGLES, 0, len(monkey.vertex_index))
        glUseProgram(0)
        glBindVertexArray(0)

        glBindVertexArray(monster_vao)
        glBindTexture(GL_TEXTURE_2D, monster_tex)
        glUseProgram(monster_shader)
        glUniformMatrix4fv(monster_view_loc, 1, GL_FALSE, view)
        glDrawArrays(GL_TRIANGLES, 0, len(monster.vertex_index))
        glUseProgram(0)
        glBindVertexArray(0)

        glfw.swap_buffers(window)

    glfw.terminate()
Esempio n. 8
0
def main():

    # initialize glfw
    if not glfw.init():
        return

    w_width, w_height = 1280, 720
    aspect_ratio = w_width / w_height

    window = glfw.create_window(w_width, w_height, "My OpenGL window", None, None)

    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)
    glfw.set_window_size_callback(window, window_resize)
    glfw.set_cursor_pos_callback(window, cursor_pos_callback)
    glfw.set_mouse_button_callback(window, mouse_button_callback)

    #        positions        texture coordinates
    cube = [-0.5, -0.5,  0.5, 0.0, 0.0,
             0.5, -0.5,  0.5, 1.0, 0.0,
             0.5,  0.5,  0.5, 1.0, 1.0,
            -0.5,  0.5,  0.5, 0.0, 1.0,

            -0.5, -0.5, -0.5, 0.0, 0.0,
             0.5, -0.5, -0.5, 1.0, 0.0,
             0.5,  0.5, -0.5, 1.0, 1.0,
            -0.5,  0.5, -0.5, 0.0, 1.0,

             0.5, -0.5, -0.5, 0.0, 0.0,
             0.5,  0.5, -0.5, 1.0, 0.0,
             0.5,  0.5,  0.5, 1.0, 1.0,
             0.5, -0.5,  0.5, 0.0, 1.0,

            -0.5,  0.5, -0.5, 0.0, 0.0,
            -0.5, -0.5, -0.5, 1.0, 0.0,
            -0.5, -0.5,  0.5, 1.0, 1.0,
            -0.5,  0.5,  0.5, 0.0, 1.0,

            -0.5, -0.5, -0.5, 0.0, 0.0,
             0.5, -0.5, -0.5, 1.0, 0.0,
             0.5, -0.5,  0.5, 1.0, 1.0,
            -0.5, -0.5,  0.5, 0.0, 1.0,

             0.5,  0.5, -0.5, 0.0, 0.0,
            -0.5,  0.5, -0.5, 1.0, 0.0,
            -0.5,  0.5,  0.5, 1.0, 1.0,
             0.5,  0.5,  0.5, 0.0, 1.0]

    cube = numpy.array(cube, dtype=numpy.float32)

    indices = [ 0,  1,  2,  2,  3,  0,
                4,  5,  6,  6,  7,  4,
                8,  9, 10, 10, 11,  8,
               12, 13, 14, 14, 15, 12,
               16, 17, 18, 18, 19, 16,
               20, 21, 22, 22, 23, 20]

    indices = numpy.array(indices, dtype=numpy.uint32)

    vertex_shader = """
    #version 330
    in layout(location = 0) vec3 position;
    in layout(location = 1) vec2 texture_cords;
    uniform mat4 vp;
    uniform mat4 model;
    out vec2 textures;
    void main()
    {
        gl_Position =  vp * model * vec4(position, 1.0f);
        textures = texture_cords;
    }
    """

    fragment_shader = """
    #version 330
    out vec4 outColor;
    in vec2 textures;
    uniform sampler2D tex_sampler;
    uniform ivec3 icolor;
    uniform int switcher;
    void main()
    {
        if(switcher == 0){
            outColor = texture(tex_sampler, textures);
        }else{
            outColor = vec4(icolor.r/255.0, icolor.g/255.0, icolor.b/255.0, 1.0);
        }
    }
    """

    shader = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
                                              OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER))

    # vertex buffer object and element buffer object
    VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, cube.itemsize * len(cube), cube, GL_STATIC_DRAW)

    EBO = glGenBuffers(1)
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.itemsize * len(indices), indices, GL_STATIC_DRAW)

    # position
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube.itemsize * 5, ctypes.c_void_p(0))
    glEnableVertexAttribArray(0)

    # textures
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, cube.itemsize * 5, ctypes.c_void_p(12))
    glEnableVertexAttribArray(1)

    crate = TextureLoader.load_texture("resources/images/planks_brown_10_diff_1k.jpg")
    metal = TextureLoader.load_texture("resources/images/green_metal_rust_diff_1k.jpg")
    brick = TextureLoader.load_texture("resources/images/castle_brick_07_diff_1k.jpg")

    ######################################################################################

    # picking texture and a frame buffer object
    pick_texture = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, pick_texture)

    FBO = glGenFramebuffers(1)
    glBindFramebuffer(GL_FRAMEBUFFER, FBO)

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1280, 720, 0, GL_RGB, GL_FLOAT, None)
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, pick_texture, 0)

    glBindFramebuffer(GL_FRAMEBUFFER, 0)
    glBindTexture(GL_TEXTURE_2D, 0)

    ######################################################################################

    glUseProgram(shader)

    glEnable(GL_DEPTH_TEST)

    view = matrix44.create_from_translation(Vector3([0.0, 0.0, -4.0]))
    projection = matrix44.create_perspective_projection_matrix(45.0, aspect_ratio, 0.1, 100.0)

    vp = matrix44.multiply(view, projection)

    vp_loc = glGetUniformLocation(shader, "vp")
    model_loc = glGetUniformLocation(shader, "model")
    icolor_loc = glGetUniformLocation(shader, "icolor")
    switcher_loc = glGetUniformLocation(shader, "switcher")

    cube_positions = [(-2.0, 0.0, 0.0), (0.0, 0.0, 0.0), (2.0, 0.0, 0.0)]
    pick_colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]

    glUniformMatrix4fv(vp_loc, 1, GL_FALSE, vp)

    while not glfw.window_should_close(window):
        glfw.poll_events()

        glClearColor(0.2, 0.3, 0.2, 1.0)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        rot_y = Matrix44.from_y_rotation(glfw.get_time() * 2)

        # draw to the default frame buffer
        glUniform1i(switcher_loc, 0)
        for i in range(len(cube_positions)):
            model = matrix44.create_from_translation(cube_positions[i])
            if i == 0:
                glBindTexture(GL_TEXTURE_2D, crate)
                if red_rot:
                    glUniformMatrix4fv(model_loc, 1, GL_FALSE, rot_y * model)
                else:
                    glUniformMatrix4fv(model_loc, 1, GL_FALSE, model)
            elif i == 1:
                glBindTexture(GL_TEXTURE_2D, metal)
                if green_rot:
                    glUniformMatrix4fv(model_loc, 1, GL_FALSE, rot_y * model)
                else:
                    glUniformMatrix4fv(model_loc, 1, GL_FALSE, model)
            else:
                glBindTexture(GL_TEXTURE_2D, brick)
                if blue_rot:
                    glUniformMatrix4fv(model_loc, 1, GL_FALSE, rot_y * model)
                else:
                    glUniformMatrix4fv(model_loc, 1, GL_FALSE, model)

            glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, None)

        # draw to the custom frame buffer object
        glUniform1i(switcher_loc, 1)
        glBindFramebuffer(GL_FRAMEBUFFER, FBO)
        glClearColor(0.0, 0.0, 0.0, 1.0)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        for i in range(len(cube_positions)):
            pick_model = matrix44.create_from_translation(cube_positions[i])
            glUniform3iv(icolor_loc, 1, pick_colors[i])
            glUniformMatrix4fv(model_loc, 1, GL_FALSE, pick_model)
            glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, None)

        if pick:
            picker()

        glBindFramebuffer(GL_FRAMEBUFFER, 0)

        glfw.swap_buffers(window)

    glfw.terminate()
Esempio n. 9
0
def main():
    # initialize glfw
    if not glfw.init():
        return

    w_width, w_height = 1280, 720
    aspect_ratio = w_width / w_height

    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 2)
    glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)
    glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
    window = glfw.create_window(w_width, w_height, "My OpenGL window", None,
                                None)

    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)
    glfw.set_window_size_callback(window, window_resize)
    glfw.set_key_callback(window, key_callback)
    glfw.set_cursor_pos_callback(window, mouse_callback)

    car_back = [
        #Badan Mobil
        #front
        -1.0,
        -1.0,
        1.0,
        0.167,
        0.0,
        0.0,
        0.0,
        1.0,
        1.0,
        -1.0,
        1.0,
        0.833,
        0.0,
        0.0,
        0.0,
        1.0,
        1.0,
        1.0,
        1.0,
        0.833,
        1.0,
        0.0,
        0.0,
        1.0,
        -1.0,
        1.0,
        1.0,
        0.167,
        1.0,
        0.0,
        0.0,
        1.0,

        #right
        1.0,
        -1.0,
        1.0,
        0.0,
        0.0,
        1.0,
        0.0,
        0.0,
        1.0,
        -1.0,
        -2.0,
        1.0,
        0.0,
        1.0,
        0.0,
        0.0,
        1.0,
        1.0,
        -2.0,
        1.0,
        1.0,
        1.0,
        0.0,
        0.0,
        1.0,
        1.0,
        1.0,
        0.0,
        1.0,
        1.0,
        0.0,
        0.0,

        #back
        -1.0,
        -1.0,
        -2.0,
        0.167,
        0.0,
        0.0,
        0.0,
        -1.0,
        1.0,
        -1.0,
        -2.0,
        0.833,
        0.0,
        0.0,
        0.0,
        -1.0,
        1.0,
        1.0,
        -2.0,
        0.833,
        1.0,
        0.0,
        0.0,
        -1.0,
        -1.0,
        1.0,
        -2.0,
        0.167,
        1.0,
        0.0,
        0.0,
        -1.0,

        #left
        -1.0,
        -1.0,
        1.0,
        0.0,
        0.0,
        -1.0,
        0.0,
        0.0,
        -1.0,
        -1.0,
        -2.0,
        1.0,
        0.0,
        -1.0,
        0.0,
        0.0,
        -1.0,
        1.0,
        -2.0,
        1.0,
        1.0,
        -1.0,
        0.0,
        0.0,
        -1.0,
        1.0,
        1.0,
        0.0,
        1.0,
        -1.0,
        0.0,
        0.0,

        #top
        -1.0,
        1.0,
        1.0,
        0.0,
        0.0,
        0.0,
        1.0,
        0.0,
        1.0,
        1.0,
        1.0,
        1.0,
        0.0,
        0.0,
        1.0,
        0.0,
        1.0,
        1.0,
        -2.0,
        1.0,
        1.0,
        0.0,
        1.0,
        0.0,
        -1.0,
        1.0,
        -2.0,
        0.0,
        1.0,
        0.0,
        1.0,
        0.0,

        #bottom
        -1.0,
        -1.0,
        1.0,
        0.0,
        0.0,
        0.0,
        -1.0,
        0.0,
        1.0,
        -1.0,
        1.0,
        1.0,
        0.0,
        0.0,
        -1.0,
        0.0,
        1.0,
        -1.0,
        -2.0,
        1.0,
        1.0,
        0.0,
        -1.0,
        0.0,
        -1.0,
        -1.0,
        -2.0,
        0.0,
        1.0,
        0.0,
        -1.0,
        0.0
    ]

    car_front = [
        #Depan Mobil
        #front
        -1.0,
        -1.0,
        2.0,
        0.0,
        0.0,
        0.0,
        0.0,
        1.0,
        1.0,
        -1.0,
        2.0,
        1.0,
        0.0,
        0.0,
        0.0,
        1.0,
        1.0,
        0,
        2.0,
        1.0,
        1.0,
        0.0,
        0.0,
        1.0,
        -1.0,
        0,
        2.0,
        0.0,
        1.0,
        0.0,
        0.0,
        1.0,

        #right
        1.0,
        -1.0,
        2.0,
        0.167,
        0.0,
        1.0,
        0.0,
        0.0,
        1.0,
        -1.0,
        1.0,
        0.833,
        0.0,
        1.0,
        0.0,
        0.0,
        1.0,
        0,
        1.0,
        0.833,
        1.0,
        1.0,
        0.0,
        0.0,
        1.0,
        0,
        2.0,
        0.167,
        1.0,
        1.0,
        0.0,
        0.0,

        #back
        -1.0,
        -1.0,
        1.0,
        0.0,
        0.0,
        0.0,
        0.0,
        -1.0,
        1.0,
        -1.0,
        1.0,
        1.0,
        0.0,
        0.0,
        0.0,
        -1.0,
        1.0,
        0,
        1.0,
        1.0,
        1.0,
        0.0,
        0.0,
        -1.0,
        -1.0,
        0,
        1.0,
        0.0,
        1.0,
        0.0,
        0.0,
        -1.0,

        #left
        -1.0,
        -1.0,
        2.0,
        0.167,
        0.0,
        -1.0,
        0.0,
        0.0,
        -1.0,
        -1.0,
        1.0,
        0.833,
        0.0,
        -1.0,
        0.0,
        0.0,
        -1.0,
        0,
        1.0,
        0.833,
        1.0,
        -1.0,
        0.0,
        0.0,
        -1.0,
        0,
        2.0,
        0.167,
        1.0,
        -1.0,
        0.0,
        0.0,

        #top
        -1.0,
        0,
        2.0,
        0.0,
        0.0,
        0.0,
        1.0,
        0.0,
        1.0,
        0,
        2.0,
        1.0,
        0.0,
        0.0,
        1.0,
        0.0,
        1.0,
        0,
        1.0,
        1.0,
        1.0,
        0.0,
        1.0,
        0.0,
        -1.0,
        0,
        1.0,
        0.0,
        1.0,
        0.0,
        1.0,
        0.0,

        #bottom
        -1.0,
        -1.0,
        2.0,
        0.0,
        0.0,
        0.0,
        -1.0,
        0.0,
        1.0,
        -1.0,
        2.0,
        1.0,
        0.0,
        0.0,
        -1.0,
        0.0,
        1.0,
        -1.0,
        1.0,
        1.0,
        1.0,
        0.0,
        -1.0,
        0.0,
        -1.0,
        -1.0,
        1.0,
        0.0,
        1.0,
        0.0,
        -1.0,
        0.0
    ]

    car_back = numpy.array(car_back, dtype=numpy.float32)
    car_front = numpy.array(car_front, dtype=numpy.float32)
    indices = [
        0,
        1,
        2,
        2,
        3,
        0,
        4,
        5,
        6,
        6,
        7,
        4,
        8,
        9,
        10,
        10,
        11,
        8,
        12,
        13,
        14,
        14,
        15,
        12,
        16,
        17,
        18,
        18,
        19,
        16,
        20,
        21,
        22,
        22,
        23,
        20,
    ]

    indices = numpy.array(indices, dtype=numpy.uint32)

    car_program = compile_shader("Shaders/CarShader.vs",
                                 "Shaders/CarShader.fs")
    wheel_program = compile_shader("Shaders/WheelShader.vs",
                                   "Shaders/WheelShader.fs")
    glClearColor(0.2, 0.3, 0.2, 1.0)
    glEnable(GL_DEPTH_TEST)

    projection = matrix44.create_perspective_projection_matrix(
        45.0, aspect_ratio, 0.1, 100.0)

    car_model_loc = glGetUniformLocation(car_program, "model")
    car_view_loc = glGetUniformLocation(car_program, "view")
    car_proj_loc = glGetUniformLocation(car_program, "proj")
    car_cam_pos = glGetUniformLocation(car_program, "viewPos")

    wheel_model_loc = glGetUniformLocation(wheel_program, "model")
    wheel_view_loc = glGetUniformLocation(wheel_program, "view")
    wheel_proj_loc = glGetUniformLocation(wheel_program, "proj")
    circle, thickness = generateCircleArray(0, 0, 0, 0.4, 0.2)

    VAO_wheel = glGenVertexArrays(1)
    glBindVertexArray(VAO_wheel)
    VBO_wheel = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO_wheel)
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, circle.itemsize * 3,
                          ctypes.c_void_p(0))

    VAO_car = glGenVertexArrays(1)
    glBindVertexArray(VAO_car)
    VBO_car = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO_car)
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, car_back.itemsize * 8,
                          ctypes.c_void_p(0))
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, car_back.itemsize * 8,
                          ctypes.c_void_p(12))
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, car_back.itemsize * 8,
                          ctypes.c_void_p(20))

    EBO = glGenBuffers(1)
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.itemsize * len(indices),
                 indices, GL_STATIC_DRAW)

    metal = TextureLoader.load_texture("Textures/badan_samping.jpg")
    metal2 = TextureLoader.load_texture("Textures/kap_samping.jpg")
    roda = TextureLoader.load_texture("Textures/roda.jpg")

    while not glfw.window_should_close(window):
        glfw.poll_events()
        do_movement()
        view = cam.get_view_matrix()
        camera_position = cam.get_cam_pos()
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        #Gambar Roda
        glBindVertexArray(VAO_car)
        glBindBuffer(GL_ARRAY_BUFFER, VBO_car)
        glBufferData(GL_ARRAY_BUFFER, circle.itemsize * len(circle), circle,
                     GL_STATIC_DRAW)

        glEnableVertexAttribArray(0)
        glEnableVertexAttribArray(1)
        glEnableVertexAttribArray(2)

        glUseProgram(car_program)

        wheel_positions = [(1.0, -1.0, 1.0), (-1.2, -1.0, 1.0),
                           (-1.2, -1.0, -2.0), (1.0, -1.0, -2.0)]

        glUniformMatrix4fv(car_proj_loc, 1, GL_FALSE, projection)

        glUniformMatrix4fv(car_view_loc, 1, GL_FALSE, view)
        glUniform3fv(car_cam_pos, 1, camera_position)

        for i in range(len(wheel_positions)):
            wheel_model = matrix44.create_from_translation(wheel_positions[i])
            glUniformMatrix4fv(car_model_loc, 1, GL_FALSE, wheel_model)
            glBindTexture(GL_TEXTURE_2D, roda)
            glDrawArrays(GL_TRIANGLES, 0, len(circle))

        #Gambar Mobil
        glBindVertexArray(VAO_car)
        glBindBuffer(GL_ARRAY_BUFFER, VBO_car)
        glBufferData(GL_ARRAY_BUFFER, car_back.itemsize * len(car_back),
                     car_back, GL_STATIC_DRAW)

        glUniformMatrix4fv(car_proj_loc, 1, GL_FALSE, projection)

        glUniformMatrix4fv(car_view_loc, 1, GL_FALSE, view)

        car_model = matrix44.create_from_translation((0, 0, 0))
        glUniformMatrix4fv(car_model_loc, 1, GL_FALSE, car_model)
        glBindTexture(GL_TEXTURE_2D, metal)
        glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, None)

        glBufferData(GL_ARRAY_BUFFER, car_front.itemsize * len(car_front),
                     car_front, GL_STATIC_DRAW)
        glBindTexture(GL_TEXTURE_2D, metal2)
        glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, None)

        glBindVertexArray(VAO_wheel)
        glBindBuffer(GL_ARRAY_BUFFER, VBO_wheel)
        glBufferData(GL_ARRAY_BUFFER, thickness.itemsize * len(thickness),
                     thickness, GL_STATIC_DRAW)

        glEnableVertexAttribArray(0)

        glUseProgram(wheel_program)

        glUniformMatrix4fv(wheel_proj_loc, 1, GL_FALSE, projection)

        glUniformMatrix4fv(wheel_view_loc, 1, GL_FALSE, view)

        for i in range(len(wheel_positions)):
            wheel_model = matrix44.create_from_translation(wheel_positions[i])
            glUniformMatrix4fv(wheel_model_loc, 1, GL_FALSE, wheel_model)
            glDrawArrays(GL_TRIANGLES, 0, len(thickness))

        glfw.swap_buffers(window)

    glfw.terminate()
Esempio n. 10
0
def main():
    if not opengl_init():
        return

    load_gedung("itb_coordinate.txt")

    # Enable key events
    glfw.set_input_mode(window,glfw.STICKY_KEYS,GL_TRUE)
    glfw.set_cursor_pos(window, 1024/2, 768/2)

    # Set opengl clear color to something other than red (color used by the fragment shader)
    glClearColor(0.0,0.0,0.0,0.0)

    # Enable depth test
    glEnable(GL_DEPTH_TEST)
    # Accept fragment if it closer to the camera than the former one
    glDepthFunc(GL_LESS);
    glEnable(GL_CULL_FACE)

    vertex_array_id = glGenVertexArrays(1)
    glBindVertexArray( vertex_array_id )

    program_id = common.LoadShaders( ".\\shaders\\Tutorial6\\TransformVertexShader.vertexshader",
        ".\\shaders\\Tutorial6\\TextureFragmentShader.fragmentshader" )

    vertex_data = createAllBuilding()

    # Two UV coordinatesfor each vertex. They were created withe Blender.
    uv_data = createUVData()

    # Get a handle for our "MVP" uniform
    matrix_id = glGetUniformLocation(program_id, "MVP");

    texture = []
    texture_id = []

    texture_id.append(glGetUniformLocation(program_id, "myTextureSampler"))
    texture_id.append(glGetUniformLocation(program_id, "myTextureSampler2"))

    tex1 = TextureLoader.load_texture("res/crate.jpg")
    tex2 = TextureLoader.load_texture("res/metal.jpg")
    # Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
    # A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices



    vertex_buffer = glGenBuffers(1);
    array_type = GLfloat * len(vertex_data)
    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer)
    glBufferData(GL_ARRAY_BUFFER, len(vertex_data) * 4, array_type(*vertex_data), GL_STATIC_DRAW)

    uv_buffer = glGenBuffers(1);
    array_type = GLfloat * len(uv_data)
    glBindBuffer(GL_ARRAY_BUFFER, uv_buffer)
    glBufferData(GL_ARRAY_BUFFER, len(uv_data) * 4, array_type(*uv_data), GL_STATIC_DRAW)

    # vsync and glfw do not play nice.  when vsync is enabled mouse movement is jittery.
    common.disable_vsyc()

    glUseProgram(program_id)

    #1rst attribute buffer : vertices
    glEnableVertexAttribArray(0)
    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
    glVertexAttribPointer(
        0,                  # attribute 0. No particular reason for 0, but must match the layout in the shader.
        3,                  # len(vertex_data)
        GL_FLOAT,           # type
        GL_FALSE,           # ormalized?
        0,                  # stride
        null                # array buffer offset (c_type == void*)
        )

    # 2nd attribute buffer : colors
    glEnableVertexAttribArray(1)
    glBindBuffer(GL_ARRAY_BUFFER, uv_buffer);
    glVertexAttribPointer(
        1,                  # attribute 1. No particular reason for 1, but must match the layout in the shader.
        2,                  # len(vertex_data)
        GL_FLOAT,           # type
        GL_FALSE,           # ormalized?
        0,                  # stride
        null                # array buffer offset (c_type == void*)
        )

    glActiveTexture(GL_TEXTURE0);

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

        controls.computeMatricesFromInputs(window)
        ProjectionMatrix = controls.getProjectionMatrix();
        ViewMatrix = controls.getViewMatrix();
        ModelMatrix = mat4.identity();
        mvp = ProjectionMatrix * ViewMatrix * ModelMatrix;

        ##################################################################### SET TEXTURE 1

        # Send our transformation to the currently bound shader,
        # in the "MVP" uniform
        # draws Aula barat, timur ; CC barat, timur

        glBindTexture(GL_TEXTURE_2D, tex1);
        glUniformMatrix4fv(matrix_id, 1, GL_FALSE,mvp.data)
Esempio n. 11
0
def main():
    # initialize glfw
    if not glfw.init():
        return

    w_width, w_height = 1280, 720
    aspect_ratio = w_width / w_height

    window = glfw.create_window(w_width, w_height, "My OpenGL window", None,
                                None)

    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)
    glfw.set_window_size_callback(window, window_resize)

    #        positions        texture_coords
    cube = [
        -0.5, -0.5, 0.5, 0.0, 0.0, 0.5, -0.5, 0.5, 1.0, 0.0, 0.5, 0.5, 0.5,
        1.0, 1.0, -0.5, 0.5, 0.5, 0.0, 1.0, -0.5, -0.5, -0.5, 0.0, 0.0, 0.5,
        -0.5, -0.5, 1.0, 0.0, 0.5, 0.5, -0.5, 1.0, 1.0, -0.5, 0.5, -0.5, 0.0,
        1.0, 0.5, -0.5, -0.5, 0.0, 0.0, 0.5, 0.5, -0.5, 1.0, 0.0, 0.5, 0.5,
        0.5, 1.0, 1.0, 0.5, -0.5, 0.5, 0.0, 1.0, -0.5, 0.5, -0.5, 0.0, 0.0,
        -0.5, -0.5, -0.5, 1.0, 0.0, -0.5, -0.5, 0.5, 1.0, 1.0, -0.5, 0.5, 0.5,
        0.0, 1.0, -0.5, -0.5, -0.5, 0.0, 0.0, 0.5, -0.5, -0.5, 1.0, 0.0, 0.5,
        -0.5, 0.5, 1.0, 1.0, -0.5, -0.5, 0.5, 0.0, 1.0, 0.5, 0.5, -0.5, 0.0,
        0.0, -0.5, 0.5, -0.5, 1.0, 0.0, -0.5, 0.5, 0.5, 1.0, 1.0, 0.5, 0.5,
        0.5, 0.0, 1.0
    ]

    cube = numpy.array(cube, dtype=numpy.float32)

    indices = [
        0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4, 8, 9, 10, 10, 11, 8, 12, 13, 14,
        14, 15, 12, 16, 17, 18, 18, 19, 16, 20, 21, 22, 22, 23, 20
    ]

    indices = numpy.array(indices, dtype=numpy.uint32)

    vertex_shader = """
    #version 330
    in layout(location = 0) vec3 position;
    in layout(location = 1) vec2 texture_cords;
    in layout(location = 2) float offset;
    uniform mat4 mvp;
    out vec2 textures;
    void main()
    {
        vec3 final_pos = vec3(position.x + offset, position.y + offset, position.z);
        gl_Position =  mvp * vec4(final_pos, 1.0f);
        textures = texture_cords;
    }
    """

    fragment_shader = """
    #version 330
    in vec2 textures;
    out vec4 color;
    uniform sampler2D tex_sampler;
    void main()
    {
        color = texture(tex_sampler, textures);
    }
    """
    shader = OpenGL.GL.shaders.compileProgram(
        OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
        OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER))

    VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, cube.itemsize * len(cube), cube,
                 GL_STATIC_DRAW)

    EBO = glGenBuffers(1)
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.itemsize * len(indices),
                 indices, GL_STATIC_DRAW)

    # position
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube.itemsize * 5,
                          ctypes.c_void_p(0))
    glEnableVertexAttribArray(0)

    # textures
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, cube.itemsize * 5,
                          ctypes.c_void_p(12))
    glEnableVertexAttribArray(1)

    # instances
    # instance_array = []
    # for i in range(0, 20, 1):
    #    instance_array.append(i)

    # instance_array = numpy.array(instance_array, numpy.float32)

    instance_array = numpy.array(numpy.arange(0, 20, 1), numpy.float32)

    instanceVBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, instanceVBO)
    glBufferData(GL_ARRAY_BUFFER,
                 instance_array.itemsize * len(instance_array), instance_array,
                 GL_STATIC_DRAW)

    glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
    glEnableVertexAttribArray(2)
    glVertexAttribDivisor(2, 1)

    texture = TextureLoader.load_texture(
        "resources/images/planks_brown_10_diff_1k.jpg")

    glUseProgram(shader)

    glClearColor(0.2, 0.3, 0.2, 1.0)
    glEnable(GL_DEPTH_TEST)

    model = matrix44.create_from_translation(Vector3([-14.0, -8.0, 0.0]))
    view = matrix44.create_from_translation(Vector3([0.0, 0.0, -20.0]))
    projection = matrix44.create_perspective_projection_matrix(
        45.0, aspect_ratio, 0.1, 100.0)

    mv = matrix44.multiply(model, view)
    mvp = matrix44.multiply(mv, projection)

    mvp_loc = glGetUniformLocation(shader, "mvp")
    glUniformMatrix4fv(mvp_loc, 1, GL_FALSE, mvp)

    while not glfw.window_should_close(window):
        glfw.poll_events()

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        glDrawElementsInstanced(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT,
                                None, 20)

        glfw.swap_buffers(window)

    glfw.terminate()
Esempio n. 12
0
def main():

    # initialize glfw
    if not glfw.init():
        return

    w_width, w_height = 800, 600
    aspect_ratio = w_width / w_height

    window = glfw.create_window(w_width, w_height, "My OpenGL window", None,
                                None)

    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)
    glfw.set_window_size_callback(window, window_resize)
    glfw.set_key_callback(window, key_callback)
    glfw.set_cursor_pos_callback(window, mouse_callback)

    obj = ObjLoader()
    obj.load_model("objects/sphere.obj")

    texture_offset = len(obj.vertex_index) * 12
    normal_offset = (texture_offset + len(obj.texture_index) * 8)

    shader = ShaderLoader.compile_shader("shaders/main_vert.vs",
                                         "shaders/main_frag.fs")

    VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, obj.model.itemsize * len(obj.model),
                 obj.model, GL_STATIC_DRAW)

    #positions
    position = glGetAttribLocation(shader, "position")
    glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE,
                          obj.model.itemsize * 3, ctypes.c_void_p(0))
    glEnableVertexAttribArray(position)
    #textures
    texCoords = glGetAttribLocation(shader, "inTexCoords")
    glVertexAttribPointer(texCoords, 2, GL_FLOAT, GL_FALSE,
                          obj.model.itemsize * 2,
                          ctypes.c_void_p(texture_offset))
    glEnableVertexAttribArray(texCoords)
    #normals
    normals = glGetAttribLocation(shader, "vertNormal")
    glVertexAttribPointer(normals, 3, GL_FLOAT,
                          GL_FALSE, obj.model.itemsize * 3,
                          ctypes.c_void_p(normal_offset))
    glEnableVertexAttribArray(normals)

    for planet in planets:
        texture = TextureLoader.load_texture(planets[planet]['image_path'])
        planets[planet]['texture'] = texture

        #initial position
        if planet == 'sun' or planet == 'stars':
            distance_from_sun = planets[planet]['distance_from_sun']
            planets[planet]['initial_position'] = [
                distance_from_sun, 0.0, distance_from_sun
            ]
        else:
            distance_from_sun = 30.0 + planets[planet]['distance_from_sun']
            x = round(random.uniform(-distance_from_sun, distance_from_sun), 3)
            z = round(math.sqrt((distance_from_sun**2) - (x**2)), 3)
            planets[planet]['initial_position'] = [x, 0.0, z]

    glEnable(GL_TEXTURE_2D)

    glUseProgram(shader)

    glClearColor(0.0, 0.2, 0.2, 1.0)
    glEnable(GL_DEPTH_TEST)

    projection = pyrr.matrix44.create_perspective_projection_matrix(
        65.0, w_width / w_height, 0.1, 10000.0)
    scale = pyrr.matrix44.create_from_scale(pyrr.Vector3([0.1, 0.1, 0.1]))

    view_loc = glGetUniformLocation(shader, "view")
    proj_loc = glGetUniformLocation(shader, "projection")
    model_loc = glGetUniformLocation(shader, "model")
    normal_loc = glGetUniformLocation(shader, "normalMatrix")
    scale_loc = glGetUniformLocation(shader, "scale")
    scale_planet_loc = glGetUniformLocation(shader, "scale_planet")

    glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection)
    glUniformMatrix4fv(scale_loc, 1, GL_FALSE, scale)

    Global_ambient_loc = glGetUniformLocation(shader, "Global_ambient")
    Light_ambient_loc = glGetUniformLocation(shader, "Light_ambient")
    Light_diffuse_loc = glGetUniformLocation(shader, "Light_diffuse")
    Light_specular_loc = glGetUniformLocation(shader, "Light_specular")
    Light_location_loc = glGetUniformLocation(shader, "Light_location")
    Material_ambient_loc = glGetUniformLocation(shader, "Material_ambient")
    Material_diffuse_loc = glGetUniformLocation(shader, "Material_diffuse")
    Material_specular_loc = glGetUniformLocation(shader, "Material_specular")
    Material_shininess_loc = glGetUniformLocation(shader, "Material_shininess")

    glUniform4f(Global_ambient_loc, 0.8, 0.8, 0.9, 0.1)
    glUniform4f(Light_ambient_loc, 0.3, 0.3, 0.3, 1.0)
    glUniform4f(Light_diffuse_loc, 0.25, 0.25, 0.25, 1.0)
    glUniform4f(Light_specular_loc, 0.9, 0.9, 0.9, 1.0)
    glUniform3f(Light_location_loc, 0, 0, 0)

    # *******************************************************************************************

    # obj2 = ObjLoader()
    # obj2.load_model("objects/cruiser.obj")
    #
    # shader2 = ShaderLoader.compile_shader("shaders/vert.vs", "shaders/frag.fs")
    #
    # VBO2 = glGenBuffers(1)
    # glBindBuffer(GL_ARRAY_BUFFER, VBO2)
    # glBufferData(GL_ARRAY_BUFFER, obj2.model.itemsize * len(obj2.model), obj2.model, GL_STATIC_DRAW)
    #
    # # positions
    # position2 = glGetAttribLocation(shader, "position")
    # glVertexAttribPointer(position2, 3, GL_FLOAT, GL_FALSE, obj2.model.itemsize * 3, ctypes.c_void_p(0))
    # glEnableVertexAttribArray(position2)
    # # textures
    # texCoords2 = glGetAttribLocation(shader2, "inTexCoords")
    # glVertexAttribPointer(texCoords2, 2, GL_FLOAT, GL_FALSE, obj2.model.itemsize * 2, ctypes.c_void_p(texture_offset))
    # glEnableVertexAttribArray(texCoords2)
    # # normals
    # normals2 = glGetAttribLocation(shader2, "vertNormal")
    # glVertexAttribPointer(normals2, 3, GL_FLOAT, GL_FALSE, obj2.model.itemsize * 3, ctypes.c_void_p(normal_offset))
    # glEnableVertexAttribArray(normals2)
    #
    #
    # texture2 = TextureLoader.load_texture(planets['cruiser']['image_path'])
    # distance_from_sun = planets['cruiser']['distance_from_sun']
    # planets['cruiser']['initial_position'] = [distance_from_sun, 0.0, distance_from_sun]
    #
    # glUseProgram(shader2)
    #
    #
    # Global_ambient_loc = glGetUniformLocation(shader, "Global_ambient")
    # Light_ambient_loc = glGetUniformLocation(shader, "Light_ambient")
    # Light_diffuse_loc = glGetUniformLocation(shader, "Light_diffuse")
    # Light_specular_loc = glGetUniformLocation(shader, "Light_specular")
    # Light_location_loc = glGetUniformLocation(shader, "Light_location")
    # Material_ambient_loc = glGetUniformLocation(shader, "Material_ambient")
    # Material_diffuse_loc = glGetUniformLocation(shader, "Material_diffuse")
    # Material_specular_loc = glGetUniformLocation(shader, "Material_specular")
    # Material_shininess_loc = glGetUniformLocation(shader, "Material_shininess")
    #
    # glUniform4f(Global_ambient_loc, 0.8, 0.8, 0.9, 0.1)
    # glUniform4f(Light_ambient_loc, 0.3, 0.3, 0.3, 1.0)
    # glUniform4f(Light_diffuse_loc, 0.25, 0.25, 0.25, 1.0)
    # glUniform4f(Light_specular_loc, 0.9, 0.9, 0.9, 1.0)
    # glUniform3f(Light_location_loc, 0, 0, 0)
    # glUniform4f(Material_ambient_loc, 0.4, 0.4, 0.4, 1.0)
    # glUniform4f(Material_diffuse_loc, 0.15, 0.15, 0.15, 1.0)
    # glUniform4f(Material_specular_loc, 1.0, 1.0, 1.0, 1.0)
    # glUniform1f(Material_shininess_loc, .95)

    # ******************************************************************************************

    while not glfw.window_should_close(window):
        glfw.poll_events()

        do_movement()
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        time = glfw.get_time()

        view = cam.get_view_matrix()
        glUniformMatrix4fv(view_loc, 1, GL_FALSE, view)

        # **********************************planets****************************************
        for planet in planets:
            if planet == 'cruiser':
                glBindTexture(GL_TEXTURE_2D, texture2)
            else:
                glBindTexture(GL_TEXTURE_2D, planets[planet]['texture'])

            revolution_speed = time * planets[planet][
                'revolution_ratio_relative_to_earth']
            rotation_speed = time * planets[planet][
                'rotation_ratio_relative_to_earth']
            # scale planet
            scale_factor = planets[planet]['size_ratio_relative_to_earth']
            scale_planet = matrix44.create_from_scale(
                pyrr.Vector3([scale_factor, scale_factor, scale_factor]))
            glUniformMatrix4fv(scale_planet_loc, 1, GL_FALSE, scale_planet)

            # translation
            model = matrix44.create_from_translation(
                pyrr.Vector3(planets[planet]['initial_position']))
            revolution = matrix44.create_from_y_rotation(revolution_speed)
            rotation = matrix44.create_from_y_rotation(rotation_speed)
            # revolution about z axis
            model = matrix44.multiply(model, revolution)
            # rotation about own axis
            model = matrix44.multiply(rotation, model)

            glUniformMatrix4fv(model_loc, 1, GL_FALSE, model)

            # ----create normalMatrix--
            modelView = numpy.matmul(view, model)
            # modelView = numpy.matmul(modelView, scale_planet)
            # modelView = numpy.matmul(modelView, scale)
            modelView33 = modelView[0:-1, 0:-1]
            normalMatrix = numpy.transpose(numpy.linalg.inv(modelView33))
            # -----------------
            glUniformMatrix3fv(normal_loc, 1, GL_FALSE, normalMatrix)

            a, b, c, d = planets[planet]['Material_ambient']
            glUniform4f(Material_ambient_loc, a, b, c, d)
            a, b, c, d = planets[planet]['Material_diffuse']
            glUniform4f(Material_diffuse_loc, a, b, c, d)
            a, b, c, d = planets[planet]['Material_specular']
            glUniform4f(Material_specular_loc, a, b, c, d)
            s = planets[planet]['Material_shininess'][0]
            glUniform1f(Material_shininess_loc, s)

            if planet == 'cruiser':
                glDrawArrays(GL_TRIANGLES, 0, len(obj2.vertex_index))
            else:
                glDrawArrays(GL_TRIANGLES, 0, len(obj.vertex_index))

        # *******************************************************************************

        glfw.swap_buffers(window)

    glfw.terminate()
Esempio n. 13
0
# Read more on https://www.khronos.org/opengl/wiki/Common_Mistakes under "Texture upload and pixel reads"
gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)

# Creating Data Buffers -----------------------------------------------------------|

# With the ability of .obj file loading, all the data will be read from the files and bound to proper buffers
primitive_objs = {
    "plane": parse_and_bind_obj_file("Assets/Primitives/plane.obj"),
    "cube": parse_and_bind_obj_file("Assets/Primitives/cube.obj"),
    "cylinder": parse_and_bind_obj_file("Assets/Primitives/cylinder.obj"),
    "cone": parse_and_bind_obj_file("Assets/Primitives/cone.obj"),
    "sphere": parse_and_bind_obj_file("Assets/Primitives/sphere.obj"),
    "disc": parse_and_bind_obj_file("Assets/Primitives/disc.obj"),
}

crate = TextureLoader.load_texture("res/crate.jpg")
metal = TextureLoader.load_texture("res/metal.jpg")
brick = TextureLoader.load_texture("res/brick.jpg")
space_ship = TextureLoader.load_texture("res/spaceship.jpg")
space_ship_yellow = TextureLoader.load_texture("res/spaceship_yellow.jpg")
space_ship_green = TextureLoader.load_texture("res/spaceship_green.jpg")
space_ship_red = TextureLoader.load_texture("res/spaceship_red.jpg")

world = TextureLoader.load_texture("res/world.jpg")

# Create Camera and Game Objects -----------------------------------------------------|

#skybox_shader = SkyboxShader(deepcopy(primitive_objs["cube"]))

perspective_projection = glm.perspective(glm.radians(45.0),
                                         screen_size.x / screen_size.y, 0.1,