Example #1
0
 def calc_projection(self):
     self.ortho = matrix44.create_orthogonal_projection(
         -self.hw / self._zoom, self.hw / self._zoom, -self.hh / self._zoom,
         self.hh / self._zoom, -100.0, 100.0)
     self.persp = matrix44.create_perspective_projection_matrix(
         50.0, self.width / self.height, 0.1, 200.0)
     if self.is_ortho:
         self.projection = self.ortho
     else:
         self.projection = self.persp
     self.default_proj = matrix44.create_orthogonal_projection(
         0, self.width, 0, self.height, -1, 1)
     self.view = matrix44.create_look_at((self.x, self.y, 10.0),
                                         (self.x, self.y, 0.0),
                                         (self.up_x, self.up_y, 0.0))
Example #2
0
 def projection(self):
     return matrix44.create_orthogonal_projection(
         -self.wnd.aspect_ratio, self.wnd.aspect_ratio,
         -1, 1,
         -1, 1,
         dtype='f4',
     )
Example #3
0
def get_projection(width, height):
    world_width = lattice_x
    world_height = world_width / width * height

    projection  = matrix44.create_orthogonal_projection(-world_width/2, world_width/2, -world_height/2, world_height/2, -1, 1)
    translation = matrix44.create_from_translation([-lattice_x/2, -lattice_y/2, 0])

    point_size = width / world_width

    return numpy.matmul(translation, projection), point_size
Example #4
0
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.projection = matrix44.create_orthogonal_projection(
            0, self.wnd.size[0], 0, self.wnd.size[1], -1.0, 1.0, dtype='f4')
        self.texts = []
        self.is_paused = False

        self.add_text("Text", 300, (300, 350), 5, (1, 0, 0))
        self.add_text("Art", 300, (150, 75), 5, (0, 1, 0))
