57
Scientific Visualization with OpenGL 22 February 2006

Scientific Visualization with OpenGL 22 February 2006

Embed Size (px)

Citation preview

Page 1: Scientific Visualization with OpenGL 22 February 2006

Scientific Visualization with

OpenGL

22 February 2006

Page 2: Scientific Visualization with OpenGL 22 February 2006

Agenda• Wrap-up Project 1

• OpenGL

• Homework– Read Chapters 1 and 2 in Cunningham– Study for next week’s Quiz 3

• User Testing• Chapter 0 – Getting Started in Cunningham

Page 3: Scientific Visualization with OpenGL 22 February 2006

Computer Graphics• Definition

• producing pictures or images using a computer

• 40 years ago – drawing lines on a cathode ray tube

• Today– produce images indistinguishable from photos– produce “realistic” and animated dinosaurs– produce 3D worlds

Page 4: Scientific Visualization with OpenGL 22 February 2006

Computer Graphics Applications

• Display of Information

• Design

• Simulation

• User Interfaces

Page 5: Scientific Visualization with OpenGL 22 February 2006

Display of Information• Maps

– GIS– Spatial Resource Planning (SRP)

• Medicine– MRI– Ultrasound

• Scientific Visualization– “seeing the unseen”– Visual Human Project– biology– electrophysiology– mathematics

Page 7: Scientific Visualization with OpenGL 22 February 2006

Spatial Resource Planning• Spatially-referenced data within engineering,

operations and distribution network management

• Analogous to what Enterprise Resource Planning (ERP) solutions do for data held in accounting, human resources, procurement and project management systems,

Page 8: Scientific Visualization with OpenGL 22 February 2006

Magnetic Resonance Imaging• Uses magnetism and radio waves to produce

images

Page 9: Scientific Visualization with OpenGL 22 February 2006

Ultrasound• Images are produced by very high frequency

sound waves of between 3.5 to 5.0 megahertz.

Page 10: Scientific Visualization with OpenGL 22 February 2006

Scientific VisualizationElectrophysiology

• Computed Potential Distribution on the Cardiac Surface during reentry: Spiral Tip Meandering, an arrhythmia model

Page 11: Scientific Visualization with OpenGL 22 February 2006

Design• “The evaluation of alternative solutions and

the specification of a solution”

• CAD

• VLSI design

• Generate a possible design, test, use solution as a basis for other solution

Page 12: Scientific Visualization with OpenGL 22 February 2006

Simulation• Flight Simulators

• Games

• Educational (edutainment) software

• Virtual Reality

Page 13: Scientific Visualization with OpenGL 22 February 2006

User Interfaces• Interaction with computers

– windows– icons– menus– a pointing device

Page 14: Scientific Visualization with OpenGL 22 February 2006

Frame Buffer• Depth -- number of bits used for each pixel

– full color systems• true color systems

• RGB color systems

• 24 or more bits per pixel

• Resolution -- number of pixels in the frame buffer

Page 15: Scientific Visualization with OpenGL 22 February 2006

Output Devices• Dominant type of display is the CRT

(cathode ray tube).

• CRT emits light for a short time -- a few milliseconds.

• For a human to see a steady image the path must be retraced or refreshed at least 50x/sec.

• How are pixels displayed?

Page 16: Scientific Visualization with OpenGL 22 February 2006

How Are Pixels Displayed?

• Noninterlaced– Displayed row by row

• Interlaced– Displayed every other row– 50-75X/second (50-75Hertz)– 60Hz display refreshes the entire screen 30x/sec– Commercial TV

Page 17: Scientific Visualization with OpenGL 22 February 2006

Color CRTs• 3 phosphors

• Arranged in (sometimes triangular) triads

• Shadow mask CRT

• Screen with small holes ensures only one phosphor is excited

Page 18: Scientific Visualization with OpenGL 22 February 2006

Other [Amazing] Raster Output Devices

