Introduction to OpenGL Programming RedBook OpenGL.

80
Introduction to OpenGL Programming RedBook OpenGL

Transcript of Introduction to OpenGL Programming RedBook OpenGL.

Page 1: Introduction to OpenGL Programming RedBook OpenGL.

Introduction to OpenGL Programming

RedBook OpenGL

Page 2: Introduction to OpenGL Programming RedBook OpenGL.

Rendering Pipeline (Foley and van Dam)

Page 3: Introduction to OpenGL Programming RedBook OpenGL.

What is OpenGL? -1

• OpenGL is a cross-platform standard for 3D rendering. and software interface to graphics hardware.

• The OpenGL Architecture Review Board(ARB), and independent consortium formed in 1992, governs the OpenGL specification.

• OpenGL routines use the prefix gl.

Page 4: Introduction to OpenGL Programming RedBook OpenGL.

What is OpenGL? -2

• Designed as a hardware-independent interface and implemented on many different hardware platforms.

• No commands for performing windowing tasks or obtaining user input are included in OpenGL.

• You must work through whatever windowing system controls the particular hardware you're using.

Page 5: Introduction to OpenGL Programming RedBook OpenGL.

What is OpenGL? -3

• OpenGL doesn't provide high-level commands for describing models of three-dimensional objects.

• With OpenGL, you must build up your desired model from a small set of geometric primitives.– points, lines, triangles, and polygons …etc.

Page 6: Introduction to OpenGL Programming RedBook OpenGL.

The Advantages of OpenGL

• Industry standard

• Fast

• Portable

• Easy to use

• Well-documented

Page 7: Introduction to OpenGL Programming RedBook OpenGL.

OpenGL rendering pipeline

Page 8: Introduction to OpenGL Programming RedBook OpenGL.

Color Models

• RGBA color– Red, Green, Blue, and Alpha channels– Up to 16M colors

• Color-indexed– Small numbers of colors accessed by indices

from a color lookup table(palette)– 8 bits = 256 colors

Page 9: Introduction to OpenGL Programming RedBook OpenGL.

Basic Features

• Transformation

• Materials Lighting & Smooth Shading

• Texture mapping

• Depth buffer test (Z-Buffer)

• Alpha blending

• Double buffering for animation

• Pixel operations

Page 10: Introduction to OpenGL Programming RedBook OpenGL.

OpenGL Buffering -1

• OpenGL supports a variety of buffers for advanced rendering– Depended on driver implementation

• Color buffers– Front-left, front-right, back-left, back-right, and

any number of auxiliary buffers

• Depth buffer– Also known as Z-Buffer for HSR

Page 11: Introduction to OpenGL Programming RedBook OpenGL.

OpenGL Buffering -2

• Alpha buffer– Blending

• Stencil buffer– To restrict drawing to certain portions of the

screen. (basic application)

• Accumulation buffer– Full scene manipulation

Page 12: Introduction to OpenGL Programming RedBook OpenGL.

OpenGL library -1

• SGI’s OpenGL SDK– OpenGL.DLL & GLU.DLL– Open source implementation for hardware

vendor reference.– Best software rendering library.– Can be downloaded from SGI’s website.

Page 13: Introduction to OpenGL Programming RedBook OpenGL.

OpenGL library -2

• Microsoft’s implementation– From Win95 OSR2, Microsoft Windows ship

with OpenGL32.DLL.– For old Win95 users, you still can download

OPENGL95.EXE via Microsoft website.– Header files and import library files are already

included in Win32 Platform SDK.

Page 14: Introduction to OpenGL Programming RedBook OpenGL.

OpenGL Utility Library

• The OpenGL Utility Library(GLU) provides many of the modeling features, such as quadric surfaces and NURBS curves and surfaces.

• GLU is a standard part of every OpenGL implementation

• GLU routines use the prefix glu.– gluLookAt( … );– …