Example #5
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        MAX_TEX_WIDTH = 8192
        N = MAX_TEX_WIDTH * 1

        def gen_initial_data(n, x_area=2.0, y_area=2.0):
            for n in range(n):
                # position
                yield (random.random() - 0.5) * x_area
                yield (random.random() - 0.5) * y_area
                # Velocity
                yield (random.random() - 0.5)
                yield (random.random() - 0.5)

        # Create geometry data
        gen = gen_initial_data(N,
                               x_area=self.aspect_ratio * 2 * 0.9,
                               y_area=2.0 * 0.95)
        data = numpy.fromiter(gen, count=N * 4, dtype='f4')
        self.boids_buffer_1 = self.ctx.buffer(data.tobytes())
        self.boids_buffer_2 = self.ctx.buffer(data=self.boids_buffer_1.read())

        self.boids_vao_1 = VAO(name='boids_1', mode=moderngl.POINTS)
        self.boids_vao_1.buffer(self.boids_buffer_1, '2f 2f',
                                ['in_position', 'in_velocity'])

        self.boids_vao_2 = VAO(name='boids_2', mode=moderngl.POINTS)
        self.boids_vao_2.buffer(self.boids_buffer_2, '2f 2f',
                                ['in_position', 'in_velocity'])

        self.boids_texture = self.ctx.texture(
            (MAX_TEX_WIDTH, N * 2 // MAX_TEX_WIDTH), components=2, dtype='f4')

        # Programs
        self.boids_render_program = self.load_program(
            'programs/boids/boids_render.glsl')
        self.boids_transform_program = self.load_program(
            'programs/boids/boids_transform.glsl')

        # Prepare for rendering
        self.m_proj = matrix44.create_orthogonal_projection(
            -self.aspect_ratio,
            self.aspect_ratio,
            -1.0,
            1.0,
            -1.0,
            1.0,
            dtype='f4',
        )
        self.boids_render_program['m_proj'].write(self.m_proj.tobytes())
        self.boids_transform_program['data'].value = 0
        self.boids_transform_program['num_boids'].value = N
        self.boids_transform_program['tex_width'].value = MAX_TEX_WIDTH
Example #6
0
 def __init__(self):
     self.objs = []
     self.view = mat4.create_identity() # matriz view
     self.projection = mat4.create_orthogonal_projection(-2, 2, -2, 2, -2, 2) # matriz projection
     self.lights = []
     self.camPos = [0.0, 0.0, 0.0]
     self.camLookAt = [0.0, 0.0, -1.0]
     
     self.shader = None
     self.diffuse = 0.2
     self.ambient = 0.2
     self.specular = 0.2
Example #7
0
def get_projection(width, height):
    world_height = geometry.size_y
    world_width = world_height / height * width

    projection = matrix44.create_orthogonal_projection(-world_width / 2,
                                                       world_width / 2,
                                                       -world_height / 2,
                                                       world_height / 2, -1, 1)
    translation = matrix44.create_from_translation(
        [-geometry.size_x / 2, -geometry.size_y / 2, 0])

    point_size = width / world_width

    return numpy.matmul(translation, projection), point_size
Example #8
0
def drawAxis():
    global shaderProgram
    global vao
    global vbo
    global model
    global uMat
    global view
    global idProj
    global idView
    global projection

    glClearColor(0, 0, 0, 1)

    # carrega os shaders do axis
    vertex_code = readShaderFile('axis.vp')
    fragment_code = readShaderFile('axis.fp')

    # compila o shaders e program
    vertexShader = shaders.compileShader(vertex_code, GL_VERTEX_SHADER)
    fragmentShader = shaders.compileShader(fragment_code, GL_FRAGMENT_SHADER)
    shaderProgram = shaders.compileProgram(vertexShader, fragmentShader)

    # cria e faz o bind no vertex arrat object
    vao = GLuint(0)
    glGenVertexArrays(1, vao)
    glBindVertexArray(vao)

    #vertices
    x = np.array([[255, 0, 0], [0, 0, 0], [-255, 0, 0]], dtype='f')
    y = np.array([[0, 255, 0], [0, 0, 0], [0, -255, 0]], dtype='f')
    z = np.array([[0, 0, 255], [0, 0, 0], [0, 0, -255]], dtype='f')
    vertices = np.concatenate((x, y, z))

    vbo = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, vbo)
    glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW)
    glVertexAttribPointer(0, 3, GL_FLOAT, False, 0,
                          None)  # first 0 is the location in shader
    glBindAttribLocation(shaderProgram, 0, 'vertexPosition')
    glEnableVertexAttribArray(0)

    view = matrix44.create_look_at(poscam, lookat, [0.0, 1.0, 0.0])
    projection = matrix44.create_orthogonal_projection(-2.0, 2.0, -2.0, 2.0,
                                                       -2.0, 2.0)

    idView = glGetUniformLocation(shaderProgram, "view")
    idProj = glGetUniformLocation(shaderProgram, "projection")

    glBindBuffer(GL_ARRAY_BUFFER, 0)
    glBindVertexArray(0)
Example #9
0
    def reshape(self, width, height):
        glViewport(0, 0, width, height)

        world_height = 1.4
        world_width = world_height / height * width

        projection = Projection(10)

        projection = matrix44.create_orthogonal_projection(
            -world_width / 2, world_width / 2, -world_height / 2,
            world_height / 2, -1, 1)
        translation = matrix44.create_from_translation([-1.05, -1.0 / 2, 0])

        self.camera_projection.update_ratio(width, height)

        self.pixels_per_unit = height / world_height
        self.projection = np.matmul(translation, projection)
