Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program.

Similar presentations


Presentation on theme: "CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program."— Presentation transcript:

1 CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program standard structure Introduce the OpenGL program standard structure

2 CSC 461: Lecture 52 A Simple Program Source code: simple.c Source code: simple.c #include 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(); // force to display } int main(int argc, char** argv){ glutCreateWindow("simple"); glutDisplayFunc(mydisplay); glutMainLoop(); } Generate a square on a solid background Generate a square on a solid background

3 CSC 461: Lecture 53 Event Loop Note that the program defines a display callback function named mydisplay Note that the program defines a display callback function named mydisplay Every glut program must have a display callback 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 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 The main function ends with the program entering an event loop

4 CSC 461: Lecture 54 Defaults simple.c is too simple simple.c is too simple Makes heavy use of state variable default values for Makes heavy use of state variable default values for Viewing Viewing Colors Colors Window parameters Window parameters Next version will make the defaults more explicit Next version will make the defaults more explicit

5 CSC 461: Lecture 55 Program Structure Most OpenGL programs have a similar structure that consists of the following functions Most OpenGL programs have a similar structure that consists of the following functions main() : main() : defines the callback functions defines the callback functions opens one or more windows with the required properties opens one or more windows with the required properties enters event loop (last executable statement) enters event loop (last executable statement) init() : sets the state variables init() : sets the state variables viewing viewing Attributes Attributes callbacks callbacks Display function Display function Input and window functions Input and window functions

6 CSC 461: Lecture 56 Simple.c revisited In this version, we will see the same output but have defined all the relevant state values through function calls with the default values In this version, we will see the same output but have defined all the relevant state values through function calls with the default values In particular, we set In particular, we set Colors Colors Viewing conditions Viewing conditions Window properties Window properties

7 CSC 461: Lecture 57 Source: main.c #include #include 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

8 CSC 461: Lecture 58 Source code: 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 color opaque window fill with white viewing volume

9 CSC 461: Lecture 59 Source code: 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();}

10 CSC 461: Lecture 510 GLUT functions glutInit allows application to get command line arguments and initializes system glutInit allows application to get command line arguments and initializes system gluInitDisplayMode requests properties of the window (the rendering context) gluInitDisplayMode requests properties of the window (the rendering context) RGB color RGB color Single buffering Single buffering Properties logically ORed together Properties logically ORed together glutWindowSize in pixels glutWindowSize in pixels glutWindowPosition from top-left corner of display glutWindowPosition from top-left corner of display glutCreateWindow create window with title “simple” glutCreateWindow create window with title “simple” glutDisplayFunc display callback glutDisplayFunc display callback glutMainLoop enter infinite event loop glutMainLoop enter infinite event loop

11 CSC 461: Lecture 511 Coordinate Systems The units in glVertex are determined by the application and are called world or problem coordinates The units in glVertex are determined by the application and are called world or problem coordinates The viewing specifications are also in world coordinates and it is the size of the viewing volume that determines what will appear in the image The viewing specifications are also in world coordinates and it is the size of the viewing volume that determines what will appear in the image Internally, OpenGL will convert to camera coordinates and later to screen coordinates Internally, OpenGL will convert to camera coordinates and later to screen coordinates

12 CSC 461: Lecture 512 OpenGL Camera OpenGL places a camera at the origin pointing in the negative z direction OpenGL places a camera at the origin pointing in the negative z direction The default viewing volume The default viewing volume is a box centered at the origin with a side of length 2 is a box centered at the origin with a side of length 2

13 CSC 461: Lecture 513 Orthographic Viewing In the default orthographic view, points are projected forward along the z axis onto the plane z=0 z=0

14 CSC 461: Lecture 514 Transformations and Viewing In OpenGL, the projection is carried out by a projection matrix (transformation) In OpenGL, the projection is carried out by a projection matrix (transformation) There is only one set of transformation functions so we must set the matrix mode first There is only one set of transformation functions so we must set the matrix mode first glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION); Transformation functions are incremental so we start with an identity matrix and alter it with a projection matrix that gives the view volume Transformation functions are incremental so we start with an identity matrix and alter it with a projection matrix that gives the view volume glLoadIdentity(); glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

15 CSC 461: Lecture 515 Three-dimensional viewing glOrtho(left,right,bottom,top,near,far) Left, right, bottom, top, near and far specify the six sides Left, right, bottom, top, near and far specify the six sides The near and far distances are measured from the camera The near and far distances are measured from the camera

16 CSC 461: Lecture 516 Two-dimensional viewing Two-dimensional vertex commands place all vertices in the plane z=0 Two-dimensional vertex commands place all vertices in the plane z=0 If the application is in two dimensions, we can use the function If the application is in two dimensions, we can use the function gluOrtho2D(left, right,bottom,top) gluOrtho2D(left, right,bottom,top) z=0 In two dimensions, the view or clipping volume becomes a clipping window In two dimensions, the view or clipping volume becomes a clipping window


Download ppt "CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program."

Similar presentations


Ads by Google