25
Introduction to OpenGL Introduction to OpenGL Computer Graphics, Chapt 08

Introduction to OpenGLIntroduction to OpenGL

  • Upload
    others

  • View
    27

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to OpenGLIntroduction to OpenGL

Introduction to OpenGLIntroduction to OpenGL

Computer Graphics, Chapt 08

Page 2: Introduction to OpenGLIntroduction to OpenGL

Introduction to OpenGL

• Creating an Image – Components, parts of a scene to be displayed

• Trees, terrainF it ll• Furniture, walls

• Store fronts and street scenes• Atoms and moleculesAtoms and molecules• Stars and galaxies

– Describe • Internal structure of the individual objects• Location (coordinates) within the scene

– Functions in a graphics package to describe the various image components: graphics output primitives or (geometric) primitives

Computer Graphics, WS 07/08 2

graphics output primitives or (geometric) primitives

Page 3: Introduction to OpenGLIntroduction to OpenGL

Introduction to OpenGL

• Software Standards in Computer Graphics– Goal of graphics standards

• Portability, independent of hardware platformSi l t f b t t• Simple transfer between systems

– GKS (Graphical Kernel System) – 1984• Adopted as first graphics software standard by• Adopted as first graphics software standard by

– International Standards Organization (ISO)– American National Standards Institute (ANSI)– Various national standards organizations

• Designed as 2D package, extended to 3D

PHIGS– PHIGS (Programmer’s Hierarchical Interactive Graphics Standard)• Extension of GKS

– Capabilities for hierarchical modeling color specification

Computer Graphics, WS 07/08 3

Capabilities for hierarchical modeling, color specification– Surface rendering, picture manipulations

Page 4: Introduction to OpenGLIntroduction to OpenGL

Introduction to OpenGL

– ERLGRAPH (Erlanger Graphik System) ~1985V R i l R h t E l• Vom Regionalen Rechenzentrums Erlangen

– Weiterentwicklung des Erlanger Graphik Systems (EGS)• Programmbibliothek für FORTRANg

– Funktionen für das Visualisieren wissenschaftlicher Daten – Mathematischer Funktionen als 2-D- und 3-D-Plots

A b f hi d A b ät• Ausgabe auf verschiedenen Ausgabegeräten

Computer Graphics, WS 07/08 4

Page 5: Introduction to OpenGLIntroduction to OpenGL

Introduction to OpenGL

– GL (Graphics Library)C ith k t ti f Sili G hi I (SGI)• Came with workstations from Silicon Graphics, Inc. (SGI)

• Widely used in graphics community and a de facto standard• Set of routines designed for fast, real-time renderingSet of routines designed for fast, real time rendering• Soon extended to other hardware systems

– OpenGLp• Hardware independent version of GL (early 1990s)• OpenGL Architecture Review Board

– responsible for maintenance and updates– Consortium of many graphics companies and organizations

• Specifically designed for efficient processing in 3DSpecifically designed for efficient processing in 3D• Also handles 2D scene descriptions as a special case• Available language binding

Computer Graphics, WS 07/08 5

– C, C++, Fortran, Ada

Page 6: Introduction to OpenGLIntroduction to OpenGL

Introduction to OpenGL

• OpenGL in computer graphics– Provides

• hardware abstraction layer through its Application Programmer Interface (API)Programmer Interface (API)

– Thereby• provides low level platform independent graphicsprovides low level, platform independent graphics• allows non-standard extensions

– It does not have• high level modeling constructs, such as scene graphs• windowing facilities • requires interface to window system: GLUT, GLX, AGL, WGL

Computer Graphics, WS 07/08 6

Page 7: Introduction to OpenGLIntroduction to OpenGL

Introduction to OpenGL

• Literature: – OpenGL Reference Manual – The Blue Book

html-version: http://www opengl org/documentation/blue book 1 0http://www.opengl.org/documentation/blue_book_1.0

