CG simple openGL point & line-course 2

48
National Taiwan Normal University Simple OpenGL Program (point & line) Chen Jing-Fung (2006/11/13) Assistant Research Fellow, Digital Media Center, National Taiwan Normal University

description

OpenGL - point & line design introduce the construction of displayers (CRT, Flat-panel, LCD, PDP, projector...) those render is based on graphic skills (point & line)

Transcript of CG simple openGL point & line-course 2

Page 1: CG simple openGL point & line-course 2

National Taiwan Normal University

Simple OpenGL Program (point & line)

Chen Jing-Fung (2006/11/13) Assistant Research Fellow,

Digital Media Center, National Taiwan Normal University

Page 2: CG simple openGL point & line-course 2

2

Displayers

• CRT & Flat-panel Display

http://www.plasmadepot.com/plasmatv/plasmatv101.html

Page 3: CG simple openGL point & line-course 2

3

CRT (Cathode ray tube)

• Cutaway rendering of a color CRT

http://en.wikipedia.org/wiki/Cathode_ray_tube

Electron guns

Electron beams

Focusing coils

Deflection coils

Anode connection Display Mask

Phospfor layer

Close-up of the Phospfor layer

Page 4: CG simple openGL point & line-course 2

4

TFT LCD (Thin Film Transistor Liquid Crystal Display)

• TFT LCD has a sandwich-like structure with liquid crystal filled between two glass plates.

http://www.plasma.com/classroom/what_is_tft_lcd.htm

Page 5: CG simple openGL point & line-course 2

5

PDP (Plasma Display Panel)

• A plasma TV is sometimes called an "emissive" display — the panel is actually self-lighting.

• The display consists of two transparent glass panels with a thin layer of pixels sandwiched in between.

http://www.plasmadepot.com/plasmatv/howplasmaworks.html

Page 6: CG simple openGL point & line-course 2

6

What is different?

• CRT – Electrostatic deflection of the electron

beam • LCD

– Back light module • PDP

– Tiny colored fluorescent lights

Page 7: CG simple openGL point & line-course 2

7

DIY your projector

http://www.audiovisualizers.com/madlab/lcd_proj.htm

http://en.wikipedia.org

Cutaway rendering of the projector Fresnel Lens

Conventional Lens

5.5’’ LCD + VCD player = $169 USD

USD$200

Page 8: CG simple openGL point & line-course 2

8

Basic video-controller refresh operations

Raster-Scan Generator

y Register x Register

Memory Address Pixel Register

Frame Buffer

Intensity

Horizontal and Vertical Deflection Voltage

Page 9: CG simple openGL point & line-course 2

9

Frame Buffer

• Cartesian reference frame with origin at the lower-left corner of a video monitor

x

y

Page 10: CG simple openGL point & line-course 2

10

Coordinate representations

1 1

1

Plotter

Other Output

Device Coordinates

Video Monitor Modeling (local or master)

Coordinates World

(Cartesian) Coordinates

Normalized Coordinates

Viewing & Projection Coordinates

Page 11: CG simple openGL point & line-course 2

11

• Coordinates: – Modeling (m), World (w), Viewing (v),

Projection (p), Normalized (n), Device (d)

• (xmc, ymc, zmc) -> (xwc, ywc, zwc) -> (xvc, yvc, zvc)

-> (xpc, ypc, zpc) -> (xnc, ync, znc) -> (xdc, ydc)

Page 12: CG simple openGL point & line-course 2

12

Graphs system

• User program, Graphics system, I/O devices

Function calls Output

I/O devices Graphics

system

Data Input

User

program

Page 13: CG simple openGL point & line-course 2

13

Application Programming Interface (API) Functions

• Divide API functions into seven groups: – Primitive functions – Attribute functions – Transformation functions – Viewing functions – Input functions – Control functions

Page 14: CG simple openGL point & line-course 2

14

Primitive & Attribute

• Primitive functions - graphics output primitives – Character strings & geometric entities

• Points, lines, areas and shapes (2D, 3D)

• Attribute functions - Properties of the output primitives – Describe how a particular primitive is to be

displayed • Color specifications, line styles, text styles and area-

filling patterns …

Page 15: CG simple openGL point & line-course 2

15

Viewing

• Select a view of the scene -> type of projection -> location on monitor

• Other routines: – Managing the scene display area

• Specifying its position, size and structure

• Identified visible objects and applied lighting conditions (3D)

Page 16: CG simple openGL point & line-course 2

16

Software standards - GL

• Graphics Library (GL) a widely used package in the graphics community – Goal: fast, real-time rendering and package was being

extended to other hardware systems. – International Standard Organization (ISO)

• Graphical kernel system (GKS) 1984 – 2D & 3D

• Programmer’s Hierarchical Interactive Graphics Standard (PHICS)

– Increase: object modeling, color specification, surface rendering…

– PHICS+ (3D)

Page 17: CG simple openGL point & line-course 2

17

Software standards - OpenGL

• OpenGL : a hardware-independent version of GL in the early 1990s.

• The graphics package is maintained and updated by OpenGL Architecture Review Board.

• Language binding – C and C++

