Embed
Email

OpenGL � as a standard in 3D scenes

Document Sample
OpenGL � as a standard in 3D scenes
Shared by: HC111129062544
Categories
Tags
Stats
views:
0
posted:
11/28/2011
language:
English
pages:
24
OpenGL®

as a standard in 3D scenes



by Petr Šupina

2005

2D Versus 3D

2D

• 2 dimensions

• 2 coordinates per

point







3D

• 3 dimensions

• 3 coordinates per

point

• Displayed in 2D

• DirectX & OpenGL

3D Modeling

• A point in 2D, e.g. (x, y)

• A point in 3D, e.g. (x, y, z)

• 2D in 3D, e.g. (x, y, 0)

• A point in OpenGL is a vertex

• A vector (line) from 2 vertices v1 = (x1, y1, z1) and v2 = (x2, y2,

z2) is computed: v1 - v2 = (x1 - x2, y1 - y2, z1 - z2)

3D Modeling

• A triangle from 3 vertices v1,2,3: v1 is a fixed point, v2-v1 and

v3-v1 are the 2 vectors defining a plane









• Don‘t forget: a vector (line) is not a vertex (point)

• 4D: (x, y, z, time)

What to Know to Start

• I will use C/C++ syntax

• OpenGL initialization won‘t be presented –

platform dependent

• Everything should be portable



• Tutorials: http://nehe.gamedev.net

• Official OpenGL site: http://www.opengl.org

OpenGL - Libraries

• 3 basic libraries:

– OpenGL: main functions

– GLU: premade objects, helper functions

– GLaux: loading textures

• GLUT and others



• Put to a C/C++ file or a header file

#include // the header file for OpenGL

#include // GLU

#include // Glaux

OpenGL - Basics

• 2 forms of functions for fractional numbers:

// uses float numbers, e.g. 3.141592f

glFunctionf(float parameter);

// uses double numbers with a higher precision, e.g. 3.14159265358979

glFunctiond(double parameter);



• Blend colors across polygons smoothly

glShadeModel(GL_SMOOTH); // Enables Smooth Shading





• Set the background color

glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background





• Enable the depth buffer to correctly display

objects one behind another

glClearDepth(1.0f); // Depth Buffer Setup

glEnable(GL_DEPTH_TEST); // Enables Depth Testing

glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do





• Enable the best perspective correction (objects

are smaller in the distance)

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

OpenGL - Basics

• Clear the background to the pre-set color and

empty the depth buffer

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);





• Select the projection matrix, reset it to identity

and calculate the perspective

glMatrixMode(GL_PROJECTION); // Select the projection matrix

glLoadIdentity(); // Reset the projection matrix

// Calculate the aspect ratio of the window

gluPerspective(45.0f, width / height, 0.1f, 100.0f);

glMatrixMode(GL_MODELVIEW); // Select the modelview matrix

glLoadIdentity(); // Reset the modelview matrix







• You don‘t have to call the functions (defaults)

OpenGL - Triangles and Quads

• Use glBegin(type) to create a polygon, glEnd() to

end creating it

• The type can be:

– Point (1 vertex)

– Line (2 vertices)

– Triangle (3 vertices)

– Quad (4 vertices)

– Polygon (5 or more vertices)

• An example of a triangle:

glBegin(GL_TRIANGLES); // Drawing Using Triangles

glVertex3f( 0.0f, 1.0f, 0.0f); // Top

glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

glEnd(); // Finished Drawing The Triangle





• Enter vertices counterclockwise to see the front

face, not the back face

OpenGL - Triangles and Quads

• Another example of a quad (square)

glBegin(GL_QUADS); // Draw A Quad

glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left

glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right

glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad

OpenGL - Colors

• 2 basic types of coloring:

– Flat (a single color)

– Smooth (blends colors across a polygon)

• Use glColor3f(red, green, blue) to set the color to

be used for every vertices until the next call to the

function

• A smooth coloring:

glBegin(GL_TRIANGLES);

glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red

glVertex3f( 0.0f, 1.0f, 0.0f); // Top Point

glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green

glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue

glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

glEnd(); // Done Drawing A Triangle





• Flat coloring is done by calling glColor3f() before

glBegin() only once in the previous example

OpenGL - Translation & Rotation

• glLoadIdentity() resets the position to the center

of the screen

• glTranslatef(x, y, z) moves along the X, Y and Z

axis by altering the modelview matrix

