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

16
CSC 461: CSC 461: Lecture 5 Lecture 5 1 CSC461 Lecture 5: Simple OpenGL Program Objectives: Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program Introduce the OpenGL program standard structure standard structure
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    227
  • download

    3

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

CSC 461: Lecture 5CSC 461: Lecture 5 11

CSC461 Lecture 5:Simple OpenGL Program

Objectives:Objectives:

Discuss a simple programDiscuss a simple program Introduce the OpenGL program standard Introduce the OpenGL program standard

structurestructure

CSC 461: Lecture 5CSC 461: Lecture 5 22

A Simple ProgramA Simple ProgramSource code: simple.cSource code: 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(); // force to display

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

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

}

Generate a square on Generate a square on a solid backgrounda solid background

CSC 461: Lecture 5CSC 461: Lecture 5 33

Event LoopEvent LoopNote that the program defines a Note that the program defines a display display

callbackcallback function named function named mydisplaymydisplay Every glut program must have a display callbackEvery glut program must have a display callback The display callback is executed whenever OpenGL The display callback is executed whenever OpenGL

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

The The mainmain function ends with the program entering function ends with the program entering an event loopan event loop

CSC 461: Lecture 5CSC 461: Lecture 5 44

DefaultsDefaults

simple.csimple.c is too simple is too simpleMakes heavy use of state variable default Makes heavy use of state variable default

values forvalues for ViewingViewing ColorsColors Window parametersWindow parameters

Next version will make the defaults more Next version will make the defaults more explicitexplicit

CSC 461: Lecture 5CSC 461: Lecture 5 55

Program StructureProgram StructureMost OpenGL programs have a similar Most OpenGL programs have a similar

structure that consists of the following structure that consists of the following functionsfunctions

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

init()init(): sets the state variables: sets the state variables viewingviewing AttributesAttributes

callbackscallbacks Display functionDisplay function Input and window functionsInput and window functions

CSC 461: Lecture 5CSC 461: Lecture 5 66

Simple.c revisitedSimple.c revisited

In this version, we will see the same output but In this version, we will see the same output but have defined all the relevant state values have defined all the relevant state values through function calls with the default valuesthrough function calls with the default values

In particular, we setIn particular, we set ColorsColors Viewing conditionsViewing conditions Window propertiesWindow properties

CSC 461: Lecture 5CSC 461: Lecture 5 77

Source:Source: main.cmain.c#include <GL/glut.h>#include <GL/glut.h>

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

init(); init();

glutMainLoop();glutMainLoop();

}}

includes gl.h

define window properties

set OpenGL state

enter event loop

display callback

CSC 461: Lecture 5CSC 461: Lecture 5 88

Source code:Source code: init.cinit.c

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

glColor3f(1.0, 1.0, 1.0); glColor3f(1.0, 1.0, 1.0);

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

black clear color

opaque window

fill with white

viewing volume

CSC 461: Lecture 5CSC 461: Lecture 5 99

Source code:Source code: mydisplay.cmydisplay.cvoid mydisplay()void mydisplay(){{glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glBegin(GL_POLYGON);

glVertex2f(-0.5, -0.5); glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5); glVertex2f(0.5, -0.5);

glEnd();glEnd();glFlush(); glFlush(); }}

CSC 461: Lecture 5CSC 461: Lecture 5 1010

GLUT functionsGLUT functions glutInit glutInit allows application to get command line allows application to get command line

arguments and initializes systemarguments and initializes system gluInitDisplayMode gluInitDisplayMode requests properties of the requests properties of the

window (the window (the rendering contextrendering context)) RGB colorRGB color Single bufferingSingle buffering Properties logically ORed togetherProperties logically ORed together

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

CSC 461: Lecture 5CSC 461: Lecture 5 1111

Coordinate SystemsCoordinate SystemsThe units in The units in glVertexglVertex are determined by the are determined by the

application and are called application and are called worldworld or or problem problem coordinatescoordinates

The viewing specifications are also in world The viewing specifications are also in world coordinates and it is the size of the viewing coordinates and it is the size of the viewing volume that determines what will appear in the volume that determines what will appear in the imageimage

Internally, OpenGL will convert to Internally, OpenGL will convert to camera camera coordinatescoordinates and later to and later to screen coordinates screen coordinates

CSC 461: Lecture 5CSC 461: Lecture 5 1212

OpenGL CameraOpenGL Camera OpenGL places a OpenGL places a

camera at the origin camera at the origin pointing in the pointing in the negative negative zz direction direction

The default viewing The default viewing volumevolume

is a box centered at is a box centered at the origin with a the origin with a side of length 2side of length 2

CSC 461: Lecture 5CSC 461: Lecture 5 1313

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

z=0

z=0

CSC 461: Lecture 5CSC 461: Lecture 5 1414

Transformations and ViewingTransformations and ViewingIn OpenGL, the projection is carried out by a In OpenGL, the projection is carried out by a

projection matrix (transformation)projection matrix (transformation)There is only one set of transformation functions There is only one set of transformation functions

so we must set the matrix mode first so we must set the matrix mode first glMatrixMode(GL_PROJECTION);glMatrixMode(GL_PROJECTION); Transformation functions are incremental so we Transformation functions are incremental so we

start with an identity matrix and alter it with a start with an identity matrix and alter it with a projection matrix that gives the view volumeprojection matrix that gives the view volume glLoadIdentity(); glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

CSC 461: Lecture 5CSC 461: Lecture 5 1515

Three-dimensional viewingThree-dimensional viewingglOrtho(left,right,bottom,top,near,far)glOrtho(left,right,bottom,top,near,far)

Left, right, bottom, Left, right, bottom, top, near and far top, near and far specify the six sidesspecify the six sides

The near and far The near and far distances are distances are measured measured fromfrom the the cameracamera

CSC 461: Lecture 5CSC 461: Lecture 5 1616

Two-dimensional viewingTwo-dimensional viewing Two-dimensional vertex commands place all vertices Two-dimensional vertex commands place all vertices

in the plane z=0in the plane z=0 If the application is in two dimensions, we can use If the application is in two dimensions, we can use

the functionthe function gluOrtho2D(left, right,bottom,top)gluOrtho2D(left, right,bottom,top)

z=0

In two dimensions, In two dimensions, the view or the view or clipping volume clipping volume becomes a becomes a clipping clipping windowwindow