Top Banner
OpenGL Course Notes Chapter 1: Introduction Jim Mims
28
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

OpenGL Course NotesChapter 1: Introduction

Jim Mims

Page 2: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Table of Contents

Origin and Purpose

C++ Essentials

Some Definitions

Required Software

Include Files

Managing Windows: Windows Procedures Vs GLUT Functions

GLUT Windows Management Functions

Handling Input Events

Some Built-In 3D Objects

Example Code

Page 3: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

OpenGL: Origin and Purpose

The OpenGL API (Application Programming Interface) began as an initiative by Silicon Graphics to create a single, vendor-independent API for the development of 2D and 3D graphics applications.

Prior to the introduction of OpenGL, many hardware vendors had different graphics libraries. This situation made it expensive for software developers to support versions of their applications on multiple hardware platforms, and it made porting of applications from one hardware platform to another very time-consuming and difficult. 

Silicon Graphics saw the lack of a standard graphics API as an inhibitor to the growth of the 3D marketplace and decided to lead an industry group in creating such a standard.  

Page 4: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

OpenGL: Origin and Purpose

The result of this work was the OpenGL API  library. The OpenGL API began as a specification, then  a sample implementation was produced that hardware vendors could use to develop OpenGL drivers for their hardware. The sample implementation has been released under an open source license. Modifications to the OpenGL API are made through the OpenGL Architecture Review Board, an industry group that contains founding, permanent, and auxiliary members.   

Page 5: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

OpenGL: Origin and Purpose

The OpenGL specification is overseen by the OpenGL Architecture Review Board (ARB).

The OpenGL Architecture Review Board (ARB) is an industry consortium that currently governs the OpenGL specification. It was formed in 1992, and defines the conformance tests, approves the OpenGL specification and advances the standard.

In July 2006, it was announced that the ARB voted to transfer control of the OpenGL specification to the Khronos Group.

Page 6: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

OpenGL: Origin and Purpose

The Khronos Group is a member-funded industry consortium focused on the creation of open standard, royalty-free APIs to enable the authoring and accelerated playback of dynamic media on a wide variety of platforms and devices.

All Khronos members are able to contribute to the development of Khronos API specifications, are empowered to vote at various stages before public deployment, and are able to accelerate the delivery of their cutting-edge 3D platforms and applications through early access to specification drafts and conformance tests.

Page 7: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

C++ Essentials

FunctionsGroupings of code that accomplish a task. Enclosed in braces {}

Main FunctionAll programs must have oneEnclosed in bracesLast line is return 0;

SemicolonsUsed to terminate a line of code

Importing Libraries#include <name of library> if in standard C++ library#include “name of library” if not in standard C++ library           

Page 8: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

C++ Essentials

Some Data Types int, float, double, char, string

Some Operators>, <, =, ==, !=     

