Programming with OpenGL Part 2

Programming with OpenGL Part 2 1 Program Structure „ Most OpenGL programs have a similar structure that consists of the following functions „ mai...
Author: Godfrey Wade
5 downloads 0 Views 926KB Size
Programming with OpenGL Part 2

1

Program Structure „

Most OpenGL programs have a similar structure that consists of the following functions „

main(): „ „ „

„

init(): sets the state variables „ „

„

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

callbacks „ „

Display function Input and window functions 2

Event Loop „

Note that the program defines a display callback function named mydisplay „ „

„

Every 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

3

includes gl.h

#include

int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); define window properties glutCreateWindow("simple"); glutDisplayFunc(mydisplay);display callback init(); glutMainLoop(); }

set OpenGL state enter event loop 4

GLUT functions „

„

glutInit allows application to get command line arguments and initializes system gluInitDisplayMode requests properties for the window (the 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 5

init black clear color

void init() opaque window { glClearColor (0.0, 0.0, 0.0, 1.0); glColor3f(1.0, 1.0, 1.0);

fill/draw with white

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

viewing volume 6

mydisplay clear window to clear color void mydisplay() { Draw polygon glClear(GL_COLOR_BUFFER_BIT); with current glBegin(GL_POLYGON); fill/draw glVertex2f(-0.5, -0.5); color glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5); glEnd(); glFlush(); } Flush buffers – cause object to be drawn 7

OpenGL Primitives

GL_POINTS

GL_POLYGON GL_LINES

GL_LINE_STRIP GL_LINE_LOOP

1 0 1

0 222 4

3

GL_TRIANGLES

1

2 4

2

3 5

3

0

GL_TRIANGLE_STRIP New point gives new triangle

1

0

GL_TRIANGLE_FAN First point in all triangles

GL_QUAD_STRIP Each pair of points adds a quad 8

Polygon Issues „

OpenGL will only display polygons correctly that are „ „

„

„

User program can check if above true „

„

Simple: edges cannot cross Convex: All points on line segment between two points in a polygon are also in the polygon Flat: all vertices are in the same plane OpenGL will produce output if these conditions are violated but it may not be what is desired

Triangles satisfy all conditions nonconvex polygon nonsimple polygon

9

Attributes „

Attributes are part of the OpenGL state and determine the appearance 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 (default) Display edges Display vertices

„

Only one set - cannot fill and display edges

„ „

10

void mydisplay() { glClear(GL_COLOR_BUFFER_BIT); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glBegin(GL_POLYGON); „

„ „ „ „ „ „ „

glColor3f(1.0, 0.0, 0.0); glVertex2f(-0.5, -0.5); glColor3f(0.0, 1.0, 0.0); glVertex2f(-0.5, 0.5); glColor3f(0.0, 0.0, 1.0); glVertex2f(0.5, 0.5); glColor3f(1.0, 1.0, 0.0); glVertex2f(0.5, -0.5); glEnd(); glFlush();

}

11

void mydisplay() { glClear(GL_COLOR_BUFFER_BIT); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glBegin(GL_POLYGON); „

„ „ „ „ „ „ „

glColor3f(1.0, 0.0, 0.0); glVertex2f(-0.5, -0.5); glColor3f(0.0, 1.0, 0.0); glVertex2f(-0.5, 0.5); glColor3f(0.0, 0.0, 1.0); glVertex2f(0.5, 0.5); glColor3f(1.0, 1.0, 0.0); glVertex2f(0.5, -0.5); glEnd(); glFlush();

}

12

void mydisplay() { glClear(GL_COLOR_BUFFER_BIT); glPolygonMode(GL_FRONT_AND_BACK, GL_POINT); glBegin(GL_POLYGON); „

„ „ „ „ „ „ „

glColor3f(1.0, 0.0, 0.0); glVertex2f(-0.5, -0.5); glColor3f(0.0, 1.0, 0.0); glVertex2f(-0.5, 0.5); glColor3f(0.0, 0.0, 1.0); glVertex2f(0.5, 0.5); glColor3f(1.0, 1.0, 0.0); glVertex2f(0.5, -0.5); glEnd(); glFlush();

}

13

RGB color „

„ „

Each color component is stored separately in the frame buffer Usually 8 bits per component in buffer (256 values) Note in glColor3f the color values range from 0.0 (none) to 1.0 (all), whereas in glColor3ub the values range from 0 to 255

14

Color and State „

„

The color as set by glColor becomes part of the state and will be 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 15

Vertices with Color in simple.cpp void mydisplay() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); „

„ „ „ „ „ „ „