Page 15: Introduction to OpenGL Programming RedBook OpenGL.

OpenGL Utility Toolkit -1

• The OpenGL Utility Toolkit (GLUT) is a window system-independent toolkit, written by Mark Kilgard, to hide the complexities of differing window system APIs.

• GLUT routines use the prefix glut.– glutPostRedisplay();– …

Page 16: Introduction to OpenGL Programming RedBook OpenGL.

OpenGL Utility Toolkit -2

• You can download this toolkit in the following website– http://reality.sgi.com/mjk_asd/glut3/glut3.html

• We focus on this toolkit.

Page 17: Introduction to OpenGL Programming RedBook OpenGL.

Compilers – Microsoft Visual C++

• Start a new workspace of Win32 Console application.

• Add OpenGL32.lib glu32.lib glut32.lib in Projects/Setting/Link/Library modules column for linking

Page 18: Introduction to OpenGL Programming RedBook OpenGL.

OpenGL on VC• New as Win32 console app

lication• settings

– opengl32.lib

– glu32.lib

– glut32.lib

• Install– glut32.dll in Windows/Syste

m

– glut32.lib in lib/

– glut.h in include/GL

Page 20: Introduction to OpenGL Programming RedBook OpenGL.

OpenGL Basic Drawing#include <whateverYouNeed.h> main() {

InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glOrtho(0.0, 1.0, 0.0, 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(); glFlush(); UpdateTheWindowAndCheckForEvents();

}

Page 21: Introduction to OpenGL Programming RedBook OpenGL.

OpenGL Command Syntax -1

• OpenGL constants prefix by GL_.

• OpenGL routines prefixed by gl and suffiexed by the number of arguments and type.– glVertex2i, glNormal3f, glTexCoord2fv…etc

Page 22: Introduction to OpenGL Programming RedBook OpenGL.

OpenGL Command Syntax -2

Page 23: Introduction to OpenGL Programming RedBook OpenGL.

OpenGL as a State Machine -1

• OpenGL is a state machine. • You put it into various states (or modes) tha

t then remain in effect until you change them.

• As you've already seen, the current color is a state variable

• Many state variables refer to modes that are enabled or disabled with the command glEnable() or glDisable().

Page 24: Introduction to OpenGL Programming RedBook OpenGL.

OpenGL as a State Machine -2

• Each state variable or mode has a default value, and at any point you can query the system for each variable's current value.

• Typically, you use one of the six following commands to do this: glGetBooleanv(), glGetDoublev(), glGetFloatv(), glGetIntegerv(), glGetPointerv(), or glIsEnabled().

Page 25: Introduction to OpenGL Programming RedBook OpenGL.

GLUT: Overview

• A very useful framework for rapid OpenGL program development.– Portable through lots of platforms.

– Provide basic window, input, and event management.

– Callback event handler.

– Idle routine and timer.

– A simple, cascading popup menu facility.

– Offers lots of basic object. (wireframe/solid)

– …

Page 26: Introduction to OpenGL Programming RedBook OpenGL.

GLUT: Initialize and creating a window -1

• glutInit( int* argc, char** argv );– Initialize GLUT and process any command line argume

nts. – This should be called before any GLUT routines.

• glutInitDisplayMode( unsigned int mode );– Specify whether to apply RGBA mode or color-indexed

mode.• GLUT_RGBA, GLUT_INDEX…

– Single or double buffering. • GLUT_SINGLE, GLUT_DOUBLE…

– Other buffers.• GLUT_DEPTH, GLUT_STENCIL, GLUT_ACCUM…

Page 27: Introduction to OpenGL Programming RedBook OpenGL.

GLUT: Initialize and creating a window -2

• glutInitWindowPosition( int x, int y );– Specify the screen location of your window

• glutInitWindowSize( int w, int h );– Specify the size of your window in pixels.

• glutCreateWindow( char* pTitle );– Create a window with OpenGL context for rend