Example #10
0
def init():
    global shaderProgram
    global vao
    global vbo
    global model
    global idMod
    global view
    global idView
    global projection
    global idProj
    global idColor
    global idLight
    global idLightPos
    global idViewPos
    global posCam

    glClearColor(0, 0, 0, 0)

    vertex_code = readShaderFile('cuboLuz.vp')
    fragment_code = readShaderFile('cuboLuz.fp')

    # compile shaders and program
    vertexShader = shaders.compileShader(vertex_code, GL_VERTEX_SHADER)
    fragmentShader = shaders.compileShader(fragment_code, GL_FRAGMENT_SHADER)
    shaderProgram = shaders.compileProgram(vertexShader, fragmentShader)

    # cria um vao
    vao = GLuint(0)
    glGenVertexArrays(1, vao)
    glBindVertexArray(vao)

    # dados do objeto q serao passados para o shaders (vertices e vetores normais)
    vertices = np.array(readVertexData(), dtype='f')
    print("vertices:", len(vertices) // 6)
    print(vertices)

    vbo = glGenBuffers(1)  # gera vbos
    glBindBuffer(GL_ARRAY_BUFFER, vbo)
    glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW)
    glVertexAttribPointer(0, 3, GL_FLOAT, False, 6 * sizeof(GLfloat),
                          ctypes.c_void_p(3 * sizeof(GLfloat)))  # vertices
    glVertexAttribPointer(1, 3, GL_FLOAT, False, 6 * sizeof(GLfloat),
                          ctypes.c_void_p(0))  # vertores normais

    # habilita os atributos
    glEnableVertexAttribArray(0)
    glEnableVertexAttribArray(1)

    # cria a matriz de transformação
    model = matrix44.create_identity()

    #ratacoes
    rotY = matrix44.create_from_y_rotation(math.radians(45))
    rotx = matrix44.create_from_x_rotation(math.radians(45))
    rotT = matrix44.multiply(rotY, rotx)

    model = matrix44.multiply(model, rotT)

    posCam = [0.0, 0.0, 0.0]
    view = matrix44.create_look_at(posCam, [0.0, 0.0, -0.1], [0.0, 1.0, 0.0])
    projection = matrix44.create_orthogonal_projection(-2.0, 2.0, -2.0, 2.0,
                                                       2.0,
                                                       -2.0)  # amplia a visao
    print(f'Model:\n{model}\n')
    print(f'View:\n{view}\n')
    print(f'Projection:\n{projection}\n')

    # atribui uma variavel uniforme para cada matriz
    idMod = glGetUniformLocation(shaderProgram, "model")
    idView = glGetUniformLocation(shaderProgram, "view")
    idProj = glGetUniformLocation(shaderProgram, "projection")

    # iluminação
    idColor = glGetUniformLocation(shaderProgram, "objectColor")
    idLight = glGetUniformLocation(shaderProgram, "lightColor")
    idLightPos = glGetUniformLocation(shaderProgram, "lightPos")
    idViewPos = glGetUniformLocation(shaderProgram, "viewPos")

    # Note that this is allowed, the call to glVertexAttribPointer registered VBO
    # as the currently bound vertex buffer object so afterwards we can safely unbind
    glBindBuffer(GL_ARRAY_BUFFER, 0)
    # Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs)
    glBindVertexArray(0)
