programming with opengl part 2: complete...

21
1 91.427 Computer Graphics I, Fall 2008 1 Programming with OpenGL Part 2: Complete Programs

Upload: vuongphuc

Post on 08-Apr-2018

218 views

Category:

Documents


3 download

TRANSCRIPT

191.427 Computer Graphics I, Fall 2008 1

Programming with OpenGLPart 2: Complete Programs

291.427 Computer Graphics I, Fall 2008 2

Objectives

•Refine first program­ Alter default values

­ Introduce standard program structure

•Simple viewing­ 2-D viewing as special case of 3-D viewing

•Fundamental OpenGL primitives

•Attributes

391.427 Computer Graphics I, Fall 2008 3

Program Structure

•Most OpenGL programs have similarstructure

•Consists of following functions-main():

• defines callback functions• opens one or more windows with required properties• enters event loop (last executable statement)

-init(): sets state variables• Viewing• Attributes

­ callbacks• Display function• Input and window functions

491.427 Computer Graphics I, Fall 2008 4

simple.c revisited

•In this version, see same output, but

•defined all relevant state valuesthrough function calls using defaultvalues

•In particular, we set­ Colors

­ Viewing conditions

­ Window properties

591.427 Computer Graphics I, Fall 2008 5

main.c

#include <GL/glut.h>

int main(int argc, char** argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(500,500); glutInitWindowPosition(0,0);glutCreateWindow("simple");glutDisplayFunc(mydisplay);

init();

glutMainLoop();}

includes gl.h

define window properties

set OpenGL state

enter event loop

display callback

691.427 Computer Graphics I, Fall 2008 6

GLUT functions

•glutInit allows application to get command linearguments and initializes system

•gluInitDisplayMode requests properties for window(rendering context)­ RGB color­ Single buffering­ Properties logically ORed together

•glutWindowSize in pixels•glutWindowPosition from top-left corner of display•glutCreateWindow create window with title “simple”•glutDisplayFunc display callback•glutMainLoop enter infinite event loop

791.427 Computer Graphics I, Fall 2008 7

init.c

void init(){glClearColor (0.0, 0.0, 0.0, 1.0);

glColor3f(1.0, 1.0, 1.0);

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

}

black clear coloropaque window

fill/draw with white

viewing volume

891.427 Computer Graphics I, Fall 2008 8

Coordinate Systems

•Units in glVertex determined by application

•object or problem coordinates

•Viewing specifications also in object coordinates

•size of viewing volume determines what will appearin image

•Internally, OpenGL convert to camera (eye)coordinates

•and later to screen coordinates

•OpenGL also uses some internal representations

•usually not visible to application

991.427 Computer Graphics I, Fall 2008 9

OpenGL Camera

•OpenGL places cameraat origin in objectspace pointing innegative z direction

•default viewing volume

= box centered at

origin with side of

length 2

1091.427 Computer Graphics I, Fall 2008 10

Orthographic Viewing

z=0

z=0

In default orthographic view, points areprojected forward along z axis ontoplane z = 0

1191.427 Computer Graphics I, Fall 2008 11

Transformations and Viewing

•In OpenGL, projection carried out by projectionmatrix (transformation)

•only one set of transformation functions ==> mustset matrix mode first

glMatrixMode (GL_PROJECTION)

• Transformation functions incremental ==>•start with identity matrix•alter it with projection matrix that gives view

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

1291.427 Computer Graphics I, Fall 2008 12

Two- and three-dim viewing

•In glOrtho(left, right, bottom, top,near, far) near and far distances measuredfrom camera

•2-D vertex commands place all vertices in planez = 0

•If application is in 2-D, can use function gluOrtho2D(left, right,bottom,top)

•In 2-D view or clipping volume becomes clippingwindow

1391.427 Computer Graphics I, Fall 2008 13

mydisplay.c

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();

}

1491.427 Computer Graphics I, Fall 2008 14

OpenGL Primitives

GL_QUAD_STRIP

GL_POLYGON

GL_TRIANGLE_STRIP GL_TRIANGLE_FAN

GL_POINTS

GL_LINES

GL_LINE_LOOP

GL_LINE_STRIP

GL_TRIANGLES

1591.427 Computer Graphics I, Fall 2008 15

Polygon Issues

•OpenGL only display correctly polygons thatare­ Simple: edges cannot cross

­ Convex: All points on segment also in polygon

­ Flat: all vertices in same plane (planar)

•User program can check if above true­ OpenGL produce output if conditions violated

• but may not be what desired

•Triangles satisfy all conditions

nonsimple polygon nonconvex polygon

1691.427 Computer Graphics I, Fall 2008 16

Attributes

•Attributes are part of OpenGL state

•determine appearance of objects­ Color (points, lines, polygons)

­ Size and width (points, lines)

­ Stipple pattern (lines, polygons)

­ Polygon mode•Display as filled: solid color or stipplepattern

•Display edges

•Display vertices

1791.427 Computer Graphics I, Fall 2008 17

RGB color

•Each color component stored separately inframe buffer

•Usually 8 bits per component in buffer•Note in glColor3f color values range from0.0 (none) to 1.0 (all), whereas inglColor3ub values range from 0 to 255

1891.427 Computer Graphics I, Fall 2008 18

Indexed Color

•Colors = indices into tables of RGB values

•Requires less memory­ indices usually 8 bits

­ not as important now•Memory inexpensive

•Need more colors for shading

1991.427 Computer Graphics I, Fall 2008 19

Color and State

•color as set by glColor becomes part of state•will be used until changed­ Colors and other attributes not part of object but­ assigned when object rendered

•can create conceptual vertex colors by code such as glColor glVertex glColor glVertex

2091.427 Computer Graphics I, Fall 2008 20

Smooth Color

•Default is smooth shading­ OpenGL interpolates vertex colors acrossvisible polygons

•Alternative is flat shading­ Color of first vertex

determines fill color

•glShadeModel(GL_SMOOTH)or GL_FLAT

2191.427 Computer Graphics I, Fall 2008 21

Viewports

•Don’t have to use entire window forimage: glViewport(x,y,w,h)

•Values in pixels (screen coordinates)