ering.– The window will appear until glutMainLoop()

is called.

Page 28: Introduction to OpenGL Programming RedBook OpenGL.

GLUT: Callback function -1

• Callback functions are not called by you. They are called by GLUT.

• You can register some functions to GLUT, and they can be called when meeting the situation.

• Some types of callback– Window event handler– IO event handler– Global handler

Page 29: Introduction to OpenGL Programming RedBook OpenGL.

GLUT: Callback function -2

• glutDisplayFunc( void (*pfn)(void) );– Display callback; GLUT called it when the cont

ent of the window needed to be updated.– You call manually call glutPostRedisplay() to

arbitrarily ask GLUT to call that callback function.

• glutReshapeFunc( void (*pfn)( int w, int h ) );– GLUT called it when the size of window is cha

nged.– Recalculate view frustum & viewport…

Page 30: Introduction to OpenGL Programming RedBook OpenGL.

GLUT: Callback function -3

• glutKeyboardFunc( void(*pfn)(unsigned char key, int x, int y )); and glutMouseFunc( void(*pfn)( int button, int state, int x, int y ));– User Input event handler.

• glutMotionFunc( void(*pfn)( int x, int y )); – GLUT call this registered callback function when the mouse is mo

ved while a mouse button is also pressed.

• glutIdleFunc( void(*pfn)(void)); – GLUT call this registered callback function when idling.

– You can simply call:• glutIdleFunc( glutPostRedisplay );

Page 31: Introduction to OpenGL Programming RedBook OpenGL.

Start GLUT framework

• glutMainLoop();– Like Win32’s message loop.– Start all GLUT operations.

Page 32: Introduction to OpenGL Programming RedBook OpenGL.

GLUT: Complete sample -1

#include <GL/glut.h>