Example #11
0
def init(obj, lista_obj):
    global shaderProgram
    global vao
    global vbo
    global shadingName
    # variaveis das tranformacoes
    global projection
    global idProj
    global idView
    global model
    global view
    global uMat
    global shear
    global shearFlag
    #camera e lookat
    global poscam
    global lookat
    # variaveis dos reflections
    global ambient
    global diffuse
    global specular
    global reflectionFlag
    global reflec
    # variaveis da luzes do uniform
    global idColor
    global idViewPos
    global idLight
    global idLightPos

    # variaveis das forcas do reflections
    global ambientForce
    global diffuseForce
    global specularForce

    glClearColor(0, 0, 0, 1)
    # carrega o shading baseado no nome que foi passado
    if (shadingName == 'phong'):
        for i in lista_obj:
            reflectionFlag = 1
            i.ambient = 1
            i.diffuse = 1
            i.specular = 1
            vertex_code = readShaderFile('reflect.vp')
            fragment_code = readShaderFile('reflect.fp')
    elif (shadingName == 'smooth'):
        for i in lista_obj:
            if (i.ambient == 1 or i.diffuse == 1 or i.specular == 1):
                # caso tenha sido feito o comando reflection atribuimos 1 para a flag e chamando o shaders das reflections
                reflectionFlag = 1
                vertex_code = readShaderFile('smooth.vp')
                fragment_code = readShaderFile('smooth.fp')
    elif (shadingName == 'flat'):
        for i in lista_obj:
            if (i.ambient == 1 or i.diffuse == 1 or i.specular == 1):
                # caso tenha sido feito o comando reflection atribuimos 1 para a flag e chamando o shaders das reflections
                reflectionFlag = 1
                vertex_code = readShaderFile('flat.vp')
                fragment_code = readShaderFile('flat.fp')

    else:
        vertex_code = readShaderFile('none.vp')
        fragment_code = readShaderFile('none.fp')

    for i in lista_obj:  # percorre a lista dos objetos, verificando os atributos das reflectons
        if (i.ambient == 1 or i.diffuse == 1 or i.specular == 1):
            # caso tenha sido feito o comando reflection atribuimos 1 para a flag e chamando o shaders das reflections
            reflectionFlag = 1
            vertex_code = readShaderFile('reflect.vp')
            fragment_code = readShaderFile('reflect.fp')
        else:
            vertex_code = readShaderFile('none.vp')
            fragment_code = readShaderFile('none.fp')

    # compile shaders and program
    vertexShader = shaders.compileShader(vertex_code, GL_VERTEX_SHADER)
    fragmentShader = shaders.compileShader(fragment_code, GL_FRAGMENT_SHADER)
    shaderProgram = shaders.compileProgram(vertexShader, fragmentShader)

    # Create and bind the Vertex Array Object
    vao = GLuint(0)
    glGenVertexArrays(1, vao)
    glBindVertexArray(vao)

    # lendo os obj pegando vertices e normais
    vertices = np.array(lendo_obj(obj), dtype='f')
    print("vertices:", len(vertices) // 6)

    vbo = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, vbo)
    glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW)
    glVertexAttribPointer(
        0, 3, GL_FLOAT, False, 6 * sizeof(GLfloat),
        ctypes.c_void_p(3 *
                        sizeof(GLfloat)))  # first 0 is the location in shader
    glVertexAttribPointer(
        1, 3, GL_FLOAT, False, 6 * sizeof(GLfloat),
        ctypes.c_void_p(0))  # first 0 is the location in shader
    glEnableVertexAttribArray(0)
    glEnableVertexAttribArray(1)

    #verifica em cada objeto a scala
    for i in lista_obj:
        i.model = pyrr.matrix44.create_identity()
        scale = pyrr.matrix44.create_from_scale(
            [i.scale_r, i.scale_g, i.scale_b], dtype='f')
        i.model = pyrr.matrix44.multiply(i.model, scale)

    #verifica em cada objeto se precisa fazer rotacao
    for i in lista_obj:
        if (i.rotate_x == 1):
            rotx = pyrr.matrix44.create_from_x_rotation(
                math.radians(i.rotate_grau))
        else:
            rotx = pyrr.matrix44.create_from_x_rotation(math.radians(0))

        if (i.rotate_y == 1):
            rotY = pyrr.matrix44.create_from_y_rotation(
                math.radians(i.rotate_grau))
        else:
            rotY = pyrr.matrix44.create_from_y_rotation(math.radians(0))

        if (i.rotate_z == 1):
            rotZ = pyrr.matrix44.create_from_z_rotation(
                math.radians(i.rotate_grau))
        else:
            rotZ = pyrr.matrix44.create_from_z_rotation(math.radians(0))

        rotT = pyrr.matrix44.multiply(rotY, rotx)
        rotT = pyrr.matrix44.multiply(rotT, rotZ)
        i.model = pyrr.matrix44.multiply(i.model, rotT)

    if (shearFlag == 1):
        for i in lista_obj:
            i.model = pyrr.matrix44.multiply(i.model, i.shear)

    #verifica em cada objeto a translacao
    for i in lista_obj:
        translate = pyrr.matrix44.create_from_translation(
            [i.translate_x, i.translate_y, i.translate_z])
        i.model = pyrr.matrix44.multiply(i.model, translate)

    #coloca os valores da view
    view = matrix44.create_look_at(poscam, lookat, [0.0, 1.0, 0.0])
    #redimenciona o mundo
    projection = matrix44.create_orthogonal_projection(-2.0, 2.0, -2.0, 2.0,
                                                       -2.0, 2.0)

    # atribui uma variavel uniforme para matriz de transformacao
    uMat = glGetUniformLocation(shaderProgram, "model")
    #atribui uma variavel uniforme para view
    idView = glGetUniformLocation(shaderProgram, "view")
    #atribui uma variavel uniform para projection
    idProj = glGetUniformLocation(shaderProgram, "projection")

    # verificando se a reflections foi ativada
    if (reflectionFlag == 1):
        # atribuindo os uniformes para uma variavel
        idColor = glGetUniformLocation(shaderProgram, "objectColor")
        idLight = glGetUniformLocation(shaderProgram, "lightColor")
        idLightPos = glGetUniformLocation(shaderProgram, "lightPos")
        idViewPos = glGetUniformLocation(shaderProgram, "viewPos")
        reflec = glGetUniformLocation(shaderProgram, "reflecc")
        ambientForce = glGetUniformLocation(shaderProgram, "ambientForce")
        diffuseForce = glGetUniformLocation(shaderProgram, "diffuseForce")
        specularForce = glGetUniformLocation(shaderProgram, "specularForce")

    # Note that this is allowed, the call to glVertexAttribPointer registered VBO
    # as the currently bound vertex buffer object so afterwards we can safely unbind
    glBindBuffer(GL_ARRAY_BUFFER, 0)
    # Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs)
    glBindVertexArray(0)