Page 18: CG simple openGL point & line-course 2

18

Other Graphics Packages

• Graphics libraries – Java 2D (Java applets)

– Java 3D

– Mathematica

– Matlab

– Maple…

Page 19: CG simple openGL point & line-course 2

19

Introduction to OpenGL

• Overview OpenGL based on VC++ – http://dev.yesky.com/424/2080424.shtml

• Basic OpenGL Syntax – Function names in OpenGL basic Library

• Ex: glBegin, glClear, glCopyPixels…

– Arguments in Function (interval: _ ) • Ex: GL_2D, GL_RGB, GL_POLYGON…

– Specific data types • Ex: GLbyte, GLshort, Glint…

Page 20: CG simple openGL point & line-course 2

20

OpenGL – Related Libraries

• GL: basic core library (prefix: gl)

• OpenGL Utility (GLU): (prefix: glu) – Setting up viewing and projection matrices

– describing complex objects with line and polygon

– Displaying quadrics and B-splines

– Processing the surface-rendering operations

– Other complex tasks

Page 21: CG simple openGL point & line-course 2

21

OpenGL- Display window

• OpenGL Extension: – X Window System (GLX) - Prefix: plX

– Apple GL (AGL) - Prefix: agl

– Microsoft Windows system (WGL) - Prefix: wgl

– IBM OS/2 (Presentation Manager to OpenGL: PGL) - Prefix: pgl

Page 22: CG simple openGL point & line-course 2

22

OpenGL Utility Toolkit (GLUT)

– Provide a library of functions for interacting with any screen-windowing system. • Prefix: glut

• Contain: describing and rendering quadric curves and surfaces

• Download: (glut-3.7.6-bin.zip ~117 KB) http://www.xmission.com/~nate/glut.html

Page 23: CG simple openGL point & line-course 2

23

How to Use GLUT?

• Put 3 files from ``glut-3.7.6-bin.zip’’ in your project directory. – README-win32.txt

• glut32.dll to %WinDir%\System

• glut32.lib to $(MSDevDir)\..\..\VC98\lib

• glut.h to $(MSDevDir)\..\..\VC98\include\GL

Page 24: CG simple openGL point & line-course 2

24

Setup for a OpenGL Program 1/5

• Microsoft Visual C++ 6.0 – Step 1: create a Win32 Console Application project

Page 25: CG simple openGL point & line-course 2

25

Setup for a OpenGL Program 2/5

– Step 2:Press Alt-F7 project -> settings...->Link

– Step 3:add opengl32.lib glu32.lib glut32.lib into Object/library modules

Page 26: CG simple openGL point & line-course 2

26

Setup for a OpenGL Program 3/5

• Microsoft Visual C++ .Net – Step 1:create Win32 console project

Page 27: CG simple openGL point & line-course 2

27

Setup for a OpenGL Program 4/5

– Step 2: select ―empty project‖

Page 28: CG simple openGL point & line-course 2

28

Setup for a OpenGL Program 5/5

– Step 3:select project->property…->linker -> input

– Step 4: add opengl32.lib glu32.lib glut32.lib

Page 29: CG simple openGL point & line-course 2

29

Header Files 2/1

• Include the header file the OpenGL core library – #include <windows.h>

• With Microsoft Windows

– #include <GL/gl.h> • Basic core library

– #include <GL/glu.h> • Object-oriented toolkit & 3D application

Page 30: CG simple openGL point & line-course 2

30

Header Files 2/2

• Use GLUT to handle the window-managing operations. – #include <GL/glut.h>

• gl.h, glu.h (redundant and could affect program portability)

• Need to include c++ code – #include <stdio.h> #include <stdlib.h> #include

<math.h>…

• ISO/ANSI standard for c++ – Include: cstdio, cstdlib and cmath

Page 31: CG simple openGL point & line-course 2

31

Display-Window Management Using GLUT

2/1 • Initialization function

– glutInit(&argc, argv);

• Create display window (DW) and caption – glutCreateWindow(―An Example OpenGL Program‖);

• Assign our picture to DW – Ex: describing a line segment – glutDisplayFunc(lineSegment);

• But DW is not yet. More GLUT function -> complete the window-processing operations – glutMainLoop( ); <- the last line (check all input devices –

mouse or keyboard)

Page 32: CG simple openGL point & line-course 2

32

Display-Window Management Using GLUT

2/2

• Top-left corner of the video display (50,100) – glutInitWindowPosition(50, 100);

• Display window of OpenGL: width=400 pixels and height= 300pixels – glutInitWindowSize(400, 300); 100

50

300 400

Display window

• A single refresh buffer & RGB mode – glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)

Page 33: CG simple openGL point & line-course 2

33

A complete OpenGL Program 2/1

• Set background color: white – glClearColor(1.0, 1.0, 1.0, 0.0); <- alpha value is

as a ``blending’’ parameter.

• Get the assigned window color displayed – glClear(GL_COLOR_BUFFER_BIT);

• Set object color – glColor3f(1.0, 0.0, 0.0);

Page 34: CG simple openGL point & line-course 2

34

A complete OpenGL Program 2/2