void display(void) { /* clear all pixels */ glClear (GL_COLOR_BUFFER_BIT); /* draw white polygon (rectangle) with corners at * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0) */ glColor3f (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(); /* don't wait! * start processing buffered OpenGL routines */ glFlush ();

}

Page 33: Introduction to OpenGL Programming RedBook OpenGL.

GLUT: Complete sample -2

void init (void) { /* select clearing (background) 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);

}

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

Page 34: Introduction to OpenGL Programming RedBook OpenGL.

GLUT: Complete sample -3

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; /* ISO C requires main to return int. */

}

Page 35: Introduction to OpenGL Programming RedBook OpenGL.

GLUT: Complete sample result

Page 36: Introduction to OpenGL Programming RedBook OpenGL.

GLUT: Simple 3D objects drawing support -1

• GLUT includes several 3D objects drawing:– Cone, cube, sphere, dodecahedron, icosahedron,

octahedron, teapot, tetrahedron, torus.– Both wireframe and solid.

• glutWireCube, glutSolidSphere … etc.

– These objects are drawn centered at the origin of the world coordinate system.

Page 37: Introduction to OpenGL Programming RedBook OpenGL.

GLUT: Simple 3D objects drawing support -2

• You can try this simple display callback function and see the result.

void display(void)

{

glClear( GL_COLOR_BUFFER_BIT );

glutWireCube( 1.0 );

glFlush();

}

Page 38: Introduction to OpenGL Programming RedBook OpenGL.

A Drawing Survival Kit -1

• Clearing the window– glClear( GLbitfield mask );

• GL_COLOR_BUFFER_BIT -> glClearColor( … );• GL_DEPTH_BUFFER_BIT -> glClearDepth( … );

• Specifying a color– glColor3f( GLfloat r, GLfloat g, GLfloat b );– glIndexi( int i );

• glutSetColor( int i, GLfloat r, GLfloat g, GLfloat b ); for setting indexed color.

Page 39: Introduction to OpenGL Programming RedBook OpenGL.

A Drawing Survival Kit -2

• Specifying Vertices– glVertex{2,3,4}{sifd}[v]( … );

• Ex: glVertex3f( 1.0f, 0.5f, 2.0f );

– Vertex array• Not introduced here.

Page 40: Introduction to OpenGL Programming RedBook OpenGL.

A Drawing Survival Kit -3

• OpenGL Geometric Drawing Primitives– glBegin( GLenum mode );

• mode is GL_POLYGON, GL_LINES … etc

– glEnd();– All primitives should be placed between these two Ope

nGL routines• Ex

glBegin(GL_POLYGON); glVertex2f(0.0, 0.0); glVertex2f(0.0, 3.0); glVertex2f(4.0, 3.0); glVertex2f(6.0, 1.5); glVertex2f(4.0, 0.0);

glEnd();

Page 41: Introduction to OpenGL Programming RedBook OpenGL.

A Drawing Survival Kit -4

Page 42: Introduction to OpenGL Programming RedBook OpenGL.

A Drawing Survival Kit -5

• Valid calls between glBegin(…); & glEnd();– glVertex*(); glNormal*(); glColor*(); glIndex*(); glTe

xCoord*(); glMaterial*(); …

• Forcing Completion of Drawing– glFlush(void);

• Asynchronous

– glFinish(void);• Synchronous

– glutSwapBuffers(void);• Swap back buffer and front buffer. (Double buffering for anim

ation)

Page 43: Introduction to OpenGL Programming RedBook OpenGL.

A Drawing Survival Kit -6

• Basic State Management– glEnable( … );

• Enable some state. Ex: glEnable( GL_LIGHTING );

– glDisable( … );• Disable some state. Ex. glDisable( GL_TEXTURE_2D );

– glIsEnabled( … );• Query if the specific state is enabled. Ex: glIsEnabled( GL_BL

END );

– glGetBooleanv(…); glGetIntegerv(…); glGetFloatv(…); glGetDoublev( … ); glGetPointerv(…);

• Query the specific state value. – Ex: glGetIntegerv( GL_VIEWPORT, vp );

Page 44: Introduction to OpenGL Programming RedBook OpenGL.

A Drawing Survival Kit -7

• Polygon Detail– glPolygonMode( GLenum face, GLenum mode

);• face: GL_FRONT, GL_BACK, GL_FRONT_AND_

BACK• mode: GL_POINT, GL_LINE, GL_FILL

– glFrontFace( GLenum mode );• mode: GL_CCW(default), GL_CW

– glCullFace( GLenum mode );• mode: GL_FRONT, GL_BACK, GL_FRONT_AN

D_BACK• glEnable( GL_CULL_FACE );

Page 45: Introduction to OpenGL Programming RedBook OpenGL.

A Drawing Survival Kit -8

• Specifying a Shading Model– glShadeModel( GLenum mode );

• mode: GL_SMOOTH(default), GL_FLAT

Page 46: Introduction to OpenGL Programming RedBook OpenGL.

Some information about Polygon

(page 55-57 Redbook)–glPolygonMode( GLenum face, GLenum mode );

•face: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK

•mode: GL_POINT, GL_LINE, GL_FILL (default)

–glFrontFace( GLenum mode );•mode: GL_CCW(default), GL_CW

–glCullFace( GLenum mode ); indicates which polygons should be discarded (culled) before they’re converted to screen coordinates

•mode: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK

•glEnable( GL_CULL_FACE );–glShadeModel( GLenum mode );

•mode: GL_SMOOTH(default), GL_FLAT

Page 47: Introduction to OpenGL Programming RedBook OpenGL.
Page 48: Introduction to OpenGL Programming RedBook OpenGL.

Hidden Surface Removal

main(){…………………………..glutInitDisplayMode (GLUT_DEPTH | .... ); glEnable(GL_DEPTH_TEST);

...} display ( ) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ………………………. ……………………….. draw_object_A();

draw_object_B(); }

Page 49: Introduction to OpenGL Programming RedBook OpenGL.

VIEWING

