Exemple #1
0
    def DrawNoTex(self, position, size, rotate, color, Grid, Selected):
        self.shader.UseProgram()

        model = glm.fmat4(1.0)
        model = glm.translate(model, glm.vec3(position, 0.0))

        model = glm.translate(model, glm.vec3(0.5 * size.x, 0.5 * size.y, 0.0))
        model = glm.rotate(model, glm.radians(rotate), glm.vec3(1.0, 0.0, 0.0))
        model = glm.translate(model, glm.vec3(-0.5 * size.x, -0.5 * size.y,
                                              0.0))

        model = glm.scale(model, glm.vec3(size, 1.0))

        glUniformMatrix4fv(glGetUniformLocation(self.shader.ID, "model"), 1,
                           GL_FALSE, glm.value_ptr(model))

        glUniform3f(glGetUniformLocation(self.shader.ID, "spriteColor"),
                    color.x, color.y, color.z)
        glUniform2f(glGetUniformLocation(self.shader.ID, "FullGrid"), Grid.x,
                    Grid.y)
        glUniform2f(glGetUniformLocation(self.shader.ID, "CurrCoord"),
                    Selected.x, Selected.y)

        # glActiveTexture(GL_TEXTURE0)

        glBindVertexArray(self.VAO)
        glDrawArrays(GL_TRIANGLES, 0, 6)
        glBindVertexArray(0)
Exemple #2
0
    def DrawSprite(self, texture, position, size, rotate, color):
        self.shader.UseProgram()

        model = glm.fmat4(1.0)
        model = glm.translate(model, glm.vec3(position, 0.0))
        # TRS
        model = glm.translate(model, glm.vec3(0.5 * size.x, 0.5 * size.y, 0.0))
        model = glm.rotate(model, rotate, glm.vec3(0.0, 0.0, 1.0))
        model = glm.translate(model, glm.vec3(-0.5 * size.x, -0.5 * size.y,
                                              0.0))

        model = glm.scale(model, glm.vec3(size, 1.0))

        glUniformMatrix4fv(glGetUniformLocation(self.shader.ID, "model"), 1,
                           GL_FALSE, glm.value_ptr(model))

        glUniform3f(glGetUniformLocation(self.shader.ID, "spriteColor"),
                    color.x, color.y, color.z)

        glActiveTexture(GL_TEXTURE0)
        texture.BindTexture()

        glBindVertexArray(self.VAO)
        glDrawArrays(GL_TRIANGLES, 0, 6)
        glBindVertexArray(0)