Example #12
0
def drawlight(lista_luz):
    global shaderProgram
    global vao
    global vbo
    global projection
    global idProj
    global idView
    global model
    global view
    global uMat
    global poscam
    global lookat
    global ambient
    global diffuse
    global specular
    global lightMode

    glClearColor(0, 0, 0, 1)

    vertex_code = readShaderFile('none.vp')
    fragment_code = readShaderFile('none.fp')

    # compile shaders and program
    vertexShader = shaders.compileShader(vertex_code, GL_VERTEX_SHADER)
    fragmentShader = shaders.compileShader(fragment_code, GL_FRAGMENT_SHADER)
    shaderProgram = shaders.compileProgram(vertexShader, fragmentShader)

    # Create and bind the Vertex Array Object
    vao = GLuint(0)
    glGenVertexArrays(1, vao)
    glBindVertexArray(vao)

    obj = 'cube'
    # lendo os obj pegando vertices e normais
    vertices = np.array(lendo_obj(obj), dtype='f')
    print("vertices:", len(vertices) // 6)

    vbo = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, vbo)
    glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW)
    glVertexAttribPointer(
        0, 3, GL_FLOAT, False, 6 * sizeof(GLfloat),
        ctypes.c_void_p(3 *
                        sizeof(GLfloat)))  # first 0 is the location in shader
    glVertexAttribPointer(
        1, 3, GL_FLOAT, False, 6 * sizeof(GLfloat),
        ctypes.c_void_p(0))  # first 0 is the location in shader
    glEnableVertexAttribArray(0)
    glEnableVertexAttribArray(1)

    #verifica em cada objeto a scala
    for i in lista_luz:
        i.model = pyrr.matrix44.create_identity()
        scale = pyrr.matrix44.create_from_scale([0.03, 0.03, 0.03], dtype='f')
        i.model = pyrr.matrix44.multiply(i.model, scale)

    #verifica em cada objeto a translacao
    for i in lista_luz:
        translate = pyrr.matrix44.create_from_translation([i.x, i.y, i.z])
        i.model = pyrr.matrix44.multiply(i.model, translate)

    #coloca os valores da view
    view = matrix44.create_look_at(poscam, lookat, [0.0, 1.0, 0.0])
    #redimenciona o mundo
    projection = matrix44.create_orthogonal_projection(-2.0, 2.0, -2.0, 2.0,
                                                       -2.0, 2.0)

    # atribui uma variavel uniforme para matriz de transformacao
    uMat = glGetUniformLocation(shaderProgram, "model")
    #atribui uma variavel uniforme para view
    idView = glGetUniformLocation(shaderProgram, "view")
    #atribui uma variavel uniform para projection
    idProj = glGetUniformLocation(shaderProgram, "projection")

    # Note that this is allowed, the call to glVertexAttribPointer registered VBO
    # as the currently bound vertex buffer object so afterwards we can safely unbind
    glBindBuffer(GL_ARRAY_BUFFER, 0)
    # Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs)
    glBindVertexArray(0)