• The camera analogy

• rendering pipeline– stages of vertex transform

Page 50: Introduction to OpenGL Programming RedBook OpenGL.

Two Transformation Matrices

• ModelView: geometry transformation and viewpoint placement (dual)

• Projection: orthographic and perspective

Page 51: Introduction to OpenGL Programming RedBook OpenGL.

Specifying Projection in OpenGL

• glFrustum• gluPerspective

– symmetry viewing volume guaranteed

• glOrtho• gluOrtho2D

Page 52: Introduction to OpenGL Programming RedBook OpenGL.

Viewport Transformation

• aspect ratio control

Page 53: Introduction to OpenGL Programming RedBook OpenGL.

Rendering Pipeline Details

• Modeling and viewing transformation

• Orthographic (parallel) and perspective projection

• 4x4 Matrix

• OpenGL implementations

Page 54: Introduction to OpenGL Programming RedBook OpenGL.

Modeling Transformation

• modeling transformation: – glTranslate

– glRotate

– glScale

– glMultMatrix

• each generates a corresponding 4x4 matrix (Appendix A)

• order of interpretation:

INML v

Page 55: Introduction to OpenGL Programming RedBook OpenGL.

Hence, ...

glRotate (30, 0, 0, 1);glTranslate (10, 0, 0);drawPlant( );

glTranslate (10, 0, 0);glRotate (30, 0, 0, 1);drawPlant( );

Page 56: Introduction to OpenGL Programming RedBook OpenGL.

Viewing Transformation

• same as modeling transformation

glTranslate (0, 0, -5)

Page 57: Introduction to OpenGL Programming RedBook OpenGL.

Viewing Transform (cont)

• common practice – compose the scene

(positioning/orienting objects)

– then, place viewpoint

• Hence, issue view transformation commands BEFORE any modeling transformation

• two ways to do so:– use glTranslate & glRo

tate • less confusing if think i

n terms of moving objects)

– gluLookAt

• default camera– position at origin, point

ed to negative-z

Page 58: Introduction to OpenGL Programming RedBook OpenGL.

Perspective Projection

Page 59: Introduction to OpenGL Programming RedBook OpenGL.

Relevant Commands

• glMatrixMode• glLoadIdentity• glLoadMatrix• glMultMatrix

• Matrix declared as m[16]

Page 60: Introduction to OpenGL Programming RedBook OpenGL.

Ex: cube.c#include <GL/glut.h>#include <stdlib.h>

void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT);}

void display(void){ glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glLoadIdentity (); /* clear the matrix */ /* viewing transformation */ gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glScalef (1.0, 2.0, 1.0); /* modeling transformation */ glutWireCube (1.0); glFlush ();}

void reshape (int w, int h){ glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); glMatrixMode (GL_MODELVIEW);}

void keyboard(unsigned char key, int x, int y){ switch (key) { case 27: exit(0); break; }}

int main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMainLoop(); return 0;}

Page 61: Introduction to OpenGL Programming RedBook OpenGL.

Lighting(Chapter 4 Redbook)

– Step 1: Define Normal Vectors for Each Vertex of Every Object

– Step 2: Create, Position, and Enable One or More Light Sources

– Step 3: Select a Lighting Model– Step 4: Define Material Properties for the

Objects in the Scene– Similar to VRML

Page 62: Introduction to OpenGL Programming RedBook OpenGL.

Step 1: Specify Normal Vector

– By yourself, define normal vectors by call glNormal{3,4}{i,s,f,d}[v](…)

• Ex: glNormal3fv(n1); n1 is array name

• Ex: glNormal3f(x1,y1,z1).

– Normal vectors must be normalized.• Automatically normalize normal vectors for you by calling glE

nable( GL_NORMALIZE ); but expensive

– glutxxx like glutSolidCube(), normal is part of model.

Page 63: Introduction to OpenGL Programming RedBook OpenGL.