def main():
    if not glfw.init():
        return 0

    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
    glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)

    screenWidth = 800
    screenHeight = 600

    window = glfw.create_window(screenWidth, screenHeight,
                                "OPENGL_TextureExample", None, None)

    if not window:
        print("Window Creation Failed")
        glfw.terminate()
        return 0

    glfw.make_context_current(window)
    glfw.set_framebuffer_size_callback(window, framebuffer_size_callback)
    glfw.set_key_callback(window, key_callback)

    #                       position      color          tex coords
    vertices = numpy.array([
        0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.5, -0.5, 0.0, 0.0, 1.0, 0.0,
        1.0, 1.0, -0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.5, -0.5, 0.0, 0.0,
        1.0, 0.0, 1.0, 1.0, -0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, -0.5,
        -0.5, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0
    ],
                           dtype="f")
    myShader.Compile()

    VBO = GLuint(0)
    VAO = GLuint(0)
    VAO = glGenVertexArrays(1)
    VBO = myRenderer.GenBuffer(1)

    glBindVertexArray(VAO)
    myRenderer.BindBuffer(GL_ARRAY_BUFFER, VBO)
    myRenderer.BufferData(GL_ARRAY_BUFFER, 4 * (numpy.size(vertices)),
                          vertices, GL_STATIC_DRAW)

    # Get vertex position
    # position = myRenderer.GetAttribLocation(myShader.ID, "aPos")
    myRenderer.VertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 32,
                                   ctypes.c_void_p(0))
    myRenderer.EnableVertexArribArray(0)

    # get color
    # Color = myRenderer.GetAttribLocation(myShader.ID, "aColor")
    myRenderer.VertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 32,
                                   ctypes.c_void_p(12))
    myRenderer.EnableVertexArribArray(1)

    # get tex coords
    # TexCoords = myRenderer.GetAttribLocation(myShader.ID, "aTexCoord")
    myRenderer.VertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 32,
                                   ctypes.c_void_p(24))
    myRenderer.EnableVertexArribArray(2)

    # load and create Texture
    Texture = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, Texture)

    # set texture wrapping parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
    # texture filtering parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)

    #glEnable(GL_DEPTH_TEST)
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
    # load image
    ImgSource = Image.open(IMAGEPATH)
    if not ImgSource:
        print("IMAGE NOT FOUND")
        return 0
    imgWidth, imgHeight = ImgSource.size

    ImgSource = ImgSource.convert("RGBA")
    ImgArray = ImgSource.tobytes()
    print(ImgArray)
    if not ImgArray:
        print("ERROR CONVERTING IMAGE tobytes()")
        return 0

    # glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imgWidth, imgHeight, 0, GL_RGBA,
                 GL_UNSIGNED_BYTE, ImgArray)
    # glGenerateMipmap(GL_TEXTURE_2D)

    myShader.UseProgram()

    scale_loc = glGetUniformLocation(myShader.ID, 'scale')
    rotate_loc = glGetUniformLocation(myShader.ID, 'rotate')
    translate_loc = glGetUniformLocation(myShader.ID, 'translate')
    #model_loc = glGetUniformLocation(myShader.ID, 'model')
    #view_loc = glGetUniformLocation(myShader.ID, 'view')
    # projection_loc = glGetUniformLocation(myShader.ID, 'P')

    projection = glm.mat4(1)
    # using glm for view frustum // perspective(fov, aspect ratio, near clipping plane, far clipping plane)
    # projection = glm.perspective(45.0, screenWidth / screenHeight, 0.1, 1000.0)

    # projection = glm.ortho(0.0, 800.0, 0.0, 600.0, 0.1, 1000.0)

    scale = glm.fmat4(1)
    rot = glm.fmat4(1)
    translate = glm.fmat4(1)

    glUniformMatrix4fv(scale_loc, 1, GL_FALSE, glm.value_ptr(scale))
    glUniformMatrix4fv(rotate_loc, 1, GL_FALSE, glm.value_ptr(rot))
    glUniformMatrix4fv(translate_loc, 1, GL_FALSE, glm.value_ptr(translate))
    #glUniformMatrix4fv(model_loc, 1, GL_FALSE, glm.value_ptr(model))
    #glUniformMatrix4fv(view_loc, 1, GL_FALSE, glm.value_ptr(view))
    # glUniformMatrix4fv(projection_loc, 1, GL_FALSE, glm.value_ptr(projection))

    glUniform1i(glGetUniformLocation(myShader.ID, "Texture"), 0)

    while not glfw.window_should_close(window):
        myRenderer.ClearColor(0.2, 0.2, 0.4, 1.0)
        myRenderer.Clear()

        glDrawArrays(GL_TRIANGLES, 0, 6)

        glfw.poll_events()
        glfw.swap_buffers(window)

    glfw.terminate()
    return 0
Exemple #4
0
# myPlayer = Player(0.0, 0.0, glm.mat4(1))

cameraPos = glm.vec3(0.0, 0.0, 1.0)
cameraFront = glm.vec3(0.0, 100.0, 0.0)
cameraUp = glm.vec3(0.0, 1.0, 0.0)

myCamera = Camera(cameraPos, cameraFront, cameraUp, 0.5)

model = glm.mat4(1)
view = glm.mat4(1)
projection = glm.mat4(1)
projection = glm.ortho(0.0, 800.0, 0.0, 600.0, 0.1, 1000.0)

view = glm.lookAt(myCamera.camPos, myCamera.camPos + myCamera.camFront,
                  myCamera.camUp)