// Move Left 1.5 Units And Into The Screen 6.0

glTranslatef(-1.5f, 0.0f, -6.0f);



• glRotatef(angle, x, y, z) rotates an object around

an axis by an angle in degrees; x, y, z is the axis

(a vector)

// Rotate The Triangle On The Y axis by 180°

glRotatef(180.0f, 0.0f, 1.0f, 0.0f);

OpenGL - 3D Shapes

• Complex objects made of triangles or other

polygons

• Can be time consuming

• More than 1 polygon between glBegin() and

glEnd()

• A rotated piramid:

glRotatef(45.0f, 1.0f, 1.0f, 0.0f); // rotate 45° around x & y axis

glBegin(GL_TRIANGLES);

glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Front)

glVertex3f(-1.0f,-1.0f, 1.0f); // Left Of Triangle (Front)

glVertex3f( 1.0f,-1.0f, 1.0f); // Right Of Triangle (Front)



glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Right)

glVertex3f( 1.0f,-1.0f, 1.0f); // Left Of Triangle (Right)

glVertex3f( 1.0f,-1.0f,-1.0f); // Right Of Triangle (Right)



glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Back)

glVertex3f( 1.0f,-1.0f,-1.0f); // Left Of Triangle (Back)

glVertex3f(-1.0f,-1.0f,-1.0f); // Right Of Triangle (Back)



glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Left)

glVertex3f(-1.0f,-1.0f,-1.0f); // Left Of Triangle (Left)

glVertex3f(-1.0f,-1.0f, 1.0f); // Right Of Triangle (Left)

glEnd(); // Done Drawing The Pyramid

OpenGL - Textures

• Small objects versus complexity (a bird)

• Limitations:

– Width & height must be a power of 2, and, at least, 64

• Loading from a file:

// Load The Bitmap And Return A Pointer

AUX_RGBImageRec *textureData = auxDIBImageLoad(Filename);





• Generating a texture, selecting it, and copying it

to the graphic memory

GLuint texture; // declare the variable to store the texture ID



glGenTextures(1, &texture); // Create The Texture



// Select the texture

glBindTexture(GL_TEXTURE_2D, texture);



// Copy to the graphic memory

glTexImage2D(GL_TEXTURE_2D, 0, 3, textureData->sizeX, textureData->sizeY, 0,

GL_RGB, GL_UNSIGNED_BYTE, textureData->data);

OpenGL - Textures

• Selecting the filtering to be used when resizing

the texture

// Linear Filtering

// when shrinking

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

// when enlarging

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);



• Drawing a textured cube (use glTexCoord2f() to

associate the corners of a texture with the corners

of a quad

glEnable(GL_TEXTURE_2D); // Enable Texture Mapping

glBindTexture(GL_TEXTURE_2D, texture); // Select Our Texture



glBegin(GL_QUADS);

// Front Face

glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left

glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right

glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right

glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left

// Back Face

glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right

glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right

glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left

glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left

... // 4 more faces

glEnd()

OpenGL - Lights

• 2 types:

– Ambient (no direction, is everywhere)

– Diffuse (in one direction can be very bright)

• Directional (the source in an infinite distance)

• Positional (the source is in the scene)

• Can have a color, intensity, position, and many

other attributes

GLfloat LightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f }; // Ambient Light Values

GLfloat LightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; // Diffuse Light Values

GLfloat LightPosition[] = { 0.0f, 0.0f, 2.0f, 1.0f }; // Light Position



glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient); // Setup The Ambient Light

glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse); // Setup The Diffuse Light

glLightfv(GL_LIGHT0, GL_POSITION, LightPosition); // Position The Light



glEnable(GL_LIGHT0); // Enable Light One

glEnable(GL_LIGHTING); // Enable Lighting

OpenGL - Lights

• Normal vectors for each polygon (flat shading) or

for each vertex (smooth shading) using

glNormal3f() – surfaces reflect light

OpenGL - Quadrics

• Sphere: x2 + y2 + z2 = r2, r = const. radius









• Cylinder: x2 + y2 = r2, r = const. radius



• Cone: x2 + y2 = z2

OpenGL - Quadrics

• Creating a quadric:

// Create A Pointer To The Quadric Object

GLUquadric *quadric = gluNewQuadric();

gluQuadricNormals(quadric, GLU_SMOOTH); // Create Smooth Normals



• CD shaped disk:

gluDisk(quadric, 0.5f, 1.5f, 32, 32); // Draw A Disc (CD Shape)



• Sphere:

gluSphere(quadric, 1.0f, 32, 32); // Draw A Sphere with a radius 1.0



• Cylinder:

// A Cylinder With A Radius Of 1.0 And A Height Of 3.0

gluCylinder(quadric, 1.0f, 1.0f, 3.0f, 32, 32);



• Cone:

// A Cone With A Bottom Radius Of 1.0 And A Height Of 3.0

gluCylinder(quadric, 1.0f, 0.0f, 3.0f, 32, 32);

OpenGL - Extensions

• Features not available on every piece of hardware

• OpenGL Extension Registry at

http://oss.sgi.com/projects/ogl-sample/registry/

• Examples of prefixes:

– ARB – Extensions officially approved by the OpenGL Architecture Review Board

– ATI – ATI Technologies

– ATIX – ATI Technologies (experimental)

– EXT – Extensions agreed upon by multiple OpenGL vendors

– HP – Hewlett-Packard

– IBM – International Business Machines

– KTX – Kinetix, maker of 3D Studio Max

– INTEL – Intel

– NV – NVIDIA Corporation

– MESA – Brian Paul’s freeware portable OpenGL implementation

– SGI – Silicon Graphics

– SGIS – Silicon Graphics (specialized)

– SGIX – Silicon Graphics (experimental)

– SUN – Sun Microsystems

– WIN – Microsoft

OpenGL - Extensions

A few examples (there are hundreds of extensions):

• EXT_texture3D: 3D Textures have n3 pixels







(Perlin Noise textures)



• ARB_multitexture: Maps more than 1 texture to a surface in

a single pass









• ARB_compressed_textures: Saves memory by compressing

textures

• ARB_multisampling: Full-screen anti-aliasing (FSAA)

OpenGL - Shaders

• The future of graphics (launched in DirectX 8)

• 2 types:

– Vertex shaders (customize geometry)

– Pixel shaders (customize pixel colors)

• Different names:

– NVIDIA: vertex programs and register combiners

– ATI: vertex shaders and fragment shaders

• Scripts handled very differently by vendors – not

very useful









(Flare) (Water interaction)

OpenGL – Other Techniques

• Simple Transparency: altering the alpha value in

colors; 4th parameter of glColor4f()









• Display lists: Lists of called functions; increases

memory usage

• Vertex arrays: Arrays of vertices, normals, and

colors loading the entire model at once

• Fonts, Shadows, Reflections, Particle systems,

MIP-maps, Bump-mapping, Blurring, …: Too

extensive; not today

Conclusion

• A complete program using platform independent

(portable) GLUT library:

#include // GLUT header file

#include // OpenGL header file



void renderScene() { // render the scene

glClear(GL_COLOR_BUFFER_BIT); // clear the background to black

glBegin(GL_TRIANGLES); // draw a smooth colored triangle

glColor3f( 0.0f, 1.0f, 0.0f); // green

glVertex3f(-0.5f,-0.5f, 0.0f); // bottom left

glColor3f( 0.0f, 0.0f, 1.0f); // blue

glVertex3f( 0.5f,-0.5f, 0.0f); // bottom right

glColor3f( 1.0f, 0.0f, 0.0f); // red

glVertex3f( 0.0f, 0.5f, 0.0f); // top

glEnd();

glFlush(); // ensure all functions ended

}



void main(int argc, char **argv) { // called when application starts

glutInit(&argc, argv); // initialize GLUT

glutInitDisplayMode(GLUT_RGBA); // set RGBA display mode

glutInitWindowPosition(100, 100); // self explaining

glutInitWindowSize(300, 300);

glutCreateWindow("GLUT test"); // create the window "GLUT test"

glutDisplayFunc(renderScene); // set our rendering function

glutMainLoop(); // process events and wait until exit

} // the program closes


Related docs
Other docs by HC111129062544
06272FJ
Views: 0  |  Downloads: 0
Univerza v Ljubljani
Views: 4  |  Downloads: 0
Theories of Leadership
Views: 2  |  Downloads: 0
2Nite
Views: 1  |  Downloads: 0
WATER CYCLE
Views: 0  |  Downloads: 0
TESTE
Views: 129  |  Downloads: 2
Des b�les aux Bulles
Views: 18  |  Downloads: 0
Subjekty
Views: 993  |  Downloads: 0
myspectrumschool teacher application
Views: 0  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!