16
1 Jan-16-04 SMD159, 2D Graphics in OpenGL 1 L INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET 2D Graphics in OpenGL David Carr Fundamentals of Computer Graphics Spring 2004 Jan-16-04 SMD159, 2D Graphics in OpenGL 2 L Overview Development of the OpenGL API and architecture Functions - Types and formats Simple program Refine the simple program Simple viewing Fundamental OpenGL primitives Attributes Jan-16-04 SMD159, 2D Graphics in OpenGL 3 L INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET Development of the OpenGL API

2D Graphics in OpenGL - sm.luth.se · •GLUT is in the package “net.java.games.jogl.util ... 2D Graphics in OpenGL 21 L Simple.java (continued) static class simpleRenderer implements

Embed Size (px)

Citation preview

1

Jan-16-04 SMD159, 2D Graphics in OpenGL 1 L

INSTITUTIONEN FÖRSYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

2D Graphics in OpenGL

David Carr

Fundamentals of Computer Graphics

Spring 2004

Jan-16-04 SMD159, 2D Graphics in OpenGL 2 L

Overview

• Development of the OpenGL API and architecture

• Functions- Types and formats

• Simple program

• Refine the simple program

• Simple viewing

• Fundamental OpenGL primitives

• Attributes

Jan-16-04 SMD159, 2D Graphics in OpenGL 3 L

INSTITUTIONEN FÖRSYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Development of the OpenGL API

2

Jan-16-04 SMD159, 2D Graphics in OpenGL 4 L

Early History of APIs

• IFIPS (1973) formed two committees to come up with astandard graphics API

- Graphical Kernel System (GKS)+ 2D but contained good workstation model

- Core+ Both 2D and 3D

- GKS adopted as IS0 and later ANSI standard (1980s)

• GKS not easily extended to 3D (GKS-3D)• Far behind hardware development

Jan-16-04 SMD159, 2D Graphics in OpenGL 5 L

PHIGS and X

• Programmers Hierarchical Graphics System (PHIGS)- Arose from CAD community- Database model with retained graphics (structures)

• X Window System- DEC/MIT effort- Client-server architecture with graphics

• PEX combined the two- Not easy to use (all the defects of each)

Jan-16-04 SMD159, 2D Graphics in OpenGL 6 L

SGI and GL

• Silicon Graphics (SGI) revolutionized the graphicsworkstation by implementing the pipeline in hardware(1982)

• To use the system, application programmers used alibrary called GL

• With GL, it was relatively simple to program threedimensional interactive applications

3

Jan-16-04 SMD159, 2D Graphics in OpenGL 7 L

OpenGL

• The success of GL lead to OpenGL (1992), a platform-independent API that was- Easy to use

- Close enough to the hardware to get excellent performance

- Focus on rendering

- Omitted windowing and input to avoid window systemdependencies

Jan-16-04 SMD159, 2D Graphics in OpenGL 8 L

OpenGL Evolution

• Controlled by an Architectural Review Board (ARB)- Members include SGI, Microsoft, Nvidia, HP,

3DLabs,IBM,…….

- Relatively stable (present version 1.4)

+ Evolution reflects new hardware capabilities• 3D texture mapping and texture objects

• Vertex programs

- Allows for platform specific features through extensions

Jan-16-04 SMD159, 2D Graphics in OpenGL 9 L

OpenGL Libraries

• OpenGL core library- OpenGL32 on Windows- GL on most unix/linux systems

• OpenGL Utility Library (GLU)- Provides functionality in OpenGL core but avoids having to

rewrite code

• Links with window system- GLX for X window systems- WGL for Widows- AGL for Macintosh

4

Jan-16-04 SMD159, 2D Graphics in OpenGL 10 L

GLUT

• OpenGL Utility Library (GLUT)- Provides functionality common to all window systems

+ Open a window

+ Get input from mouse and keyboard

+ Menus

+ Event-driven

- Code is portable but GLUT lacks the functionality of a goodtoolkit for a specific platform

+ Slide bars

Jan-16-04 SMD159, 2D Graphics in OpenGL 11 L