LoopsFor (int i = 0; i<10;i++){ //code goes here}          Calling a FunctionUse the name of the function         VB 6: ByRef or ByVal        Comments// or /*……*/

Page 9: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Some Definitions

Geometric PrimitivesPoints, lines, and polygons

ModelsObjects constructed from geometric primitives

RenderingProcess by which computer generates images (things on the screen) from models

Bit

A 0 or a 1

Bitplane

Area in memory that holds one bit of information for every pixel on the screen (how green, for example)

Framebuffer

Contains the bitplanes – all the graphics information the graphics display needs to control the color and intensity of all the pixels on the screen

Page 10: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Some Definitions

Rendering PipelineSeries of processing stages – summarized next

Rendering Pipeline: Display ListsData structure for storing all data (geometric primitives or pixels) for subsequent use. The alternative is to process the data immediately (immediate mode)

Rendering Pipeline: EvaluatorsAll geometric primitives described by vertices. Surfaces and curves described by control points and basis functions. Evaluators derive vertices from the surface and curve representations.

Rendering Pipeline: Primitive AssemblyIncludes cleanup operations such things as clipping.

Rendering Pipeline: Pixel OperationsOperations on pixels – geometric data takes a different route.

Page 11: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Some Definitions

Rendering Pipeline: Texture AssemblyApplying textures onto geometric objects to make them look more realistic.

Rendering Pipeline: RasterizationThe conversion of both geometric and pixel data into fragments. Each fragment square corresponds to a pixel in the framebuffer. Color and depth values are assigned for each square.

Rendering Pipeline: Fragment OperationsSeries of operations that are performed before the values are stored in the framebuffer. Fragments may be altered or removed.

Page 12: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Required Software

The location of the following software is system dependent.

For Windows, the following files should be in a directory similar to the following: Program Files\Microsoft Visual Studio\VC\include\gl

gl.hPrimary OpenGL header file. Contains prototypes for various functions

glu.hHeader file for the OPenGL Utility library

glut.hHeader file for OpenGL Graphic Library Utility Toolkit - used to manage Windows. Ensures that gl.h and glu.h are included automatically - including them with separate code is redundant.

 

Page 13: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Required Software

For Windows, the following files should be in a directory similar to the following: Program Files\Microsoft Visual Studio\VC\lib

Opengl32.libLibrary containing bindings to OpenGL functions.

glu32.libLibrary containing bindings to OpenGL Utility Library functions

Glut32.libLibrary contining bindings to GLUT Library functions

Page 14: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Required Software

For Windows, the following files should be in a directory similar to the following: Windows\System32

opengl32.dllDynamic link library containing OpenGL function implementations and hooks into video hardware drivers.

glu32.dllDynamic link library containing OpenGL Utility Library function implementations. 

Page 15: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Include Files

#include <GL/gl.h>#include <GL/glu.h>

#include is a C++ preprocessor directive. It tells the compiler to insert the file included in < >

The <> indicates that the files are in the standard library directory for C++

The GL is a sub directory in the include directory – created when you install Windows.

Page 16: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Managing Windows: Windows Vs GLUT Functions

There are 2 options: using Windows procedures (not intuitive) or using GLUT functions. This course emphasizes the latter.

To use Windows procedures you open C++ and select File, New, Win32 Application. You must #include <windows.h>

GLUT stands for OpenGL Utility Toolkit – it is used to perform windows activities – controlling the window.

To use GLUT functions, you open C++ and select File, New, Win32 Console Application. You must #include <glut.h>. Glut.h ensure that both gl.h and glu.h are included.

Page 17: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Windows Management Functions

glutInit()Initializes GLUT – should be called before any other GLUT function.

glutInitDisplayMode(int mode)Specifies whether to use an RGBA or color-index color model and to set some other items.

glutInitWindowPosition(int x, int y)Specifies the screen location for the upper-left corner of the window.

glutWindowSize(int width, int size)Specifies the size, in pixels, of the window.

glutCreateWindow()Creates a window

glutMainLoop()Displays the window

Page 18: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Handling Input Events

glutReshapeFunc(int w, int h)Indicates what action should be taken when the window is resized.

glutKeyboardFunc(char key, int x, int y)glutMouseFunc(int button, int state, int x, int y)Allow you to link a keyboard key or a mouse button with a routine that is invokes when the key or mouse button is pressed or released

glutMotionFunc(int x, int y)Regisers a routine call back when the mouse is moved while a mouse button is also pressed.

Page 19: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Some Built-In 3D Objects

glutSolidSphere(double radius, int slices, int stacks)glutWireSphere(double radius, int slices, int stacks)

Renders a sphere centered at the modeling coordinates origin of the specified radius. The sphere is subdivided around the Z axis into slices and along the Z axis into stacks.

radius is the radius of the sphereslices is  the number of subdivisions around the Z axis stacks is the number of subdivisions along the Z axis  glutSolidCube (double size)glutWireCube(double size)

Render a solid or wireframe. The cube is centered at the modeling coordinates origin with sides of length size.  

Page 20: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Some Built-In 3D Objects

glutSolidCone(base, height, slices, stacks)glutWireCone(base, height, slices, stacks) Render a solid or wireframe cone respectively oriented along the z axis. The base of the cone is placed at z=0, and the top at z=height. The cone is subdivided around the z  axis into slices, and along the z axis into stacks.  base is the radius of the base of the coneheight is the height of the coneslices is the number of subdivisions around the Z axisstacks is the number of subdivisions along the Z axis  

Page 21: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Some Built-In 3D Objects

glutSolidTorus(double innerRadius, double outerRadius, int nsides, int rings)glutWireTorus (double innerRadius, double outerRadius, int nsides, int rings) Render a solid or wireframe torus (doughnut) respectively centered at the modeling coordinates origin whose axis is aligned with the Z axis.  innerRadius is the inner radius of the torousouterRadius is the outer radius of the torusnsides is teh number of sides for each radial sectionrings is the number of radial divisions for the torus  

Page 22: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Some Built-In 3D Objects

glutSolidTeapot(double size)glutWireTEapot(double size) Render a solid or wireframe teapot respectively. Both surface normals and texture coordinates for the teapot are generated. Size is the relative size of the teapot

glutSolidTetrahedron()glutWireTetrahedron() Render a solid or wireframe tetrahedron respectively centered at the modeling coordinates origin with a radius square root of 3.     

Page 23: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Many Teapots

    

Page 24: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Clipped Wireframe Sphere

    

Page 25: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Example Code

#include <GL/glut.h>#include <stdlib.h>void display(void){//clear all pixels glClear (GL_COLOR_BUFFER_BIT);//draw white polygon (rectangle) with indicated cornersglColor3f (1.0, 1.0, 1.0); glBegin(GL_POLYGON); glVertex3f (0.25, 0.25, 0.0); glVertex3f (0.75, 0.25, 0.0); glVertex3f (0.75, 0.75, 0.0); glVertex3f (0.25, 0.75, 0.0); glEnd();

Page 26: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Example Code

//start processing buffered OpenGL routines glFlush ();}void init (void) {//Select clearing color glClearColor (0.0, 0.0, 0.0, 0.0);//Initialize viewing values glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);}

Page 27: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Example Code

/************************************************************************************** Declare initial window size, position, and display mode (single buffer, RGBA). Open window with "hello“ in its title bar. Call initialization routines - register callback function to display graphics.Enter main loop and process events. *************************************************************************************/

int main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (250, 250); glutInitWindowPosition (100, 100); glutCreateWindow ("hello"); init (); glutDisplayFunc(display); glutMainLoop(); return 0;}

Page 28: OpenGL Course Notes Chapter 1: Introduction Jim Mims.

Example Code: Output