CA 302 Computer Graphics and Visual Programming Lecture 2: Introduction to OpenGL Aydın Öztürk...

Post on 18-Jan-2016

216 views 0 download

Tags:

Transcript of CA 302 Computer Graphics and Visual Programming Lecture 2: Introduction to OpenGL Aydın Öztürk...

CA 302 CA 302 Computer Graphics and Visual Computer Graphics and Visual Programming Programming

Lecture 2: Introduction to OpenGL

Aydın Öztürk

aydin.ozturk@ege.edu.trhttp://www.ube.ege.edu.tr/~ozturk

Introduction to OpenGLIntroduction to OpenGL

OpenGL: Open Graphics Library

OpenGL provides a library of functions for various graphical operations including• Graphics pirimitives• Attiributes• Geometric transformations• Viewing transformations

OpenGL is hardware independent

Basic OpenGL SyntaxBasic OpenGL Syntax

In OpenGL, function names are prefixed with gl.glBegin

glClear

glCopyPixels

glPolygonMode

Symbolic constants assigened as an argument of a function begin as GL followed by its name

GL_2D

GL_RGB

GL_POLYGON

Example: glBegin(GL_POLYGON);

Basic OpenGL Syntax(cont.)Basic OpenGL Syntax(cont.)

In OpenGL functions expect specific data types.The OpenGL uses special built-in data-type

names

GLbyte

GLshort

GLint

GLfloat

GLdouble

GLboolean

Basic OpenGL Syntax(cont.)Basic OpenGL Syntax(cont.)

Suffix

Data Type Typical Corresponding C-Language Type

OpenGL Type Definition

b 8-bit integer signed char GLbyte

s 16-bit integer short GLshort

i 32-bit integer long GLint, GLsizei

f 32-bit floating-point

float GLfloat, GLclampf

d 64-bit floating-point

double GLdouble, GLclampd

ub 8-bit unsigned integer

unsigned char GLubyte, GLboolean

us 16-bit unsigned integer

unsigned short GLushort

ui 32-bit unsigned integer

unsigned long GLuint, GLenum, GLbitfield

Basic OpenGL Syntax(cont.)Basic OpenGL Syntax(cont.)

The two commands are equivalent,

glVertex2i(1, 3);

glVertex2f(1.0, 3.0);

Except that the first specifies the vertex'scoordinates as 32-bit integers and the second

specifies themas single-precision floating-point numbers.

Basic OpenGL Syntax(cont.)Basic OpenGL Syntax(cont.)

Some OpenGL commands can take a final letter v, which indicates that the command takes a pointer to a vector (or array) of values rather than a series of individual arguments.

glColor3f(1.0, 0.0, 0.0);

float color_array[] = {1.0, 0.0, 0.0};

glColor3fv(color_array);

The OpenGL Utility LibraryThe OpenGL Utility Library

In addition to the OpenGL core library, there are a number of associated libraries for handling special operations:

The OpenGL Utility Library (GLU) contains several routines that use lower-level OpenGL commands to perform such tasks as setting up matrices for specific viewing orientations and projections, performing polygon tessellation, and rendering surfaces.

GLU (The OpenGL Utility) library: Provides routines for Setting up vieving and projection matrices Describing complex objects with line and polygon

approximations Displaying B_splines and processing surface-rendering

operations.

Related LibrariesRelated Libraries

Every OpenGL implementation includes the GLU library.

GLU function names start with the prefix glu:

gluBeginCurve

gluCylinder

gluPerspective

gluNurbsSurface

Related Libraries(cont.)Related Libraries(cont.)

Since OpenGL is hardware independent, we need to set up a display window on our video screen by using OpenGL.

This is a rectangular area of the screen in which our picture will be displayed.

There are several window-system libraries that support OpenGL functions for a variety of machines.

For Microsoft Windows systems, the WGL routines provide a Windows-to-OpenGL interface. These routines are prefixed with the letters wgl.

Related Libraries(cont.)Related Libraries(cont.)

The OpenGL Utility Toolkit (GLUT) provides a library of functions for interacting with any screen-windowing system.

The GLUT library functions are prefixed with glut and this library contains methods for rendering curves and surfaces.

glutCreateWindowglutDisplayFuncglutFullScreenglutSetColor

Related Libraries(cont.)Related Libraries(cont.)

Since the OpenGL Utility Toolkit(GLUT) is an interface to other device-specific windows systems, we can use GLUT so that our programs will be device independent.

Header FilesHeader Files

Files: .h, .lib, .dll• The entire folder gl is placed in the Include

directory of Visual C++• The individual lib files are placed in the lib

directory of Visual C++• The individual dll files are placed in C:\

Windows\System32

Header FilesHeader Files

In all graphics programs we need to include the header file for the OpenGL core library.

For most applications we also need header file for GLU and for window system.

#include <windows.h>

#include <GL/gl.h>

#include <GL/glu.h>

Header Files(cont.)Header Files(cont.)

If we use GLUT we do not need to include gl.h and glu.h Thus, we replace the header files for OpenGL and GLU with

#include <GL/glut.h>