Step 2: Create a Light Source(page 181 RedBook)

– void glLight{if}(GLenum light, GLenum pname, TYPE param);void glLight{if}v(GLenum light, GLenum pname, TYPE *param);

• GL_LIGHT0, GL_LIGHT1, ... , or GL_LIGHT7 are availble.

• The characteristic of the light being set is defined by pname.

• param indicates the values to which the pname characteristic is set.

• Ex: glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient)

Page 64: Introduction to OpenGL Programming RedBook OpenGL.

–The default values listed for GL_DIFFUSE and GL_SPECULAR apply only to GL_LIGHT0.

–For other lights, the default value is (0.0, 0.0, 0.0, 1.0 ) for both GL_DIFFUSE and GL_SPECULAR.

Page 65: Introduction to OpenGL Programming RedBook OpenGL.

Example:GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };

GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };

GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };

GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);

glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position);

See page 53 in notes chapter 4

Page 66: Introduction to OpenGL Programming RedBook OpenGL.

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

GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat mat_shininess[] = { 50.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_SMOOTH); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST);

}

Page 67: Introduction to OpenGL Programming RedBook OpenGL.

Positions and Enable Lighting

• Position ( x, y, z, w )– w is ZERO, Directional light– W (i.e., 1) is NONZERO, Positional light

• Define multiple lights in your scene and enable or disable them– glEnable( GL_LIGHT0 );– glDisable( GL_LIGHT7 );

• Enable OpenGL Lighting– glEnable( GL_LIGHTING );

• See page 44 in notes

Page 68: Introduction to OpenGL Programming RedBook OpenGL.

Control Light’s Movements

• OpenGL treats the position and direction of a light source just as it treats the position of a geometric primitive (i.e., a 3D point)– MODELVIEW Transformation will be applied.

• Three types of control (see page 187 Redbook)– A light position that remains fixed

– A light that moves around a stationary object

– A light that moves along with the viewpoint

Page 69: Introduction to OpenGL Programming RedBook OpenGL.

Keeping the Light Stationary(page 187 Redbook)

glMatrixMode (GL_PROJECTION); glLoadIdentity(); if (w <= h)

glOrtho (-1.5, 1.5, -1.5*h/w, 1.5*h/w, -10.0, 10.0); else

glOrtho (-1.5*w/h, 1.5*w/h, -1.5, 1.5, -10.0, 10.0); glMatrixMode (GL_MODELVIEW); glLoadIdentity(); …/* later in init() */ GLfloat light_position[] = { 1.0, 1.0, 1.0, 1.0 }; glLightfv(GL_LIGHT0, GL_POSITION, position); /* NO other MODELVIEW transformation is set before it…*/

Page 70: Introduction to OpenGL Programming RedBook OpenGL.

Independently Moving the Light(see page 188 RedBook)

static GLdouble spin; void display(void) {

GLfloat light_position[] = { 0.0, 0.0, 1.5, 1.0 };glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glPushMatrix();

gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glPushMatrix();

glRotated(spin, 1.0, 0.0, 0.0); glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glPopMatrix(); glutSolidTorus (0.275, 0.85, 8, 15);

glPopMatrix(); glFlush();

}

Page 71: Introduction to OpenGL Programming RedBook OpenGL.

Moving the Light Source Together with Your Viewpoint

(see page 191 Redbook)

GLfloat light_position() = { 0.0, 0.0, 0.0, 1.0 }; glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glLightfv(GL_LIGHT0, GL_POSITION, light_position); ……static GLdouble ex, ey, ez, upx, upy, upz; void display(void) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glPushMatrix();

gluLookAt (ex, ey, ez, 0.0, 0.0, 0.0, upx, upy, upz); glutSolidTorus (0.275, 0.85, 8, 15);

glPopMatrix(); glFlush();

}

Page 72: Introduction to OpenGL Programming RedBook OpenGL.

Attenuation (page 206 Redbook)

