Introduction to OpenGL

17
Introduction to OpenGL

description

Introduction to OpenGL. OpenGL Overview. OpenGL low-level graphics library specification Hardware Independent use with the C and C++ programming languages but there are also bindings for a number of other programming languages such as Java, Tcl, Ada, and FORTRAN - PowerPoint PPT Presentation

Transcript of Introduction to OpenGL

Page 1: Introduction  to OpenGL

Introduction to OpenGL

Page 2: Introduction  to OpenGL

OpenGL Overview

OpenGL low-level graphics library specification Hardware Independent use with the C and C++ programming languages

• but there are also bindings for a number of other programming languages such as Java, Tcl, Ada, and FORTRAN

Graphics primitives and attributes • points, lines, polygons, images, and bitmaps

geometric transformation• Translation, rotation, scaling, reflection

viewing transformation• viewport

• Parallel/Perspective projection

Page 3: Introduction  to OpenGL

OpenGL Overview

API Hierarchy OpenGL applications use the window

system’s window, input, and event mechanism

GLU supports quadrics, NURBS, complex polygons, matrix utilities, and more

The OpenGL Visualization Programming Pipeline

Unix System

Windows System

Page 4: Introduction  to OpenGL

Basic OpenGL Syntax

OpenGL Basic Library (OpenGL Core Library) Function name

• Prefix gl• First letter CapitalEx) glBegin, glClear, glCopyPixels, glPolygonMode

Constants name• Begin with the uppercase letter GL• Capital Letter• The underscore(_) is used as a seperatorEx) GL_2D, GL_RGB, GL_CCW, GL_POLYGON,

GL_AMIBIENT_AND_DIFFUSE Data type

• Begin with the uppercase letter GL• The remainder of the name : standard data-type (lower case)Ex) GLbyte, GLshort, GLint, GLfloat, GLdouble, GLboolean

Page 5: Introduction  to OpenGL

Related Libraries

OpenGL Utility (GLU) GLU library

• Setting up viewing and projection matrices

• Describing complex objects

• Displaying quadric and B-splines

• Processing the surface-rendeing operations

Prefix glu

Open Inventor An Object oriented toolkit based on OpenGL Written in C++

Page 6: Introduction  to OpenGL

Related Libraries

Display window Window-management operation depend on the computer

• OpenGL Extension to the X Window System (GLX)– Unix System (Prefix glx)

• Windows-to-OpenGL (WGL)– Microsoft Windows System (prefix wgl)

• Apple GL (AGL)– Apple System (prefix agl)

• Presentation Manager to OpenGL (PGL)– IBM OS/2 (prefix pgl)

OpenGL Utility Toolkit (GLUT)• window system independent toolkit

– Interacting with any screen-windowing system

• Prefix glut

Page 7: Introduction  to OpenGL

Installation (1/4)

OpenGL Header files

• gl.h, glu.h

C:\Program Files\Microsoft Visual Studio\VC98\Include\GL

Library files• opengl32.lib, glu32.lib C:\Program Files\Microsoft Visual Studio\VC98\lib

dll files• openGL32.dll, glu32.dll

C:\WINNT\system32

Page 8: Introduction  to OpenGL

Installation (2/4)

Download GLUT (http://www.xmission.com/~nate/glut.html)

• glut-3.7.6-bin.zip (117 KB) – GLUT for Win32 dll, lib and header file

• glut-3.7.6-src.zip (4.76 MB) – GLUT source code distribution

Glut-3.7.6-bin.zip glut.h C:\Program Files\Microsoft Visual Studio\VC98\

Include\GL glut32.lib C:\Program Files\Microsoft Visual Studio\VC98\Lib glut32.dll C:\WINNT\system32

Page 9: Introduction  to OpenGL

Installation (3/4)

Link Library Files : ProjectSettingsLink

Page 10: Introduction  to OpenGL

Installation (4/4)

Page 11: Introduction  to OpenGL

Header file

Include the header files GLUT to handle the window-managing operation

• #include <GL/glut.h>

OpenGL core library• #include <GL/gl.h>

OpenGL Utility• #include <GL/glu.h>

Required by the C++ code• #include <stdio.h>

• #include <stdlib.h>

• #include <math.h>

Page 12: Introduction  to OpenGL

Display-Window Management Using GLUT

Getting Started GLUT initialization

• glutInit (&argc, argv); Buffering and color modes for the display window

• glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB) Window position & size

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

A display window is to be created on the screen• glutCreateWindow(“An Example OpenGL Program”);

Specify what the display window is to contain• glutDisplayFunc(lineSegment);

– Describing a line segment in a procedure called lineSegment

All display windows activate• glutMainLoop();

Page 13: Introduction  to OpenGL

A Complete OpenGL Program

OpenGL Program Set background color

• glClearColor (1.0, 1.0, 1.0, 0.0);

• 1~3 argument : red, green, blue colors (between 0.0 and 1.0)

• Forth parameter : alpha value – Activate the OpenGL blending operations– 0.0 totally transparent object– 1.0 an opaque object

Assigned window color display• glClear (GL_COLOR_BUFFER_BIT);

– Color buffer (refresh buffer) that are to be set to the values indicated in the glClearColor function

Set object color• glColor3f (1.0, 0.0, 1.0);

Page 14: Introduction  to OpenGL

A Complete OpenGL Program

Display a 2D line segment Orthogonal Projection

• glMatrixModel(GL_PROJECTION);

• gluOrtho2D (0.0, 200.0, 0.0, 150.0)– Map 2D retangular area of world coordinates to the screen– Lower-left window corner (0.0,0.0)– Upper-right window corner (200.0, 150.0)

Create line segmentglBegin (GL_LINES);

glVertex2i (180, 15);

glVertex2i (10, 145);

glEnd();

Page 15: Introduction  to OpenGL

glVertex3fv( glVertex3fv( vv ) )

Number ofNumber ofcomponentscomponents

2 - (x,y) 2 - (x,y) 3 - (x,y,z)3 - (x,y,z)4 - (x,y,z,w)4 - (x,y,z,w)

Data TypeData Typeb - byteb - byteub - unsigned byteub - unsigned bytes - shorts - shortus - unsigned shortus - unsigned shorti - inti - intui - unsigned intui - unsigned intf - floatf - floatd - doubled - double

VectorVector

omit “v” foromit “v” forscalar formscalar form

glVertex2f( x, y )glVertex2f( x, y )

OpenGL Command Formats

Page 16: Introduction  to OpenGL

OpenGL Geometric PrimitivesOpenGL Geometric Primitives

All geometric primitives are specified by vertices

glBegin(mode) and glEnd() and a list of vertices in between glBegin(mode) glVertex(v0); glVertex(v1); ...glEnd();

Page 17: Introduction  to OpenGL

#include <GL/glut.h> // (or others, depending on the system in use)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 geometry.glVertex2i (10, 145);

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

}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 Example OpenGL Program"); // Create display window.init ( ); // Execute initialization procedure.glutDisplayFunc (lineSegment); // Send graphics to display window.glutMainLoop ( ); // Display everything and wait.

}