programming in opengl ryan holmes cse 570 february 12 th, 2003

26
Programming in OpenGL Ryan Holmes CSE 570 February 12 th , 2003

Upload: ethel-benson

Post on 25-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

Programming in OpenGL

Ryan HolmesCSE 570February 12th, 2003

Page 2: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

Overview

What does OpenGL include? What doesn’t OpenGL include? How do you get started with OpenGL? Tips and Tricks Resources Questions and Answers

Page 3: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

OpenGL Is

A low-level 3D graphics API A separate piece of code you access

through an API An interface to hardware Primitive-based

A state machine

Page 4: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

A low-level 3D graphics API

Separate code Opengl32.dll on Windows Vendors package their own version of

this library with the graphics card Windows 2000 supports a software-

only version of OpenGL 1.1 out of the box

Page 5: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

A low-level 3D graphics API

An interface to hardware The library knows how to interface

with the card drivers to get the hardware to handle the graphics.

Anything not done in hardware is done in software

Page 6: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

A low-level 3D graphics API

Primitive-based Objects consist of points, line-

segments, and polygons OpenGL is not aware of any

connections between primitives Exception

The GLU libraries include quadric and NURBS “objects” that encapsulate primitives for you

Page 7: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

A state machine

Functions are global and change the state of the OpenGL environment State can be pushed onto stacks and popped back off OpenGL properties remain as you set them until you set them again

Page 8: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

OpenGL Is Not

A modeling language Compiled directly into your code 3D object-oriented

Page 9: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

Getting Started - Syntax

OpenGL core functions are prefixed with gl OpenGL utility functions are prefixed with glu OpenGL typedef defined types are prefixed with GL OpenGL constants are all caps and prefixed with GL_

Page 10: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

Getting Started - Example::glBegin(GL_LINE_LOOP);

::glVertex3d(0, 0, 0);

::glVertex3d(x, 0, 0);

::glVertex3d(x, y, 0);

::glVertex3d(0, y, 0);

::glEnd();

::glBegin(GL_LINE_LOOP);

::glVertex3d(0, 0, z);

::glVertex3d(x, 0, z);

::glVertex3d(x, y, z);

::glVertex3d(0, y, z);

::glEnd();

::glBegin(GL_LINES);

::glVertex3d(0, 0, 0);

::glVertex3d(0, 0, z);

::glVertex3d(x, 0, 0);

::glVertex3d(x, 0, z);

::glVertex3d(x, y, 0);

::glVertex3d(x, y, z);

::glVertex3d(0, y, 0);

::glVertex3d(0, y, z);

::glEnd();

Page 11: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

Getting Started - Interface

OpenGL has no direct interface functionality Mouse, keyboard and window management handled by separate interface Windows wgl (“wiggle”) functions

Page 12: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

Environment Choice

MFC Strong interface support (Dialogs,

menus, mouse handling) Steep learning curve

GLUT Portable Easier learning curve

Page 13: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

MFC Program Organization

Document/View architecture Document – Encapsulate Data View – Display Data from Document

OnDraw() Called every time the window needs

to be displayed. Indirectly called by you with the Invalidate() call.

Page 14: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

GLUT Program Organization

Callback architecture – Register functions to be called when events happen. glutCreateWindow() – Setup glutDisplayFunc() – Registers your “OnDraw” equivalent glutMainLoop() – Handles redraw decisions

Page 15: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

Creating Primitives

glBegin(type) and glEnd() All primitives begin as vertices GL_POINTS GL_POLYGON GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_QUADS GL_QUAD_STRIP GL_TRIANGLES GL_TRIANGLE_FAN GL_TRIANGLE_STRIP

Page 16: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

Vertex Data

x,y,z coordinates Color (If lighting is disabled) Materials (If lighting is enabled) Normal (If lighting is enabled) Other data (e.g. texture coordinates)

Vertex data is part of the current state!

Page 17: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

Shading

OpenGL supports flat shading (GL_FLAT) and smooth (GL_SMOOTH) (Gouraud) shading only – glShadeModel(type) Shading is vertex-centric, not face-centric Lighting disabled – glColor Lighting enabled – glMaterial and normals must be specified properly

Page 18: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

Flat Shading Sequence

// Set material with glMaterial glNormal3d(0, 0, 1.0); glBegin(GL_TRIANGLES); glVertex3d(0,0,0); glVertex3d(2, 0, 0); glVertex3d(1, 1, 0);glEnd();

Page 19: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

Smooth Shading Sequence

// Set material with glMaterialglBegin(GL_TRIANGLES); glNormal3d(0, 0, 1.0); glVertex3d(0,0,0); glNormal3d(0, 0, 1.0); glVertex3d(2, 0, 0); glNormal3d(0, 0, 1.0); glVertex3d(1, 1, 0);glEnd();

Page 20: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

Typical Rendering Sequence

Clear the frame buffer and depth buffer – glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

Specify all primitives in the scene glBegin(type) glVertex()… glEnd()

Swap the buffers – windowing system specific

Page 21: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

Tips and Tricks I

Plan firstBreak things into pieces Put only what is necessary into OnDraw Choose your “default” state and initialize it during program start-up. Make sure that every function returns the state to that default before it exits

Page 22: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

Tips and Tricks II

Read the Red Book Appendices contain a wealth of

speed-up and optimization information

Learn the debugging tools. When a graphics program doesn’t display anything, it can be difficult to figure out what’s wrong

Page 23: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

Fundamental Principle of Graphics Programming

Graphics programming is 30% understanding what data you need and 70% keeping track of where that data is and updating it.

Design your data structures accordingly.

Page 24: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

Resources I

http://www.eas.asu.edu/~cse470/ Project Zero walk-through and

skeleton code. Links page

The “Red Book” http://biology.ncsa.uiuc.edu/library/SGI_bookshelves/SGIindex/SGI_Developer_OpenGL_PG.html

Page 25: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

Resources II

GLUT - http://www.xmission.com/~nate/glut.html

GLUT Documentation - http://www.opengl.org/developers/documentation/glut/

.NET Framework - http://csgl.sourceforge.net/

Page 26: Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003

Questions and Answers