• Liquid Crystal Displays (LCD)

• Printers

Page 19: Scientific Visualization with OpenGL 22 February 2006

Ways to Read an Input Device

• Sampling– What is its input right now ?

• Event-based– Wait until the user does something

Page 20: Scientific Visualization with OpenGL 22 February 2006

Objects and Viewers3d world

• Object is a constant

• Viewer forms the image– human viewing system - back of eye– camera - film plane– different viewers see the same object differently

Page 21: Scientific Visualization with OpenGL 22 February 2006

The Human Vision System• Resolution

– the measure of what size objects we can see– how close we can place two points and they remain

distinct

• Intensity– physical measure of light energy

• Brightness– measure of how intense we perceive the light to be.

Page 22: Scientific Visualization with OpenGL 22 February 2006

Programming with OpenGLPart 1: Background

Ed AngelProfessor of Computer Science, Electrical and Computer Engineering,

and Media Arts: University of New Mexico

Page 23: Scientific Visualization with OpenGL 22 February 2006

Objectives• Development of the OpenGL API

• OpenGL Architecture– OpenGL as a state machine

• Functions – Types– Formats

• Simple program

Page 24: Scientific Visualization with OpenGL 22 February 2006

Early History of APIs• IFIPS (1973) formed two committees to come up with

a standard 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

Page 25: Scientific Visualization with OpenGL 22 February 2006

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)

Page 26: Scientific Visualization with OpenGL 22 February 2006

SGI and GL• Silicon Graphics (SGI) revolutionized the

graphics workstation by implementing the pipeline in hardware (1982)

• To use the system, application programmers used a library called GL

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

Page 27: Scientific Visualization with OpenGL 22 February 2006

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

system dependencies

Page 28: Scientific Visualization with OpenGL 22 February 2006

OpenGL Evolution• Controlled by an Architectural Review Board (ARB)

– Members include SGI, Microsoft, Nvidia, HP, 3DLabs,IBM,…….

– Relatively stable – Evolution reflects new hardware capabilities

–3D texture mapping and texture objects–Vertex programs

– Allows for platform specific features through extensions

Page 29: Scientific Visualization with OpenGL 22 February 2006

OpenGL Libraries• OpenGL core library

– OpenGL32 on Windows– GL on OS X/UNIX/Linux systems

• OpenGL Utility Library (GLU)– Provides functionality in OpenGL core but avoids

having to rewrite code• Links with window system

– OpenGl does not “do” windowing– Windowing is OS specific

Page 30: Scientific Visualization with OpenGL 22 February 2006

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 good toolkit for a specific platform

• Slide bars

Page 31: Scientific Visualization with OpenGL 22 February 2006

Software Organization

GLUT

GLU

GL

GLX, AGLor WGL

X, Win32, Mac O/S

software and/or hardware

application program

OpenGL Motifwidget or similar

Page 32: Scientific Visualization with OpenGL 22 February 2006

OpenGL ArchitectureImmediate Mode

DisplayList

PolynomialEvaluator

Per VertexOperations &

PrimitiveAssembly

RasterizationPer Fragment

Operations

TextureMemory

CPU

PixelOperations

FrameBuffer

Geometric pipeline

Page 33: Scientific Visualization with OpenGL 22 February 2006

OpenGL Functions• Primitives

– Points– Line Segments– Polygons

• Attributes• Transformations

– Viewing– Modeling

• Control• Input (GLUT)

Page 34: Scientific Visualization with OpenGL 22 February 2006

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

primitive are controlled by the state

– State changing• Transformation functions• Attribute functions

Page 35: Scientific Visualization with OpenGL 22 February 2006

Lack of Object Orientation• OpenGL is not object oriented so that there

are multiple functions 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 is efficiency

Page 36: Scientific Visualization with OpenGL 22 February 2006

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

Page 37: Scientific Visualization with OpenGL 22 February 2006

