Example #1
0
def load_texture(texture_id, fname):
	gl.bindTexture(gl.TEXTURE_2D, texture_id) # All upcoming GL_TEXTURE_2D operations now have effect on our texture object
    
	# Set our texture parameters
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)	# Set texture wrapping to GL_REPEAT
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)

	# Set texture filtering
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	# Load, create texture and generate mipmaps

	img = PIL.Image.open(fname)
	img = PIL.ImageOps.flip(img)
	width, height = img.size
	data = img.convert("RGBA").tobytes("raw", "RGBA")
    # unsigned char* image = SOIL_load_image(FileSystem::getPath("resources/textures/container.jpg").c_str(), &width, &height, 0, SOIL_LOAD_RGB);

	gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, data)
	gl.generateMipmap(gl.TEXTURE_2D)
	gl.bindTexture(gl.TEXTURE_2D, 0)  # Unbind texture when done, so we won't accidentily mess up our texture.
Example #2
0
def main():
	glfw.setErrorCallback(error_callback)

	glfw.init()
	glfw.windowHint(glfw.CONTEXT_VERSION_MAJOR, 3)
	glfw.windowHint(glfw.CONTEXT_VERSION_MINOR, 3)
	glfw.windowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
	# glfw.windowHint(glfw.RESIZABLE, gl.FALSE)
	glfw.windowHint(glfw.OPENGL_FORWARD_COMPAT, 1)

	window = glfw.createWindow(800, 600, "LearnOpenGL")

	if window is None:
		print('could not open window.')
		glfw.terminate()
		sys.exit()

	window.makeContextCurrent()
	window.setKeyCallback(key_callback)

	err = gl.getError()
	if err:
		print("WINDOW OPEN ERROR:", err)

	shaderProgram = Shader(VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE)
	shaderProgram.use()

	vao = gl.genVertexArrays(1)[0]
	vbo, ebo = gl.genBuffers(2)

	# 1. Bind Vertex Array Object
	gl.bindVertexArray(vao)

    # 2. Copy our vertices array in a buffer for OpenGL to use
	gl.bindBuffer(gl.ARRAY_BUFFER, vbo)
	gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW)

    # 2. Copy our index array in a buffer for OpenGL to use
	gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebo)
	gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW)

	# Position attribute
	gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.position.offset)
	gl.enableVertexAttribArray(0)
	# Color attribute
	gl.vertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.color.offset)
	gl.enableVertexAttribArray(1)
	# TexCoord attribute
	gl.vertexAttribPointer(2, 2, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.texcoord.offset)
	gl.enableVertexAttribArray(2)

	# 4. unbind the vbo
	gl.bindBuffer(gl.ARRAY_BUFFER, 0)

	# 4. Unbind the VAO
	gl.bindVertexArray(0)

	# create the texture object, load the texture
    # Load and create a texture 
	texture1, texture2 = gl.genTextures(2)
    # ====================
    # Texture 1
    # ====================
 
	gl.bindTexture(gl.TEXTURE_2D, texture1) # All upcoming GL_TEXTURE_2D operations now have effect on our texture object
    
	# Set our texture parameters
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)	# Set texture wrapping to GL_REPEAT
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)

	# Set texture filtering
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	# Load, create texture and generate mipmaps

	img = PIL.Image.open('learningopengl/resources/textures/container.jpg')
	img = PIL.ImageOps.flip(img)
	width, height = img.size
	data = img.convert("RGBA").tobytes("raw", "RGBA")
    # unsigned char* image = SOIL_load_image(FileSystem::getPath("resources/textures/container.jpg").c_str(), &width, &height, 0, SOIL_LOAD_RGB);

	gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, data)
	gl.generateMipmap(gl.TEXTURE_2D)
	gl.bindTexture(gl.TEXTURE_2D, 0)  # Unbind texture when done, so we won't accidentily mess up our texture.
	
	# ===================
	# Texture 2
	# ===================
	gl.bindTexture(gl.TEXTURE_2D, texture2)

	# Set our texture parameters
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)

	# Set texture filtering
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)

	# Load, create texture and generate mipmaps
	img = PIL.Image.open('learningopengl/resources/textures/awesomeface.png')
	img = PIL.ImageOps.flip(img)
	width, height = img.size
	data = img.convert("RGBA").tobytes("raw", "RGBA")

	gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, data)
	gl.generateMipmap(gl.TEXTURE_2D)
	# SOIL_free_image_data(image);
	gl.bindTexture(gl.TEXTURE_2D, 0);

	while not window.shouldClose():
		glfw.pollEvents()

		framebuffer_width, framebuffer_height = window.getFramebufferSize()
		gl.viewport(0, 0, framebuffer_width, framebuffer_height)
		gl.clearColor(0.2, 0.3, 0.3, 1.0)
		gl.clear(gl.COLOR_BUFFER_BIT)

		shaderProgram.use()

		# Bind Textures using texture units
		gl.activeTexture(gl.TEXTURE0)
		gl.bindTexture(gl.TEXTURE_2D, texture1);
		gl.uniform1i(shaderProgram.getUniformLocation("ourTexture1"), 0);
		gl.activeTexture(gl.TEXTURE1);
		gl.bindTexture(gl.TEXTURE_2D, texture2);
		gl.uniform1i(shaderProgram.getUniformLocation("ourTexture2"), 1);

		gl.bindVertexArray(vao)
		gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, 0)
		# gl.drawArrays(gl.TRIANGLES, 0, 3); 
		gl.bindVertexArray(0)

		window.swapBuffers()

	gl.deleteVertexArrays([vao])
	gl.deleteBuffers([vbo, ebo])