• OpenGL processes our picture (2D line) through the full 3D viewing operations – “project’’ – glMatrixMode(GL_PROJECTION);

• Map rectangle – glOrtho2D(0.0, 200.0, 0.0, 150.0);

• Cartesian endpoint coordinate – glBegin(GL_LINES);

glVertex2i(180, 15); glVertex2i(10, 145); glEnd();

Page 35: CG simple openGL point & line-course 2

35

Page 36: CG simple openGL point & line-course 2

36

Display function

User define

Page 37: CG simple openGL point & line-course 2

37

Point Specification

• OpenGL functions – glVertex*();<-The coordinate values

– EX:

• C++ class or structure – Ex:

x

y glBegin (GL_POINTS);

glVertex2iv (p1); //i: integer glVertex2iv (p2); glVertex2iv (p3);

glEnd ();

p1

p2

p3 Class wcPt2D { Public: GLfloat x, y;

};

wcPt2D pointPos;

pointPos.x = 120.75; pointPos.y = 45.30;

glBegin (GL_POINTS); glVertex2f (pointPos.x, pointPos.y); glEnd ();

Page 38: CG simple openGL point & line-course 2

38

OpenGL Line functions

glBegin (*); glVertex2iv (p1); glVertex2iv (p2); glVertex2iv (p3); glVertex2iv (p4); glVertex2iv (p5);

glEnd ();

p1

p2

p3

p4

p1

p2

p3

p4

p5 p1

p2

p3

p4

p5

Primitive line generated with GL_LINES

polyline generated with GL_LINE_STRIP

Closed polyline generated with GL_LINE_LOOP

Page 39: CG simple openGL point & line-course 2

39

Line-drawing algorithms 2/1

• A straight-line segment in a scene is defined by the coordinate positions for the endpoints of the segment.

• Line is compose of the nearest pixel positions along the line path between the two endpoints.

• Ex: point (10.48, 20.51)->(10,21)

Page 40: CG simple openGL point & line-course 2

40

• The Cartesian slope-intercept equation for a straight line – y=m*x+b – slope m=(yend-y0)/(xend-x0) – Intercept b=y0-m*x0

• Digital differential analyzer Dx = 1, yk+1 = yk + m Dy = 1, xk+1 = xk + 1/m

• Steps? ?? Dx = -1 ? or Dy = -1 ?

x0 xend

y0

yend

Dy Dx

Line-drawing algorithms 2/2

xk xend

yk

yend

Dy

Dx

Page 41: CG simple openGL point & line-course 2

National Taiwan Normal University

How to a parameter drawing line?

Page 42: CG simple openGL point & line-course 2

42

Bresenham’s line algorithm 3/1

• Bresenham’s line is an accurate and efficient raster line-generating algorithm

• Use only incremental integer calculations.

• Determine at next sample point whether to plot. – The negative-slope line:

if start point (50, 50), next point? • (51, 50) or (51, 49)

Page 43: CG simple openGL point & line-course 2

43

Analysis Bresenham’s line algorithm

• The scan-conversion process: – Set the positive slope < 1.0 and start point (x0, y0)

– Let the kth step in this process, therefore, the displayed pixel (xk, yk) • Next pixel (xk+1, yk) or (xk+1, yk+1)

– The sampling position xk+1, depend on (dlow- dup) • The label vertical pixel separations from dlow and dup

• Pk=Dx(dlow-dup) is the same as the sign of (dlow- dup) – If yk ~ line, (dlow< dup), then pk< 0

– Next pk+1 ?

– First parameter p0 ?

xk+1

yk

yk+1 m 1-m

Page 44: CG simple openGL point & line-course 2

44

Bresenham’s line algorithm 3/3(m<1.0)

• Step 1: Input left endpoint (x0, y0) • Step 2: Set the color for frame-buffer position (x0, y0) • Step 3: Calculate Dx, Dy, 2Dy and 2Dy-2Dx

– If yk+1>yk, p0=2Dy-Dx, else yk+1<yk, p0=2Dy+Dx

• Step 4: The decision parameter pk+1 – If pk<0 & next point is (xk+1, yk)

• pk+1= pk+2Dy

– Else next point is (xk+1, yk+1) • pk+1= pk+2Dy-2Dx

• Step 5: return step 4 Dx-1 times

xk

yk

yk+1

yk+2

yk+3

xk+1 xk+2 xk+3

y

xk+1

yk

yk+1

y = mx + b

m 1-m

Page 45: CG simple openGL point & line-course 2

45

Bresenham’s line example

• Start point: (20, 10)

• End point: (30, 18)

Page 46: CG simple openGL point & line-course 2

46

Homework

• HW 1: Draw line from (30, 150) to (200, 10) – Hint : line-drawing algorithm

– If you use one decision parameter to do, you will get the reward.

• HW 2: Plot the regular polygon (3, 4, 5, cycle)

Page 47: CG simple openGL point & line-course 2

47

Two important points of Homework

• Explain (30%)

• Program (70%)

Page 48: CG simple openGL point & line-course 2

48

References

• .Net Education website: – http://yes.nctu.edu.tw/DotNet/index.ht

m

• Mathworld – http://mathworld.wolfram.com/