40
Bina Nusantara 1

T0074-02 ok

Embed Size (px)

DESCRIPTION

scribd is a dumb shit ok

Citation preview

Page 1: T0074-02 ok

Bina Nusantara 1

Page 2: T0074-02 ok

Lecture 02 Computer Graphics System

Subjects : T0074 / Computer Graphics Year : 2012

Bina Nusantara 2

Page 3: T0074-02 ok

Bina Nusantara

Conceptual model for computer graphicsConceptual model for computer graphics

Overview of graphics system and graphics system Overview of graphics system and graphics system H/W such as CRT and other input devicesH/W such as CRT and other input devices

Program with primitive instruction of openGLProgram with primitive instruction of openGL

Lecture Outlines

3

Page 4: T0074-02 ok

Bina Nusantara

Application model : • a database of description and properties.

– Description of primitives that defines the shape of the objects

– Object attributes like line styles, color, texture that defines the look of the objects

– Connectivity relations and positioning data that defines how component fit together

– Textual, numeric data (equations, formulas, etc) and procedures often included in the model

Conceptual Model for CG

4

Page 5: T0074-02 ok

Bina Nusantara

Conceptual Model for CG

Application program : • a collection of output plotting routines based on 2D or

3D geometry of the object to be displayed.– View transform : specifies what part of the world scene is to

be displayed and convert those points into view coordinates points.