Header Files(cont.)Header Files(cont.)

In addition, we need to include header files that are required by the C++. For example,

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

With the new ISO/ANSI standard for C++ , these header files are called cstdio, stdlib and math.

Display-Window Management Using Display-Window Management Using GLUTGLUT

Since we use GLUT for window operations, we need to initialize it.

We perform the GLUT initialization with the statement

glutinit (&argc, argv);

Display-Window Management Using Display-Window Management Using GLUTGLUT

To create a display window on the screen we write the statement

glutCreateWindow ("An OpenGL program");

Display-Window Management Using Display-Window Management Using GLUT(cont)GLUT(cont)

The next step is to specify what the display window is to contain.

We create a picture using OpenGL functions and pass it to glutDisplayFunc which assigns the picture to the display window by the statement

glutDisplayFunc(lineSegment);

Display-Window Management Using Display-Window Management Using GLUT(cont)GLUT(cont)

We need one more GLUT function to complete the window-processing operations.

glutMainLoop ( );

After execution of above statement, all display windows that have been created are now activated.

Display-Window Management Using Display-Window Management Using GLUT(cont)GLUT(cont)

Although the display window that we created will be in some default location and size, we can control it by the following statements which locates the top left corner of the resulting window at the point with coordinates(50,100) and with size 400×300

glutInitWindowPosition (50, 100);glutInitWindowSize (400, 300);

Display-Window Management Using Display-Window Management Using GLUT(cont)GLUT(cont)

We can also set a number of other options for the display window.

For example the following command specifies that a single refresh buffer is to be used and that the RGB color mode is to be used.

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);

A Complete OpenGL ProgramA Complete OpenGL Program

We need a few additional task to obtain a complete program

First we choose a background color (e.g. white):

glClearColor (1.0, 1.0, 1.0, 0.0);

The first argument : RedThe secondargument: GreenThe third argument : BlueThe fourth argument: Alpha value (0: Completely

transparent 1: Opac)

A Complete OpenGL Program A Complete OpenGL Program (cont.)(cont.)

Although glClearColor command assigns a color to the display window , it does not put the display window on the screen.

The following command gets the assigned window color displayed

glClear (GL_COLOR_BUFFER_BIT);

A Complete OpenGL Program A Complete OpenGL Program (cont.)(cont.)

In addition to setting the background color for the display window, we can choose colors for the objects we want to display.

For example to specify three color components(RGB) using floating-point (f) values:

glColor3f (1.0, 0.0, 0.0);

A Complete OpenGL Program A Complete OpenGL Program (cont.)(cont.)

To display a straight-line (for example) we need to tell OpenGL how we want to project our picture onto the display window.

glMatrixMode (GL_PROJECTION);

gluOrtho2D (0.0, 200.0, 0.0, 150.0);

The secod commands specify that the orthographic projection map the contents of a 2D rectangular area of world coordinates to the screen with coordinate ranges (x,y)→[(0.0, 200.0), (0.0, 150.0)]

A Complete OpenGL Program A Complete OpenGL Program (cont.)(cont.)

Finally, we need to call the appropriate OpenGL routines to create the requred picture(e.g. the line segment).

The folowing code defines a 2D line segment with integer, Cartesian endpoint coordinates (180,15) and (10,145).

glBegin(GL_LINES);

glVertex2i(180, 15);

glVertex2i(10, 145);

glEnd ( );

A Complete OpenGL Program A Complete OpenGL Program (cont.)(cont.)

The following OpenGL program is organized into three procedures:• init : Place all initializations and one-time parameter

settings.• lineSegment : Geometric description of the picture

we want to display. This will be referenced by glutDisplayFunc.

• main : Contains the GLUT functions for setting up the display window and getting our line segment onto the screen.

An Example OpenGL ProgramAn Example OpenGL Program

#include <GL/glut.h>void init (void){

glClearColor (1.0, 1.0, 1.0, 0.0); //Set display-window color to white.glMatrixMode (GL_PROJECTION); //Set projection parameters.gluOrtho2D (0.0, 200.0, 0.0, 150.0);

}void lineSegment (void){

glClear (GL_COLOR_BUFFER_BIT); //Clear display window.glColor3f(1.0, 0.0, 0.0); //Set line segment color to red.

glBegin(GL_LINES);glVertex2i(180, 15); //Specify line-segment geometryglVertex2i(10, 145);

glEnd ( );

glFlush ( ); //Process all OpenGL routines as quickly as possible

}

An Example OpenGL Program(cont.)An Example OpenGL Program(cont.)

void main (int argc, char** argv)

{

glutInit (&argc, argv); //Initialize GLUT.

glutInitDisplayMode (GLUT_SINGLE|GLUT_RGB); //Set display mode.

glutInitWindowPosition (50, 100); //Set top-left display-window position.

glutInitWindowSize(400, 300); //Set display-window width and height.

glutCreateWindow ("An OpenGL program"); //Create display window.

init ( ); //Execute initialization procedure

glutDisplayFunc(lineSegment); //Send graphics to display window.

glutMainLoop ( ); //Display everything and wait.

}