Exemple #1
0
    def __init__(self, strVS, strFS):
        self.program = glutils.loadShaders(strVS=strVS, strFS=strFS)
        glUseProgram(self.program)
        self.pMatrixUniform = glGetUniformLocation(self.program, b'uPMatrix')
        self.mvMatrixUniform = glGetUniformLocation(self.program, b'uMVMatrix')

        self.tex2D = glGetUniformLocation(self.program, b'tex2D')          # 2D texture

        # define triangle strip vertices
        vertex_data = numpy.array([-0.5, -0.5, 0.0,
                                   0.5, -0.5, 0.0,
                                   -0.5, 0.5, 0.0,
                                   0.5, 0.5, 0.0], numpy.float32)

        # set up vertex array object (VAO)
        self.vao = glGenVertexArrays(1)
        glBindVertexArray(self.vao)

        # vertices
        self.vertex_buffer = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, self.vertex_buffer)

        glBufferData(GL_ARRAY_BUFFER, 4 * len(vertex_data), vertex_data, GL_STATIC_DRAW)  # set buffer data
        glEnableVertexAttribArray(0)            # enable vertex array
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)       # set buffer data pointer
        glBindVertexArray(0)              

        self.t = 0
        self.tex_id = glutils.loadTexture('star.png')
        self.showCircle = False                                 # show circle flag
Exemple #2
0
    def __init__(self):
        self.t = 0.0
        self.t = 0
        self.x = 0.0
        self.y = 0
        self.z = 0
        self.scale = 1.0
        self.rz = 0.0

        #initialise glut
        glutInit(sys.argv)
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
        self.width = 1000
        self.height = 1000
        self.aspect = self.width / float(self.height)
        glutInitWindowSize(1000, 1000)
        glutCreateWindow("The return of the Bunny")

        self.init()
        #initialize GL
        glutReshapeFunc(self.reshape)
        glutDisplayFunc(self.display)
        glutKeyboardFunc(self.keyboard)
        #glutSpecialFunc(self.glutSpecialKey)

        # load all the required textures here
        self.texid_sawblade = glutils.loadTexture('../assets/sawblade.jpg')
        self.texid_ground = glutils.loadTexture('../assets/ground_tile.jpg')
        self.texid_slab = glutils.loadTexture('../assets/slab.jpg')
        self.texid_star = glutils.loadTextureWithTransparency(
            '../assets/new_spark2.png')

        self.texid_cube = glutils.loadTexture('../assets/wood.jpg')
        self.texid_cone = glutils.loadTexture('../assets/blue.jpg')
        self.texid_sphere = glutils.loadTextureWithTransparency(
            '../assets/sphere.png')
        self.texid_bunny = glutils.loadTextureWithTransparency(
            '../assets/bunny.png')

        #create an emitter for the particle system
        self.emitter = Emitter(self.texid_star)
        self.frame = 0
        self.time = 0
        self.timebase = 0
        glutMainLoop()
Exemple #3
0
    def __init__(self, model, motion):
        self.model = model
        self.motion = motion
        self.bones = numpy.recarray((model.bones.shape[0], ),
                                    dtype=[
                                        ("rLoc", numpy.float32, (3, )),
                                        ("rRot", numpy.float32, (4, )),
                                        ("aLoc", numpy.float32, (3, )),
                                        ("aRot", numpy.float32, (4, )),
                                        ("aMat", numpy.float32, (4, 4)),
                                    ])
        self.frame = 0

        for (i, tex) in enumerate(model.texs):
            glActiveTexture(GL_TEXTURE0 + i)
            glBindTexture(GL_TEXTURE_2D, i)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
            glutils.loadTexture(numpy.asarray(Image.open(tex)))

        self.shader = glutils.Shader(self.vertSrc, self.fragSrc)
        self.shader.use()
        self.shader.attrib(b"aP", model.verts.vert)
        self.shader.attrib(b"aN", model.verts.norm)
        self.shader.attrib(b"aUv", model.verts.uv)

        glDepthFunc(GL_LEQUAL)
        glEnable(GL_DEPTH_TEST)

        glFrontFace(GL_CW)
        #glCullFace( GL_BACK )
        glEnable(GL_CULL_FACE)

        glBlendFuncSeparate(
            GL_SRC_ALPHA,
            GL_ONE_MINUS_SRC_ALPHA,
            GL_ONE,
            GL_ONE_MINUS_SRC_ALPHA,
        )
        glEnable(GL_BLEND)