• d = distance between the light's position and the vertex

• kc = GL_CONSTANT_ATTENUATION

• kl = GL_LINEAR_ATTENUATION

• kq = GL_QUADRATIC_ATTENUATION

• Ex: glLightf(GL_LIGHT0,option, value)

– If light is directional light, the attenuation is 1

Page 73: Introduction to OpenGL Programming RedBook OpenGL.

Spot Light (page 184 in RedBook)– Define your light as positional light– Define light spot direction

• GLfloat spot_direction[] = { -1.0, -1.0, 0.0 }; glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);

– Define light spot exponent• glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0); (default: 0)

– Define light spot cutoff• glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0);• By default, the spotlight feature is disabled because the GL_SPOT_CUT

OFF parameter is 180.0. (i.e. 2*180=360, all directions so it is not a spotlight at all),

• The value for GL_SPOT_CUTOFF is restricted to being within the range [0.0,90.0] (unless it has the special value 180.0).

Page 74: Introduction to OpenGL Programming RedBook OpenGL.

Step 3: Lighting Model (see page 193 Redbook)

• Selecting a lighting model– void glLightModel{if}(GLenum pname, TYPEparam);

void glLightModel{if}v(GLenum pname, TYPE *param);

• Sets properties of the lighting model. • The characteristic of the lighting model being set is defined by pna

me, which specifies a named parameter.• param indicates the values to which the pname characteristic is set• Ex: Glfloat lmodel_ambient[]={0.2,0.2,0.2,1}

– glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient);

– Even there is no light in your scene, object is lit by it.

Page 75: Introduction to OpenGL Programming RedBook OpenGL.

Lighting Models

Page 76: Introduction to OpenGL Programming RedBook OpenGL.

Step 4: Object Materials (see page 196 Redbook)

– void glMaterial{if}(GLenum face, GLenum pname, TYPEparam);void glMaterial{if}v(GLenum face, GLenum pname, TYPE *param);

• Specifies a current material property for use in lighting calculations. • face can be GL_FRONT, GL_BACK, or GL_FRONT_AND_BAC

K.• The particular material property being set is identified by pname and

the desired values for that property are given by param.• GL_AMBIENT_AND_DIFFUSE allows you to set both• Glfloat mat_specular[] = {1.0,1.0,1.0,1.0}

– glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular)

Page 77: Introduction to OpenGL Programming RedBook OpenGL.

Material (like VRML)

See example on page 47 in notes

Page 78: Introduction to OpenGL Programming RedBook OpenGL.

Examples

No ambient

Reflection

Gray ambient

Reflection

Blue ambient

Reflection Diffuse Add Higher Emission

Only Specular Shininess

Page 79: Introduction to OpenGL Programming RedBook OpenGL.

Color Tracking (see page 45 in notes)

Another technique for minimizing performance costs associated with changing material properties is to use glColorMaterial().

–void glColorMaterial(GLenum face, GLenum mode); •Causes the material property (or properties) specified by mode of the specified material face (or faces) specified by face to track the value of the current color at all times.

•A change to the current color (using glColor*()) immediately updates the specified material properties. The face parameter can be GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK (the default).

•The mode parameter can be GL_AMBIENT, GL_DIFFUSE, GL_AMBIENT_AND_DIFFUSE (the default), GL_SPECULAR, or GL_EMISSION. At any given time, only one mode is active. glColorMaterial() has no effect on color-index lighting.

Page 80: Introduction to OpenGL Programming RedBook OpenGL.

Color Tracking

• glEnable(GL_COLOR_MATERIAL);

• glColorMaterial(GL_FRONT,GL_DIFFUSE);

• glColor3f(0.2,0.5,0.8)

• /** draw some objects here ***/

• glColorMaterial(GL_FRONT,GL_SPECULAR);

• glColor3f(0.9,0.0,0.2);

• /** draw some objects here **/