– Maps data to device coordinates : all geometric and non geometric details in the data structure are converted and passed to graphic system (e.g. objects are scan-converted into pixels and the frame buffer displayed.

5

Page 6: T0074-02 ok

Bina Nusantara

Conceptual Model for CG

Graphics System– High level language interface to graphical devices.– Intended for development of portable code.– Standard graphical systems include

• Graphical Kernel System (GKS), GKS+• Programmers Hierarchical Interactive Graphics System

(PHIGS), PHIGS+• OpenGL, DirectX, QuickDraw 3D, VRML, Open Inventor, X-

Windows.– Requirement of….. 6

Page 7: T0074-02 ok

Bina Nusantara

– Requirements of Graphical Systems• Portability

– Device Independence (concept of a logical device)

– language Independence (language bindings for graphical systems)

– Computer Independence (trade-off between standard hardware and high performance hardware).

• Programmer Independence

Conceptual Model for CG

7

Page 8: T0074-02 ok

Bina Nusantara

Graphics Display H/W

• Raster graphics architecture with display processor cut Raster graphics architecture with display processor cut down the load of the CPU down the load of the CPU

• Performs scan line conversion and raster operations Performs scan line conversion and raster operations

• CPU and graphics processor communicate via queuing CPU and graphics processor communicate via queuing system allowing CPU to get on with other jobs while system allowing CPU to get on with other jobs while graphics processor handles the displaygraphics processor handles the display

8

Page 9: T0074-02 ok

Bina Nusantara

Frame Buffer

Frame Buffer : A memory array in which the computer stores an image

- On most computers, separate memory bank from main memory

- A true-color (24-bit or 32-bit) frame buffer stores one byte each for red, green, and blue. Each pixel can thus be one of 224 colors 9

Page 10: T0074-02 ok

Bina Nusantara

Frame Buffer

• An indexed-color (8-bit or pseudo color) frame buffer stores An indexed-color (8-bit or pseudo color) frame buffer stores one byte per pixel (also: GIF image format)one byte per pixel (also: GIF image format)• This byte indexes into a color map or lookup tableThis byte indexes into a color map or lookup table

10

Page 11: T0074-02 ok

Bina Nusantara

Video Controller Layout

• Raster scan generator is activated each time the screen is Raster scan generator is activated each time the screen is refreshed. It does two things :refreshed. It does two things :

-- controls the X and Y addresses registers, giving the controls the X and Y addresses registers, giving the x x and y coordinates of the pixel.and y coordinates of the pixel.-- generates the correct vertical and horizontal generates the correct vertical and horizontal deflectiondeflection

• The X and Y address registers combined by the linear The X and Y address registers combined by the linear address calculator access the relevant part of memory for address calculator access the relevant part of memory for the given pixelthe given pixel

11

Page 12: T0074-02 ok

Bina Nusantara

Cathode Ray Tubes (CRTs)• Most common display device today• Evacuated glass bottle (last of the vacuum tube)• Heating element (filament)• Electrons pulled towards anode focusing cylinder• Vertical and horizontal deflection plates• Beam strikes phosphor coating on front of tube

Display Devices : CRT’s

12

Page 13: T0074-02 ok

Bina Nusantara

Display Devices : CRT’s• A CRT can operate as a A CRT can operate as a random scan (vector display)random scan (vector display) or a or a raster scan raster scan

(raster display)(raster display)

– Vector DisplayVector DisplayElectron beam directed only at parts of screen containing the picture.Electron beam directed only at parts of screen containing the picture.

– Early computer displays : basically an oscilloscopeEarly computer displays : basically an oscilloscope

– Control X, Y with vertical/horizontal plateControl X, Y with vertical/horizontal plate

– Often used intensity as ZOften used intensity as Z

– Raster DisplayRaster DisplayElectron beam scans whole screen, with the intensity set to ON or OFF Electron beam scans whole screen, with the intensity set to ON or OFF to produce the picture.to produce the picture.

– Picture composed of a set of points (pixels). Picture composed of a set of points (pixels).

– Screen divided into scan linesScreen divided into scan lines

– Horizontal and vertical retrace of beam.Horizontal and vertical retrace of beam.

– Interlaced and non-interlaced devices.Interlaced and non-interlaced devices.13

Page 14: T0074-02 ok

Bina Nusantara

Display Devices : CRT’s

• Raster Displays– Frame must be “refreshed” to draw new images

• Raster : A rectangular array of points or dots• Pixel : One dot or picture element of the raster• Scanline : A row of pixels• Rasterize : find the set of pixels corresponding to a

2D shape (line, circle, polygon) – As new pixels are struck by electron beam, others are

decaying– Electron beam must hit all pixels frequently to eliminate

flicker– Critical fusion frequency

• Typically 60 times/sec• Varies with intensity, individuals, phosphor

persistence, lighting...

14

Page 15: T0074-02 ok

Bina Nusantara

• Color CRTs have– Three electron guns– A metal shadow mask to differentiate the beams

Color CRT’s

15

Page 16: T0074-02 ok

Bina Nusantara

• Raster CRT pros:– Allows solids, not just wireframes– Leverages low-cost CRT technology (i.e., TVs)– Bright! Display emits light

• Cons:– Requires screen-size memory array– Discreet sampling (pixels)– Practical limit on size (call it 40 inches)– Bulky– Finicky (convergence, warp, etc)

Display Devices : Raster CRT’s

16

Page 17: T0074-02 ok

Bina Nusantara

• Liquid Crystal Displays (LCDs)– LCDs: organic molecules, naturally in crystalline state,

that liquefy when excited by heat or E field– Crystalline state twists polarized light 90º.

Display Devices : LCD’s

17

Page 18: T0074-02 ok

Bina Nusantara

Display Devices : LCD’s

• Transmissive & reflective LCDs:– LCDs act as light valves, not light emitters, and thus rely

on an external light source.– Laptop screen: backlit, transmissive display– Palm Pilot/Game Boy: reflective display

18

Page 19: T0074-02 ok

Bina Nusantara

Display Devices : Plasma

• Plasma display panels– Similar in principle to

fluorescent light tubes– Small gas-filled capsules

are excited by electric field,emits UV light

– UV excites phosphor– Phosphor relaxes, emits

some other color

19

Page 20: T0074-02 ok

Bina Nusantara

Input Devices : Physical Input • Physical Input Devices:

– Trackballs and Joysticks.• Analog input controlled by transducers• Convert user’s movement into changes in voltage/amperage

by varying the resistance of the circuit– Mouse

• Good for choosing, positioning, and drawing• Digital version like optical mouse and acoustical mouse are

also available– Keyboard

• Good for entering alpha numeric data• Encode keyboard characters using ASCII

– Light pen• The tip of the light pen is light sensitive, activated during

contact on the screen– Touch screen, Graphics Tablet, Image Digitizer, etc

20

Page 21: T0074-02 ok

Bina Nusantara

Input Devices : Logical Input

• Logical Input Devices:– Locator.

• Provide coordinate information 2D or 3D– Button

• Used to select and activate events controlling the interactive flow

– Keyboard• Conceptually and functionally be considered as a

collection of buttons– Valuator (slider)

• Provide a single value, generally a real number– Pick

• Identifies or select objects or sub pictures within the displayed picture

21

Page 22: T0074-02 ok

22

Program Structure• Most OpenGL programs have a similar structure that

consists of the following functions– main():

• specifies the callback functions • opens one or more windows with the required properties• enters event loop (last executable statement)

– init(): sets the state variables• Viewing• Attributes

– initShader():read, compile and link shaders

– callbacks• Display function• Input and window functions

Bina NusantaraBina Nusantara

Page 23: T0074-02 ok

23

simple.c revisited• main() function similar to last lecture

– Mostly GLUT functions

• init() will allow more flexible colors• initShader() will hides details of setting up

shaders for now• Key issue is that we must form a data array to

send to GPU and then render it

Bina Nusantara

Bina Nusantara

Page 24: T0074-02 ok

24

main.c#include <GL/glew.h>#include <GL/glut.h>

int main(int argc, char** argv){

glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("simple"); glutDisplayFunc(mydisplay); glewInit(); init(); glutMainLoop();

}

includes gl.h

specify window properties

set OpenGL state and initialize shaders

enter event loop

display callback

Bina NusantaraBina Nusantara

Page 25: T0074-02 ok

25

GLUT functions• glutInit allows application to get command line arguments

and initializes system• gluInitDisplayMode requests properties for the window (the

rendering context)– RGB color– Single buffering– Properties logically ORed together

• glutWindowSize in pixels• glutWindowPosition from top-left corner of display• glutCreateWindow create window with title “simple”• glutDisplayFunc display callback• glutMainLoop enter infinite event loop

Bina NusantaraBina Nusantara

Page 26: T0074-02 ok

26

Terminating a program• In our original programs, there was no way to

terminate them through OpenGL• We can use the simple mouse callback

void mouse(int btn, int state, int x, int y){ if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) exit(0);}

