32
INTRODUCTION TO OPENGL

INTRODUCTION TO OPENGL

Embed Size (px)

DESCRIPTION

INTRODUCTION TO OPENGL. INTRODUCTION TO OPENGL. What is OpenGL API Functions Event Driven Programming Using OpenGL. OpenGL. OpenGL is a platform-independent API that is Easy to use Close enough to the hardware to get excellent performance Focus on rendering - PowerPoint PPT Presentation

Citation preview

Page 1: INTRODUCTION TO OPENGL

INTRODUCTION TO OPENGL

Page 2: INTRODUCTION TO OPENGL

INTRODUCTION TO OPENGL

What is OpenGL APIFunctions Event Driven Programming Using

OpenGL

Page 3: INTRODUCTION TO OPENGL

OpenGL

OpenGL is a platform-independent API that is ◦Easy to use◦Close enough to the hardware to get

excellent performance◦Focus on rendering◦Omitted windowing and input to

avoid window system dependencies

Page 4: INTRODUCTION TO OPENGL

4

OpenGL InterfaceComponents of the OpenGL

Interface◦GL: core OpenGL functions◦GLU: graphics utility library

(helpers for creating common objects, eg. Spheres)

◦GLUT: GL Utility Toolkit(interface to windowing system)

◦GLX: low-level interface to X Windows

Page 5: INTRODUCTION TO OPENGL

Basic OpenGL SyntaxFunction names from GL library

◦ prefixed with gl

Symbolic constants◦ Capital letters with underscore

OpenGL built-in data types

glBegin, glClear, glCopyPixelsglBegin, glClear, glCopyPixels

GL_2D, GL_RGB, GL_POLYGONGL_2D, GL_RGB, GL_POLYGON

GLbyte, GLshort, Gint, GLfloat, GLdouble, GLbooleanGLbyte, GLshort, Gint, GLfloat, GLdouble, GLboolean

Page 6: INTRODUCTION TO OPENGL

GLUTOpenGL Utility Toolkit (GLUT)

◦Provides functionality common to all window systems Open a window Get input from mouse and keyboard Menus Event-driven

◦Code is portable but GLUT lacks the functionality of a good toolkit for a specific platform No slide bars

Page 7: INTRODUCTION TO OPENGL

GLUT

Initializes GLUT and should be called before any OpenGL functions.

void glutInit (int argc, char ** argv)void glutInit (int argc, char ** argv)

Function names from GLUT library prefixed with glut

Creates a window on the screen with the title given by the argument.

void glutCreateWindow (*char title)void glutCreateWindow (*char title)

Page 8: INTRODUCTION TO OPENGL

Event Loops & Callback Function

The function func() is called each time there is a display callback

void glutDisplayFunc (void (*func) (void))void glutDisplayFunc (void (*func) (void))

Events – mouse, keyboard, windows events.

Callback function – define how the program should react to specific events.

Page 9: INTRODUCTION TO OPENGL

Event Loops & Callback Function

Causes the program to enter an event-processing loop.

Should be the last function in main().

void glutMainLoop()void glutMainLoop()

Page 10: INTRODUCTION TO OPENGL

OpenGL function format

glVertex3f(x,y,z)

belongs to GL library

function name

x,y,z are floats

dimensions

Page 11: INTRODUCTION TO OPENGL

A Simple Program#include <GL/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();

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

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

}

Page 12: INTRODUCTION TO OPENGL

A Simple Program

Page 13: INTRODUCTION TO OPENGL

Event LoopNote that the program defines a

display callback function named mydisplay◦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 main function ends with the program entering an event loop

Page 14: INTRODUCTION TO OPENGL

USING RGB COLOR#include <GL\glut.h>

void display(){

glClear (GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 0.0, 0.0); 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 ();}

int main(int argc, char** argv)