Exemple #4
0
    def __init__(self):
        # create shader
        self.program = glutils.loadShaders(strVS, strFS)

        glUseProgram(self.program)

        self.pMatrixUniform = glGetUniformLocation(self.program, 
                                                   'uPMatrix')
        self.mvMatrixUniform = glGetUniformLocation(self.program, 
                                                  "uMVMatrix")
        self.colorU = glGetUniformLocation(self.program, "uColor")

        # color
        self.col0 = [1.0, 0.0, 0.0, 1.0]

        # texture 
        self.tex2D = glGetUniformLocation(self.program, "tex2D")

        # define quad vertices 
        quadV = [
            -0.5, -0.5, 0.0, 
            0.5, -0.5, 0.0, 
            -0.5, 0.5, 0.0,
             0.5, 0.5, 0.0
            ]

        # set up vertex array object (VAO)
        self.vao = glGenVertexArrays(1)
        glBindVertexArray(self.vao)
        # vertices
        self.vertexBuffer = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer)
        vertexData = numpy.array(quadV, numpy.float32)
        glBufferData(GL_ARRAY_BUFFER, 4*len(vertexData), vertexData, 
                     GL_STATIC_DRAW)
        # enable vertex array
        glEnableVertexAttribArray(0)
        # set buffer data
        glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer)
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)
        # unbind VAO
        glBindVertexArray(0)

        # time
        self.t = 0 

        # texture
        self.texId = glutils.loadTexture('test.png')

        # show circle?
        self.showCircle = False
Exemple #5
0
def build():
	global tars_dic
	global tex_Cu
	global tex_tar
	tex_box=glutils.loadTexture("box.png")
	tex_wood=glutils.loadTexture("wood.png")
	tex_grass=glutils.loadTexture("grass.png")
	tex_metal=glutils.loadTexture("metal.png")
	tex_sky=glutils.loadTexture("sky.png")
	tex_Cu=glutils.loadTexture("Cu.png")
	tex_tar=glutils.loadTexture("target.png")
	for i in range(-21,22):
		for j in range(-21,22):
			if i==-21 or i==21 or j==-21 or j==21:
				height_dic[(i,j)]=100
			else:
				height_dic[(i,j)]=0
	for i in range(-10,11):
		for j in range(-10,11):
			if (i+j)%2==0:
				t=thing(cube_vertices,cube_tex_ma,tex_wood,i,j,-1,1,0)
			else:
				t=thing(cube_vertices,cube_tex_ma,tex_grass,i,j,-1,1,0)
			things.append(t)
			height_dic[(i,j)]+=1
	level=[[(-3,3),(-2,3),(-1,3),(0,3),(1,3),(2,3),(3,3),(-3,4),(-2,4),(-1,4),(0,4),(1,4),(2,4),(3,4),(-2,5),(-1,5),(0,5),(1,5),(2,5),(-1,6),(0,6),(1,6),(0,7)],
	[(-2,5),(-1,5),(0,5),(1,5),(2,5),(-1,6),(0,6),(1,6),(0,7)],[(-1,6),(0,6),(1,6),(0,7)],[(0,7)]]
	for L in range(4):
		for each in level[L]:
			t=thing(cube_vertices,cube_tex_ma,tex_box,each[0],each[1],L,1,0)
			height_dic[(each[0],each[1])]+=1
			things.append(t)
	tars=[(-2,3),(-4,5),(5,7),(0,0),(4,5),(-7,-8),(-4,-2)]
	for each in tars:
		t=thing(cube_vertices,cube_tex_ma,tex_tar,each[0],each[1],5,1,0)
		t.is_tar=1
		tars_dic[(each[0],each[1])]=t
		things.append(t)
	t2=thing(surface_vertices,surface_tex_ma,tex_sky,1,0,5,40,0)
	things.append(t2)
	t3=thing(surface_vertices,surface_tex_ma,tex_metal,1,0,-2,40,0)
	things.append(t3)	
	t4=thing(surface_vertices2,surface_tex_ma,tex_sky,0,20,0,1,0)
	things.append(t4)
	t5=thing(surface_vertices2,surface_tex_ma,tex_sky,0,-20,0,1,0)
	things.append(t5)	
	t6=thing(surface_vertices3,surface_tex_ma,tex_sky,20,0,0,1,0)
	things.append(t6)
	t7=thing(surface_vertices3,surface_tex_ma,tex_sky,-20,0,0,1,0)
	things.append(t7)