Software Organization

GLUT

GLU

GL

GLX, AGLor WGL

X, Win32, Mac O/S

software and/or hardware

Application Program

OpenGL Motifwidget or similar

Jan-16-04 SMD159, 2D Graphics in OpenGL 12 L

OpenGL Architecture

Immediate Mode

DisplayList

PolynomialEvaluator

Per VertexOperations &

PrimitiveAssembly

Rasterization Per FragmentOperations

TextureMemory

CPU FrameBuffer

Geometric pipeline

PixelOperations

5

Jan-16-04 SMD159, 2D Graphics in OpenGL 13 L

INSTITUTIONEN FÖRSYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

OpenGL Functions

Jan-16-04 SMD159, 2D Graphics in OpenGL 14 L

Function Types

• Primitives- Points

- Line Segments

- Polygons

• Attributes

• Transformations- Viewing

- Modeling

• Control

• Input (GLUT)

Jan-16-04 SMD159, 2D Graphics in OpenGL 15 L

OpenGL State

• OpenGL is a state machine

• OpenGL functions are of two types- Primitive generating

+ Can cause output if primitive is visible

+ How vertices are processes and appearance of primitiveare controlled by the state

- State changing

+ Transformation functions

+ Attribute functions

6

Jan-16-04 SMD159, 2D Graphics in OpenGL 16 L

Lack of Object Orientation

• OpenGL is not object oriented so that there are multiplefunctions for a given logical function, e.g.glVertex3f, glVertex2i,glVertex3dv,…..

• Underlying storage mode is the same

• Easy to create overloaded functions in C++ but issue isefficiency

Jan-16-04 SMD159, 2D Graphics in OpenGL 17 L

OpenGL function format

glVertex3f(x,y,z)

belongs to GL library

function name

x,y,z are floats

glVertex3fv(p)

p is a pointer to an array

Jan-16-04 SMD159, 2D Graphics in OpenGL 18 L

OpenGL Classes

• Most definitions by importing “net.java.games.jogl”

• The two principle interfaces are:- GL and GLU

- Constants named GL.Glconstant or GLU.GLUconstant

- For methods you need to get the GL and GLU objects andinvoke the method.

+ gl = drawable.getGL(); gl.glBegin(GL.GL_POLYGON);

• GLUT is in the package “net.java.games.jogl.util”

7

Jan-16-04 SMD159, 2D Graphics in OpenGL 19 L

A Simple Program

Generate a square on a solid background

Jan-16-04 SMD159, 2D Graphics in OpenGL 20 L

Simple.java

import java.awt.*;import java.awt.event.*;import net.java.games.jogl.*;