Bina NusantaraBina Nusantara

Page 27: T0074-02 ok

27

Using the mouse position• In the next example, we draw a small square at the

location of the mouse each time the left mouse button is clicked

• This example does not use the display callback but one is required by GLUT; We can use the empty display callback functionmydisplay(){}

Bina NusantaraBina Nusantara

Page 28: T0074-02 ok

28

Drawing squares at cursor locationvoid mymouse(int btn, int state, int x, int y){ if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) exit(0);

if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)drawSquare(x, y);

}

void drawSquare(int x, int y){ y=w-y; /* invert y position */ points[i] = point2(x+size, y+size); points[i+1] = point2(x-size, y+size); points[i+2] = point2(x-size, y-size); points[i+3] = point2 x+size, y-size); i+=4}

Bina NusantaraBina Nusantara

Page 29: T0074-02 ok

Immediate Mode Graphics• Geometry specified by vertices

– Locations in space( 2 or 3 dimensional)– Points, lines, circles, polygons, curves, surfaces

• Immediate mode– Each time a vertex is specified in application, its location

is sent to the GPU– Old style uses glVertex– Creates bottleneck between CPU and GPU– Removed from OpenGL 3.1

29Bina NusantaraBina Nusantara

Page 30: T0074-02 ok

Retained Mode Graphics• Put all vertex and attribute data in array• Send array to GPU to be rendered immediately• Almost OK but problem is we would have to send

array over each time we need another render of it• Better to send array over and store on GPU for

multiple renderings

30Bina NusantaraBina Nusantara

Page 31: T0074-02 ok

Display Callback• Once we get data to GLU, we can initiate the

rendering with a simple callback

• Arrays are buffer objects that contain vertex arrays

31

void mydisplay(){

glClear(GL_COLOR_BUFFER_BIT);glDrawArrays(GL_TRIANGLES, 0, 3);glFlush();

}

Bina NusantaraBina Nusantara

Page 32: T0074-02 ok

Vertex Arrays• Vertices can have many attributes

– Position– Color– Texture Coordinates– Application data

• A vertex array holds these data

• Using types in vec.h

32

point2 vertices[3] = {point2(0.0, 0.0), point2( 0.0, 1.0), point2(1.0, 1.0)};

Bina NusantaraBina Nusantara

Page 33: T0074-02 ok

Vertex Array Object• Bundles all vertex data (positions, colors, ..,)• Get name for buffer then bind

• At this point we have a current vertex array but no contents

• Use of glBindVertexArray lets us switch between VBOs

33

Glunit abuffer;glGenVertexArrays(1, &abuffer);glBindVertexArray(abuffer);

Bina NusantaraBina Nusantara

Page 34: T0074-02 ok

Buffer Object• Buffers objects allow us to transfer large

amounts of data to the GPU• Need to create, bind and identify data

• Data in current vertex array is sent to GPU

34

Gluint buffer;glGenBuffers(1, &buffer);glBindBuffer(GL_ARRAY_BUFFER, buffer);glBufferData(GL_ARRAY_BUFFER,

sizeof(points), points);

Bina NusantaraBina Nusantara

Page 35: T0074-02 ok

Initialization• Vertex array objects and buffer objects can be set

up on init()• Also set clear color and other OpeGL parameters• Also set up shaders as part of initialization

– Read– Compile– Link

• First let’s consider a few other issues

35Bina NusantaraBina Nusantara

Page 36: T0074-02 ok

36

Coordinate Systems• The units in points are determined by the

application and are called object, world, model or problem coordinates

• Viewing specifications usually are also in object coordinates

• Eventually pixels will be produced in window coordinates

• OpenGL also uses some internal representations that usually are not visible to the application but are important in the shaders

Bina NusantaraBina Nusantara

Page 37: T0074-02 ok

37

OpenGL Camera• OpenGL places a camera at the origin in object

space pointing in the negative z direction• The default viewing volume is a box centered at the origin with sides of length 2

Bina NusantaraBina Nusantara

Page 38: T0074-02 ok

38

Orthographic Viewing

z=0

z=0

In the default orthographic view, points are projected forward along the z axis onto theplane z=0

Bina NusantaraBina Nusantara

Page 39: T0074-02 ok

39

Viewports• Do not have use the entire window for the image: glViewport(x,y,w,h)

• Values in pixels (window coordinates)

Bina NusantaraBina Nusantara

Page 40: T0074-02 ok

40

Transformations and Viewing

• In OpenGL, projection is carried out by a projection matrix (transformation)

• Transformation functions are also used for changes in coordinate systems

Bina NusantaraBina Nusantara