D H M B B k C t G hi ith O GL– D. Hearn, M.B. Baker: Computer Graphics with OpenGL, (3rd edition)

– OpenGL(R) Programming Guide: The Official Guide to Learning OpenGL(R) Version 2 (5th Edition) (OpenGL)Learning OpenGL(R), Version 2 (5th Edition) (OpenGL)

Computer Graphics, WS 07/08 7

Page 8: Introduction to OpenGLIntroduction to OpenGL

Introduction to OpenGL

• Basic OpenGL Syntax– Function names

• Prefixed with glE h t d h it fi t l tt it li d• Each component word has its first letter capitalized: e.g.: glClear, glCopyPixel, glPolygonMode

– Assignment of symbolic constantsAssignment of symbolic constants• Certain functions require one or more

e.g: GL_2D, GL_RGB, GL_POLYGON• All begin with GL_ and component words in capital letters

– Data typesP bl ifi ti b diff t diff t hi• Problem: specifications can be different on different machines (e.g. integer)

• Special built in data-types

Computer Graphics, WS 07/08 8

p ype.g.: GLbyte, GLshort, GLfloat, GLdouble

Page 9: Introduction to OpenGLIntroduction to OpenGL

Introduction to OpenGL

• Related Libraries– OpenGL Utility (GLU)

• Included by every OpenGL implementationC fi l• Common prefix: glu

• Supplies advanced routines, e.g.– Setting up viewing and projection matricesSett g up e g a d p oject o at ces– Complex objects with line and polygon approximations– Display of quadrics and B-splines using linear approximations

P i f f d i ti– Processing of surface rendering operations

– Open Inventor• Object oriented toolkit based on OpenGL written in C++• Object oriented toolkit based on OpenGL, written in C++ • Routines and predefined object shapes for interactive 3D

applications

Computer Graphics, WS 07/08 9

• Scene description by scene graphs

Page 10: Introduction to OpenGLIntroduction to OpenGL

Introduction to OpenGL

– Graphics display with OpenGLS t di l i d th t d d t• Set up a display window on the screen ↔ computer dependent

– Not directly with OpenGL (device independent functions only!)– Window system libraries supporting OpenGLy pp g p

• GLX (OpenGL extension to the X Window System)– set of routines prefixed with: glX

AGL (A l GL) A l t• AGL (Apple GL) – on Apple systems– set of routines prefixed with: agl

• WGL (Windows to OpenGL) – on Microsoft Windows systems( p ) y– set of routines prefixed with: wgl

• PGL (Presentation Manager to OpenGL) – for IBM OS/2t f ti fi d ith– set of routines prefixed with: pgl

• OpenGL Utility Toolkit (GLUT)– set of routines prefixed with: glut

Computer Graphics, WS 07/08 10

p g

– Interaction with any screen-windowing system

Page 11: Introduction to OpenGLIntroduction to OpenGL

Introduction to OpenGL

• Header Files– OpenGL core library– GLU library for most applications– Windowing systems– Example for Microsoft Windows

#include <windows.h>#inlcude <GL/gl.h>#include <GL/glu.h>

– If GLUT is used

#include <GL/glu.h>

#i l d i d h#include <windows.h>#inlcude <GL/glut.h>

Computer Graphics, WS 07/08 11

Page 12: Introduction to OpenGLIntroduction to OpenGL

Introduction to OpenGL

• Display-window management using GLUTI iti li ti– Initialization• glutInit(&argc, argv);

– Creation of a display window with caption– Creation of a display window with caption• glutCreateWindow(“OpenGL example”);

– Specification what the display window should containp p y• Supposing a code for line description called lineSegment• glutDisplayFunc(lineSegment);• Passes line segment description to display window• Passes line-segment description to display window

– Activation of all display window including graphic content• glutMainLoop ();g p ();• Must be the last function

– Displays the graphics and puts program to infinite loopChecks for input from devices (e g : mouse keyboard)

Computer Graphics, WS 07/08 12