Exemple #6
0
    def __init__(self):
        # Compile and load the shaders.
        self.program = glutils.loadShaders(
            str_vector_shader, str_fragment_shader)

        glUseProgram(self.program)

        # Connect the variables in the Python code with those in the shaders
        # Returns the location of a uniform variable
        self.p_matrix_uniform = glGetUniformLocation(self.program, 'uPMatrix')
        self.mv_matrix_uniform = glGetUniformLocation(
            self.program, 'uMVMatrix')

        # Texture
        self.tex2D = glGetUniformLocation(self.program, 'tex2D')

        # Define triangle strip vertices
        vertex_data = numpy.array([
            -0.5, -0.5, 0.0,
            0.5, -0.5, 0.0,
            -0.5, 0.5, 0.0,
            0.5, 0.5, 0.0,
        ], numpy.float32)

        # Set up vertex array object(VAO)
        self.vao = glGenVertexArrays(1)
        glBindVertexArray(self.vao)
        # Vertices
        self.vertex_buffer = glGenBuffers(1)  # Create VBO
        # VBO is a memory buffer in the high speed memory of your video card
        # designed to hold information about vertices
        glBindBuffer(GL_ARRAY_BUFFER, self.vertex_buffer)
        # Set buffer data
        glBufferData(GL_ARRAY_BUFFER, 4*len(vertex_data),
                     vertex_data, GL_STATIC_DRAW)
        # Enable vertex array
        glEnableVertexAttribArray(0)
        # Set buffer data pointer
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)
        # Unbind VAO
        glBindVertexArray(0)
        # Loads the image as an OpenGL texture
        self.tex_id = glutils.loadTexture('star.png')

        self.t = 0  # time
        self.show_circle = False
Exemple #7
0
    def __init__(self, numP):
        # no. of particles
        self. numP = numP
        # time variable
        self.t = 0.0	
        self.lifeTime = 5.0
        self.startPos = numpy.array([0.0, 0.0, 0.5])
        # load texture
        self.texid = glutils.loadTexture('star.png')
        # create shader
        self.program = glutils.loadShaders(strVS, strFS)
        glUseProgram(self.program)

        # set sampler
        texLoc = glGetUniformLocation(self.program, b"uTex")
        glUniform1i(texLoc, 0)

        # uniforms
        self.timeU =  glGetUniformLocation(self.program, b"uTime")
        self.lifeTimeU =  glGetUniformLocation(self.program, b"uLifeTime")
        self.pMatrixUniform = glGetUniformLocation(self.program, b'uPMatrix')
        self.mvMatrixUniform = glGetUniformLocation(self.program, 
                                                  b"uMVMatrix")
        self.bMatrixU = glGetUniformLocation(self.program, b"bMatrix")
        self.colorU = glGetUniformLocation(self.program, b"uColor")
        self.samplerU = glGetUniformLocation(self.program, b"uSampler")
        self.posU = glGetUniformLocation(self.program, b"uPos")

        # attributes
        self.vertIndex = glGetAttribLocation(self.program, b"aVert")
        self.texIndex = glGetAttribLocation(self.program, b"aTexCoord")
        self.time0Index = glGetAttribLocation(self.program, b"aTime0")
        self.velIndex = glGetAttribLocation(self.program, b"aVel")

        # render flags
        self.enableBillboard = True
        self.disableDepthMask = True
        self.enableBlend = True

        # which texture to use
        self.useStarTexture = True
        # restart - first time
        self.restart(numP)
Exemple #8
0
    def __init__(self, numP):
        # no. of particles
        self.numP = numP
        # time variable
        self.t = 0.0
        self.lifeTime = 5.0
        self.startPos = numpy.array([0.0, 0.0, 0.5])
        # load texture
        self.texid = glutils.loadTexture('star.png')
        # create shader
        self.program = glutils.loadShaders(strVS, strFS)
        glUseProgram(self.program)

        # set sampler
        texLoc = glGetUniformLocation(self.program, b"uTex")
        glUniform1i(texLoc, 0)

        # uniforms
        self.timeU = glGetUniformLocation(self.program, b"uTime")
        self.lifeTimeU = glGetUniformLocation(self.program, b"uLifeTime")
        self.pMatrixUniform = glGetUniformLocation(self.program, b'uPMatrix')
        self.mvMatrixUniform = glGetUniformLocation(self.program, b"uMVMatrix")
        self.bMatrixU = glGetUniformLocation(self.program, b"bMatrix")
        self.colorU = glGetUniformLocation(self.program, b"uColor")
        self.samplerU = glGetUniformLocation(self.program, b"uSampler")
        self.posU = glGetUniformLocation(self.program, b"uPos")

        # attributes
        self.vertIndex = glGetAttribLocation(self.program, b"aVert")
        self.texIndex = glGetAttribLocation(self.program, b"aTexCoord")
        self.time0Index = glGetAttribLocation(self.program, b"aTime0")
        self.velIndex = glGetAttribLocation(self.program, b"aVel")

        # render flags
        self.enableBillboard = True
        self.disableDepthMask = True
        self.enableBlend = True

        # which texture to use
        self.useStarTexture = True
        # restart - first time
        self.restart(numP)