{

glutInit (&argc, argv);

glutCreateWindow (“Simple");

glClearColor(1.0, 1.0, 1.0, 0.0);

glutDisplayFunc (display);

glutMainLoop ();

}

Page 15: INTRODUCTION TO OPENGL

USING RGB COLOR◦Set a particular color:◦ glColor3f(r, g, b);

◦Set a background color:◦ glClearColor(r, g, b, 1);

◦Clear the window to background color:

◦glClear(GL_COLOR_BUFFER_BIT);

Page 16: INTRODUCTION TO OPENGL

USING RGB COLOR

Page 17: INTRODUCTION TO OPENGL

Program StructureMost OpenGL programs have a similar

structure that consists of the following functions◦ main():

defines the callback functions opens one or more windows with the required

properties enters event loop (last executable statement)

◦ init(): sets the state variables Viewing Attributes

◦ callbacks Display function Input and window functions

Page 18: INTRODUCTION TO OPENGL

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

}

Page 19: INTRODUCTION TO OPENGL

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

Page 20: INTRODUCTION TO OPENGL

Coordinate SystemsThe 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

Page 21: INTRODUCTION TO OPENGL

Transformations and ViewingIn OpenGL, 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

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

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

Page 22: INTRODUCTION TO OPENGL

Two- and three-dimensional viewingIn 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

Page 23: INTRODUCTION TO OPENGL

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

[Edward Angel, Interactive computer Graphics, 2009]

Page 24: INTRODUCTION TO OPENGL

Event Driven ProgrammingMouse Event

◦Event that occurs when the mouse button is pressed or released.

Mouse Motion Event

◦Event that occurs when the mouse is moved while one of the buttons is pressed.

Page 25: INTRODUCTION TO OPENGL

Mouse Event

Contains the following information:

◦Button: The mouse button that is pressed – left, middle, right.

◦State: The state of the button – up, down.

◦Position: The position of the mouse when the event occurs (x, y).

Page 26: INTRODUCTION TO OPENGL

Mouse Event◦Registering with MouseEvent:◦ glutMouseFunc(myMouse);

◦Call-back function:◦ void myMouse(int button, int state, int x, int y)

◦Values for button:◦ GLUT_LEFT_BUTTON ◦ GLUT_MIDDLE_BUTTON◦ GLUT_RIGHT_BUTTON◦Values for state:◦ GLUT_UP◦ GLUT_DOWN◦x, y: ◦ screen coordinates of mouse position ◦ (origin at top-left corner)

Page 27: INTRODUCTION TO OPENGL

Mouse Motion EventContains the following

information:

◦Position: The current position of the mouse as the mouse is being dragged holding one of the buttons pressed. The events are continuously generated as the mouse button is pressed and dragged.

Page 28: INTRODUCTION TO OPENGL

Mouse Motion Event◦Registering with MouseMotionEvent:◦ glutMotionFunc(myMovedMouse);

◦Call-back function:◦ void myMovedMouse(int x, int y)

◦x, y: ◦ screen coordinates of mouse

position ◦ (origin at top-left corner)

Page 29: INTRODUCTION TO OPENGL

Keyboard EventContains the following

information:

◦key: The ASCII value of the key pressed.

◦Position: The current position of the mouse when the key is pressed.

Page 30: INTRODUCTION TO OPENGL

Keyboard Event◦Registering with KeyboardEvent:◦ glutKeyboardFunc(myKeyboard);

◦Call-back function:◦ void myKeyboard(unsigned int key, int x,

int y)

◦Values for keys:◦ ASCII values for normal keys ◦ GLUT_KEY_LEFT, GLUT_KEY_RIGHT, …

(arrow keys)◦x, y: ◦ screen coordinates of mouse position ◦ (origin at top-left corner)

Page 31: INTRODUCTION TO OPENGL

Window EventsThe Window Redraw Event

occurs whenever the window needs to be redrawn. This happens when the window is first opened and when the window is exposed by moving another window off of it.

The Window Reshape Event is generated when the window is resized with the mouse.

Page 32: INTRODUCTION TO OPENGL

Window Events◦Registering with

WindowRedrawEvent:◦ glutDisplayFunc(myDisplay);

◦Registering with WindowReshapeEvent:

◦ glutReshapeFunc(Reshape);