– Checks for input from devices (e.g.: mouse, keyboard)

Page 13: Introduction to OpenGLIntroduction to OpenGL

Introduction to OpenGL

– Initial location of display windowl tI itWi d P iti (50 100)• glutInitWindowPosition(50, 100);

• Top left corner of the display

– Initial pixel width and height of display window– Initial pixel width and height of display window• glutInitWindowsSize(400, 300);

– Other options (e g : buffering color mode)Other options (e.g.: buffering, color mode)• glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

• Values of constants passed with logical or operations– other values: GLUT_DOUBLE, GLUT_DEPTH

50100

50

400 300

Computer Graphics, WS 07/08 13

Page 14: Introduction to OpenGLIntroduction to OpenGL

Introduction to OpenGL

• To complete a program– Set background color

• glClearColor(1.0, 1.0, 1.0, 0.0);

Th 4th l ifi th l h l d i bl di t• The 4th value specifies the alpha value used in blending to mimic transparency

– Displaying the assigned window colorDisplaying the assigned window color• glClear(GL_COLOR_BUFFER_BIT);

• GL_COLOR_BUFFER_BIT specifies the bit values in the color buffer (refresh buffer) to be set to the values indicated in glClearColor

Assignment of color to objects– Assignment of color to objects• glColor3f(1.0, 0.0, 0.0);

• RGB components using floating-point (f) values

Computer Graphics, WS 07/08 14

p g g p ( )• Note that each vertex can have a different color

Page 15: Introduction to OpenGLIntroduction to OpenGL

Introduction to OpenGL

– Definition of output primitivesP i t iti t i ht li i l th i ti• Point position, straight lines, circles, other conic sections

• Quadric surfaces, spline curves and surfaces• Polygon color areas, character stringsPolygon color areas, character strings

– After geometry definition• Projection of output primitives j p p• Onto a 2D plane ⇔ display area of output device• Scan conversion: integer pixel positions in frame buffer• glFlush();

– Process all OpenGL routines as quickly as possible

Computer Graphics, WS 07/08 15

Page 16: Introduction to OpenGLIntroduction to OpenGL

Introduction to OpenGL

– 2D Cartesian reference frame in OpenGL (also see chapter 3 of Hearn & Baker for further details)(also see chapter 3 of Hearn & Baker for further details)

• Defining coordinate frame for screen display window– gluOrtho2D ⇔ orthogonal projection

glMatrixMode (GL_PROJECTION);glLoadIdentity ();

– Assign identity matrix in

gluOrtho2D(xmin, xmax, ymin, ymax);

OpenGL projection matrix– Prevents accumulation

with previous values ymaxp– Place coordinate values in

OpenGL projection matrix

ymax

ymin

Computer Graphics, WS 07/08 16

xmin xmax

ymin

Page 17: Introduction to OpenGLIntroduction to OpenGL

Introduction to OpenGL

• Example program – Main routine

#include <GL/glut.h>

void main (int argc, char** argv){glutInit(&argc, argv); // initialize GLUTl tI itDi l M d (GLUT SINGLE|GLUT RGB)glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowPosition(50, 100); // top-left pos.glutInitWindowsSize(400, 300); // width, heightglutCreateWindow(“OpenGL example); // create display windowinit(); // initialization procedureglutDisplayFunc(lineSegment); // send graphics to displayglutMainLoop (); // display and wait

}

Computer Graphics, WS 07/08 17

Page 18: Introduction to OpenGLIntroduction to OpenGL

Introduction to OpenGL

– Additional routines

void init (void) { // Initialization pglClearColor(1.0, 1.0, 1.0, 0.0);glMatrixMode (GL PROJECTION); // projection parametersglMatrixMode (GL_PROJECTION); // projection parametersglLoadIdentity ();gluOrtho2D(0.0, 200.0, 0.0, 150.0);

}}