Exemple #9
0
    def __init__(self):
        #从字符串调用着色器,编译并链接成一个OpenGL程序对象
        self.program = glutils.loadShaders(strVS, strFS)
        #设置代码使用特定的“程序对象”(一个项目可能有多个程序)
        glUseProgram(self.program)

        #从着色器中取到pMatrixUniform,mvMatrixUniform,tex2D参数的值
        self.pMatrixUniform = glGetUniformLocation(self.program, b"uPMatrix")
        self.mvMatrixUniform = glGetUniformLocation(self.program, b"uMVMatrix")
        self.tex2D = glGetUniformLocation(self.program, b"tex2D")

        #定义三角形带的顶点数组,用于绘制正方形
        vertexData = numpy.array(
            [-0.5, -0.5, 0.0, 0.5, -0.5, 0.0, -0.5, 0.5, 0.0, 0.5, 0.5, 0.0],
            numpy.float32)

        #创建一个VAO,并绑定到该VAO,接下来的所有调用将绑定到它
        self.vao = glGenVertexArrays(1)
        glBindVertexArray(self.vao)
        #创建一个VBO用来管理顶点数据和渲染
        self.vertexBuffer = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer)
        #根据已定义的顶点,设置缓冲区数据
        glBufferData(GL_ARRAY_BUFFER, 4 * len(vertexData), vertexData,
                     GL_STATIC_DRAW)
        #着色区可以访问这些数据
        glEnableVertexAttribArray(0)
        #设置顶点属性数组的位置和数据格式,属性的下标是0,组件个数是3,顶点的数据类型是GL_FLOAT
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)
        #取消VAO绑定
        glBindVertexArray(0)

        self.t = 0
        #将图像加载为OpenGL的纹理,返回的纹理ID用于渲染
        self.texId = glutils.loadTexture('test.png')

        self.showCircle = False
Exemple #10
0
    def __init__(self):
        # create shader
        vShader = ""
        with open("shaders/spinny.v.glsl", 'r') as src:
            vShader = src.read()
        fShader = ""
        with open("shaders/spinny.f.glsl", 'r') as src:
            fShader = src.read()

        self.program = glutils.loadShaders(vShader, fShader)

        self.bkpgm = glutils.loadShaders(
            *[open("shaders/bkgnd.%s.glsl" % i, "r").read() for i in "vf"])
        self.backTex = glGetUniformLocation(self.bkpgm, b'tex2D')
        self.bkId = glutils.loadTexture('star.png')
        _, frame = cap.read()
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frame = cv2.flip(frame, 0)
        height, width, _ = frame.shape
        glBindTexture(GL_TEXTURE_2D, self.bkId)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
                     GL_UNSIGNED_BYTE, frame)
        glBindTexture(GL_TEXTURE_2D, 0)

        glUseProgram(self.program)

        self.pMatrixUniform = glGetUniformLocation(self.program, b'uPMatrix')
        self.mvMatrixUniform = glGetUniformLocation(self.program, b'uMVMatrix')
        # texture
        self.tex2D = glGetUniformLocation(self.program, b'tex2D')

        # define triange strip vertices
        vertexData = np.array([
            -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5,
            0.5, -0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5,
            -0.5, 0.5, -0.5, -0.5, -0.5, 0.5, -0.5, -0.5, -0.5, 0.5, -0.5,
            -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5
        ], np.float32)

        # set up vertex array object (VAO)
        self.vao = glGenVertexArrays(1)
        glBindVertexArray(self.vao)
        # vertices
        self.vertexBuffer = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer)
        # set buffer data
        glBufferData(GL_ARRAY_BUFFER, 14 * len(vertexData), vertexData,
                     GL_STATIC_DRAW)
        # enable vertex array
        glEnableVertexAttribArray(0)
        # set buffer data pointer
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)
        # unbind VAO
        glBindVertexArray(0)

        # time
        self.t = 0

        # texture
        self.texId = glutils.loadTexture('star.png')
        _, frame = cap.read()
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        height, width, _ = frame.shape
        glBindTexture(GL_TEXTURE_2D, self.texId)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
                     GL_UNSIGNED_BYTE, frame)
        glBindTexture(GL_TEXTURE_2D, 0)

        # show circle?
        self.showCircle = False

        ## opencv stuff
        self.sift = cv2.xfeatures2d.SIFT_create()
        ref_img = cv2.imread(REFERENCE_IMG, 0)
        self.ref_kp, self.ref_des = self.sift.detectAndCompute(ref_img, None)

        FLANN_INDEX_KDTREE = 0
        index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
        search_params = dict(checks=50)
        self.flann = cv2.FlannBasedMatcher(index_params, search_params)