OpenGL #defines• Most constants are defined in the include

files gl.h, glu.h and glut.h– Note #include <glut.h> should

automatically include the others– Examples– glBegin(GL_PLOYGON)– glClear(GL_COLOR_BUFFER_BIT)

• include files also define OpenGL data types: Glfloat, Gldouble,….

Page 38: Scientific Visualization with OpenGL 22 February 2006

A Simple ProgramGenerate a square on a solid background

Page 39: Scientific Visualization with OpenGL 22 February 2006

simple.c#include <glut.h>void mydisplay(){ glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_POLYGON); glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5);

glEnd();glFlush();

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

glutCreateWindow("simple"); glutDisplayFunc(mydisplay); glutMainLoop();

}

Page 40: Scientific Visualization with OpenGL 22 February 2006

Event Loop• Note that the program defines a display

callback function named mydisplay– Every glut program must have a display callback– The display callback is executed whenever

OpenGL decides the display must be refreshed, for example when the window is opened

– The main function ends with the program entering an event loop

Page 41: Scientific Visualization with OpenGL 22 February 2006

Graphics System as a Black Box

UserProgram

GraphicsSystem

Input/OutputDevices

Function Calls

Data

Output

Input

Page 42: Scientific Visualization with OpenGL 22 February 2006

Pipeline Architectures• Example

• Benefits– translates well to computer graphics– four steps to producing an image

• transforms (rotations, scale, translations)• clipper• projector (from 3d to 2d)• rasterizer (scan conversion process)

Page 43: Scientific Visualization with OpenGL 22 February 2006

Camera Analogy

(from the red book)

Page 44: Scientific Visualization with OpenGL 22 February 2006

Stages of Vertex Transformationin OpenGL

Page 45: Scientific Visualization with OpenGL 22 February 2006

The Geometry Pipeline: Stages and Mappings

Page 46: Scientific Visualization with OpenGL 22 February 2006

3D Coordinate Systems

LEFT HANDED RIGHT HANDED

X

Z

Y

Z

Y

X

Page 47: Scientific Visualization with OpenGL 22 February 2006

3D Geometry: Model Coordinate Systems

• modeling - The process of creating and defining this geometry is called– This is usually done by defining each object in terms of a

coordinate system that makes sense for that particular object.

– Ants vs Star Wars

Page 48: Scientific Visualization with OpenGL 22 February 2006

3D Geometry: Graphics Pipeline

• MIT graphics

Page 49: Scientific Visualization with OpenGL 22 February 2006

3D Geometry• Clipping

Page 50: Scientific Visualization with OpenGL 22 February 2006

3D Geometry• Projections

Page 51: Scientific Visualization with OpenGL 22 February 2006

3D Geometry• Projections – Parallel vs Orthographic

Page 52: Scientific Visualization with OpenGL 22 February 2006

Appearance• Color - RBG or RGBA

Page 53: Scientific Visualization with OpenGL 22 February 2006

Appearance

• Texture mapping

Page 54: Scientific Visualization with OpenGL 22 February 2006

Appearance• Depth buffering

Page 55: Scientific Visualization with OpenGL 22 February 2006

A Basic OpenGL Program• Download Heat Distribution Program• Open Xcode (Icon has blue rectangle with hammer)• File / New Project• Select Command Line Utility / C++ Tool• Name the project heatflow• Add heatflow.c to the project source files. • Add System/Library/Frameworks/Glut.framework

and System/Library/Frameworks/OpenGL.framework to the project.

Page 56: Scientific Visualization with OpenGL 22 February 2006

An OpenGL Program• Select Build and Run• Fix the errors

– For a Mac substitute <GLUT/glut.h> for “glut.h”– With c main() should return an int. Change the return

value of main().

• Build and Run

Page 57: Scientific Visualization with OpenGL 22 February 2006

Unique to C• Definitions precede code statements in

functions.• main() returns an int.• argv, argc