Computer Graphics: Numpy, PyGame, and GPU Programming

Summary

These lecture notes cover the basics of computer graphics, including the Numpy library for matrix operations, the PyGame library for creating video games, and the use of GPUs with OpenGL and Direct3D. The document also discusses topics such as 3D object projection, translation, scaling, rotation, and game engines like Unity3D.

Full Transcript

numpy library numpy python library that operates with matrices import numpy as np a = np.array([3,2,1]) b = np.array([,,]) c = np.array([6,1,9]) x = a.dot(b) y = a+c print x.tolist() print y.tolist() PyGame cross-platform set of Python modules it is designed f...

numpy library numpy python library that operates with matrices import numpy as np a = np.array([3,2,1]) b = np.array([,,]) c = np.array([6,1,9]) x = a.dot(b) y = a+c print x.tolist() print y.tolist() PyGame cross-platform set of Python modules it is designed for writing video games includes computer graphics and sound libraries PyGame Library import pygame This code creates window with width 400 pygame.init() and height 300 pixels. screen = pygame.display.set_mode((400, 300)) done = False Draws rectangle with on x and y locations is_blue = True By pressing UP, DOWN, LEFT, RIGHT you color = (255, 100, 0) can change location of rectangle x = 30 y = 30 while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True pressed = pygame.key.get_pressed() if pressed[pygame.K_UP]: y -= 3 if pressed[pygame.K_DOWN]: y += 3 if pressed[pygame.K_LEFT]: x -= 3 if pressed[pygame.K_RIGHT]: x += 3 pygame.draw.rect(screen, color, pygame.Rect(x, y, 60, 60)) pygame.display.flip() Drawing in PyGame: pygame.draw.line(surface, color, (startX, startY), (endX, endY), width) pygame.draw.line(surface, color, (startX, startY), (endX, endY), width) # draw a rectangle pygame.draw.rect(surface, color, pygame.Rect(10, 10, 100, 100), 10) # draw a circle pygame.draw.circle(surface, color, (300, 60), 50, 10) PyGame tutorial links http://pygametutorials.wikidot.com/system:page-tags/tag/pygame http://gamedevelopment.tutsplus.com/tutorials/how-to-learn-pygame--cms-2 4184 Translate image Translating image is performed by adding translation matrix Scale image Scaling can be used for zooming, or making some figure bigger or smaller Same to rotation, by default it scales correspondingly to origin (0, 0) point. So to scale it according to some specific point, do same operation as for rotation Rotate image To rotate some set of points, multiply each point vector by rotate matrix r rotated_point = r*p In this case points of polygons rotates around (0, 0) point Rotation around specific point To rotate around some specific center, point should be subtracted by center point and then multiplied by rotate matrix and center must be added again r_point = rotate.dot(p-center)+center Shear image Shearing images also is done by multiplying shear matrix to point images and screenshots are taken from: http://www.willamette.edu/~gorr/classes/GeneralGraphics/Transforms/transforms2d.htm Perspective projection When the human eye views a scene, objects in the distance appear smaller than objects close by - this is known as perspective. While orthographic projection ignores this effect to allow accurate measurements, perspective projection shows distant objects as smaller to provide additional realism. The perspective projection requires a more involved definition as compared to orthographic projections. A conceptual aid to understanding the mechanics of this projection is to imagine the 2D projection as though the object(s) are being viewed through a camera viewfinder. The camera's position, orientation, and field of view control the behavior of the projection transformation. The following variables are defined to describe this transformation: Projection on x, y plane projecting 3d object onto 2d image axyz - coordinates of point cxyz - coordinates of camera θxyz - three angles of camera bxyz - coordinates of point project on 2d screen Graphics processing unit What is GPU? GPU - Graphics processing unit GPU is a specialized electronic circuit designed to rapidly manipulate and alter memory to accelerate the creation of images in a frame buffer intended for output to a display Modern GPUs use transistors to do calculations related to 3D computer graphics. They were initially used to accelerate the memory-intensive work of texture mapping and rendering polygons, later adding units to accelerate geometric calculations such as the rotation and translation of vertices into different coordinate systems. CPU vs GPU CPU performs operations faster GPU usually is slower than CPU CPU can do complex operations GPU has limited list of operations, such CPU works in a sequence as: ○ matrix, vector multiplication Analogy: plane is faster, but when you need to GPU works in parallel send big amount of things (operations), it is better to use ship How to draw graphics with the use of GPU? OpenGL OpenGL is an API (application programming interface) to work with GPU. OpenGL works with many different programming languages and platforms, such as Python, Java, Javascript, Android Direct3D Direct3D is analog of OpenGL of Microsoft company Windows and XBox 3D games can be created by using Direct3D Game Engines A game engine is a software framework designed for the creation and development of video games. Developers use them to create games for consoles, mobile devices and personal computers. The core functionality: a rendering engine (“renderer”) for 2D or 3D graphics a physics engine or collision detection (and collision response) sound, scripting, animation, artificial intelligence, networking, streaming, memory management, threading, localization support, and a scene graph. The process of game development is often economized, in large part, by reusing/adapting the same game engine to create different games, or to make it easier to "port" games to multiple platforms http://www.develop-online.net/tools-and-tech/the-top-16-game-engines-for-2 Unity3D Unity is a cross-platform game engine developed by Unity Technologies and used to develop video games for PC, consoles, mobile devices and websites. Unity Pro is available for a fee and Unity Personal has no fee; it is available for any use to individuals or companies with profit less than US $100,000. Unity Technologies made the complete engine available for free including all features, less source code and premium support. Unity is noted for an ability to target games to multiple platforms.