translate = glm.fmat4(1)

MVP = projection * view * model * translate

# img = Image.open(FILEPATH)
#
# width, height = img.size
#
# img1 = img.convert('RGBA')
# img2 = img1.tobytes("PNG")
# img1.show()
# print(img2)
# print("\n")
# print(width, height)

    def Draw(self,
             texture,
             position,
             size,
             rotate,
             color,
             grid,
             selected,
             TexID,
             VerticalFlip=0):

        self.Shader.UseProgram()

        model = glm.fmat4(1.0)
        model = glm.translate(model, glm.vec3(position, 0.0))

        model = glm.translate(model, glm.vec3(0.5 * size.x, 0.5 * size.y, 0.0))
        model = glm.rotate(model, rotate, glm.vec3(0.0, 0.0, 1.0))
        model = glm.translate(model, glm.vec3(-0.5 * size.x, -0.5 * size.y,
                                              0.0))

        model = glm.scale(model, glm.vec3(size, 1.0))

        glPos1 = model * glm.vec4(0.0, 1.0, 0.0, 1.0)
        glPos2 = model * glm.vec4(1.0, 0.0, 0.0, 1.0)
        glPos3 = model * glm.vec4(0.0, 0.0, 0.0, 1.0)
        glPos4 = model * glm.vec4(1.0, 1.0, 0.0, 1.0)
        # 1 -- (selected.x / grid.x)
        # 0 -- ((selected.x - 1) / grid.x)
        if VerticalFlip == 1:
            # Pos                 color                            TexCoords                                    TexID
            vertices = numpy.array([
                glPos1.x, glPos1.y, color.x, color.y, color.z,
                (selected.x / grid.x), (selected.y / grid.y), TexID,
                glPos2.x, glPos2.y, color.x, color.y, color.z,
                ((selected.x - 1) / grid.x), ((selected.y - 1) / grid.y),
                TexID, glPos3.x, glPos3.y, color.x, color.y, color.z,
                (selected.x / grid.x), ((selected.y - 1) / grid.y), TexID,
                glPos1.x, glPos1.y, color.x, color.y, color.z,
                (selected.x / grid.x), (selected.y / grid.y), TexID,
                glPos4.x, glPos4.y, color.x, color.y, color.z,
                ((selected.x - 1) / grid.x), (selected.y / grid.y), TexID,
                glPos2.x, glPos2.y, color.x, color.y, color.z,
                ((selected.x - 1) / grid.x), ((selected.y - 1) / grid.y), TexID
            ],
                                   dtype="f")
        else:
            # Pos                 color                            TexCoords
            # TexID
            vertices = numpy.array([
                glPos1.x, glPos1.y, color.x, color.y, color.z,
                ((selected.x - 1) / grid.x), (selected.y / grid.y), TexID,
                glPos2.x, glPos2.y, color.x, color.y, color.z,
                (selected.x / grid.x), ((selected.y - 1) / grid.y), TexID,
                glPos3.x, glPos3.y, color.x, color.y, color.z,
                ((selected.x - 1) / grid.x), ((selected.y - 1) / grid.y),
                TexID, glPos1.x, glPos1.y, color.x, color.y, color.z,
                ((selected.x - 1) / grid.x), (selected.y / grid.y), TexID,
                glPos4.x, glPos4.y, color.x, color.y, color.z,
                (selected.x / grid.x), (selected.y / grid.y), TexID, glPos2.x,
                glPos2.y, color.x, color.y, color.z, (selected.x / grid.x),
                ((selected.y - 1) / grid.y), TexID
            ],
                                   dtype="f")

        if self.TextureIndex >= self.MaxTexture:
            print(
                "ERROR : Texture binding limit reached, currently set at : " +
                str(self.MaxTexture))
            exit()

        if (texture not in self.Textures) and (TexID < self.MaxTexture):
            self.Textures[TexID] = texture
            # self.TextureIndex = self.TextureIndex + 1

        self.Objects.append(vertices)