public class Simple { public static void main(String[] args) { Frame simpleFrame = new Frame("simple"); GLCanvas drawable = GLDrawableFactory.getFactory(). createGLCanvas(new GLCapabilities()); drawable.addGLEventListener(new simpleRenderer()); simpleFrame.add(drawable); simpleFrame.show(); } …

Jan-16-04 SMD159, 2D Graphics in OpenGL 21 L

Simple.java (continued) static class simpleRenderer implements GLEventListener { public void displayChanged(GLDrawable drawable, …) {} public void reshape(GLDrawable drawable, …) {} public void display(GLDrawable drawable) { private GL gl = drawable.getGL(); private GLU glu = drawable.getGLU(); gl.glClear(GL.GL_COLOR_BUFFER_BIT); // clear window gl.glBegin(GL.GL_POLYGON); // draw unit square polygon gl.glVertex2f(-0.5f, -0.5f); gl.glVertex2f(-0.5f, 0.5f); gl.glVertex2f(0.5f, 0.5f); gl.glVertex2f(0.5f, -0.5f); gl.glEnd(); gl.glFlush(); // flush GL buffers } public void init(GLDrawable drawable) { }}}

8

Jan-16-04 SMD159, 2D Graphics in OpenGL 22 L

Event Loop

• Note that the program defines a display callback classthat implements GLEventListener- Every GL program must have a display callback

- GLEventListener has a display method

- The display method is executed whenever OpenGL decidesthe display must be refreshed, for example when the windowis opened

- The main method ends with the program entering an eventloop

Jan-16-04 SMD159, 2D Graphics in OpenGL 23 L

Defaults

• simple.c is too simple

• Makes heavy use of state variable default values for- Viewing

- Colors

- Window parameters

• Next version will make the defaults more explicit

Jan-16-04 SMD159, 2D Graphics in OpenGL 24 L

INSTITUTIONEN FÖRSYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Refining the Simple Program

9

Jan-16-04 SMD159, 2D Graphics in OpenGL 25 L

Program Structure

• Most OpenGL programs have a similar structure that consists ofthe following methods- main():

+ Defines the renderer+ Opens one or more windows with the required properties+ Enters event loop (last executable statement)

- init(): sets the state variables

+ Viewing+ Attributes+ Method in the GLEventListener interface

- callbacks+ display(GLDrawable d) method of the GLEventListener interface+ Input and window methods (awt)

Jan-16-04 SMD159, 2D Graphics in OpenGL 26 L

Simple.java Revisited

• In this version, we will see the same output but havedefined all the relevant state values through functioncalls with the default values

• In particular, we set- Colors

- Viewing conditions

- Window properties

Jan-16-04 SMD159, 2D Graphics in OpenGL 27 L

public class Simple { public static void main(String[] args) { Frame simpleFrame = new Frame("simple"); GLCapabilities glc = new GLCapabilities(); glc.setDoubleBuffered(false); GLCanvas drawable = GLDrawableFactory.getFactory(). createGLCanvas(glc); drawable.addGLEventListener(new simpleRenderer()); simpleFrame.add(drawable);

simpleFrame.setLocation(100,100); simpleFrame.setSize(500, 500); simpleFrame.show(); } …

Define window properties

Insert drawing routines

Show window

Create OpenGL drawing area

Method main()

Configure OpenGLAWT window

10

Jan-16-04 SMD159, 2D Graphics in OpenGL 28 L

GLUT Functions

• Largely unimplemented in jogl:- Some via GLUT object

- Some accessed via GLCapabilities object

+ Single/double buffering, color depth

- Some accessed via AWT Frame object

+ Size, Location, Title

+ Frame.add(GLCanvas) puts the drawing area in the window

+ Frame.show() displays the window

• Event loop access- Via GLEventListener

- Inserted in GLCanvas

Jan-16-04 SMD159, 2D Graphics in OpenGL 29 L

public void init(GLDrawable drawable) { GL gl = drawable.getGL(); // get interfaces GLU glu = drawable.getGLU(); gl.glClearColor(0f,0f,0f,0f); // set background and fill color gl.glColor3f(1.0f,1.0f,1.0f); gl.glMatrixMode(GL.GL_PROJECTION); // view initialization gl.glLoadIdentity(); gl.glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); }

More Elaborate init() Method

Get these locally due tothreading issues

Jan-16-04 SMD159, 2D Graphics in OpenGL 30 L

INSTITUTIONEN FÖRSYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Simple Viewing

11

Jan-16-04 SMD159, 2D Graphics in OpenGL 31 L

Coordinate Systems

• The units of in glVertex are determined by theapplication and are called world or problem coordinates

• The viewing specifications are also in world coordinates andit is the size of the viewing volume that determines what willappear in the image

• Internally, OpenGL will convert to camera coordinates andlater to screen coordinates

Jan-16-04 SMD159, 2D Graphics in OpenGL 32 L

OpenGL Camera

• OpenGL places a camera at the origin pointing in thenegative z direction

• The default viewing volume

is a box centered at the

origin with a side of

length 2

Jan-16-04 SMD159, 2D Graphics in OpenGL 33 L

Orthographic Viewing

z=0

z=0

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

12

Jan-16-04 SMD159, 2D Graphics in OpenGL 34 L

Transformations and Viewing

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

• There is only one set of transformation functions so we must setthe matrix mode first

glMatrixMode (GL_PROJECTION)

• Transformation functions are incremental so we start with anidentity matrix and alter it with a projection matrix that gives theview volume

glLoadIdentity ();glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

Jan-16-04 SMD159, 2D Graphics in OpenGL 35 L

Two- and three-dimensional viewing

• In glOrtho(left, right, bottom, top, near,far) the near and far distances are measured from the camera

• Two-dimensional vertex commands place all vertices in the plane z=0

• If the application is in two dimensions, we can use the function gluOrtho2D(left, right,bottom,top)• In two dimensions, the view or clipping volume becomes a clipping

window

Jan-16-04 SMD159, 2D Graphics in OpenGL 36 L

The display() Method public void display(GLDrawable drawable) { GL gl = drawable.getGL(); GLU glu = drawable.getGLU();

gl.glClear(GL.GL_COLOR_BUFFER_BIT); // clear window

gl.glBegin(GL.GL_POLYGON); // draw unit square polygon gl.glVertex2f(-0.5f, -0.5f); gl.glVertex2f(-0.5f, 0.5f); gl.glVertex2f(0.5f, 0.5f); gl.glVertex2f(0.5f, -0.5f);

gl.glEnd(); gl.glFlush(); // flush GL buffers }

13

Jan-16-04 SMD159, 2D Graphics in OpenGL 37 L

INSTITUTIONEN FÖRSYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Fundamental OpenGL Primitives

Jan-16-04 SMD159, 2D Graphics in OpenGL 38 L

OpenGL Primitives

GL_QUAD_STRIPGL_QUAD_STRIP

GL_POLYGONGL_POLYGON

GL_TRIANGLE_STRIPGL_TRIANGLE_STRIPGL_TRIANGLE_FANGL_TRIANGLE_FAN

GL_POINTSGL_POINTS

GL_LINESGL_LINES

GL_LINE_LOOPGL_LINE_LOOP

GL_LINE_STRIPGL_LINE_STRIP

GL_TRIANGLESGL_TRIANGLES

Jan-16-04 SMD159, 2D Graphics in OpenGL 39 L

Polygon Issues

• OpenGL will only display polygons correctly that are

- Simple: edges cannot cross

- Convex: All points on line segment between two points in apolygon are also in the polygon

- Flat: all vertices are in the same plane

• User program must check if above true

• Triangles satisfy all conditions

non-simple polygon non-convex polygon

14

Jan-16-04 SMD159, 2D Graphics in OpenGL 40 L

INSTITUTIONEN FÖRSYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Attributes

Jan-16-04 SMD159, 2D Graphics in OpenGL 41 L

Attributes

• Attributes are part of the OpenGL and determine theappearance of objects- Color (points, lines, polygons)

- Size and width (points, lines)

- Stipple pattern (lines, polygons)

- Polygon mode

+ Display as filled: solid color or stipple pattern

+ Display edges

Jan-16-04 SMD159, 2D Graphics in OpenGL 42 L

RGB color

• Each color component stored separately in the frame buffer

• Usually 8 bits per component in buffer• Note in glColor3f the color values range from 0.0 (none) to

1.0 (all), while in glColor3ub the values range from 0 to 255

15

Jan-16-04 SMD159, 2D Graphics in OpenGL 43 L

Indexed Color

• Colors are indices into tables of RGB values

• Requires less memory- indices usually 8 bits

- not as important now

+ Memory inexpensive

+ Need more colors for shading

Jan-16-04 SMD159, 2D Graphics in OpenGL 44 L

Color and State

• The color as set by glColor becomes part of the state and willbe used until changed- Colors and other attributes are not part of the object but are

assigned when the object is rendered• We can create conceptual vertex colors by code such as glColor glVertex glColor glVertex

Jan-16-04 SMD159, 2D Graphics in OpenGL 45 L

Smooth Color

• Default is smooth shading

- OpenGL interpolates vertex colors across visible polygons

• Alternative is flat shading

- Color of first vertex

determines fill color

• glShadeModel(GL_SMOOTH)or GL_FLAT

16

Jan-16-04 SMD159, 2D Graphics in OpenGL 46 L

Viewports

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

• Values in pixels (screen coordinates)

Jan-16-04 SMD159, 2D Graphics in OpenGL 47 L

Questions?