glColor3f(1.0, 0.0, 0.0); glVertex2f(-0.5, -0.5); glColor3f(0.0, 1.0, 0.0); glVertex2f(-0.5, 0.5); glColor3f(0.0, 0.0, 1.0); glVertex2f(0.5, 0.5); glColor3f(1.0, 1.0, 0.0); glVertex2f(0.5, -0.5); glEnd(); glFlush();

}

16

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 17

Vertices with Color in simple.cpp void mydisplay() { glClear(GL_COLOR_BUFFER_BIT); glShadeModel(GL_FLAT); glBegin(GL_POLYGON); „

„ „ „ „ „ „ „

glColor3f(1.0, 0.0, 0.0); glVertex2f(-0.5, -0.5); glColor3f(0.0, 1.0, 0.0); glVertex2f(-0.5, 0.5); glColor3f(0.0, 0.0, 1.0); glVertex2f(0.5, 0.5); glColor3f(1.0, 1.0, 0.0); glVertex2f(0.5, -0.5); glEnd(); glFlush();

}

18

Viewports „

„

Do not have to use the entire window for the image: glViewport(x,y,w,h) Values in pixels (screen coordinates)

19

Viewport in simple.cpp void mydisplay() { glViewport(250,250,250,250); glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); „

„ „ „ „ „ „ „

glColor3f(1.0, 0.0, 0.0); glVertex2f(-0.5, -0.5); glColor3f(0.0, 1.0, 0.0); glVertex2f(-0.5, 0.5); glColor3f(0.0, 0.0, 1.0); glVertex2f(0.5, 0.5); glColor3f(1.0, 1.0, 0.0); glVertex2f(0.5, -0.5); glEnd(); glFlush();

}

20

Rendering: Transformations „ „

„

So far, discussion has been in screen space But model is stored in model space (a.k.a. object space or world space) Three sets of geometric transformations: „ „ „

Modeling transforms Viewing transforms Projection transforms

21

Coordinate Systems „

The units in glVertex are determined by the application and are called object or problem

coordinates

„

„

„

The viewing specifications are also in object coordinates and it is the size of the viewing volume that determines what will appear in the image Internally, OpenGL will convert to camera (eye) coordinates and later to screen coordinates OpenGL also uses some internal representations that usually are not visible to the application 22

The Rendering Pipeline: 3-D Scene graph Object geometry

Result:

Modeling Transforms

• All vertices of scene in shared 3-D “world” coordinate system

Lighting Calculations

• Vertices shaded according to lighting model

Viewing Transform

• Scene vertices in 3-D “view” or “camera” coordinate system

Clipping

Projection Transform

• Exactly those vertices & portions of polygons in view frustum • 2-D screen coordinates of clipped vertices

23

Rendering: Transformations „

Modeling transforms „

„ „

Size, place, scale, and rotate objects and parts of the model Put all your objects into one modeling space Object coordinates to world coordinates

24

Rendering: Transformations „

Viewing transform „

Rotate & translate the world to lie directly in front of the camera Typically place camera at origin „ Typically looking down -Z axis „ So move objects to –Z or you may think to move the camera backwards „

„

World coordinates to view coordinates

25

OpenGL Camera OpenGL places a camera at the origin in object space pointing in the negative z direction „ The default viewing volume is a box centered at the origin with a side of length 2 „

26

Rendering: Transformations „

Perspective Camera

„

Orthographic Camera

27

Rendering: Transformations „

Projection transform „

Apply perspective foreshortening „

„

Projection model

View coordinates ¿ screen coordinates

28

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

z=0

z=0

29

OpenGL Matrix Stacks „

OpenGL basically just renders vertices „ „

„

Vertices can be grouped to form polygons Polygons can be grouped to form shapes (solids)

Each glVertex rendered by OpenGL is transformed by the top matrix on the MODELVIEW matrix stack

30

OpenGL Matrix Stacks „

Every vertex rendered by an OpenGL camera is also multiplied by the top matrix on the PROJECTION matrix „

The projection matrix controls such effects as: „ „ „ „

Field of view Perspective vs. Orthographic Clipping planes Viewing frustum

31

OpenGL Matrix Basics „

glMatrixMode() specifies which matrix stack is being altered „

„

„ „

There is only one set of transformation functions so we must set the matrix mode first glMatrixMode (GL_PROJECTION) glMatrixMode (GL_MODELVIEW)

glPushMatrix() and glPopMatrix() are two common commands to control stack glLoadIdentity() will put identity matrix on top of stack glTranslate and glRotate also place matrices on stack that cause translations and rotations

32

2D and 3D 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 33