void lineSegment(void){// l di l i dglClear(GL_COLOR_BUFFER_BIT); // clear display window

glColor3f(1.0, 0.0, 0.0); // set line colorglBegin (GL_LINES); // line geometry

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

glEnd ();

Computer Graphics, WS 07/08 18

g ()glFlush();

}

Page 19: Introduction to OpenGLIntroduction to OpenGL

Graphics Output Primitives

• OpenGL point functions– Specify geometry of a point

• glVertex*( coordinate position in reference frame )* ffi d d• *: suffix codes are used

– Identify spatial dimension: values 2, 3, 4– State data type: i (integer), s (short), f (float), d (double)yp ( g ) ( ) ( ) ( )

– Identification of output primitive (here points)glBegin (gl POINTS);

C di t l

g g (g _ )glVertex* ();

glEnd ();

– Coordinate values• List explicitly• Array referencing coordinate position

Computer Graphics, WS 07/08 19

• Array referencing coordinate position – append suffix code v (for vector)

Page 20: Introduction to OpenGLIntroduction to OpenGL

Graphics Output Primitives

– Geometric primitives are passed throughth li hti / h di l ith• the lighting /shading algorithms,

• the 3D viewing (projection) algorithms,• and finally rasterization (scan conversion)and finally rasterization (scan conversion)

– Default display• Color (white) ( )• Size (one screen pixel)

– Coordinate extents of a primitive• Within range of display window

– All of the primitive is displayedBeyond range of display window• Beyond range of display window

– Only those parts within the coordinate range of the display window are shown (clipped object)

Computer Graphics, WS 07/08 20

Page 21: Introduction to OpenGLIntroduction to OpenGL

Graphics Output Primitives

– Example l i ( )glBegin (GL_POINTS);

glVertex2i (50, 100);glVertex2i (75, 150);glVertex2i (100, 200);

glEnd ();200

• Coordinate definition in arrays

int point1 [] = {50, 100};75 150

100

int point2 [] = {75, 150};int point3 [] = {100, 200};

100 200glBegin (GL_POINTS);

glVertex2iv (point1);glVertex2iv (point2);

Computer Graphics, WS 07/08 21

g pglVertex2iv (point3);

glEnd ();

Page 22: Introduction to OpenGLIntroduction to OpenGL

Graphics Output Primitives

• OpenGL line functions– Define the two endpoint coordinate positions – Specify one or more straight-line segments

• Use glVertex funtions• Enclose between glBegin / glEnd

U b li t t f lB i– Use symbolic constant for glBegin• G_LINES• GL LINE STRIPGL_LINE_STRIP• GL_LINE_LOOP

– Default displayp y• Solid, white lines

Computer Graphics, WS 07/08 22

Page 23: Introduction to OpenGLIntroduction to OpenGL

Graphics Output Primitives

– Examplel i ( )

p3glBegin (GL_LINES);

glVertex2iv (p1);glVertex2iv (p2);

p1

p

glVertex2iv (p3);glVertex2iv (p4);glVertex2iv (p5);g p

glEnd (); p2 p4

p3glBegin (GL_LINE_STRIP);glVertex2iv (p1);glVertex2iv (p2);

p1

p3

p5

glVertex2iv (p3);glVertex2iv (p4);glVertex2iv (p5);

Computer Graphics, WS 07/08 23

g (p );glEnd (); p2 p4

Page 24: Introduction to OpenGLIntroduction to OpenGL

Graphics Output Primitives

• continued

glBegin (GL_LINE_LOOP);glVertex2iv (p1);glVertex2iv (p2); p1

p3

p5glVertex2iv (p2);glVertex2iv (p3);glVertex2iv (p4);lV t 2i ( 5)glVertex2iv (p5);

glEnd (); p2 p4

Computer Graphics, WS 07/08 24

Page 25: Introduction to OpenGLIntroduction to OpenGL

Introduction to OpenGL

• Tutorial (some kind of) : T t O GL D G tTuto -- an OpenGL-Demo-Generator– available on CIP

Computer Graphics, WS 07/08 25